PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SLIPEncodedUSBSerial.h 2.3KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Extends the Serial class to encode SLIP over serial
  3. */
  4. #include "Arduino.h"
  5. #ifndef SLIPEncodedUSBSerial_h
  6. #define SLIPEncodedUSBSerial_h
  7. #include <Stream.h>
  8. #if (defined(TEENSYDUINO) && defined(USB_SERIAL)) || (!defined(TEENSYDUINO) && defined(__AVR_ATmega32U4__)) || defined(__SAM3X8E__) || (defined(_USB) && defined(_USE_USB_FOR_SERIAL_)) || defined(BOARD_maple_mini)
  9. //import the serial USB object
  10. #if defined(TEENSYDUINO) && defined (__arm__)
  11. #include <usb_serial.h>
  12. #elif defined(TEENSYDUINO) && defined (__AVR__)
  13. #include <usb_api.h>
  14. #elif defined(BOARD_maple_mini)
  15. #include <usb_serial.h>
  16. #elif defined(__SAM3X8E__)
  17. #include <USB/USBAPI.h>
  18. #elif defined(__PIC32MX__)
  19. #include "HardwareSerial.h"
  20. #elif defined(__AVR_ATmega32U4__)
  21. // leonardo
  22. //#include "Platform.h"
  23. #include "USBAPI.h"
  24. #include <avr/wdt.h>
  25. #else
  26. #error Unknown USB port
  27. #endif
  28. class SLIPEncodedUSBSerial: public Stream{
  29. private:
  30. enum erstate {CHAR, FIRSTEOT, SECONDEOT, SLIPESC } rstate;
  31. //different type for each platform
  32. #if defined(CORE_TEENSY)
  33. usb_serial_class
  34. #elif defined(__SAM3X8E__) || defined(__AVR_ATmega32U4__)
  35. Serial_
  36. #elif defined(__PIC32MX__) || defined(BOARD_maple_mini)
  37. USBSerial
  38. #else
  39. #error Unknown USBserial type
  40. #endif
  41. * serial;
  42. public:
  43. SLIPEncodedUSBSerial(
  44. //different constructor for each platform
  45. #if defined(CORE_TEENSY)
  46. usb_serial_class
  47. #elif defined(__SAM3X8E__) || defined(__AVR_ATmega32U4__)
  48. Serial_
  49. #elif defined(__PIC32MX__) || defined(BOARD_maple_mini)
  50. USBSerial
  51. #else
  52. #error Unknown USBserial type
  53. #endif
  54. & );
  55. int available();
  56. int read();
  57. int readBytes( uint8_t *buffer, size_t size);
  58. int peek();
  59. void flush();
  60. //same as Serial.begin
  61. void begin(unsigned long);
  62. //SLIP specific method which begins a transmitted packet
  63. void beginPacket();
  64. //SLIP specific method which ends a transmittedpacket
  65. void endPacket();
  66. // SLIP specific method which indicates that an EOT was received
  67. bool endofPacket();
  68. //the arduino and wiring libraries have different return types for the write function
  69. #if defined(WIRING) || defined(BOARD_DEFS_H)
  70. void write(uint8_t b);
  71. void write(const uint8_t *buffer, size_t size);
  72. #else
  73. //overrides the Stream's write function to encode SLIP
  74. size_t write(uint8_t b);
  75. size_t write(const uint8_t *buffer, size_t size);
  76. //using Print::write;
  77. #endif
  78. };
  79. #endif
  80. #endif