From 2e3e17702c7cb129ec9d89d928cc890b2286a305 Mon Sep 17 00:00:00 2001 From: NoSloppy <53964195+NoSloppy@users.noreply.github.com> Date: Sat, 9 Dec 2023 18:25:49 -0500 Subject: [PATCH 1/3] Blade ID v2 (#591) --- ProffieOS.ino | 2 +- blades/ws2811_blade.h | 20 +++++------ props/dual_prop.h | 82 ++++++++++++++++++++++++++++--------------- props/prop_base.h | 49 +++++++++++++++----------- sound/hybrid_font.h | 10 ++++-- 5 files changed, 99 insertions(+), 64 deletions(-) diff --git a/ProffieOS.ino b/ProffieOS.ino index c3fcb462d..ae9346f1f 100644 --- a/ProffieOS.ino +++ b/ProffieOS.ino @@ -1567,7 +1567,7 @@ void setup() { Looper::DoSetup(); // Time to identify the blade. - prop.FindBlade(); + prop.FindBlade(true); SaberBase::DoBoot(); #if defined(ENABLE_SD) if (!sd_card_found) ProffieOSErrors::sd_card_not_found(); diff --git a/blades/ws2811_blade.h b/blades/ws2811_blade.h index 9bb989326..7f251cf08 100644 --- a/blades/ws2811_blade.h +++ b/blades/ws2811_blade.h @@ -253,11 +253,11 @@ WS2811_Blade(WS2811PIN* pin, if (!current_style_ || !run_) { loop_counter_.Reset(); #ifdef BLADE_ID_SCAN_MILLIS - if (pin_->pin() == bladeIdentifyPin && ScanBladeIdNow()) { - pin_->Enable(powered_); - SLEEP(1); - } -#endif + if (pin_->pin() == bladePin && ScanBladeIdNow()) { + pin_->Enable(powered_); + SLEEP(1); + } +#endif // BLADE_ID_SCAN_MILLIS continue; } // Wait until it's our turn. @@ -288,12 +288,12 @@ WS2811_Blade(WS2811PIN* pin, while (!pin_->IsReadyForEndFrame()) BLADE_YIELD(); #ifdef BLADE_ID_SCAN_MILLIS - if (pin_->pin() == bladeIdentifyPin && ScanBladeIdNow()) { - pin_->Enable(powered_); - SLEEP(1); - if (current_blade != this) goto retry; + if (pin_->pin() == bladePin && ScanBladeIdNow()) { + pin_->Enable(powered_); + SLEEP(1); + if (current_blade != this) goto retry; } -#endif +#endif // BLADE_ID_SCAN_MILLIS pin_->EndFrame(); loop_counter_.Update(); diff --git a/props/dual_prop.h b/props/dual_prop.h index 5423a72b4..d86e56dfc 100644 --- a/props/dual_prop.h +++ b/props/dual_prop.h @@ -3,29 +3,49 @@ /* dual_prop.h allows for 2 discrete prop files to be used, -alternating on a latched switch (Blade Detect). +alternating on a latched switch (Blade Detect) or with Blade ID.. This is useful when you want a saber to toggle to a blaster for example, and you want the buttons to take on different behaviors. How to use: +Edit your config file as follows. +Setup the prop section like this: #ifdef CONFIG_PROP #include "../props/dual_prop.h" #include "../props/saber_sa22c_buttons.h" #include "../props/blaster.h" #undef PROP_TYPE -#define PROP_TYPE DualProp +#define PROP_TYPE DualProp // these are the classes found in the 2 props. #endif -** Note the prop file SaberSA22CButtons here would change -to the file name of the prop you choose. - -Then setup your config file for 2 states: -Bladein preset bank, and a BladeOut preset bank. -Give the BladeConfig 2 sets of descriptions, and -#define BLADE_DETECT_PIN properly. +** Note the prop file names (saber_sa22c_buttons.h and blaster.h) +as well as the class names would change based on the prop you choose. + +Now setup your CONFIG_PRESETS section to have multiple preset banks: +A no-blade (Blaster) preset bank, followed by one or more blade-in preset banks. +Also, the BladeConfig needs each blade (and no-blade) to have a description entry in +the same order of the Presets arrays. The Blade ID values will depend on whether +you choose to use Blade Detect or Blade ID. + +- Blade Detect using a wired latching system: +Add #define BLADE_DETECT_PIN with the pin number used. +See here for setup info: +https://pod.hubbe.net/howto/blade-detect.html + +- Blade ID using the measured resistance of the blade to switch: +Add the following to the config file + #define ENABLE_POWER_FOR_ID PowerPINS (based on the LED pads you use) + #define SHARED_POWER_PINS + #define BLADE_ID_SCAN_MILLIS 1000 + #define BLADE_ID_TIMES 15 +// This will make the blade ID class for that range return NO_BLADE +// (use NO_BLADE as the blade definition value.) +#define NO_BLADE_ID_RANGE 685000,1000000000 + +See here for setup info: +https://pod.hubbe.net/howto/blade-id.html */ - #define DUAL_PROP_CONDITION blade_present() #ifdef PROP_INHERIT_PREFIX @@ -110,24 +130,26 @@ class SaberBlasterProp : public virtual Saber, public virtual Blaster { } const char* name() override { return "DualProp"; } bool Event(enum BUTTON button, EVENT event) override { + +#ifdef BLADE_DETECT_PIN if (button == BUTTON_BLADE_DETECT) { if (event == EVENT_LATCH_ON) { - Saber::Off(); - Saber::blade_detected_ = true; - Saber::FindBladeAgain(); - SaberBase::DoBladeDetect(true); - current_modifiers = 0; - return true; + Saber::Off(); + Saber::blade_detected_ = true; + Saber::FindBladeAgain(); + SaberBase::DoBladeDetect(true); } else if (event == EVENT_LATCH_OFF) { - Saber::Off(); - Saber::blade_detected_ = false; - Saber::FindBladeAgain(); - SaberBase::DoBladeDetect(false); - current_modifiers = 0; - return true; + Saber::Off(); + Saber::blade_detected_ = false; + Saber::FindBladeAgain(); + SaberBase::DoBladeDetect(false); } + current_modifiers = 0; + return true; } - if (Saber::blade_detected_) { +#endif // BLADE_DETECT_PIN + + if (DUAL_PROP_CONDITION) { return Saber::Event(button, event); } else { button = static_cast(map_button(button)); @@ -148,7 +170,7 @@ class SaberBlasterProp : public virtual Saber, public virtual Blaster { } bool Event2(enum BUTTON button, EVENT event, uint32_t modifiers) override { - if (Saber::blade_detected_) { + if (DUAL_PROP_CONDITION) { return Saber::Event2(button, event, modifiers); } else { return Blaster::Event2(button, event, modifiers); @@ -156,15 +178,17 @@ class SaberBlasterProp : public virtual Saber, public virtual Blaster { } void SetPreset(int preset_num, bool announce) override { - if (Saber::blade_detected_) { + if (DUAL_PROP_CONDITION) { Saber::SetPreset(preset_num, announce); + // Blaster is always on, so turn Off before going to Saber mode + if (Saber::IsOn()) Saber::Off(); } else { Blaster::SetPreset(preset_num, announce); } } void Loop() override { - if (Saber::blade_detected_) { + if (DUAL_PROP_CONDITION) { Saber::Loop(); } else { Blaster::Loop(); @@ -172,7 +196,7 @@ class SaberBlasterProp : public virtual Saber, public virtual Blaster { } void DoMotion(const Vec3& motion, bool clear) override { - if (Saber::blade_detected_) { + if (DUAL_PROP_CONDITION) { Saber::DoMotion(motion, clear); } else { Blaster::DoMotion(motion, clear); @@ -180,7 +204,7 @@ class SaberBlasterProp : public virtual Saber, public virtual Blaster { } void Clash(bool stab, float strength) override { - if (Saber::blade_detected_) { + if (DUAL_PROP_CONDITION) { Saber::Clash(stab, strength); } else { Blaster::Clash(stab, strength); @@ -188,7 +212,7 @@ class SaberBlasterProp : public virtual Saber, public virtual Blaster { } void SB_Effect(EffectType effect, float location) override { - if (Saber::blade_detected_) { + if (DUAL_PROP_CONDITION) { Saber::SB_Effect(effect, location); } else { Blaster::SB_Effect(effect, location); diff --git a/props/prop_base.h b/props/prop_base.h index fbb7c427d..3ecdbbeec 100644 --- a/props/prop_base.h +++ b/props/prop_base.h @@ -444,7 +444,7 @@ class PropBase : CommandParser, Looper, protected SaberBase { chdir(current_preset_.font.get()); if (on) On(); if (announce) { - STDOUT << "DISPLAY: " << current_preset_name() << "\n"; + PVLOG_STATUS << "Current Preset: " << current_preset_name() << "\n"; SaberBase::DoNewFont(); } TRACE(PROP, "end"); @@ -552,32 +552,39 @@ class PropBase : CommandParser, Looper, protected SaberBase { } // Measure and return the blade identifier resistor. - float id() { + float id(bool announce = false) { EnableBooster(); BLADE_ID_CLASS_INTERNAL blade_id; float ret = blade_id.id(); - PVLOG_STATUS << "BLADE ID: " << ret << "\n"; + + if (announce) { + PVLOG_STATUS << "BLADE ID: " << ret << "\n"; #ifdef SPEAK_BLADE_ID - talkie.Say(spI); - talkie.Say(spD); - talkie.SayNumber((int)ret); -#endif +#ifdef DISABLE_TALKIE + #error You cannot define both DISABLE_TALKIE and SPEAK_BLADE_ID +#else + talkie.Say(spI); + talkie.Say(spD); + talkie.SayNumber((int)ret); +#endif // DISABLE_TALKIE +#endif // SPEAK_BLADE_ID + } #ifdef BLADE_DETECT_PIN if (!blade_detected_) { - STDOUT << "NO "; + PVLOG_STATUS << "NO "; ret += NO_BLADE; } - STDOUT << "Blade Detected\n"; + PVLOG_STATUS << "Blade Detected\n"; #endif - return ret; + return ret; } - size_t FindBestConfig() { + size_t FindBestConfig(bool announce = false) { static_assert(NELEM(blades) > 0, "blades array cannot be empty"); size_t best_config = 0; if (NELEM(blades) > 1) { - float resistor = id(); + float resistor = id(announce); float best_err = 100000000.0; for (size_t i = 0; i < NELEM(blades); i++) { @@ -603,9 +610,9 @@ class PropBase : CommandParser, Looper, protected SaberBase { 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; + // 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; } @@ -628,12 +635,12 @@ class PropBase : CommandParser, Looper, protected SaberBase { } #else void PollScanId() {} -#endif +#endif // BLADE_ID_SCAN_MILLIS // Called from setup to identify the blade and select the right // Blade driver, style and sound font. - void FindBlade() { - size_t best_config = FindBestConfig(); + void FindBlade(bool announce = false) { + size_t best_config = FindBestConfig(announce); PVLOG_STATUS << "blade = " << best_config << "\n"; current_config = blades + best_config; @@ -648,7 +655,7 @@ class PropBase : CommandParser, Looper, protected SaberBase { ResumePreset(); #else SetPreset(0, false); -#endif +#endif // SAVE_PRESET return; #if NUM_BLADES != 0 @@ -750,7 +757,7 @@ class PropBase : CommandParser, Looper, protected SaberBase { ONCEPERBLADE(DEACTIVATE); SaveVolumeIfNeeded(); - FindBlade(); + FindBlade(true); } bool CheckInteractivePreon() { @@ -1279,7 +1286,7 @@ class PropBase : CommandParser, Looper, protected SaberBase { bool Parse(const char *cmd, const char* arg) override { if (!strcmp(cmd, "id")) { - id(); + id(true); return true; } if (!strcmp(cmd, "scanid")) { diff --git a/sound/hybrid_font.h b/sound/hybrid_font.h index 24baf7584..0e159d1fa 100644 --- a/sound/hybrid_font.h +++ b/sound/hybrid_font.h @@ -627,15 +627,19 @@ class HybridFont : public SaberBase, public Looper { PlayPolyphonic(&X); return; } - if (detected && SFX_boot) { - PlayPolyphonic(&SFX_boot); + // SFX_bladein/out doesn't exist, playing font.wav instead + if (SFX_font) { + PVLOG_STATUS << "SFX_bladein/out doesn't exist, playing font.wav instead.\n"; + PlayPolyphonic(&SFX_font); return; } + PVLOG_STATUS << " Didn't find font.wav, playing beep instead.\n"; + // Otherwise, just beep to indicate blade status change. beeper.Beep(0.05, 2000.0); } void SB_NewFont() { if (!PlayPolyphonic(&SFX_font)) { - beeper.Beep(0.05, 2000.0); + beeper.Beep(0.05, 1046.5); } } void SB_Change(SaberBase::ChangeType change) override { From 718a72780a65209401ab2f7de6b3b366df3ff3dd Mon Sep 17 00:00:00 2001 From: NoSloppy <53964195+NoSloppy@users.noreply.github.com> Date: Sat, 9 Dec 2023 19:53:29 -0500 Subject: [PATCH 2/3] make blaster.h DISABLE_TALKIE compliant (#604) --- props/blaster.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/props/blaster.h b/props/blaster.h index bcbed2911..c8b77ae28 100644 --- a/props/blaster.h +++ b/props/blaster.h @@ -443,7 +443,11 @@ class Blaster : public PROP_INHERIT_PREFIX PropBase { } else if (SFX_mode) { hybrid_font.PlayCommon(&SFX_mode); } else { +#ifndef DISABLE_TALKIE talkie.Say(spSTUN); +#else + beeper.Beep(0.05, 2000.0); +#endif } break; case MODE_KILL: @@ -452,7 +456,11 @@ class Blaster : public PROP_INHERIT_PREFIX PropBase { } else if (SFX_mode) { hybrid_font.PlayCommon(&SFX_mode); } else { +#ifndef DISABLE_TALKIE talkie.Say(spKILL); +#else + beeper.Beep(0.05, 2000.0); +#endif } break; case MODE_AUTO: @@ -461,7 +469,11 @@ class Blaster : public PROP_INHERIT_PREFIX PropBase { } else if (SFX_mode) { hybrid_font.PlayCommon(&SFX_mode); } else { +#ifndef DISABLE_TALKIE talkie.Say(spAUTOFIRE); +#else + beeper.Beep(0.05, 2000.0); +#endif } break; } From 7dd6d26178f8130d564e718e5339b87d1e2eb956 Mon Sep 17 00:00:00 2001 From: NoSloppy <53964195+NoSloppy@users.noreply.github.com> Date: Sat, 9 Dec 2023 23:11:09 -0500 Subject: [PATCH 3/3] fix DUAL_PROP_CONDITION (#605) Avoid error: there are no arguments to 'blade_present' that depend on a template parameter, so a declaration of 'blade_present' must be available [-fpermissive] --- props/dual_prop.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/props/dual_prop.h b/props/dual_prop.h index d86e56dfc..fcd57065e 100644 --- a/props/dual_prop.h +++ b/props/dual_prop.h @@ -46,7 +46,7 @@ See here for setup info: https://pod.hubbe.net/howto/blade-id.html */ -#define DUAL_PROP_CONDITION blade_present() +#define DUAL_PROP_CONDITION this->blade_present() #ifdef PROP_INHERIT_PREFIX #error dual_prop.h must be included first