@@ -121,6 +121,13 @@ void connection_update_sensor_temp(float temp)
121121 sensor_temp = ((temp - 25 ) * 2 + 128.5f ); // -38.5 - +88.5 -> 1-255
122122}
123123
124+ static int64_t timeout_time = INT64_MAX ;
125+
126+ void connection_update_sensor_timeout_time (int64_t timeout )
127+ {
128+ timeout_time = timeout ;
129+ }
130+
124131void connection_update_battery (bool battery_available , bool plugged , bool charged , uint32_t battery_pptt , int battery_mV ) // format for packet send
125132{
126133 if (!battery_available ) // No battery, and voltage is <=1500mV
@@ -164,6 +171,13 @@ void connection_update_button(int button)
164171 button_update_time = k_uptime_get ();
165172}
166173
174+ static bool shutdown = false;
175+
176+ void connection_set_shutdown (void )
177+ {
178+ shutdown = true;
179+ }
180+
167181//|b0 |b1 |b2 |b3 |b4 |b5 |b6 |b7 |b8 |b9 |b10 |b11 |b12 |b13 |b14 |b15 |
168182//|type |id |packet data |
169183//|0 |id |batt |batt_v |temp |brd_id |mcu_id |resv |imu_id |mag_id |fw_date |major |minor |patch |rssi |
@@ -172,8 +186,10 @@ void connection_update_button(int button)
172186//|3 |id |svr_stat|status |resv |rssi |
173187//|4 |id |q0 |q1 |q2 |q3 |m0 |m1 |m2 |
174188//|5 |id |runtime |resv |rssi |
175- //|6 |id |button |resv |rssi |
176- //|7 |id |button |resv |q_buf |a0 |a1 |a2 |rssi |
189+ //|6 |id |button |sleeptime | |rssi |
190+ //|7 |id |button |sleeptime |q_buf |a0 |a1 |a2 |rssi |
191+
192+ // runtime is in microseconds (overkill), sleeptime is in milliseconds (overkill but less)
177193
178194void connection_write_packet_0 () // device info
179195{
@@ -315,12 +331,19 @@ void connection_write_packet_5() // runtime
315331 hid_write_packet_n (data ); // TODO:
316332}
317333
318- void connection_write_packet_6 () // reduced precision quat and accel with button
334+ void connection_write_packet_6 () // reduced precision quat and accel with button and sleep time
319335{
320336 uint8_t data [16 ] = {0 };
321337 data [0 ] = 6 ; // packet 6
322338 data [1 ] = tracker_id ;
323339 data [2 ] = tracker_button ;
340+ uint16_t * buf = (uint16_t * )& data [3 ];
341+ if (shutdown )
342+ buf = 1 ;
343+ else
344+ buf = timeout_time < 1 ? 1 : timeout_time ;
345+ if (k_ticks_to_ms_floor64 (sys_get_battery_remaining_time_estimate ()) < 60000 && timeout_time == UINT16_MAX )
346+ timeout_time = UINT16_MAX - 1 ;
324347 data [15 ] = 0 ; // rssi (supplied by receiver)
325348 k_mutex_lock (& data_buffer_mutex , K_FOREVER );
326349 memcpy (data_buffer , data , sizeof (data ));
@@ -335,12 +358,19 @@ void connection_write_packet_6() // reduced precision quat and accel with button
335358 hid_write_packet_n (data ); // TODO:
336359}
337360
338- void connection_write_packet_7 () // button
361+ void connection_write_packet_7 () // button and sleep time
339362{
340363 uint8_t data [16 ] = {0 };
341364 data [0 ] = 7 ; // packet 7
342365 data [1 ] = tracker_id ;
343366 data [2 ] = tracker_button ;
367+ uint16_t * buf = (uint16_t * )& data [3 ];
368+ if (shutdown )
369+ buf = 1 ;
370+ else
371+ buf = timeout_time < 1 ? 1 : timeout_time ;
372+ if (k_ticks_to_ms_floor64 (sys_get_battery_remaining_time_estimate ()) < 60000 && timeout_time == UINT16_MAX )
373+ timeout_time = UINT16_MAX - 1 ;
344374 float v [3 ] = {0 };
345375 q_fem (sensor_q , v ); // exponential map
346376 for (int i = 0 ; i < 3 ; i ++ )
@@ -383,7 +413,6 @@ static int64_t last_status2_time = 0;
383413
384414void connection_thread (void )
385415{
386- bool use_button = !CONFIG_0_SETTINGS_READ (CONFIG_0_USER_EXTRA_ACTIONS ); // TODO: until info2 has extra data, it can be disabled for now if extra actions overrides button
387416 uint8_t data_copy [21 ];
388417 // TODO: checking for connection_update events from sensor_loop, here we will time and send them out
389418 while (1 )
@@ -417,7 +446,7 @@ void connection_thread(void)
417446 continue ;
418447 }
419448 // if time for info2 and precise quat not needed
420- else if (use_button && quat_update_time && !send_precise_quat && k_uptime_get () - last_info2_time > 100 )
449+ else if (quat_update_time && !send_precise_quat && k_uptime_get () - last_info2_time > 100 )
421450 {
422451 quat_update_time = 0 ;
423452 last_quat_time = k_uptime_get ();
@@ -439,7 +468,7 @@ void connection_thread(void)
439468 connection_write_packet_0 ();
440469 continue ;
441470 }
442- else if (use_button && k_uptime_get () - last_info2_time > 100 )
471+ else if (k_uptime_get () - last_info2_time > 100 )
443472 {
444473 last_info2_time = k_uptime_get ();
445474 connection_write_packet_6 ();
0 commit comments