Skip to content

Commit 3fc70ec

Browse files
authored
chore: Refactor binary example code (#108)
* chore: Refactor binary example code * fix: demo termination behavior
1 parent 5bd8a61 commit 3fc70ec

File tree

14 files changed

+385
-8442
lines changed

14 files changed

+385
-8442
lines changed

.github/workflows/note-arduino-ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ jobs:
3838
- ./examples/Example5_UsingTemplates/Example5_UsingTemplates.ino
3939
- ./examples/Example6_SensorTutorial/Example6_SensorTutorial.ino
4040
- ./examples/Example7_PowerControl/Example7_PowerControl.ino
41+
- ./examples/Example8_BinarySendReceive/Example8_BinarySendReceive.ino
42+
- ./examples/Example9_BinarySendReceiveChunked/Example9_BinarySendReceiveChunked.ino
4143
fully-qualified-board-name:
4244
- STMicroelectronics:stm32:BluesW:pnum=SWAN_R5
4345
- esp32:esp32:featheresp32

examples/Example0_LibrarylessCommunication/Example0_LibrarylessCommunication.ino

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ void loop()
9090
static unsigned eventCounter = 0;
9191
if (++eventCounter > 25)
9292
{
93+
txRxPinsSerial.println("Demo cycle complete. Program stopped. Press RESET to restart.");
94+
delay(10000); // 10 seconds
9395
return;
9496
}
9597

examples/Example1_NotecardBasics/Example1_NotecardBasics.ino

+2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ void loop()
127127
static unsigned eventCounter = 0;
128128
if (++eventCounter > 25)
129129
{
130+
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.");
131+
delay(10000); // 10 seconds
130132
return;
131133
}
132134

examples/Example2_PeriodicCommunications/Example2_PeriodicCommunications.ino

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ void loop()
173173
static unsigned eventCounter = 0;
174174
if (++eventCounter > 25)
175175
{
176+
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.");
177+
delay(10000); // 10 seconds
176178
return;
177179
}
178180

examples/Example5_UsingTemplates/Example5_UsingTemplates.ino

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ void loop()
155155
static unsigned eventCounter = 0;
156156
if (++eventCounter > 25)
157157
{
158+
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.");
159+
delay(10000); // 10 seconds
158160
return;
159161
}
160162

examples/Example6_SensorTutorial/Example6_SensorTutorial.ino

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ void loop()
6666
static unsigned eventCounter = 0;
6767
if (++eventCounter > 25)
6868
{
69+
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.");
70+
delay(10000); // 10 seconds
6971
return;
7072
}
7173

examples/Example7_PowerControl/Example7_PowerControl.ino

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ void loop()
134134
// Bump the number of cycles
135135
if (++globalState.cycles > 25)
136136
{
137+
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.");
138+
delay(10000); // 10 seconds
137139
return;
138140
}
139141

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Copyright 2023 Blues Inc. All rights reserved.
2+
//
3+
// Use of this source code is governed by licenses granted by the
4+
// copyright holder including that found in the LICENSE file.
5+
//
6+
// This example exercises the Notecard's ability to send and receive a binary
7+
8+
#include <stddef.h>
9+
#include <stdint.h>
10+
#include <stdlib.h>
11+
#include <string.h>
12+
13+
#include <Notecard.h>
14+
15+
// Note that both of these definitions are optional; just prefix either line
16+
// with `//` to remove it.
17+
// - Remove txRxPinsSerial if you wired your Notecard using I2C SDA/SCL pins
18+
// instead of serial RX/TX
19+
// - Remove usbSerial if you don't want the Notecard library to output debug
20+
// information
21+
22+
// #define txRxPinsSerial Serial1
23+
#define usbSerial Serial
24+
25+
// This is the unique Product Identifier for your device
26+
#ifndef PRODUCT_UID
27+
#define PRODUCT_UID "" // "com.my-company.my-name:my-project"
28+
#pragma message "PRODUCT_UID is not defined in this example. Please ensure your Notecard has a product identifier set before running this example or define it in code here. More details at https://dev.blues.io/tools-and-sdks/samples/product-uid"
29+
#endif
30+
31+
#define myProductID PRODUCT_UID
32+
Notecard notecard;
33+
34+
// One-time Arduino initialization
35+
void setup()
36+
{
37+
// Set up for debug output (if available).
38+
#ifdef usbSerial
39+
// If you open Arduino's serial terminal window, you'll be able to watch
40+
// JSON objects being transferred to and from the Notecard for each request.
41+
usbSerial.begin(115200);
42+
const size_t usb_timeout_ms = 3000;
43+
for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;)
44+
;
45+
notecard.setDebugOutputStream(usbSerial);
46+
#endif
47+
48+
// Initialize the physical I/O channel to the Notecard
49+
#ifdef txRxPinsSerial
50+
notecard.begin(txRxPinsSerial, 9600);
51+
#else
52+
notecard.begin();
53+
#endif
54+
55+
// Configure the productUID, and instruct the Notecard to stay connected to
56+
// the service.
57+
J *req = notecard.newRequest("hub.set");
58+
if (myProductID[0])
59+
{
60+
JAddStringToObject(req, "product", myProductID);
61+
}
62+
JAddStringToObject(req, "mode", "continuous");
63+
notecard.sendRequestWithRetry(req, 5); // 5 seconds
64+
65+
// Reset the state of the Notecard's binary data store to a known value.
66+
NoteBinaryReset();
67+
}
68+
69+
// In the Arduino main loop which is called repeatedly, add outbound data every
70+
// 30 seconds
71+
void loop()
72+
{
73+
// Stop the demo after five iterations to conserve data
74+
static unsigned event_counter = 0;
75+
if (++event_counter > 5)
76+
{
77+
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.");
78+
delay(10000); // 10 seconds
79+
return;
80+
}
81+
82+
// Send data to the Notecard storage and pull it back
83+
{
84+
/////////////////////////////////////////////////
85+
// Transmit that beautiful bean footage
86+
/////////////////////////////////////////////////
87+
char data[64] = "https://youtu.be/0epWToAOlFY?t=21";
88+
size_t data_len = strlen(data);
89+
const size_t notecard_binary_area_offset = 0;
90+
NoteBinaryTransmit(reinterpret_cast<uint8_t *>(data), data_len, sizeof(data), notecard_binary_area_offset);
91+
notecard.logDebugf("\n[INFO] Transmitted %d bytes.\n", data_len);
92+
93+
// Log for the sake of curiosity
94+
notecard.logDebug("\n*** Encoded Binary Transmission ***\n");
95+
size_t tx_len = NoteBinaryEncodedLength(reinterpret_cast<uint8_t *>(data), data_len);
96+
for (size_t i = 0 ; i < tx_len ; ++i) {
97+
notecard.logDebugf("%02x ", data[i]);
98+
if ((i + 1) % 16 == 0) {
99+
notecard.logDebug("\n");
100+
}
101+
}
102+
notecard.logDebug("\n*** Encoded Binary Transmission ***\n\n");
103+
104+
/////////////////////////////////////////////////
105+
// Receive data from the Notecard binary data store
106+
/////////////////////////////////////////////////
107+
size_t rx_buffer_len = 0;
108+
NoteBinaryRequiredRxMaxBuffer(&rx_buffer_len);
109+
uint8_t *rx_buffer = (uint8_t *)malloc(rx_buffer_len);
110+
data_len = NOTE_C_BINARY_RX_ALL; // NOTE_C_BINARY_RX_ALL is a special value
111+
// meaning "return all bytes from offset"
112+
NoteBinaryReceive(reinterpret_cast<uint8_t *>(rx_buffer), rx_buffer_len, notecard_binary_area_offset, &data_len);
113+
notecard.logDebugf("\n[INFO] Received %d bytes.\n", data_len);
114+
115+
// Display received buffer
116+
notecard.logDebug("\n*** Decoded Data ***\n");
117+
for (size_t i = 0 ; i < data_len ; ++i) {
118+
notecard.logDebugf("%c", rx_buffer[i]);
119+
}
120+
notecard.logDebug("\n*** Decoded Data ***\n\n");
121+
122+
// NOTE: The binary data store is not cleared on receive, which
123+
// allows us to submit it to Notehub in the next step.
124+
}
125+
126+
// Send it to Notehub
127+
{
128+
// Submit binary object to the Notehub using `note.add`. This will send
129+
// the binary to Notehub in the payload field of the Note. The payload
130+
// will not be visible in the Notehub UI, but the data will be forwarded
131+
// to any pre-configured routes.
132+
if (J *req = notecard.newRequest("note.add"))
133+
{
134+
JAddStringToObject(req, "file", "cobs.qo");
135+
JAddBoolToObject(req, "binary", true);
136+
JAddBoolToObject(req, "live", true);
137+
if (!notecard.sendRequest(req)) {
138+
// The binary data store is cleared on successful transmission,
139+
// but we need to reset it manually if the request failed.
140+
NoteBinaryReset();
141+
}
142+
}
143+
}
144+
145+
// Delay between sends
146+
delay(30 * 1000); // 30 seconds
147+
}

examples/Example8_SendReceiveBinary/Example8_SendReceiveBinary.ino

-140
This file was deleted.

0 commit comments

Comments
 (0)