From 2443d9ab749201402735111fab3eb683c7cb5c6a Mon Sep 17 00:00:00 2001 From: Hayden Roche Date: Sun, 21 Jan 2024 18:03:51 -0800 Subject: [PATCH] Improve examples so that they work for small MCUs (e.g. Arduino Uno). - 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). --- .../Example1_NotecardBasics.ino | 10 +++- .../Example2_PeriodicCommunications.ino | 12 +++- .../Example3_InboundPolling.ino | 14 +++-- .../Example4_InboundInterrupts.ino | 12 +++- .../Example5_UsingTemplates.ino | 10 +++- .../Example6_SensorTutorial.ino | 10 +++- .../Example7_PowerControl.ino | 11 +++- .../Example8_BinarySendReceive.ino | 33 +++++++---- .../Example9_BinarySendReceiveChunked.ino | 56 +++++++++++++------ 9 files changed, 122 insertions(+), 46 deletions(-) diff --git a/examples/Example1_NotecardBasics/Example1_NotecardBasics.ino b/examples/Example1_NotecardBasics/Example1_NotecardBasics.ino index 65fb804..58d74d0 100644 --- a/examples/Example1_NotecardBasics/Example1_NotecardBasics.ino +++ b/examples/Example1_NotecardBasics/Example1_NotecardBasics.ino @@ -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 @@ -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; } diff --git a/examples/Example2_PeriodicCommunications/Example2_PeriodicCommunications.ino b/examples/Example2_PeriodicCommunications/Example2_PeriodicCommunications.ino index 8cef03f..a22f372 100644 --- a/examples/Example2_PeriodicCommunications/Example2_PeriodicCommunications.ino +++ b/examples/Example2_PeriodicCommunications/Example2_PeriodicCommunications.ino @@ -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 @@ -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); @@ -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 diff --git a/examples/Example3_InboundPolling/Example3_InboundPolling.ino b/examples/Example3_InboundPolling/Example3_InboundPolling.ino index 6870d8c..a20abea 100644 --- a/examples/Example3_InboundPolling/Example3_InboundPolling.ino +++ b/examples/Example3_InboundPolling/Example3_InboundPolling.ino @@ -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 @@ -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); diff --git a/examples/Example4_InboundInterrupts/Example4_InboundInterrupts.ino b/examples/Example4_InboundInterrupts/Example4_InboundInterrupts.ino index e622e5b..c777578 100644 --- a/examples/Example4_InboundInterrupts/Example4_InboundInterrupts.ino +++ b/examples/Example4_InboundInterrupts/Example4_InboundInterrupts.ino @@ -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 @@ -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); diff --git a/examples/Example5_UsingTemplates/Example5_UsingTemplates.ino b/examples/Example5_UsingTemplates/Example5_UsingTemplates.ino index 575969d..ff1ba81 100644 --- a/examples/Example5_UsingTemplates/Example5_UsingTemplates.ino +++ b/examples/Example5_UsingTemplates/Example5_UsingTemplates.ino @@ -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 @@ -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; } diff --git a/examples/Example6_SensorTutorial/Example6_SensorTutorial.ino b/examples/Example6_SensorTutorial/Example6_SensorTutorial.ino index 202dd58..2581883 100644 --- a/examples/Example6_SensorTutorial/Example6_SensorTutorial.ino +++ b/examples/Example6_SensorTutorial/Example6_SensorTutorial.ino @@ -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 @@ -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; } diff --git a/examples/Example7_PowerControl/Example7_PowerControl.ino b/examples/Example7_PowerControl/Example7_PowerControl.ino index 743f01e..47f7947 100644 --- a/examples/Example7_PowerControl/Example7_PowerControl.ino +++ b/examples/Example7_PowerControl/Example7_PowerControl.ino @@ -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(); @@ -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; } diff --git a/examples/Example8_BinarySendReceive/Example8_BinarySendReceive.ino b/examples/Example8_BinarySendReceive/Example8_BinarySendReceive.ino index 6a64606..8c3e903 100644 --- a/examples/Example8_BinarySendReceive/Example8_BinarySendReceive.ino +++ b/examples/Example8_BinarySendReceive/Example8_BinarySendReceive.ino @@ -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 @@ -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; } @@ -89,7 +95,9 @@ void loop() uint32_t data_len = strlen(data); const uint32_t notecard_binary_area_offset = 0; NoteBinaryStoreTransmit(reinterpret_cast(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 @@ -97,15 +105,16 @@ void loop() // 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 @@ -125,14 +134,16 @@ void loop() // Receive the data NoteBinaryStoreReceive(reinterpret_cast(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); diff --git a/examples/Example9_BinarySendReceiveChunked/Example9_BinarySendReceiveChunked.ino b/examples/Example9_BinarySendReceiveChunked/Example9_BinarySendReceiveChunked.ino index e54bd34..9df8725 100644 --- a/examples/Example9_BinarySendReceiveChunked/Example9_BinarySendReceiveChunked.ino +++ b/examples/Example9_BinarySendReceiveChunked/Example9_BinarySendReceiveChunked.ino @@ -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 @@ -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; } @@ -114,16 +120,22 @@ 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(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 @@ -131,15 +143,16 @@ void loop() // 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 @@ -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(rx_buffer), rx_buffer_len, notecard_binary_area_offset, rx_len)) { --chunk; - notecard.logDebug("Failed to receive.\n"); + usbSerial.println("Failed to receive."); continue; } @@ -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);