Skip to content

Commit

Permalink
Improve examples so that they work for small MCUs (e.g. Arduino Uno).
Browse files Browse the repository at this point in the history
- 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).
  • Loading branch information
haydenroche5 committed Jan 22, 2024
1 parent c54d450 commit 2443d9a
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 46 deletions.
10 changes: 8 additions & 2 deletions examples/Example1_NotecardBasics/Example1_NotecardBasics.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ void setup()
const size_t usb_timeout_ms = 3000;
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
;

// For low-memory platforms, don't turn on internal Notecard logs.
#ifndef NOTE_LOWMEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_LOWMEM
#endif // usbSerial

// Initialize the physical I/O channel to the Notecard
#ifdef txRxPinsSerial
Expand Down Expand Up @@ -127,7 +133,7 @@ void loop()
static unsigned eventCounter = 0;
if (++eventCounter > 25)
{
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
usbSerial.println("Demo cycle complete. Program stopped. Press RESET to restart.");
delay(10000); // 10 seconds
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,14 @@ void setup()
const size_t usb_timeout_ms = 3000;
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
;

// For low-memory platforms, don't turn on internal Notecard logs.
#ifndef NOTE_LOWMEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_LOWMEM
#endif // usbSerial

// Initialize the physical I/O channel to the Notecard
#ifdef txRxPinsSerial
Expand Down Expand Up @@ -151,7 +157,7 @@ void loop()
if (millis() > lastStatusMs + 10000)
{
lastStatusMs = millis();
notecard.logDebug("press button to simulate a sensor measurement\n");
usbSerial.println("press button to simulate a sensor measurement");
}
delay(25);
digitalWrite(ledPin, LOW);
Expand All @@ -165,7 +171,7 @@ void loop()
}

// The button was pressed, so we should begin a transaction
notecard.logDebug("performing sensor measurement\n");
usbSerial.println("performing sensor measurement");
lastStatusMs = millis();

// Read the notecard's current temperature and voltage, as simulated sensor
Expand Down
14 changes: 10 additions & 4 deletions examples/Example3_InboundPolling/Example3_InboundPolling.ino
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,14 @@ void setup()
const size_t usb_timeout_ms = 3000;
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
;

// For low-memory platforms, don't turn on internal Notecard logs.
#ifndef NOTE_LOWMEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_LOWMEM
#endif // usbSerial

// Initialize the physical I/O channel to the Notecard
#ifdef txRxPinsSerial
Expand Down Expand Up @@ -129,9 +135,9 @@ void loop()
{

// Simulate Processing the response here
notecard.logDebug("INBOUND REQUEST: ");
notecard.logDebug(JGetString(body, INBOUND_QUEUE_COMMAND_FIELD));
notecard.logDebug("\n\n");
usbSerial.println("INBOUND REQUEST: ");
usbSerial.println(JGetString(body, INBOUND_QUEUE_COMMAND_FIELD));
usbSerial.println();
}
}
notecard.deleteResponse(rsp);
Expand Down
12 changes: 10 additions & 2 deletions examples/Example4_InboundInterrupts/Example4_InboundInterrupts.ino
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ void setup()
const size_t usb_timeout_ms = 3000;
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
;

// For low-memory platforms, don't turn on internal Notecard logs.
#ifndef NOTE_LOWMEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_LOWMEM
#endif // usbSerial

// Initialize the physical I/O channel to the Notecard
#ifdef txRxPinsSerial
Expand Down Expand Up @@ -160,7 +166,9 @@ void loop()
{
// Simulate Processing the response here
char *myCommandType = JGetString(body, INBOUND_QUEUE_COMMAND_FIELD);
notecard.logDebugf("INBOUND REQUEST: %s\n\n", myCommandType);
usbSerial.print("INBOUND REQUEST: ");
usbSerial.println(myCommandType);
usbSerial.println();
}
}
notecard.deleteResponse(rsp);
Expand Down
10 changes: 8 additions & 2 deletions examples/Example5_UsingTemplates/Example5_UsingTemplates.ino
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ void setup()
const size_t usb_timeout_ms = 3000;
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
;

// For low-memory platforms, don't turn on internal Notecard logs.
#ifndef NOTE_LOWMEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_LOWMEM
#endif // usbSerial

// Initialize the physical I/O channel to the Notecard
#ifdef txRxPinsSerial
Expand Down Expand Up @@ -155,7 +161,7 @@ void loop()
static unsigned eventCounter = 0;
if (++eventCounter > 25)
{
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
usbSerial.println("Demo cycle complete. Program stopped. Press RESET to restart.");
delay(10000); // 10 seconds
return;
}
Expand Down
10 changes: 8 additions & 2 deletions examples/Example6_SensorTutorial/Example6_SensorTutorial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ void setup()
const size_t usb_timeout_ms = 3000;
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
;

// For low-memory platforms, don't turn on internal Notecard logs.
#ifndef NOTE_LOWMEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_LOWMEM
#endif // usbSerial

// Initialize the physical I/O channel to the Notecard
#ifdef txRxPinsSerial
Expand All @@ -66,7 +72,7 @@ void loop()
static unsigned eventCounter = 0;
if (++eventCounter > 25)
{
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
usbSerial.println("Demo cycle complete. Program stopped. Press RESET to restart.");
delay(10000); // 10 seconds
return;
}
Expand Down
11 changes: 9 additions & 2 deletions examples/Example7_PowerControl/Example7_PowerControl.ino
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,15 @@ void setup()
const size_t usb_timeout_ms = 3000;
for (const size_t start_ms = millis(); !txRxSerial && (millis() - start_ms) < usb_timeout_ms;)
;

// For low-memory platforms, don't turn on internal Notecard logs.
#ifndef NOTE_LOWMEM
notecard.setDebugOutputStream(txRxSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_LOWMEM
#endif // txRxSerial


// Initialize the physical I2C I/O channel to the Notecard
notecard.begin();
Expand Down Expand Up @@ -134,7 +141,7 @@ void loop()
// Bump the number of cycles
if (++globalState.cycles > 25)
{
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
txRxSerial.println("Demo cycle complete. Program stopped. Press RESET to restart.");
delay(10000); // 10 seconds
return;
}
Expand Down
33 changes: 22 additions & 11 deletions examples/Example8_BinarySendReceive/Example8_BinarySendReceive.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ void setup()
const size_t usb_timeout_ms = 3000;
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
;

// For low-memory platforms, don't turn on internal Notecard logs.
#ifndef NOTE_LOWMEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_LOWMEM
#endif // usbSerial

// Initialize the physical I/O channel to the Notecard
#ifdef txRxPinsSerial
Expand Down Expand Up @@ -74,7 +80,7 @@ void loop()
static unsigned event_counter = 0;
if (++event_counter > 5)
{
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
usbSerial.println("Demo cycle complete. Program stopped. Press RESET to restart.");
delay(10000); // 10 seconds
return;
}
Expand All @@ -89,23 +95,26 @@ void loop()
uint32_t data_len = strlen(data);
const uint32_t notecard_binary_area_offset = 0;
NoteBinaryStoreTransmit(reinterpret_cast<uint8_t *>(data), data_len, sizeof(data), notecard_binary_area_offset);
notecard.logDebugf("\n[INFO] Transmitted %d bytes.\n", data_len);
usbSerial.print("\n[INFO] Transmitted ");
usbSerial.print(data_len);
usbSerial.println(" bytes.");

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

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

// Receive the data
NoteBinaryStoreReceive(reinterpret_cast<uint8_t *>(rx_buffer), rx_buffer_len, 0, data_len);
notecard.logDebugf("\n[INFO] Received %d bytes.\n", data_len);
usbSerial.print("\n[INFO] Received ");
usbSerial.print(data_len);
usbSerial.println(" bytes.");

// Display received buffer
notecard.logDebug("\n*** Decoded Data ***\n");
usbSerial.println("\n*** Decoded Data ***");
for (size_t i = 0 ; i < data_len ; ++i) {
notecard.logDebugf("%c", rx_buffer[i]);
usbSerial.print(rx_buffer[i]);
}
notecard.logDebug("\n*** Decoded Data ***\n\n");
usbSerial.println("\n*** Decoded Data ***\n");

// Free the receive buffer
free(rx_buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ void setup()
const size_t usb_timeout_ms = 3000;
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
;

// For low-memory platforms, don't turn on internal Notecard logs.
#ifndef NOTE_LOWMEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_LOWMEM
#endif // usbSerial

// Initialize the physical I/O channel to the Notecard
#ifdef txRxPinsSerial
Expand Down Expand Up @@ -74,7 +80,7 @@ void loop()
static unsigned event_counter = 0;
if (++event_counter > 5)
{
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
usbSerial.println("Demo cycle complete. Program stopped. Press RESET to restart.");
delay(10000); // 10 seconds
return;
}
Expand Down Expand Up @@ -114,32 +120,39 @@ void loop()
memcpy(tx_buffer, (data_source + notecard_binary_area_offset), data_len);

// Transmit the chunk
notecard.logDebugf("Transmitting chunk #%d, containing %d bytes.\n", chunk, data_len);
usbSerial.print("Transmitting chunk #");
usbSerial.print(chunk);
usbSerial.print(", containing ");
usbSerial.print(data_len);
usbSerial.println(" bytes.");
if (NoteBinaryStoreTransmit(reinterpret_cast<uint8_t *>(tx_buffer), data_len, tx_buffer_len, notecard_binary_area_offset)) {
--chunk;
notecard.logDebug("Failed to transmit.\n");
usbSerial.println("Failed to transmit.");
continue;
}

// Update the offset
notecard_binary_area_offset += data_len;
notecard.logDebugf("[INFO] Transmitted %d bytes.\n", data_len);
usbSerial.print("[INFO] Transmitted ");
usbSerial.print(data_len);
usbSerial.println(" bytes.");

// Log for the sake of curiosity (not necessary for operation)
// NOTE: NoteBinaryMaxEncodedLength() is imprecise. It will most
// commonly return a number greater than the actual bytes
// encoded. However, in this contrived example there is no
// difference, so it works for the purposes of displaying the
// encoded data -- which would never be done in practice.
notecard.logDebug("\n*** Encoded Binary Transmission ***\n");
usbSerial.println("\n*** Encoded Binary Transmission ***");
uint32_t tx_len = NoteBinaryCodecMaxEncodedLength(data_len);
for (size_t i = 0 ; i < tx_len ; ++i) {
notecard.logDebugf("%02x ", tx_buffer[i]);
usbSerial.print(tx_buffer[i], HEX);
usbSerial.print(" ");
if ((i + 1) % 16 == 0) {
notecard.logDebug("\n");
usbSerial.println();
}
}
notecard.logDebug("\n*** Encoded Binary Transmission ***\n\n");
usbSerial.println("\n*** Encoded Binary Transmission ***\n");
}

// Free the transmit buffer
Expand Down Expand Up @@ -177,10 +190,14 @@ void loop()
: (rx_data_len - notecard_binary_area_offset));

// Receive the chunk
notecard.logDebugf("Receiving chunk #%d, containing %d bytes.\n", chunk, rx_len);
usbSerial.print("Receiving chunk #");
usbSerial.print(chunk);
usbSerial.print(", containing ");
usbSerial.print(rx_len);
usbSerial.println(" bytes.");
if (NoteBinaryStoreReceive(reinterpret_cast<uint8_t *>(rx_buffer), rx_buffer_len, notecard_binary_area_offset, rx_len)) {
--chunk;
notecard.logDebug("Failed to receive.\n");
usbSerial.println("Failed to receive.");
continue;
}

Expand All @@ -192,22 +209,25 @@ void loop()

// Update the offset
notecard_binary_area_offset += rx_len;
notecard.logDebugf("[INFO] Received %d bytes.\n", rx_len);
usbSerial.print("\n[INFO] Received ");
usbSerial.print(rx_len);
usbSerial.println(" bytes.");

// Log for the sake of curiosity
notecard.logDebug("*** Decoded Binary Data ***\n");
usbSerial.println("*** Decoded Binary Data ***");
for (size_t i = 0 ; i < rx_len ; ++i) {
notecard.logDebugf("%02x ", rx_buffer[i]);
usbSerial.print(rx_buffer[i], HEX);
usbSerial.print(" ");
}
notecard.logDebug("\n*** Decoded Binary Data ***\n\n");
usbSerial.println("\n*** Decoded Binary Data ***\n");
}

// Display complete buffer
notecard.logDebug("*** Decoded Data ***\n");
usbSerial.println("*** Decoded Data ***");
for (size_t i = 0 ; i < rx_data_len ; ++i) {
notecard.logDebugf("%c", data_store[i]);
usbSerial.print(data_store[i]);
}
notecard.logDebug("\n*** Decoded Data ***\n\n");
usbSerial.println("\n*** Decoded Data ***\n");

// Free the receive buffer
free(rx_buffer);
Expand Down

0 comments on commit 2443d9a

Please sign in to comment.