![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A cute tiny piece of code implementing an IoT NAND device, demonstrating how to setup and advertise a cute GATT (NAND) service. The code has been tested on a Nordic nRF51822-DK.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate_IDB0XA1 by
bricks/trace.cpp@23:2e73c391bb12, 2017-01-08 (annotated)
- Committer:
- hux
- Date:
- Sun Jan 08 23:15:53 2017 +0000
- Revision:
- 23:2e73c391bb12
A cute tiny piece of code implementing an Iot NAND device, demonstrating how to setup and advertise a cute GATT (NAND) service.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 23:2e73c391bb12 | 1 | // trace.cpp - trace a message depending on verbose level |
hux | 23:2e73c391bb12 | 2 | // |
hux | 23:2e73c391bb12 | 3 | |
hux | 23:2e73c391bb12 | 4 | #include "bricks/trace.h" |
hux | 23:2e73c391bb12 | 5 | |
hux | 23:2e73c391bb12 | 6 | Serial pc(USBTX, USBRX); // serial port to PC terminal |
hux | 23:2e73c391bb12 | 7 | |
hux | 23:2e73c391bb12 | 8 | //============================================================================== |
hux | 23:2e73c391bb12 | 9 | // Managing the Verbose Lewvel |
hux | 23:2e73c391bb12 | 10 | //============================================================================== |
hux | 23:2e73c391bb12 | 11 | |
hux | 23:2e73c391bb12 | 12 | static int threshold = 0; // verbose threshold, no verbose messages |
hux | 23:2e73c391bb12 | 13 | |
hux | 23:2e73c391bb12 | 14 | void verbose(O&o, int level) // setup verbose level |
hux | 23:2e73c391bb12 | 15 | { |
hux | 23:2e73c391bb12 | 16 | threshold = level; // update verbose threshold |
hux | 23:2e73c391bb12 | 17 | } |
hux | 23:2e73c391bb12 | 18 | |
hux | 23:2e73c391bb12 | 19 | //============================================================================== |
hux | 23:2e73c391bb12 | 20 | // Printing Trace Messages |
hux | 23:2e73c391bb12 | 21 | //============================================================================== |
hux | 23:2e73c391bb12 | 22 | |
hux | 23:2e73c391bb12 | 23 | void trace(O&o, int level, const char *msg) // trace a message |
hux | 23:2e73c391bb12 | 24 | { |
hux | 23:2e73c391bb12 | 25 | if (level <= threshold) // update verbose threshold |
hux | 23:2e73c391bb12 | 26 | { char buf[2] = " "; // must be terminated |
hux | 23:2e73c391bb12 | 27 | |
hux | 23:2e73c391bb12 | 28 | for (; *msg; msg++) |
hux | 23:2e73c391bb12 | 29 | { buf[0] = *msg; |
hux | 23:2e73c391bb12 | 30 | pc.printf(buf); |
hux | 23:2e73c391bb12 | 31 | if (*msg == '\n') |
hux | 23:2e73c391bb12 | 32 | { buf[0] = '\r'; // auto adding carriage return |
hux | 23:2e73c391bb12 | 33 | pc.printf(buf); |
hux | 23:2e73c391bb12 | 34 | } |
hux | 23:2e73c391bb12 | 35 | } |
hux | 23:2e73c391bb12 | 36 | } |
hux | 23:2e73c391bb12 | 37 | } |
hux | 23:2e73c391bb12 | 38 | |
hux | 23:2e73c391bb12 | 39 | void trace(O&o, int level, const char*format, int value) |
hux | 23:2e73c391bb12 | 40 | { |
hux | 23:2e73c391bb12 | 41 | char buf[80]; |
hux | 23:2e73c391bb12 | 42 | if (strlen(format) > 70) |
hux | 23:2e73c391bb12 | 43 | trace(o,level," *** buffer overflow *** "); |
hux | 23:2e73c391bb12 | 44 | else |
hux | 23:2e73c391bb12 | 45 | { sprintf(buf,format,value); |
hux | 23:2e73c391bb12 | 46 | trace(o,level,buf); |
hux | 23:2e73c391bb12 | 47 | } |
hux | 23:2e73c391bb12 | 48 | } |
hux | 23:2e73c391bb12 | 49 | |
hux | 23:2e73c391bb12 | 50 | void traceln(O&o, int level, const char*msg) // trace with CR & LF |
hux | 23:2e73c391bb12 | 51 | { |
hux | 23:2e73c391bb12 | 52 | trace(o,level,msg); trace(o,level,"\r\n"); |
hux | 23:2e73c391bb12 | 53 | } |