SoftwareSerial.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. This library is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU Lesser General Public
  15. License as published by the Free Software Foundation; either
  16. version 2.1 of the License, or (at your option) any later version.
  17. This library is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. Lesser General Public License for more details.
  21. You should have received a copy of the GNU Lesser General Public
  22. License along with this library; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. The latest version of this library can always be found at
  25. http://arduiniana.org.
  26. */
  27. #ifndef SoftwareSerial_h
  28. #define SoftwareSerial_h
  29. #include <inttypes.h>
  30. #include <Stream.h>
  31. /******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. #define _SS_MAX_RX_BUFF 64 // RX buffer size
  35. #ifndef GCC_VERSION
  36. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  37. #endif
  38. class SoftwareSerial : public Stream
  39. {
  40. private:
  41. // per object data
  42. uint8_t _receivePin;
  43. uint8_t _receiveBitMask;
  44. volatile uint8_t *_receivePortRegister;
  45. uint8_t _transmitBitMask;
  46. volatile uint8_t *_transmitPortRegister;
  47. uint16_t _rx_delay_centering;
  48. uint16_t _rx_delay_intrabit;
  49. uint16_t _rx_delay_stopbit;
  50. uint16_t _tx_delay;
  51. uint16_t _buffer_overflow:1;
  52. uint16_t _inverse_logic:1;
  53. // static data
  54. static char _receive_buffer[_SS_MAX_RX_BUFF];
  55. static volatile uint8_t _receive_buffer_tail;
  56. static volatile uint8_t _receive_buffer_head;
  57. static SoftwareSerial *active_object;
  58. // private methods
  59. void recv();
  60. uint8_t rx_pin_read();
  61. void tx_pin_write(uint8_t pin_state);
  62. void setTX(uint8_t transmitPin);
  63. void setRX(uint8_t receivePin);
  64. // private static method for timing
  65. static inline void tunedDelay(uint16_t delay);
  66. public:
  67. // public methods
  68. SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
  69. ~SoftwareSerial();
  70. void begin(long speed);
  71. bool listen();
  72. void end();
  73. bool isListening() { return this == active_object; }
  74. bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
  75. int peek();
  76. virtual size_t write(uint8_t byte);
  77. virtual int read();
  78. virtual int available();
  79. virtual void flush();
  80. using Print::write;
  81. // public only for easy access by interrupt handlers
  82. static inline void handle_interrupt();
  83. };
  84. // Arduino 0012 workaround
  85. #undef int
  86. #undef char
  87. #undef long
  88. #undef byte
  89. #undef float
  90. #undef abs
  91. #undef round
  92. #endif