121 lines
3.6 KiB
C++
121 lines
3.6 KiB
C++
/*
|
|
* SoftwareSerial.h (formerly NewSoftSerial.h)
|
|
*
|
|
* Multi-instance software serial library for Arduino/Wiring
|
|
* -- Interrupt-driven receive and other improvements by ladyada
|
|
* (http://ladyada.net)
|
|
* -- Tuning, circular buffer, derivation from class Print/Stream,
|
|
* multi-instance support, porting to 8MHz processors,
|
|
* various optimizations, PROGMEM delay tables, inverse logic and
|
|
* direct port writing by Mikal Hart (http://www.arduiniana.org)
|
|
* -- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
|
|
* -- 20MHz processor support by Garrett Mace (http://www.macetech.com)
|
|
* -- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*
|
|
* The latest version of this library can always be found at
|
|
* http://arduiniana.org.
|
|
*/
|
|
|
|
#ifndef SOFTWARESERIAL_H
|
|
#define SOFTWARESERIAL_H
|
|
|
|
#include "include/arduino.h"
|
|
#include <stdint.h>
|
|
//#include "serial.h"
|
|
#include <Stream.h>
|
|
#include <Print.h>
|
|
|
|
/******************************************************************************
|
|
* Definitions
|
|
******************************************************************************/
|
|
|
|
#define _SS_MAX_RX_BUFF 64 // RX buffer size
|
|
|
|
class SoftwareSerial : public Stream
|
|
{
|
|
private:
|
|
// per object data
|
|
pin_t _receivePin;
|
|
pin_t _transmitPin;
|
|
// uint32_t _receiveBitMask; // for rx interrupts
|
|
uint32_t _receivePort;
|
|
uint32_t _receivePortPin;
|
|
|
|
|
|
// Expressed as 4-cycle delays (must never be 0!)
|
|
uint16_t _rx_delay_centering;
|
|
uint16_t _rx_delay_intrabit;
|
|
uint16_t _rx_delay_stopbit;
|
|
uint16_t _tx_delay;
|
|
|
|
uint16_t _buffer_overflow:1;
|
|
uint16_t _inverse_logic:1;
|
|
|
|
// static data
|
|
static unsigned char _receive_buffer[_SS_MAX_RX_BUFF];
|
|
static volatile uint8_t _receive_buffer_tail;
|
|
static volatile uint8_t _receive_buffer_head;
|
|
static SoftwareSerial *active_object;
|
|
|
|
// private methods
|
|
void recv();
|
|
uint32_t rx_pin_read();
|
|
void tx_pin_write(uint8_t pin_state);
|
|
void setTX(pin_t transmitPin);
|
|
void setRX(pin_t receivePin);
|
|
void setRxIntMsk(bool enable);
|
|
|
|
// private static method for timing
|
|
static inline void tunedDelay(uint32_t delay);
|
|
|
|
public:
|
|
// public methods
|
|
|
|
SoftwareSerial(pin_t receivePin, pin_t transmitPin, bool inverse_logic = false);
|
|
~SoftwareSerial();
|
|
void begin(long speed);
|
|
bool listen();
|
|
void end();
|
|
bool isListening() { return this == active_object; }
|
|
bool stopListening();
|
|
bool overflow() { bool ret = _buffer_overflow; if (ret) _buffer_overflow = false; return ret; }
|
|
int peek();
|
|
|
|
virtual size_t write(uint8_t byte);
|
|
virtual int read();
|
|
virtual int available();
|
|
virtual void flush();
|
|
operator bool() { return true; }
|
|
|
|
using Print::write;
|
|
//using HalSerial::write;
|
|
|
|
// public only for easy access by interrupt handlers
|
|
static inline void handle_interrupt() __attribute__((__always_inline__));
|
|
};
|
|
|
|
// Arduino 0012 workaround
|
|
#undef int
|
|
#undef char
|
|
#undef long
|
|
#undef byte
|
|
#undef float
|
|
#undef abs
|
|
#undef round
|
|
|
|
#endif // SOFTWARESERIAL_H
|