Skip to content

Commit 79d96c7

Browse files
authored
Improve examples so that they work for small MCUs (e.g. Arduino Uno). (#123)
- If NOTE_LOWMEM is defined, don't call notecard.setDebugOutputStream. This saves RAM by removing logging strings. - Don't use notecard.logDebug for application logging (i.e. logging in the .ino files).
1 parent d1c69b7 commit 79d96c7

File tree

9 files changed

+124
-48
lines changed

9 files changed

+124
-48
lines changed

examples/Example1_NotecardBasics/Example1_NotecardBasics.ino

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,14 @@ void setup()
6969
const size_t usb_timeout_ms = 3000;
7070
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
7171
;
72+
73+
// For low-memory platforms, don't turn on internal Notecard logs.
74+
#ifndef NOTE_C_LOW_MEM
7275
notecard.setDebugOutputStream(usbSerial);
73-
#endif
76+
#else
77+
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
78+
#endif // !NOTE_C_LOW_MEM
79+
#endif // usbSerial
7480

7581
// Initialize the physical I/O channel to the Notecard
7682
#ifdef txRxPinsSerial
@@ -127,7 +133,7 @@ void loop()
127133
static unsigned eventCounter = 0;
128134
if (++eventCounter > 25)
129135
{
130-
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
136+
usbSerial.println("[APP] Demo cycle complete. Program stopped. Press RESET to restart.");
131137
delay(10000); // 10 seconds
132138
return;
133139
}

examples/Example2_PeriodicCommunications/Example2_PeriodicCommunications.ino

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,14 @@ void setup()
8888
const size_t usb_timeout_ms = 3000;
8989
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
9090
;
91+
92+
// For low-memory platforms, don't turn on internal Notecard logs.
93+
#ifndef NOTE_C_LOW_MEM
9194
notecard.setDebugOutputStream(usbSerial);
92-
#endif
95+
#else
96+
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
97+
#endif // !NOTE_C_LOW_MEM
98+
#endif // usbSerial
9399

94100
// Initialize the physical I/O channel to the Notecard
95101
#ifdef txRxPinsSerial
@@ -151,7 +157,7 @@ void loop()
151157
if (millis() > lastStatusMs + 10000)
152158
{
153159
lastStatusMs = millis();
154-
notecard.logDebug("press button to simulate a sensor measurement\n");
160+
usbSerial.println("[APP] press button to simulate a sensor measurement");
155161
}
156162
delay(25);
157163
digitalWrite(ledPin, LOW);
@@ -165,7 +171,7 @@ void loop()
165171
}
166172

167173
// The button was pressed, so we should begin a transaction
168-
notecard.logDebug("performing sensor measurement\n");
174+
usbSerial.println("[APP] performing sensor measurement");
169175
lastStatusMs = millis();
170176

171177
// Read the notecard's current temperature and voltage, as simulated sensor

examples/Example3_InboundPolling/Example3_InboundPolling.ino

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ void setup()
5858
const size_t usb_timeout_ms = 3000;
5959
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
6060
;
61+
62+
// For low-memory platforms, don't turn on internal Notecard logs.
63+
#ifndef NOTE_C_LOW_MEM
6164
notecard.setDebugOutputStream(usbSerial);
62-
#endif
65+
#else
66+
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
67+
#endif // !NOTE_C_LOW_MEM
68+
#endif // usbSerial
6369

6470
// Initialize the physical I/O channel to the Notecard
6571
#ifdef txRxPinsSerial
@@ -129,9 +135,9 @@ void loop()
129135
{
130136

131137
// Simulate Processing the response here
132-
notecard.logDebug("INBOUND REQUEST: ");
133-
notecard.logDebug(JGetString(body, INBOUND_QUEUE_COMMAND_FIELD));
134-
notecard.logDebug("\n\n");
138+
usbSerial.print("[APP] INBOUND REQUEST: ");
139+
usbSerial.println(JGetString(body, INBOUND_QUEUE_COMMAND_FIELD));
140+
usbSerial.println();
135141
}
136142
}
137143
notecard.deleteResponse(rsp);

examples/Example4_InboundInterrupts/Example4_InboundInterrupts.ino

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,14 @@ void setup()
7171
const size_t usb_timeout_ms = 3000;
7272
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
7373
;
74+
75+
// For low-memory platforms, don't turn on internal Notecard logs.
76+
#ifndef NOTE_C_LOW_MEM
7477
notecard.setDebugOutputStream(usbSerial);
75-
#endif
78+
#else
79+
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
80+
#endif // !NOTE_C_LOW_MEM
81+
#endif // usbSerial
7682

7783
// Initialize the physical I/O channel to the Notecard
7884
#ifdef txRxPinsSerial
@@ -160,7 +166,9 @@ void loop()
160166
{
161167
// Simulate Processing the response here
162168
char *myCommandType = JGetString(body, INBOUND_QUEUE_COMMAND_FIELD);
163-
notecard.logDebugf("INBOUND REQUEST: %s\n\n", myCommandType);
169+
usbSerial.print("[APP] INBOUND REQUEST: ");
170+
usbSerial.println(myCommandType);
171+
usbSerial.println();
164172
}
165173
}
166174
notecard.deleteResponse(rsp);

examples/Example5_UsingTemplates/Example5_UsingTemplates.ino

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,14 @@ void setup()
7676
const size_t usb_timeout_ms = 3000;
7777
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
7878
;
79+
80+
// For low-memory platforms, don't turn on internal Notecard logs.
81+
#ifndef NOTE_C_LOW_MEM
7982
notecard.setDebugOutputStream(usbSerial);
80-
#endif
83+
#else
84+
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
85+
#endif // !NOTE_C_LOW_MEM
86+
#endif // usbSerial
8187

8288
// Initialize the physical I/O channel to the Notecard
8389
#ifdef txRxPinsSerial
@@ -155,7 +161,7 @@ void loop()
155161
static unsigned eventCounter = 0;
156162
if (++eventCounter > 25)
157163
{
158-
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
164+
usbSerial.println("[APP] Demo cycle complete. Program stopped. Press RESET to restart.");
159165
delay(10000); // 10 seconds
160166
return;
161167
}

examples/Example6_SensorTutorial/Example6_SensorTutorial.ino

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ void setup()
3838
const size_t usb_timeout_ms = 3000;
3939
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
4040
;
41+
42+
// For low-memory platforms, don't turn on internal Notecard logs.
43+
#ifndef NOTE_C_LOW_MEM
4144
notecard.setDebugOutputStream(usbSerial);
42-
#endif
45+
#else
46+
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
47+
#endif // !NOTE_C_LOW_MEM
48+
#endif // usbSerial
4349

4450
// Initialize the physical I/O channel to the Notecard
4551
#ifdef txRxPinsSerial
@@ -66,18 +72,18 @@ void loop()
6672
static unsigned eventCounter = 0;
6773
if (++eventCounter > 25)
6874
{
69-
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
75+
usbSerial.println("[APP] Demo cycle complete. Program stopped. Press RESET to restart.");
7076
delay(10000); // 10 seconds
7177
return;
7278
}
7379

7480
float temperature = sensor.temp();
7581
float humidity = sensor.humidity();
7682

77-
usbSerial.print("Temperature = ");
83+
usbSerial.print("[APP] Temperature = ");
7884
usbSerial.print(temperature);
7985
usbSerial.println(" *C");
80-
usbSerial.print("Humidity = ");
86+
usbSerial.print("[APP] Humidity = ");
8187
usbSerial.print(humidity);
8288
usbSerial.println(" %");
8389

examples/Example7_PowerControl/Example7_PowerControl.ino

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,15 @@ void setup()
7171
const size_t usb_timeout_ms = 3000;
7272
for (const size_t start_ms = millis(); !txRxSerial && (millis() - start_ms) < usb_timeout_ms;)
7373
;
74+
75+
// For low-memory platforms, don't turn on internal Notecard logs.
76+
#ifndef NOTE_C_LOW_MEM
7477
notecard.setDebugOutputStream(txRxSerial);
75-
#endif
78+
#else
79+
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
80+
#endif // !NOTE_C_LOW_MEM
81+
#endif // txRxSerial
82+
7683

7784
// Initialize the physical I2C I/O channel to the Notecard
7885
notecard.begin();
@@ -134,7 +141,7 @@ void loop()
134141
// Bump the number of cycles
135142
if (++globalState.cycles > 25)
136143
{
137-
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
144+
txRxSerial.println("[APP] Demo cycle complete. Program stopped. Press RESET to restart.");
138145
delay(10000); // 10 seconds
139146
return;
140147
}

examples/Example8_BinarySendReceive/Example8_BinarySendReceive.ino

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ void setup()
4242
const size_t usb_timeout_ms = 3000;
4343
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
4444
;
45+
46+
// For low-memory platforms, don't turn on internal Notecard logs.
47+
#ifndef NOTE_C_LOW_MEM
4548
notecard.setDebugOutputStream(usbSerial);
46-
#endif
49+
#else
50+
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
51+
#endif // !NOTE_C_LOW_MEM
52+
#endif // usbSerial
4753

4854
// Initialize the physical I/O channel to the Notecard
4955
#ifdef txRxPinsSerial
@@ -74,7 +80,7 @@ void loop()
7480
static unsigned event_counter = 0;
7581
if (++event_counter > 5)
7682
{
77-
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
83+
usbSerial.println("[APP] Demo cycle complete. Program stopped. Press RESET to restart.");
7884
delay(10000); // 10 seconds
7985
return;
8086
}
@@ -89,23 +95,26 @@ void loop()
8995
uint32_t data_len = strlen(data);
9096
const uint32_t notecard_binary_area_offset = 0;
9197
NoteBinaryStoreTransmit(reinterpret_cast<uint8_t *>(data), data_len, sizeof(data), notecard_binary_area_offset);
92-
notecard.logDebugf("\n[INFO] Transmitted %d bytes.\n", data_len);
98+
usbSerial.print("\n[APP] Transmitted ");
99+
usbSerial.print(data_len);
100+
usbSerial.println(" bytes.");
93101

94102
// Log for the sake of curiosity (not necessary for operation)
95103
// NOTE: NoteBinaryMaxEncodedLength() is imprecise. It will most
96104
// commonly return a number greater than the actual bytes encoded.
97105
// However, in this contrived example there is no difference,
98106
// so it works for the purposes of displaying the encoded data --
99107
// which would never be done in practice.
100-
notecard.logDebug("\n*** Encoded Binary Transmission ***\n");
108+
usbSerial.println("\n[APP] *** Encoded Binary Transmission ***");
101109
uint32_t tx_len = NoteBinaryCodecMaxEncodedLength(data_len);
102110
for (size_t i = 0 ; i < tx_len ; ++i) {
103-
notecard.logDebugf("%02x ", data[i]);
111+
usbSerial.print(data[i], HEX);
112+
usbSerial.print(" ");
104113
if ((i + 1) % 16 == 0) {
105-
notecard.logDebug("\n");
114+
usbSerial.println();
106115
}
107116
}
108-
notecard.logDebug("\n*** Encoded Binary Transmission ***\n\n");
117+
usbSerial.println("\n[APP] *** Encoded Binary Transmission ***\n");
109118

110119
/////////////////////////////////////////////////
111120
// Receive data from the Notecard binary data store
@@ -125,14 +134,16 @@ void loop()
125134

126135
// Receive the data
127136
NoteBinaryStoreReceive(reinterpret_cast<uint8_t *>(rx_buffer), rx_buffer_len, 0, data_len);
128-
notecard.logDebugf("\n[INFO] Received %d bytes.\n", data_len);
137+
usbSerial.print("\n[APP] Received ");
138+
usbSerial.print(data_len);
139+
usbSerial.println(" bytes.");
129140

130141
// Display received buffer
131-
notecard.logDebug("\n*** Decoded Data ***\n");
142+
usbSerial.println("\n[APP] *** Decoded Data ***");
132143
for (size_t i = 0 ; i < data_len ; ++i) {
133-
notecard.logDebugf("%c", rx_buffer[i]);
144+
usbSerial.print(rx_buffer[i]);
134145
}
135-
notecard.logDebug("\n*** Decoded Data ***\n\n");
146+
usbSerial.println("\n[APP] *** Decoded Data ***\n");
136147

137148
// Free the receive buffer
138149
free(rx_buffer);

0 commit comments

Comments
 (0)