@@ -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 }
0 commit comments