Blynk library for embedded hardware. Works with Arduino, ESP8266, Raspberry Pi, Intel Edison/Galileo, LinkIt ONE, Particle Core/Photon, Energia, ARM mbed, etc. http://www.blynk.cc/

Dependents:   Blynk_RBL_BLE_Nano Blynk_MicroBit Blynk_Serial Blynk_RBL_BLE_Nano

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlynkDebug.h Source File

BlynkDebug.h

Go to the documentation of this file.
00001 /**
00002  * @file       BlynkDebug.h
00003  * @author     Volodymyr Shymanskyy
00004  * @license    This project is released under the MIT License (MIT)
00005  * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
00006  * @date       Jan 2015
00007  * @brief      Debug utilities
00008  *
00009  */
00010 
00011 #ifndef BlynkDebug_h
00012 #define BlynkDebug_h
00013 
00014 #include <Blynk/BlynkConfig.h>
00015 
00016 #include <stddef.h>
00017 #ifdef ESP8266
00018     extern "C" {
00019     #include "ets_sys.h"
00020     #include "os_type.h"
00021     #include "mem.h"
00022     }
00023 #else
00024     #include <inttypes.h>
00025 #endif
00026 
00027 #if defined(ARDUINO_ARCH_ARC32)
00028     typedef uint64_t millis_time_t;
00029 #else
00030     typedef uint32_t millis_time_t;
00031 #endif
00032 
00033 void            BlynkSystemInit();
00034 void            BlynkDelay(millis_time_t ms);
00035 millis_time_t   BlynkMillis();
00036 size_t          BlynkFreeRam();
00037 void            BlynkReset() BLYNK_NORETURN;
00038 void            BlynkFatal() BLYNK_NORETURN;
00039 
00040 
00041 #if defined(SPARK) || defined(PARTICLE)
00042     #include "application.h"
00043 #endif
00044 
00045 #if defined(ARDUINO)
00046     #if ARDUINO >= 100
00047         #include <Arduino.h>
00048     #else
00049         #include <WProgram.h>
00050     #endif
00051 #endif
00052 
00053 #if defined(LINUX)
00054     #if defined(RASPBERRY)
00055         #include <wiringPi.h>
00056     #endif
00057 #endif
00058 
00059 #if !defined(BLYNK_RUN_YIELD)
00060     #if defined(BLYNK_NO_YIELD)
00061         #define BLYNK_RUN_YIELD() {}
00062     #elif defined(SPARK) || defined(PARTICLE)
00063         #define BLYNK_RUN_YIELD() { Particle.process(); }
00064     #elif !defined(ARDUINO) || (ARDUINO < 151)
00065         #define BLYNK_RUN_YIELD() {}
00066     #else
00067         #define BLYNK_RUN_YIELD() { BlynkDelay(0); }
00068     #endif
00069 #endif
00070 
00071 #if defined(__AVR__)
00072     #include <avr/pgmspace.h>
00073     #define BLYNK_HAS_PROGMEM
00074     #define BLYNK_PROGMEM PROGMEM
00075     #define BLYNK_F(s) F(s)
00076     #define BLYNK_PSTR(s) PSTR(s)
00077 #else
00078     #define BLYNK_PROGMEM
00079     #define BLYNK_F(s) s
00080     #define BLYNK_PSTR(s) s
00081 #endif
00082 
00083 #ifdef ARDUINO_AVR_DIGISPARK
00084     typedef fstr_t __FlashStringHelper;
00085 #endif
00086 
00087 #if defined(BLYNK_DEBUG_ALL) && !(__cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__))
00088     #warning "Compiler features not enabled -> please contact yor board vendor to enable c++0x"
00089 #endif
00090 
00091 // Diagnostic defines
00092 
00093 #define BLYNK_FATAL(msg)     { BLYNK_LOG1(msg); BlynkFatal(); }
00094 #define BLYNK_LOG_RAM()      { BLYNK_LOG2(BLYNK_F("Free RAM: "), BlynkFreeRam()); }
00095 #define BLYNK_LOG_FN()       BLYNK_LOG3(BLYNK_F(__FUNCTION__), '@', __LINE__);
00096 #define BLYNK_LOG_TROUBLE(t) BLYNK_LOG2(BLYNK_F("Trouble detected: http://docs.blynk.cc/#troubleshooting-"), t)
00097 
00098 #ifndef BLYNK_PRINT
00099 #undef BLYNK_DEBUG
00100 #endif
00101 
00102 #ifdef BLYNK_DEBUG_ALL
00103 #define BLYNK_DEBUG
00104 #endif
00105 
00106 #ifdef BLYNK_PRINT
00107 
00108     #if defined(ARDUINO) || defined(SPARK) || defined(PARTICLE)
00109 
00110 #if defined(ARDUINO_ARCH_ARC32)
00111         // This will cause error - on purpose
00112         #define BLYNK_LOG(msg, ...)  BLYNK_LOG_UNAVAILABLE(msg, ##__VA_ARGS__)
00113 #else
00114         #define BLYNK_LOG(msg, ...)  blynk_dbg_print(BLYNK_PSTR(msg), ##__VA_ARGS__)
00115 #endif
00116 
00117         #define BLYNK_LOG1(p1)            { BLYNK_LOG_TIME(); BLYNK_PRINT.println(p1); }
00118         #define BLYNK_LOG2(p1,p2)         { BLYNK_LOG_TIME(); BLYNK_PRINT.print(p1); BLYNK_PRINT.println(p2); }
00119         #define BLYNK_LOG3(p1,p2,p3)      { BLYNK_LOG_TIME(); BLYNK_PRINT.print(p1); BLYNK_PRINT.print(p2); BLYNK_PRINT.println(p3); }
00120         #define BLYNK_LOG4(p1,p2,p3,p4)   { BLYNK_LOG_TIME(); BLYNK_PRINT.print(p1); BLYNK_PRINT.print(p2); BLYNK_PRINT.print(p3); BLYNK_PRINT.println(p4); }
00121         #define BLYNK_LOG6(p1,p2,p3,p4,p5,p6) { BLYNK_LOG_TIME(); BLYNK_PRINT.print(p1); BLYNK_PRINT.print(p2); BLYNK_PRINT.print(p3); BLYNK_PRINT.print(p4); BLYNK_PRINT.print(p5); BLYNK_PRINT.println(p6); }
00122         #define BLYNK_LOG_IP(msg, ip)     { BLYNK_LOG_TIME(); BLYNK_PRINT.print(BLYNK_F(msg)); \
00123                                             BLYNK_PRINT.print(ip[0]); BLYNK_PRINT.print('.');  \
00124                                             BLYNK_PRINT.print(ip[1]); BLYNK_PRINT.print('.');  \
00125                                             BLYNK_PRINT.print(ip[2]); BLYNK_PRINT.print('.');  \
00126                                             BLYNK_PRINT.println(ip[3]); }
00127         #define BLYNK_LOG_IP_REV(msg, ip) { BLYNK_LOG_TIME(); BLYNK_PRINT.print(BLYNK_F(msg)); \
00128                                             BLYNK_PRINT.print(ip[3]); BLYNK_PRINT.print('.');  \
00129                                             BLYNK_PRINT.print(ip[2]); BLYNK_PRINT.print('.');  \
00130                                             BLYNK_PRINT.print(ip[1]); BLYNK_PRINT.print('.');  \
00131                                             BLYNK_PRINT.println(ip[0]); }
00132 
00133         static
00134         void BLYNK_LOG_TIME() {
00135             BLYNK_PRINT.print('[');
00136             BLYNK_PRINT.print(BlynkMillis());
00137             BLYNK_PRINT.print(BLYNK_F("] "));
00138         }
00139 
00140 #ifdef BLYNK_DEBUG
00141         #include <ctype.h>
00142         #define BLYNK_DBG_BREAK()    { for(;;); }
00143         #define BLYNK_ASSERT(expr)   { if(!(expr)) { BLYNK_LOG2(BLYNK_F("Assertion failed: "), BLYNK_F(#expr)); BLYNK_DBG_BREAK() } }
00144 
00145         static
00146         void BLYNK_DBG_DUMP(const char* msg, const void* addr, size_t len) {
00147             if (len) {
00148                 BLYNK_LOG_TIME();
00149                 BLYNK_PRINT.print(msg);
00150                 int l2 = len;
00151                 const uint8_t* octets = (const uint8_t*)addr;
00152                 bool prev_print = true;
00153                 while (l2--) {
00154                     const uint8_t c = *octets++ & 0xFF;
00155                     if (c >= 32 && c < 127) {
00156                         if (!prev_print) { BLYNK_PRINT.print(']'); }
00157                         BLYNK_PRINT.print((char)c);
00158                         prev_print = true;
00159                     } else {
00160                         BLYNK_PRINT.print(prev_print?'[':'|');
00161                         if (c < 0x10) { BLYNK_PRINT.print('0'); }
00162                         BLYNK_PRINT.print(c, HEX);
00163                         prev_print = false;
00164                     }
00165                 }
00166                 if (!prev_print) {
00167                     BLYNK_PRINT.print(']');
00168                 }
00169                 BLYNK_PRINT.println();
00170             }
00171         }
00172 #endif
00173 
00174         #if !defined(ARDUINO_ARCH_ARC32)
00175         #include <stdio.h>
00176         #include <stdarg.h>
00177 
00178         BLYNK_UNUSED
00179         void blynk_dbg_print(const char* BLYNK_PROGMEM fmt, ...)
00180         {
00181             va_list ap;
00182             va_start(ap, fmt);
00183             char buff[128];
00184             BLYNK_PRINT.print('[');
00185             BLYNK_PRINT.print(BlynkMillis());
00186             BLYNK_PRINT.print(BLYNK_F("] "));
00187 #if defined(__AVR__)
00188             vsnprintf_P(buff, sizeof(buff), fmt, ap);
00189 #else
00190             vsnprintf(buff, sizeof(buff), fmt, ap);
00191 #endif
00192             BLYNK_PRINT.println(buff);
00193             va_end(ap);
00194         }
00195         #endif // ARDUINO_ARCH_ARC32
00196 
00197     #elif defined(__MBED__)
00198 
00199         #define BLYNK_LOG(msg, ...)       { BLYNK_PRINT.printf("[%ld] " msg "\n", BlynkMillis(), ##__VA_ARGS__); }
00200         #define BLYNK_LOG1(p1)            { BLYNK_LOG(p1);}
00201         #define BLYNK_LOG2(p1,p2)         { BLYNK_LOG(p1,p2);}
00202         #define BLYNK_LOG3(p1,p2,p3)      { BLYNK_LOG(p1,p2,p3);}
00203         #define BLYNK_LOG4(p1,p2,p3,p4)   { BLYNK_LOG(p1,p2,p3,p4);}
00204         #define BLYNK_LOG6(p1,p2,p3,p4,p5,p6)   { BLYNK_LOG(p1,p2,p3,p4,p5,p6);}
00205 
00206         #define BLYNK_LOG_TIME() BLYNK_PRINT.printf("[%ld]", BlynkMillis());
00207 
00208 #ifdef BLYNK_DEBUG
00209         #define BLYNK_DBG_BREAK()    raise(SIGTRAP);
00210         #define BLYNK_ASSERT(expr)   assert(expr)
00211 
00212         static
00213         void BLYNK_DBG_DUMP(const char* msg, const void* addr, size_t len) {
00214             BLYNK_LOG_TIME();
00215             BLYNK_PRINT.printf(msg);
00216             int l2 = len;
00217             const uint8_t* octets = (const uint8_t*)addr;
00218             bool prev_print = true;
00219             while (l2--) {
00220                 const uint8_t c = *octets++ & 0xFF;
00221                 if (c >= 32 && c < 127) {
00222                     if (!prev_print) { BLYNK_PRINT.putc(']'); }
00223                     BLYNK_PRINT.putc((char)c);
00224                     prev_print = true;
00225                 } else {
00226                     BLYNK_PRINT.putc(prev_print?'[':'|');
00227                     BLYNK_PRINT.printf("%02x", c);
00228                     prev_print = false;
00229                 }
00230             }
00231             BLYNK_PRINT.printf("%s\n", prev_print?"":"]");
00232         }
00233 #endif
00234 
00235     #elif defined(LINUX)
00236 
00237         #include <assert.h>
00238         #include <stdio.h>
00239         #include <string.h>
00240         #include <errno.h>
00241         #include <signal.h>
00242 
00243         #include <iostream>
00244         using namespace std;
00245         #define BLYNK_LOG(msg, ...)       { fprintf(BLYNK_PRINT, "[%ld] " msg "\n", BlynkMillis(), ##__VA_ARGS__); }
00246         #define BLYNK_LOG1(p1)            { BLYNK_LOG_TIME(); cout << p1 << endl; }
00247         #define BLYNK_LOG2(p1,p2)         { BLYNK_LOG_TIME(); cout << p1 << p2 << endl; }
00248         #define BLYNK_LOG3(p1,p2,p3)      { BLYNK_LOG_TIME(); cout << p1 << p2 << p3 << endl; }
00249         #define BLYNK_LOG4(p1,p2,p3,p4)   { BLYNK_LOG_TIME(); cout << p1 << p2 << p3 << p4 << endl; }
00250         #define BLYNK_LOG6(p1,p2,p3,p4,p5,p6)   { BLYNK_LOG_TIME(); cout << p1 << p2 << p3 << p4 << p5 << p6 << endl; }
00251 
00252         #define BLYNK_LOG_TIME() cout << '[' << BlynkMillis() << "] ";
00253 
00254 #ifdef BLYNK_DEBUG
00255         #define BLYNK_DBG_BREAK()    raise(SIGTRAP);
00256         #define BLYNK_ASSERT(expr)   assert(expr)
00257 
00258         static
00259         void BLYNK_DBG_DUMP(const char* msg, const void* addr, size_t len) {
00260             BLYNK_LOG_TIME();
00261             fprintf(BLYNK_PRINT, "%s", msg);
00262             int l2 = len;
00263             const uint8_t* octets = (const uint8_t*)addr;
00264             bool prev_print = true;
00265             while (l2--) {
00266                 const uint8_t c = *octets++ & 0xFF;
00267                 if (c >= 32 && c < 127) {
00268                     if (!prev_print) { fputc(']', BLYNK_PRINT); }
00269                     fputc((char)c, BLYNK_PRINT);
00270                     prev_print = true;
00271                 } else {
00272                     fputc(prev_print?'[':'|', BLYNK_PRINT);
00273                     fprintf(BLYNK_PRINT, "%02x", c);
00274                     prev_print = false;
00275                 }
00276             }
00277             fprintf(BLYNK_PRINT, "%s\n", prev_print?"":"]");
00278         }
00279 #endif
00280 
00281     #else
00282 
00283         #warning "Cannot detect platform"
00284 
00285     #endif
00286 
00287 #endif
00288 
00289 #ifndef BLYNK_LOG
00290     #define BLYNK_LOG(...)
00291     #define BLYNK_LOG1(p1)
00292     #define BLYNK_LOG2(p1,p2)
00293     #define BLYNK_LOG3(p1,p2,p3)
00294     #define BLYNK_LOG4(p1,p2,p3,p4)
00295     #define BLYNK_LOG6(p1,p2,p3,p4,p5,p6)
00296     #define BLYNK_LOG_IP(msg, ip)
00297     #define BLYNK_LOG_IP_REV(msg, ip)
00298 #endif
00299 
00300 #ifndef BLYNK_DBG_BREAK
00301     #define BLYNK_DBG_BREAK()
00302     #define BLYNK_ASSERT(expr)
00303     #define BLYNK_DBG_DUMP(msg, addr, len)
00304 #endif
00305 
00306 #endif