@@ -63,7 +63,7 @@ void setup()
63
63
notecard.sendRequestWithRetry (req, 5 ); // 5 seconds
64
64
65
65
// Reset the state of the Notecard's binary store to a known value.
66
- NoteBinaryReset ();
66
+ NoteBinaryStoreReset ();
67
67
}
68
68
69
69
// In the Arduino main loop which is called repeatedly, add outbound data every
@@ -74,7 +74,7 @@ void loop()
74
74
static unsigned event_counter = 0 ;
75
75
if (++event_counter > 5 )
76
76
{
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 " );
78
78
delay (10000 ); // 10 seconds
79
79
return ;
80
80
}
@@ -89,45 +89,55 @@ void loop()
89
89
// data source. In a real application, you might be reading from an
90
90
// EEPROM or other large data source.
91
91
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);
93
93
94
94
// We intend to transmit the buffer in chunks of 8 bytes. The data is
95
95
// 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);
100
101
uint8_t *tx_buffer = (uint8_t *)malloc (tx_buffer_len);
101
102
102
103
// Transmit the data in chunks of 8 bytes
103
- size_t notecard_binary_area_offset = 0 ;
104
+ uint32_t notecard_binary_area_offset = 0 ;
104
105
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));
108
109
109
110
// Copy bytes from the data source into the buffer. Note that the
110
111
// data must be copied sequentially into the Notecard binary area.
111
112
// Therefore, we use the offset for the Notecard binary area as the
112
113
// 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 );
114
115
115
116
// 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)) {
118
119
--chunk;
119
120
notecard.logDebug (" Failed to transmit.\n " );
120
121
continue ;
121
122
}
122
123
123
124
// 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.
128
134
notecard.logDebug (" \n *** Encoded Binary Transmission ***\n " );
135
+ uint32_t tx_len = NoteBinaryCodecMaxEncodedLength (data_len);
129
136
for (size_t i = 0 ; i < tx_len ; ++i) {
130
137
notecard.logDebugf (" %02x " , tx_buffer[i]);
138
+ if ((i + 1 ) % 16 == 0 ) {
139
+ notecard.logDebug (" \n " );
140
+ }
131
141
}
132
142
notecard.logDebug (" \n *** Encoded Binary Transmission ***\n\n " );
133
143
}
@@ -144,28 +154,31 @@ void loop()
144
154
// other large data store.
145
155
char data_store[64 ] = {0 };
146
156
157
+ // Calcluate the length of the decoded data
158
+ uint32_t rx_data_len = 0 ;
159
+ NoteBinaryStoreDecodedLength (&rx_data_len);
160
+
147
161
// We intend to receive the Notecard's binary data store in chunks of
148
162
// 12 bytes. The `offset` and `length` used to request data describe
149
163
// decoded data. Therefore we will need to allocate a buffer that is
150
164
// 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);
155
170
uint8_t *rx_buffer = (uint8_t *)malloc (rx_buffer_len);
156
- size_t rx_data_len = 0 ;
157
- NoteBinaryDataLength (&rx_data_len);
158
171
159
172
// Receive the data in chunks of 12 bytes
160
173
notecard_binary_area_offset = 0 ;
161
174
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));
165
178
166
179
// Receive the chunk
167
180
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)) {
169
182
--chunk;
170
183
notecard.logDebug (" Failed to receive.\n " );
171
184
continue ;
@@ -196,6 +209,9 @@ void loop()
196
209
}
197
210
notecard.logDebug (" \n *** Decoded Data ***\n\n " );
198
211
212
+ // Free the receive buffer
213
+ free (rx_buffer);
214
+
199
215
// NOTE: The binary data store is not cleared on receive, which
200
216
// allows us to submit it to Notehub in the next step.
201
217
}
@@ -214,7 +230,7 @@ void loop()
214
230
if (!notecard.sendRequest (req)) {
215
231
// The binary store is cleared on successful transmission, but
216
232
// we need to reset it manually if the request failed.
217
- NoteBinaryReset ();
233
+ NoteBinaryStoreReset ();
218
234
}
219
235
}
220
236
}
0 commit comments