BlynkDebug.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @file BlynkDebug.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 Debug utilities
  8. *
  9. */
  10. #ifndef BlynkDebug_h
  11. #define BlynkDebug_h
  12. #include <Blynk/BlynkConfig.h>
  13. #include <stddef.h>
  14. #ifdef ESP8266
  15. extern "C" {
  16. #include "ets_sys.h"
  17. #include "os_type.h"
  18. #include "mem.h"
  19. }
  20. #else
  21. #include <inttypes.h>
  22. #endif
  23. #if defined(SPARK) || defined(PARTICLE)
  24. #include "application.h"
  25. #endif
  26. #if defined(ARDUINO)
  27. #if ARDUINO >= 100
  28. #include "Arduino.h"
  29. #else
  30. #include "WProgram.h"
  31. #endif
  32. #endif
  33. #if !defined(ARDUINO) || (ARDUINO < 151)
  34. #define BLYNK_NO_YIELD
  35. #endif
  36. // General defines
  37. #define STRINGIFY(x) #x
  38. #define TOSTRING(x) STRINGIFY(x)
  39. #define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
  40. #define BLYNK_ATTR_PACKED __attribute__ ((__packed__))
  41. #define BLYNK_NORETURN __attribute__ ((noreturn))
  42. // Causes problems on some platforms
  43. #define BLYNK_FORCE_INLINE inline //__attribute__((always_inline))
  44. #if defined(__AVR__)
  45. #include <avr/pgmspace.h>
  46. #define BLYNK_HAS_PROGMEM
  47. #define BLYNK_PROGMEM PROGMEM
  48. #define BLYNK_PSTR(s) PSTR(s)
  49. #else
  50. #define BLYNK_PROGMEM
  51. #define BLYNK_PSTR(s) s
  52. #endif
  53. #ifndef LED_BUILTIN
  54. # define LED_BUILTIN 2
  55. #endif
  56. // Diagnostic defines
  57. size_t BlynkFreeRam();
  58. void BlynkReset() BLYNK_NORETURN;
  59. void BlynkFatal() BLYNK_NORETURN;
  60. #define BLYNK_FATAL(msg, ...){ BLYNK_LOG(msg, ##__VA_ARGS__); BlynkFatal(); }
  61. #define BLYNK_LOG_RAM() { BLYNK_LOG("Free RAM: %d", BlynkFreeRam()); }
  62. #define BLYNK_LOG_FN() BLYNK_LOG("%s@%d", __FUNCTION__, __LINE__);
  63. #define BLYNK_LOG_TROUBLE(t) BLYNK_LOG("Trouble detected: http://docs.blynk.cc/#troubleshooting-%s", t)
  64. #ifdef BLYNK_PRINT
  65. #if defined(ARDUINO) || defined(SPARK) || defined(PARTICLE)
  66. #include <stdio.h>
  67. #include <stdarg.h>
  68. #define BLYNK_DBG_DUMP(msg, addr, len) if (len) { BLYNK_PRINT.print(msg); BLYNK_PRINT.write((uint8_t*)addr, len); BLYNK_PRINT.println(); }
  69. #define BLYNK_DBG_BREAK() { for(;;); }
  70. #if defined(__SAM3X8E__)
  71. #define BLYNK_LOG(msg, ...) blynk_dbg_print(msg, ##__VA_ARGS__)
  72. #else
  73. #define BLYNK_LOG(msg, ...) blynk_dbg_print(BLYNK_PSTR(msg), ##__VA_ARGS__)
  74. #endif
  75. #define BLYNK_ASSERT(expr) { if(!(expr)) { BLYNK_LOG("Assertion %s failed.", #expr); BLYNK_DBG_BREAK() } }
  76. static
  77. void blynk_dbg_print(const char* BLYNK_PROGMEM fmt, ...)
  78. {
  79. va_list ap;
  80. va_start(ap, fmt);
  81. char buff[128];
  82. BLYNK_PRINT.print('[');
  83. BLYNK_PRINT.print(millis());
  84. BLYNK_PRINT.print(F("] "));
  85. #if defined(__AVR__)
  86. vsnprintf_P(buff, sizeof(buff), fmt, ap);
  87. #else
  88. vsnprintf(buff, sizeof(buff), fmt, ap);
  89. #endif
  90. BLYNK_PRINT.println(buff);
  91. va_end(ap);
  92. }
  93. #elif defined(LINUX) || defined(MBED_LIBRARY_VERSION)
  94. #include <assert.h>
  95. #include <stdio.h>
  96. #include <string.h>
  97. #include <errno.h>
  98. #include <signal.h>
  99. #define BLYNK_DBG_DUMP(msg, addr, len) if (len) { fprintf(BLYNK_PRINT, msg); fwrite(addr, len, 1, BLYNK_PRINT); fputc('\n', BLYNK_PRINT); }
  100. #define BLYNK_DBG_BREAK() raise(SIGTRAP);
  101. #define BLYNK_LOG(msg, ...) { fprintf(BLYNK_PRINT, "[%ld] " msg "\n", millis(), ##__VA_ARGS__); }
  102. #define BLYNK_ASSERT(expr) assert(expr)
  103. #elif defined(WINDOWS)
  104. #include <windows.h>
  105. #include <stdio.h>
  106. #define BLYNK_DBG_DUMP(msg, addr, len)
  107. #define BLYNK_DBG_BREAK() DebugBreak();
  108. #define BLYNK_LOG(...) { char buff[1024]; snprintf(buff, sizeof(buff), __VA_ARGS__); OutputDebugString(buff); }
  109. #define BLYNK_ASSERT(expr) { if(!(expr)) { BLYNK_DBG_BREAK() } }
  110. #else
  111. #warning Could not detect platform
  112. #define BLYNK_DBG_DUMP(msg, addr, len)
  113. #define BLYNK_DBG_BREAK() { *(char*)(NULL) = 0xFF; } // SEGV!!!
  114. #define BLYNK_LOG(...)
  115. #define BLYNK_ASSERT(expr) { if(!(expr)) { BLYNK_DBG_BREAK() } }
  116. #endif
  117. #else
  118. #define BLYNK_DBG_BREAK()
  119. #define BLYNK_LOG(msg, ...)
  120. #define BLYNK_ASSERT(expr)
  121. #endif
  122. #endif