AltSoftSerial.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* An Alternative Software Serial Library
  2. * http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
  3. * Copyright (c) 2014 PJRC.COM, LLC, Paul Stoffregen, paul@pjrc.com
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #ifndef AltSoftSerial_h
  24. #define AltSoftSerial_h
  25. #include <inttypes.h>
  26. #if ARDUINO >= 100
  27. #include "Arduino.h"
  28. #else
  29. #include "WProgram.h"
  30. #include "pins_arduino.h"
  31. #endif
  32. #if defined(__arm__) && defined(CORE_TEENSY)
  33. #define ALTSS_BASE_FREQ F_BUS
  34. #else
  35. #define ALTSS_BASE_FREQ F_CPU
  36. #endif
  37. class AltSoftSerial : public Stream
  38. {
  39. public:
  40. AltSoftSerial() { }
  41. ~AltSoftSerial() { end(); }
  42. static void begin(uint32_t baud) { init((ALTSS_BASE_FREQ + baud / 2) / baud); }
  43. static void end();
  44. int peek();
  45. int read();
  46. int available();
  47. int availableForWrite();
  48. #if ARDUINO >= 100
  49. size_t write(uint8_t byte) { writeByte(byte); return 1; }
  50. void flush() { flushOutput(); }
  51. #else
  52. void write(uint8_t byte) { writeByte(byte); }
  53. void flush() { flushInput(); }
  54. #endif
  55. using Print::write;
  56. static void flushInput();
  57. static void flushOutput();
  58. // for drop-in compatibility with NewSoftSerial, rxPin & txPin ignored
  59. AltSoftSerial(uint8_t rxPin, uint8_t txPin, bool inverse = false) { }
  60. bool listen() { return false; }
  61. bool isListening() { return true; }
  62. bool overflow() { bool r = timing_error; timing_error = false; return r; }
  63. static int library_version() { return 1; }
  64. static void enable_timer0(bool enable) { }
  65. static bool timing_error;
  66. private:
  67. static void init(uint32_t cycles_per_bit);
  68. static void writeByte(uint8_t byte);
  69. };
  70. #endif