1-
2-
3-
1+ /* ==========================================================================================
2+ MIT License
3+
4+ Copyright (c) 2023-2026 https://madflight.com
5+
6+ Permission is hereby granted, free of charge, to any person obtaining a copy
7+ of this software and associated documentation files (the "Software"), to deal
8+ in the Software without restriction, including without limitation the rights
9+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ copies of the Software, and to permit persons to whom the Software is
11+ furnished to do so, subject to the following conditions:
12+
13+ The above copyright notice and this permission notice shall be included in all
14+ copies or substantial portions of the Software.
15+
16+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ SOFTWARE.
23+ ===========================================================================================*/
424
525// BinLog uses 32bit microsecond timestamps (good for 1 hour of recording before wrap-around)
626
@@ -23,15 +43,13 @@ namespace BinLogWriter {
2343 static void bbx_task (void *pvParameters);
2444 void FMT_sendFMT ();
2545
26-
27- Command_t command = NONE;
2846 State_t state = READY;
2947 uint32_t startMicros = 0 ;
3048 uint32_t missCnt = 0 ;
3149
3250 struct msg_t
3351 {
34- uint8_t buf[MAX_MSG_LEN]; // NOTE: first byte overwritten with msg len when stuffed in queue
52+ uint8_t buf[MAX_MSG_LEN]; // NOTE: first byte overwritten with msg len when stuffed in queue, when len==0 then second byte is Command_t
3553 };
3654
3755 QueueHandle_t queue = nullptr ;
@@ -44,20 +62,15 @@ namespace BinLogWriter {
4462 uint32_t typeRegistry[LOG_TYPE_LEN] = {}; // MSB is FMT+FMTU written flag, 31 LSB is message name
4563 uint8_t typeRegistry_len = 0 ;
4664
47-
48-
49-
5065 void stop () {
51- command = STOP;
5266 // send zero length message to wake up BB task to process command
53- uint8_t msg = 0 ;
67+ uint8_t msg[] = { 0 , STOP} ;
5468 xQueueSend (queue, (void *)&msg, 0 );
5569 }
5670
5771 void start () {
58- command = START;
5972 // send zero length message to wake up BB task to process command
60- uint8_t msg = 0 ;
73+ uint8_t msg[] = { 0 , START} ;
6174 xQueueSend (queue, (void *)&msg, 0 );
6275 }
6376
@@ -76,10 +89,12 @@ namespace BinLogWriter {
7689
7790 // append message to queue
7891 bool queueSend (uint8_t *buf, uint8_t len, uint8_t keepfree) {
79- if (keepfree && uxQueueSpacesAvailable (queue) < keepfree) return false ;
80- // NOTE: random data will pad the message - improve this?
92+ if (keepfree && uxQueueSpacesAvailable (queue) < keepfree) {
93+ missCnt++;
94+ return false ;
95+ }
8196 buf[0 ] = len; // overwrite header1 with message length
82- bool ok = ( xQueueSend (queue, (void *)buf, 0 ) == pdPASS );
97+ bool ok = ( xQueueSend (queue, (void *)buf, 0 ) == pdPASS ); // NOTE: random data will pad the message
8398 if (!ok) missCnt++;
8499 return ok;
85100 }
@@ -214,7 +229,7 @@ namespace BinLogWriter {
214229
215230
216231 void cmd_start () {
217- if (state!= READY) return ;
232+ if (state != READY) return ;
218233 state = STARTING;
219234
220235 // empty the queue
@@ -263,6 +278,9 @@ namespace BinLogWriter {
263278
264279 // allow logging
265280 state = STARTED;
281+
282+ // log flight mode
283+ bbx.log_mode ();
266284 }
267285
268286 void cmd_stop () {
@@ -284,21 +302,22 @@ namespace BinLogWriter {
284302 static msg_t msg;
285303 for (;;) {
286304 bool updated = (xQueueReceive (queue, &msg, portMAX_DELAY) == pdPASS);
305+ runtimeTrace.start ();
287306 if (updated) {
288- runtimeTrace. start () ;
289- switch (command ) {
290- case START:
291- command = NONE;
292- cmd_start ();
293- break ;
294- case STOP:
295- command = NONE;
296- cmd_stop ();
297- break ;
298- case NONE:
299- // extract message length and restore header1
300- uint8_t len = msg. buf [ 0 ];
301- msg.buf [0 ] = HEAD_BYTE1;
307+ uint8_t len = msg. buf [ 0 ] ;
308+ if (len == 0 ) {
309+ // decode command
310+ switch ( (Command_t)msg. buf [ 1 ] ) {
311+ case START:
312+ cmd_start () ;
313+ break ;
314+ case STOP:
315+ cmd_stop ();
316+ break ;
317+ }
318+ } else {
319+ // write message
320+ msg.buf [0 ] = HEAD_BYTE1; // restore header1
302321 if (bbx.gizmo ) bbx.gizmo ->write (msg.buf , len);
303322 }
304323 }
0 commit comments