Skip to content

Commit 0ba513a

Browse files
committed
Update files impacted by new note-c
1 parent 06ac4bb commit 0ba513a

File tree

4 files changed

+79
-46
lines changed

4 files changed

+79
-46
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ jobs:
105105
- ./examples/Example5_UsingTemplates/Example5_UsingTemplates.ino
106106
- ./examples/Example6_SensorTutorial/Example6_SensorTutorial.ino
107107
- ./examples/Example7_PowerControl/Example7_PowerControl.ino
108-
# TODO: Uncomment these once note-c is updated with the necessary NoteBinary* functions.
109-
# - ./examples/Example8_BinarySendReceive/Example8_BinarySendReceive.ino
110-
# - ./examples/Example9_BinarySendReceiveChunked/Example9_BinarySendReceiveChunked.ino
108+
- ./examples/Example8_BinarySendReceive/Example8_BinarySendReceive.ino
109+
- ./examples/Example9_BinarySendReceiveChunked/Example9_BinarySendReceiveChunked.ino
111110
fully-qualified-board-name:
112111
- STMicroelectronics:stm32:BluesW:pnum=SWAN_R5
113112
- esp32:esp32:featheresp32

examples/Example8_BinarySendReceive/Example8_BinarySendReceive.ino

+31-13
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void setup()
6363
notecard.sendRequestWithRetry(req, 5); // 5 seconds
6464

6565
// Reset the state of the Notecard's binary data store to a known value.
66-
NoteBinaryReset();
66+
NoteBinaryStoreReset();
6767
}
6868

6969
// In the Arduino main loop which is called repeatedly, add outbound data every
@@ -74,7 +74,7 @@ void loop()
7474
static unsigned event_counter = 0;
7575
if (++event_counter > 5)
7676
{
77-
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.");
77+
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
7878
delay(10000); // 10 seconds
7979
return;
8080
}
@@ -84,15 +84,21 @@ void loop()
8484
/////////////////////////////////////////////////
8585
// Transmit that beautiful bean footage
8686
/////////////////////////////////////////////////
87+
8788
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);
89+
uint32_t data_len = strlen(data);
90+
const uint32_t notecard_binary_area_offset = 0;
91+
NoteBinaryStoreTransmit(reinterpret_cast<uint8_t *>(data), data_len, sizeof(data), notecard_binary_area_offset);
9192
notecard.logDebugf("\n[INFO] Transmitted %d bytes.\n", data_len);
9293

93-
// Log for the sake of curiosity
94+
// Log for the sake of curiosity (not necessary for operation)
95+
// NOTE: NoteBinaryMaxEncodedLength() is imprecise. It will most
96+
// commonly return a number greater than the actual bytes encoded.
97+
// However, in this contrived example there is no difference,
98+
// so it works for the purposes of displaying the encoded data --
99+
// which would never be done in practice.
94100
notecard.logDebug("\n*** Encoded Binary Transmission ***\n");
95-
size_t tx_len = NoteBinaryEncodedLength(reinterpret_cast<uint8_t *>(data), data_len);
101+
uint32_t tx_len = NoteBinaryCodecMaxEncodedLength(data_len);
96102
for (size_t i = 0 ; i < tx_len ; ++i) {
97103
notecard.logDebugf("%02x ", data[i]);
98104
if ((i + 1) % 16 == 0) {
@@ -104,12 +110,21 @@ void loop()
104110
/////////////////////////////////////////////////
105111
// Receive data from the Notecard binary data store
106112
/////////////////////////////////////////////////
107-
size_t rx_buffer_len = 0;
108-
NoteBinaryRequiredRxMaxBuffer(&rx_buffer_len);
113+
114+
// Calcluate the length of the decoded data
115+
data_len = 0;
116+
NoteBinaryStoreDecodedLength(&data_len);
117+
118+
// Create a buffer to receive the entire data store. This buffer must be
119+
// large enough to hold the encoded data that will be transferred from
120+
// the Notecard, as well as the terminating newline.
121+
// `NoteBinaryMaxEncodedLength()` will compute the worst-case size of
122+
// the encoded length plus the byte required for the newline terminator.
123+
uint32_t rx_buffer_len = NoteBinaryCodecMaxEncodedLength(data_len);
109124
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);
125+
126+
// Receive the data
127+
NoteBinaryStoreReceive(reinterpret_cast<uint8_t *>(rx_buffer), rx_buffer_len, 0, data_len);
113128
notecard.logDebugf("\n[INFO] Received %d bytes.\n", data_len);
114129

115130
// Display received buffer
@@ -119,6 +134,9 @@ void loop()
119134
}
120135
notecard.logDebug("\n*** Decoded Data ***\n\n");
121136

137+
// Free the receive buffer
138+
free(rx_buffer);
139+
122140
// NOTE: The binary data store is not cleared on receive, which
123141
// allows us to submit it to Notehub in the next step.
124142
}
@@ -137,7 +155,7 @@ void loop()
137155
if (!notecard.sendRequest(req)) {
138156
// The binary data store is cleared on successful transmission,
139157
// but we need to reset it manually if the request failed.
140-
NoteBinaryReset();
158+
NoteBinaryStoreReset();
141159
}
142160
}
143161
}

examples/Example9_BinarySendReceiveChunked/Example9_BinarySendReceiveChunked.ino

+45-29
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void setup()
6363
notecard.sendRequestWithRetry(req, 5); // 5 seconds
6464

6565
// Reset the state of the Notecard's binary store to a known value.
66-
NoteBinaryReset();
66+
NoteBinaryStoreReset();
6767
}
6868

6969
// In the Arduino main loop which is called repeatedly, add outbound data every
@@ -74,7 +74,7 @@ void loop()
7474
static unsigned event_counter = 0;
7575
if (++event_counter > 5)
7676
{
77-
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.");
77+
notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart.\n");
7878
delay(10000); // 10 seconds
7979
return;
8080
}
@@ -89,45 +89,55 @@ void loop()
8989
// data source. In a real application, you might be reading from an
9090
// EEPROM or other large data source.
9191
const char data_source[] = "https://youtu.be/0epWToAOlFY?t=21";
92-
const size_t data_source_len = strlen(data_source);
92+
const uint32_t data_source_len = strlen(data_source);
9393

9494
// We intend to transmit the buffer in chunks of 8 bytes. The data is
9595
// encoded in place, so we will need to allocate a buffer that is large
96-
// enough to hold the encoded data. `NoteBinaryRequiredBuffer()` will
97-
// compute the worst-case size of the encoded buffer.
98-
const size_t tx_chunk_size = 8;
99-
const size_t tx_buffer_len = NoteBinaryRequiredBuffer(tx_chunk_size);
96+
// enough to hold the encoded data, as well as the terminating newline.
97+
// `NoteBinaryMaxEncodedLength()` will compute the worst-case size of
98+
// the encoded length plus the byte required for the newline terminator.
99+
const uint32_t tx_chunk_size = 8;
100+
const uint32_t tx_buffer_len = NoteBinaryCodecMaxEncodedLength(tx_chunk_size);
100101
uint8_t *tx_buffer = (uint8_t *)malloc(tx_buffer_len);
101102

102103
// Transmit the data in chunks of 8 bytes
103-
size_t notecard_binary_area_offset = 0;
104+
uint32_t notecard_binary_area_offset = 0;
104105
for (size_t chunk = 0 ; notecard_binary_area_offset < data_source_len ; ++chunk) {
105-
size_t tx_len = (((data_source_len - notecard_binary_area_offset) > tx_chunk_size)
106-
? tx_chunk_size
107-
: (data_source_len - notecard_binary_area_offset));
106+
uint32_t data_len = (((data_source_len - notecard_binary_area_offset) > tx_chunk_size)
107+
? tx_chunk_size
108+
: (data_source_len - notecard_binary_area_offset));
108109

109110
// Copy bytes from the data source into the buffer. Note that the
110111
// data must be copied sequentially into the Notecard binary area.
111112
// Therefore, we use the offset for the Notecard binary area as the
112113
// offset into the data source to ensure our data is aligned.
113-
memcpy(tx_buffer, (data_source + notecard_binary_area_offset), tx_len);
114+
memcpy(tx_buffer, (data_source + notecard_binary_area_offset), data_len);
114115

115116
// Transmit the chunk
116-
notecard.logDebugf("Transmitting chunk #%d, containing %d bytes.\n", chunk, tx_len);
117-
if (NoteBinaryTransmit(reinterpret_cast<uint8_t *>(tx_buffer), tx_len, tx_buffer_len, notecard_binary_area_offset)) {
117+
notecard.logDebugf("Transmitting chunk #%d, containing %d bytes.\n", chunk, data_len);
118+
if (NoteBinaryStoreTransmit(reinterpret_cast<uint8_t *>(tx_buffer), data_len, tx_buffer_len, notecard_binary_area_offset)) {
118119
--chunk;
119120
notecard.logDebug("Failed to transmit.\n");
120121
continue;
121122
}
122123

123124
// Update the offset
124-
notecard_binary_area_offset += tx_len;
125-
notecard.logDebugf("[INFO] Transmitted %d bytes.\n", tx_len);
126-
127-
// Log for the sake of curiosity
125+
notecard_binary_area_offset += data_len;
126+
notecard.logDebugf("[INFO] Transmitted %d bytes.\n", data_len);
127+
128+
// Log for the sake of curiosity (not necessary for operation)
129+
// NOTE: NoteBinaryMaxEncodedLength() is imprecise. It will most
130+
// commonly return a number greater than the actual bytes
131+
// encoded. However, in this contrived example there is no
132+
// difference, so it works for the purposes of displaying the
133+
// encoded data -- which would never be done in practice.
128134
notecard.logDebug("\n*** Encoded Binary Transmission ***\n");
135+
uint32_t tx_len = NoteBinaryCodecMaxEncodedLength(data_len);
129136
for (size_t i = 0 ; i < tx_len ; ++i) {
130137
notecard.logDebugf("%02x ", tx_buffer[i]);
138+
if ((i + 1) % 16 == 0) {
139+
notecard.logDebug("\n");
140+
}
131141
}
132142
notecard.logDebug("\n*** Encoded Binary Transmission ***\n\n");
133143
}
@@ -144,28 +154,31 @@ void loop()
144154
// other large data store.
145155
char data_store[64] = {0};
146156

157+
// Calcluate the length of the decoded data
158+
uint32_t rx_data_len = 0;
159+
NoteBinaryStoreDecodedLength(&rx_data_len);
160+
147161
// We intend to receive the Notecard's binary data store in chunks of
148162
// 12 bytes. The `offset` and `length` used to request data describe
149163
// decoded data. Therefore we will need to allocate a buffer that is
150164
// large enough to hold the encoded data that will be transferred from
151-
// the Notecard. `NoteBinaryRequiredBuffer()` will compute the
152-
// worst-case size of the encoded data.
153-
const size_t rx_chunk_size = 12;
154-
const size_t rx_buffer_len = NoteBinaryRequiredBuffer(rx_chunk_size);
165+
// the Notecard, as well as the terminating newline.
166+
// `NoteBinaryMaxEncodedLength()` will compute the worst-case size of
167+
// the encoded length plus the byte required for the newline terminator.
168+
const uint32_t rx_chunk_size = 12;
169+
const uint32_t rx_buffer_len = NoteBinaryCodecMaxEncodedLength(rx_chunk_size);
155170
uint8_t *rx_buffer = (uint8_t *)malloc(rx_buffer_len);
156-
size_t rx_data_len = 0;
157-
NoteBinaryDataLength(&rx_data_len);
158171

159172
// Receive the data in chunks of 12 bytes
160173
notecard_binary_area_offset = 0;
161174
for (size_t chunk = 0 ; notecard_binary_area_offset < rx_data_len ; ++chunk) {
162-
size_t rx_len = (((rx_data_len - notecard_binary_area_offset) > rx_chunk_size)
163-
? rx_chunk_size
164-
: (rx_data_len - notecard_binary_area_offset));
175+
uint32_t rx_len = (((rx_data_len - notecard_binary_area_offset) > rx_chunk_size)
176+
? rx_chunk_size
177+
: (rx_data_len - notecard_binary_area_offset));
165178

166179
// Receive the chunk
167180
notecard.logDebugf("Receiving chunk #%d, containing %d bytes.\n", chunk, rx_len);
168-
if (NoteBinaryReceive(reinterpret_cast<uint8_t *>(rx_buffer), rx_buffer_len, notecard_binary_area_offset, &rx_len)) {
181+
if (NoteBinaryStoreReceive(reinterpret_cast<uint8_t *>(rx_buffer), rx_buffer_len, notecard_binary_area_offset, rx_len)) {
169182
--chunk;
170183
notecard.logDebug("Failed to receive.\n");
171184
continue;
@@ -196,6 +209,9 @@ void loop()
196209
}
197210
notecard.logDebug("\n*** Decoded Data ***\n\n");
198211

212+
// Free the receive buffer
213+
free(rx_buffer);
214+
199215
// NOTE: The binary data store is not cleared on receive, which
200216
// allows us to submit it to Notehub in the next step.
201217
}
@@ -214,7 +230,7 @@ void loop()
214230
if (!notecard.sendRequest(req)) {
215231
// The binary store is cleared on successful transmission, but
216232
// we need to reset it manually if the request failed.
217-
NoteBinaryReset();
233+
NoteBinaryStoreReset();
218234
}
219235
}
220236
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Blues Wireless Notecard
2-
version=1.4.5
2+
version=1.5.0
33
author=Blues Wireless
44
maintainer=Blues Wireless <[email protected]>
55
sentence=An easy to use Notecard Library for Arduino.

0 commit comments

Comments
 (0)