From 7c6f58f796a3d13d7910b94baa48e05e5248735e Mon Sep 17 00:00:00 2001 From: Christopher Shannon Date: Sat, 28 Sep 2024 18:10:43 -0400 Subject: [PATCH] Add Blade ID Scan Timeout --- props/prop_base.h | 48 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/props/prop_base.h b/props/prop_base.h index 916582106..c8f675f3e 100644 --- a/props/prop_base.h +++ b/props/prop_base.h @@ -356,6 +356,9 @@ class PropBase : CommandParser, Looper, protected SaberBase, public ModeInterfac BladeSet BladeOff() { #ifdef IDLE_OFF_TIME last_on_time_ = millis(); +#endif +#ifdef BLADE_ID_SCAN_MILLIS + blade_id_scan_start_ = millis(); #endif bool on = IsOn(); BladeSet ret = SaberBase::OnBlades(); @@ -580,23 +583,31 @@ class PropBase : CommandParser, Looper, protected SaberBase, public ModeInterfac #ifdef BLADE_ID_SCAN_MILLIS #ifndef SHARED_POWER_PINS #warning SHARED_POWER_PINS is recommended when using BLADE_ID_SCAN_MILLIS +#endif +#ifndef BLADE_ID_SCAN_TIMEOUT +#define BLADE_ID_SCAN_TIMEOUT 60 * 10 * 1000 #endif bool find_blade_again_pending_ = false; uint32_t last_scan_id_ = 0; - bool ScanBladeIdNow() { - uint32_t now = millis(); - if (now - last_scan_id_ > BLADE_ID_SCAN_MILLIS) { - last_scan_id_ = now; - size_t best_config = FindBestConfig(); - if (current_config != blades + best_config) { - // We can't call FindBladeAgain right away because - // we're called from the blade. Wait until next loop() call. - find_blade_again_pending_ = true; - } - return true; + bool ScanBladeIdNow() { + uint32_t now = millis(); + if (IsOn() == false && (now - blade_id_scan_start_) < BLADE_ID_SCAN_TIMEOUT) { + if (now - last_scan_id_ > BLADE_ID_SCAN_MILLIS) { + last_scan_id_ = now; + size_t best_config = FindBestConfig(); + if (current_config != blades + best_config) { + // We can't call FindBladeAgain right away because + // we're called from the blade. Wait until next loop() call. + find_blade_again_pending_ = true; + } + return true; + } + return false; + } + else{ + return false; + } } - return false; - } // Must be called from loop() void PollScanId() { @@ -1120,6 +1131,13 @@ class PropBase : CommandParser, Looper, protected SaberBase, public ModeInterfac current_mode->mode_Loop(); +#ifdef BLADE_ID_SCAN_MILLIS + if (SaberBase::IsOn() || + (current_style() && current_style()->Charging())) { + blade_id_scan_start_ = millis(); + } +#endif + #ifdef IDLE_OFF_TIME if (SaberBase::IsOn() || (current_style() && current_style()->Charging())) { @@ -1140,6 +1158,10 @@ class PropBase : CommandParser, Looper, protected SaberBase, public ModeInterfac #ifdef IDLE_OFF_TIME uint32_t last_on_time_; #endif + +#ifdef BLADE_ID_SCAN_MILLIS + uint32_t blade_id_scan_start_; +#endif #ifdef SOUND_LIBRARY_REQUIRED RefPtr wav_player_;