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
…#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).
  • Loading branch information
haydenroche5 authored Jan 31, 2024
1 parent d1c69b7 commit 79d96c7
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 48 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_C_LOW_MEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_C_LOW_MEM
#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("[APP] 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_C_LOW_MEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_C_LOW_MEM
#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("[APP] 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("[APP] 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_C_LOW_MEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_C_LOW_MEM
#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.print("[APP] 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_C_LOW_MEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_C_LOW_MEM
#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("[APP] 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_C_LOW_MEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_C_LOW_MEM
#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("[APP] Demo cycle complete. Program stopped. Press RESET to restart.");
delay(10000); // 10 seconds
return;
}
Expand Down
14 changes: 10 additions & 4 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_C_LOW_MEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_C_LOW_MEM
#endif // usbSerial

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

float temperature = sensor.temp();
float humidity = sensor.humidity();

usbSerial.print("Temperature = ");
usbSerial.print("[APP] Temperature = ");
usbSerial.print(temperature);
usbSerial.println(" *C");
usbSerial.print("Humidity = ");
usbSerial.print("[APP] Humidity = ");
usbSerial.print(humidity);
usbSerial.println(" %");

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_C_LOW_MEM
notecard.setDebugOutputStream(txRxSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_C_LOW_MEM
#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("[APP] 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_C_LOW_MEM
notecard.setDebugOutputStream(usbSerial);
#endif
#else
#pragma message("INFO: Notecard debug logs disabled. (non-fatal)")
#endif // !NOTE_C_LOW_MEM
#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("[APP] 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[APP] 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[APP] *** 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[APP] *** 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[APP] Received ");
usbSerial.print(data_len);
usbSerial.println(" bytes.");

// Display received buffer
notecard.logDebug("\n*** Decoded Data ***\n");
usbSerial.println("\n[APP] *** 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[APP] *** Decoded Data ***\n");

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

0 comments on commit 79d96c7

Please sign in to comment.