mirror of
https://gitlab.com/JKANetwork/SoftwareSerialLite.git
synced 2026-02-14 17:21:36 +01:00
146 lines
4.7 KiB
C++
146 lines
4.7 KiB
C++
/*
|
|
SoftwareSerialLite.h , derivated from SoftwareSerial.h (formerly NewSoftSerial.h)
|
|
|
|
This is a lite version from SoftwareSerial, with only the basic, Serial
|
|
comunication between two arduinos by software. It consumes about 300bytes less of program code
|
|
than original. You can use the original one if you not define LITE in definitions part.
|
|
Some functions removed too in this lite version like listen() and stopListening()
|
|
because you can use only one serial comunication, end(), and peek(), and inverse logic.
|
|
But they are in code, you can tweak it to include or not.
|
|
|
|
///Original text of lib
|
|
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 SoftwareSerialLite_h
|
|
#define SoftwareSerialLite_h
|
|
|
|
#include <inttypes.h>
|
|
#include <Stream.h>
|
|
|
|
/******************************************************************************
|
|
* Definitions
|
|
******************************************************************************/
|
|
|
|
#ifndef _SS_MAX_RX_BUFF
|
|
#define _SS_MAX_RX_BUFF 32 // RX buffer size (64 is the original one)
|
|
#endif
|
|
|
|
#ifndef LITE
|
|
#define LITE
|
|
#endif
|
|
|
|
#ifndef GCC_VERSION
|
|
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
|
|
#endif
|
|
|
|
class SoftwareSerialLite : public Stream
|
|
{
|
|
private:
|
|
// per object data
|
|
uint8_t _receivePin;
|
|
uint8_t _receiveBitMask;
|
|
volatile uint8_t *_receivePortRegister;
|
|
uint8_t _transmitBitMask;
|
|
volatile uint8_t *_transmitPortRegister;
|
|
volatile uint8_t *_pcint_maskreg;
|
|
uint8_t _pcint_maskvalue;
|
|
|
|
// 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;
|
|
|
|
bool _buffer_overflow:1;
|
|
#ifndef LITE
|
|
uint16_t _inverse_logic:1;
|
|
#endif
|
|
|
|
// static data
|
|
static uint8_t _receive_buffer[_SS_MAX_RX_BUFF];
|
|
static volatile uint8_t _receive_buffer_tail;
|
|
static volatile uint8_t _receive_buffer_head;
|
|
static SoftwareSerialLite *active_object;
|
|
|
|
// private methods
|
|
inline void recv() __attribute__((__always_inline__));
|
|
uint8_t rx_pin_read();
|
|
void setTX(uint8_t transmitPin);
|
|
void setRX(uint8_t receivePin);
|
|
inline void setRxIntMsk(bool enable) __attribute__((__always_inline__));
|
|
|
|
// Return num - sub, or 1 if the result would be < 1
|
|
static uint16_t subtract_cap(uint16_t num, uint16_t sub);
|
|
|
|
// private static method for timing
|
|
static inline void tunedDelay(uint16_t delay);
|
|
|
|
public:
|
|
// public methods
|
|
#ifdef LITE
|
|
SoftwareSerialLite(uint8_t receivePin, uint8_t transmitPin);
|
|
void begin(unsigned int speed);
|
|
virtual int peek() {return ;}; //Simply return, deleted function for lite
|
|
#else
|
|
SoftwareSerialLite(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
|
|
~SoftwareSerialLite();
|
|
void begin(long speed);
|
|
bool stopListening();
|
|
void end();
|
|
bool isListening() { return this == active_object; }
|
|
bool listen();
|
|
int peek();
|
|
#endif
|
|
bool overflow() { bool ret = _buffer_overflow; if (ret) _buffer_overflow = false; return ret; }
|
|
|
|
virtual size_t write(uint8_t byte);
|
|
virtual int read();
|
|
virtual int available();
|
|
virtual void flush() {return ;}; //Simply return, no tx buffering
|
|
operator bool() { return true; }
|
|
|
|
using Print::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
|