Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/profezzorn/ProffieOS
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Dec 10, 2023
2 parents d48e7a9 + 7dd6d26 commit 391870d
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 65 deletions.
2 changes: 1 addition & 1 deletion ProffieOS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,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();
Expand Down
20 changes: 10 additions & 10 deletions blades/ws2811_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();

Expand Down
12 changes: 12 additions & 0 deletions props/blaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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;
}
Expand Down
84 changes: 54 additions & 30 deletions props/dual_prop.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,50 @@

/*
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<SaberSA22CButtons, Blaster>
#define PROP_TYPE DualProp<SaberSA22CButtons, Blaster> // 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<bladePowerPin2, bladePowerPin3> (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()
#define DUAL_PROP_CONDITION this->blade_present()

#ifdef PROP_INHERIT_PREFIX
#error dual_prop.h must be included first
Expand Down Expand Up @@ -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<enum BUTTON>(map_button(button));
Expand All @@ -148,47 +170,49 @@ 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);
}
}

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();
}
}

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);
}
}

void Clash(bool stab, float strength) override {
if (Saber::blade_detected_) {
if (DUAL_PROP_CONDITION) {
Saber::Clash(stab, strength);
} else {
Blaster::Clash(stab, strength);
}
}

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);
Expand Down
49 changes: 28 additions & 21 deletions props/prop_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
}
Expand All @@ -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;

Expand All @@ -648,7 +655,7 @@ class PropBase : CommandParser, Looper, protected SaberBase {
ResumePreset();
#else
SetPreset(0, false);
#endif
#endif // SAVE_PRESET
return;

#if NUM_BLADES != 0
Expand Down Expand Up @@ -750,7 +757,7 @@ class PropBase : CommandParser, Looper, protected SaberBase {

ONCEPERBLADE(DEACTIVATE);
SaveVolumeIfNeeded();
FindBlade();
FindBlade(true);
}

bool CheckInteractivePreon() {
Expand Down Expand Up @@ -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")) {
Expand Down
10 changes: 7 additions & 3 deletions sound/hybrid_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 391870d

Please sign in to comment.