SHARE
TWEET

SoftwareSerial.h

a guest Sep 16th, 2012 2,473 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. SoftwareSerial.h (formerly NewSoftSerial.h) -
  3. Multi-instance software serial library for Arduino/Wiring
  4. -- Interrupt-driven receive and other improvements by ladyada
  5.    (http://ladyada.net)
  6. -- Tuning, circular buffer, derivation from class Print/Stream,
  7.    multi-instance support, porting to 8MHz processors,
  8.    various optimizations, PROGMEM delay tables, inverse logic and
  9.    direct port writing by Mikal Hart (http://www.arduiniana.org)
  10. -- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
  11. -- 20MHz processor support by Garrett Mace (http://www.macetech.com)
  12. -- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
  13.  
  14. This library is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU Lesser General Public
  16. License as published by the Free Software Foundation; either
  17. version 2.1 of the License, or (at your option) any later version.
  18.  
  19. This library is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  22. Lesser General Public License for more details.
  23.  
  24. You should have received a copy of the GNU Lesser General Public
  25. License along with this library; if not, write to the Free Software
  26. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  27.  
  28. The latest version of this library can always be found at
  29. http://arduiniana.org.
  30. */
  31.  
  32. #ifndef SoftwareSerial_h
  33. #define SoftwareSerial_h
  34.  
  35. #include <inttypes.h>
  36. #include <Stream.h>
  37.  
  38. /******************************************************************************
  39. * Definitions
  40. ******************************************************************************/
  41.  
  42. #define _SS_MAX_RX_BUFF 64 // RX buffer size
  43. #ifndef GCC_VERSION
  44. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  45. #endif
  46.  
  47. class SoftwareSerial : public Stream
  48. {
  49. private:
  50.   // per object data
  51.   uint8_t _receivePin;
  52.   uint8_t _receiveBitMask;
  53.   volatile uint8_t *_receivePortRegister;
  54.   uint8_t _transmitBitMask;
  55.   volatile uint8_t *_transmitPortRegister;
  56.  
  57.   uint16_t _rx_delay_centering;
  58.   uint16_t _rx_delay_intrabit;
  59.   uint16_t _rx_delay_stopbit;
  60.   uint16_t _tx_delay;
  61.  
  62.   uint16_t _buffer_overflow:1;
  63.   uint16_t _inverse_logic:1;
  64.  
  65.   // static data
  66.   static char _receive_buffer[_SS_MAX_RX_BUFF];
  67.   static volatile uint8_t _receive_buffer_tail;
  68.   static volatile uint8_t _receive_buffer_head;
  69.   static SoftwareSerial *active_object;
  70.  
  71.   // private methods
  72.   void recv();
  73.   uint8_t rx_pin_read();
  74.   void tx_pin_write(uint8_t pin_state);
  75.   void setTX(uint8_t transmitPin);
  76.   void setRX(uint8_t receivePin);
  77.  
  78.   // private static method for timing
  79.   static inline void tunedDelay(uint16_t delay);
  80.  
  81. public:
  82.   // public methods
  83.   SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
  84.   ~SoftwareSerial();
  85.   void begin(long speed);
  86.   bool listen();
  87.   void end();
  88.   bool isListening() { return this == active_object; }
  89.   bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
  90.   int peek();
  91.  
  92.   virtual size_t write(uint8_t byte);
  93.   virtual int read();
  94.   virtual int available();
  95.   virtual void flush();
  96.  
  97.   using Print::write;
  98.  
  99.   // public only for easy access by interrupt handlers
  100.   static inline void handle_interrupt();
  101. };
  102.  
  103. // Arduino 0012 workaround
  104. #undef int
  105. #undef char
  106. #undef long
  107. #undef byte
  108. #undef float
  109. #undef abs
  110. #undef round
  111.  
  112. #endif
RAW Paste Data
Top