Skip to content

Commit 38b0590

Browse files
Add Two-Way Array Cycling. (#836)
1 parent 5653d0d commit 38b0590

File tree

1 file changed

+60
-42
lines changed

1 file changed

+60
-42
lines changed

props/saber_sabersense_buttons.h

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* V7/8-265.
1+
/* V7/8-284.
22
============================================================
33
================= SABERSENSE PROP FILE =================
44
================= by =================
@@ -101,7 +101,8 @@ FUNCTIONS WITH BLADE OFF
101101
Play Character Quote Fast double-click, hilt pointing up, plays sequentially. **
102102
Play Music Track Fast double-click, hilt pointing down. **
103103
Speak battery voltage Fast double-click-and-hold, hilt horizontal.
104-
Run BladeID/Array Select Fast triple-click while OFF. (Applicable installs only).
104+
Run BladeID/Array Select Fast triple-click while OFF. (Configurable, applicable installs only).
105+
Array Selector is point up for forwards, down for backwards.
105106
Restore Factory Defaults Fast four-clicks while OFF, hold on last click.
106107
Release once announcement starts.
107108
Enter/Exit VOLUME MENU Hold and clash while OFF.
@@ -157,7 +158,8 @@ FUNCTIONS WITH BLADE OFF
157158
Play Character Quote Fast double-click POWER, hilt pointing up, plays sequentially. **
158159
Play Music Track Fast double-click POWER, pointing down. **
159160
Speak battery voltage Fast double-click-and-hold POWER.
160-
Run BladeID/Array Select Fast triple-click. (Applicable installs only).
161+
Run BladeID/Array Select Fast triple-click POWER. (Configurable, applicable installs only).
162+
Array Selector is point up for forwards, down for backwards.
161163
Restore Factory Defaults Fast four-clicks POWER, hold on last click.
162164
Release once announcement starts.
163165
Enter/Exit VOLUME MENU Hold POWER then quickly click AUX and release both simultaneously.
@@ -210,13 +212,13 @@ COLOUR CHANGE FUNCTIONS WITH BLADE ON
210212
Plays array-specific bladeidX.wav files when switching.
211213
212214
#define SABERSENSE_ARRAY_SELECTOR
213-
Replaces regular BladeID and allows cycling between
214-
different blade/preset arrays manually, regardless
215-
of actual BladeID status. Plays array-specific
216-
arrayX.wav files when switching.
215+
Replaces regular BladeID and allows forwards or
216+
backwards cycling between different blade/preset
217+
arrays manually, regardless of actual BladeID status.
218+
Plays array-specific arrayX.wav files when switching.
217219
Requires arrays to be numbered consecutively,
218-
starting at zero, in the field that would otherwise
219-
contain BladeID values. Like this:
220+
starting at zero, in the field that would
221+
otherwise contain BladeID values. Like this:
220222
{ 0, ... }
221223
{ 1, ... }
222224
{ 2, ... }
@@ -230,8 +232,8 @@ COLOUR CHANGE FUNCTIONS WITH BLADE ON
230232
Note that you can have any number of Blade-In arrays
231233
in your config, but only one NO_BLADE array is supported.
232234
Note also that NO_BLADE replaces the zero array,
233-
meaning that Blade-In array numbering must be consecutive
234-
starting at 1. Like this:
235+
meaning that Blade-In array numbering must be
236+
consecutive starting at 1. Like this:
235237
{ NO_BLADE, ... }
236238
{ 1, ... }
237239
{ 2, ... }
@@ -349,47 +351,60 @@ GESTURE CONTROLS
349351
// Check user-defined array is valid at compile time.
350352
static_assert(
351353
SABERSENSE_DEFAULT_BLADE_ARRAY < NELEM(blades),
352-
"[Sabersense] ERROR: "
353-
"#define SABERSENSE_DEFAULT_BLADE_ARRAY must be less than the number of blade arrays present."
354+
"SABERSENSE_DEFAULT_BLADE_ARRAY must reference a valid array (note zero-based counting)."
354355
);
356+
355357
#ifdef BLADE_DETECT_PIN
356358
static_assert(
357359
SABERSENSE_DEFAULT_BLADE_ARRAY != 0,
358-
"[Sabersense] ERROR: "
359-
"#define SABERSENSE_DEFAULT_BLADE_ARRAY must be 1 or higher when using Blade Detect."
360+
"[Sabersense] ERROR: Default array index must be 1 or higher when using Blade Detect."
360361
);
362+
constexpr int min_blades_required = 3;
363+
#else
364+
constexpr int min_blades_required = 2;
361365
#endif
362366

367+
// Check that Array Selector is actually needed (prevents possible unexpected behaviour).
368+
static_assert(
369+
NELEM(blades) >= min_blades_required,
370+
"[Sabersense] ERROR: Array Selector is not required with only one selectable blade array. "
371+
"Please undefine SABERSENSE_ARRAY_SELECTOR."
372+
);
373+
363374
#ifndef SABERSENSE_DISABLE_SAVE_ARRAY
364375
class SaveArrayStateFile : public ConfigFile {
365376
public:
366377
void iterateVariables(VariableOP *op) override {
367-
// Default array if no save file present...
368378
CONFIG_VARIABLE2(sabersense_array_index, SABERSENSE_DEFAULT_BLADE_ARRAY);
369379
}
370-
int sabersense_array_index; // Stores current array index.
380+
int sabersense_array_index;
371381
};
372382
#endif
373383

374-
struct SabersenseArraySelector {
375-
static int return_value; // Tracks current array index.
376-
float id() {
377-
if (return_value < 0 || return_value >= NELEM(blades)) {
378-
Serial.println("[Sabersense] ALERT: User or externally-specified array index invalid. "
379-
"Resetting to default.");
380-
return_value = SABERSENSE_DEFAULT_BLADE_ARRAY;
381-
}
382-
return return_value;
384+
struct SabersenseArraySelector {
385+
static int return_value;
386+
float id() {
387+
if (return_value < 0 || return_value >= NELEM(blades)) {
388+
Serial.println("[Sabersense] ALERT: User or externally-specified array index invalid. "
389+
"Resetting to default.");
390+
return_value = SABERSENSE_DEFAULT_BLADE_ARRAY;
383391
}
384-
static void cycle() {
385-
#ifndef BLADE_DETECT_PIN
386-
return_value = (return_value + 1) % NELEM(blades);
387-
#else // Improved logic fixes early Array1 repetition when using Blade Detect.
388-
return_value = 1 + return_value % (NELEM(blades) - 1);
392+
return return_value;
393+
}
394+
395+
static void cycle_array(bool forward) {
396+
int size = NELEM(blades);
397+
int offset = 0;
398+
#ifdef BLADE_DETECT_PIN
399+
offset = 1;
400+
size--;
389401
#endif
390-
}
391-
};
402+
return_value += forward ? 1 : (size - 1);
403+
return_value = (return_value + size - offset) % size + offset;
404+
}
405+
};
392406

407+
// Set initial index based on Blade Detect mode.
393408
#ifndef BLADE_DETECT_PIN
394409
int SabersenseArraySelector::return_value = 0;
395410
#else
@@ -400,7 +415,7 @@ class SaveArrayStateFile : public ConfigFile {
400415
#define BLADE_ID_CLASS_INTERNAL SabersenseArraySelector
401416
#undef BLADE_ID_CLASS
402417
#define BLADE_ID_CLASS SabersenseArraySelector
403-
#endif
418+
#endif // SABERSENSE_ARRAY_SELECTOR
404419

405420
#include "prop_base.h"
406421

@@ -720,7 +735,7 @@ class SabersenseButtons : public PROP_INHERIT_PREFIX PropBase {
720735
}
721736
#endif
722737

723-
void NextBladeArray() {
738+
void PlayArraySound() {
724739
#ifdef SABERSENSE_ENABLE_ARRAY_FONT_IDENT // Plays 'array' sound AND 'font' sound.
725740
SFX_array.Select(current_config - blades);
726741
hybrid_font.PlayCommon(&SFX_array);
@@ -920,9 +935,9 @@ bool Event2(enum BUTTON button, EVENT event, uint32_t modifiers) override {
920935
// Next/previous preset and volume down. Next preset (UP), previous preset (DOWN).
921936
#if NUM_BUTTONS == 2
922937
case EVENTID(BUTTON_AUX, EVENT_FIRST_SAVED_CLICK_SHORT, MODE_OFF):
923-
// backwards if pointing down
938+
// backwards if pointing down (anything ≤ 0 is down)
924939
if (!mode_volume_) {
925-
SetPreset(current_preset_.preset_num + (fusor.angle1() < -M_PI / 4 ? -1 : 1), true);
940+
SetPreset(current_preset_.preset_num + (fusor.angle1() > 0 ? 1 : -1), true);
926941
} else {
927942
VolumeDown();
928943
}
@@ -1018,16 +1033,19 @@ bool Event2(enum BUTTON button, EVENT event, uint32_t modifiers) override {
10181033
case EVENTID(BUTTON_POWER, EVENT_THIRD_SAVED_CLICK_SHORT, MODE_OFF):
10191034
// Check for blade present if using Blade Detect.
10201035
#ifdef BLADE_DETECT_PIN
1021-
if (!blade_detected_) return true; // Do nothing if no blade detected.
1036+
if (!blade_detected_) return true; // Do nothing if no blade detected.
10221037
#endif
1023-
// Cycles through blade arrays regardless of BladeID status.
1024-
SabersenseArraySelector::cycle();
1038+
{
1039+
// Cycles through blade arrays regardless of BladeID status.
1040+
bool forward = fusor.angle1() > 0;
1041+
SabersenseArraySelector::cycle_array(forward);
1042+
}
10251043
FindBladeAgain();
1026-
NextBladeArray();
1044+
PlayArraySound();
10271045
#ifndef SABERSENSE_DISABLE_SAVE_ARRAY
10281046
SaveArrayState();
10291047
#endif
1030-
return true;
1048+
return true;
10311049
#endif
10321050

10331051
// SOUND EFFECT PLAYERS.

0 commit comments

Comments
 (0)