123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- /*
- * 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 <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
|