WidgetTerminal.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * @file WidgetTerminal.h
  3. * @author Volodymyr Shymanskyy
  4. * @license This project is released under the MIT License (MIT)
  5. * @copyright Copyright (c) 2015 Volodymyr Shymanskyy
  6. * @date Jan 2015
  7. * @brief
  8. */
  9. #ifndef WidgetTerminal_h
  10. #define WidgetTerminal_h
  11. #if !(defined(LINUX) || defined(MBED_LIBRARY_VERSION))
  12. #define BLYNK_USE_PRINT_CLASS
  13. #endif
  14. #include <Blynk/BlynkApi.h>
  15. #ifdef BLYNK_USE_PRINT_CLASS
  16. #if !(defined(SPARK) || defined(PARTICLE) || (PLATFORM_ID==88) || defined(ARDUINO_RedBear_Duo)) // 88 -> RBL Duo
  17. // On Particle this is auto-included
  18. #include <Print.h>
  19. #endif
  20. #endif
  21. class WidgetTerminal
  22. #ifdef BLYNK_USE_PRINT_CLASS
  23. : public Print
  24. #endif
  25. {
  26. public:
  27. WidgetTerminal(int vPin)
  28. : mPin(vPin), mOutQty(0)
  29. {}
  30. virtual size_t write(uint8_t byte) {
  31. mOutBuf[mOutQty++] = byte;
  32. if (mOutQty >= sizeof(mOutBuf)) {
  33. flush();
  34. }
  35. return 1;
  36. }
  37. void flush() {
  38. if (mOutQty) {
  39. Blynk.virtualWriteBinary(mPin, mOutBuf, mOutQty);
  40. mOutQty = 0;
  41. }
  42. }
  43. #ifdef BLYNK_USE_PRINT_CLASS
  44. using Print::write;
  45. size_t write(const void* buff, size_t len) {
  46. return write((char*)buff, len);
  47. }
  48. #else
  49. size_t write(const void* buff, size_t len) {
  50. uint8_t* buf = (uint8_t*)buff;
  51. while (len--) {
  52. write(*buf++);
  53. }
  54. return len;
  55. }
  56. size_t write(const char* str) {
  57. return write(str, strlen(str));
  58. }
  59. #endif
  60. private:
  61. uint8_t mPin;
  62. uint8_t mOutBuf[32];
  63. uint8_t mOutQty;
  64. };
  65. #endif