blynk & neopixelring & w7500

Fork of WIZwiki-7500_Blynk by IOP

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