Skip to content

Commit ac508b0

Browse files
Add DEFAULT ARRAY define (#832)
1 parent be326d0 commit ac508b0

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

props/saber_sabersense_buttons.h

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* V7/8-261.
1+
/* V7/8-265.
22
============================================================
33
================= SABERSENSE PROP FILE =================
44
================= by =================
@@ -238,6 +238,22 @@ COLOUR CHANGE FUNCTIONS WITH BLADE ON
238238
{ 3, ... }
239239
etc.
240240
241+
#define SABERSENSE_DEFAULT_BLADE_ARRAY 3
242+
This feature is really intended for busy installers
243+
and sets the default blade array in multi-array systems.
244+
Using this feature, you can have a blade array with all
245+
the common blade lengths included, and then select which
246+
length to default to on first boot (i.e. until a save
247+
file is written).
248+
The alternative is to simply re-order the blade array
249+
manually, but then you have to re-order the array1.wav,
250+
array2.wav etc. files on the SD card to match, which
251+
is a hassle.
252+
Note that the define uses zero-based numbering, the
253+
same as the blade array itself, so you must use the
254+
number as it is shown in the blade array - i.e. number 3
255+
for the fourth array down the full list.
256+
241257
#define SABERSENSE_DISABLE_SAVE_ARRAY
242258
By default, SABERSENSE_ARRAY_SELECTOR saves the current
243259
array so that the saber will always boot into the last
@@ -322,19 +338,47 @@ GESTURE CONTROLS
322338

323339
#ifdef SABERSENSE_ARRAY_SELECTOR
324340

341+
#ifndef SABERSENSE_DEFAULT_BLADE_ARRAY
342+
#ifdef BLADE_DETECT_PIN
343+
#define SABERSENSE_DEFAULT_BLADE_ARRAY 1
344+
#else
345+
#define SABERSENSE_DEFAULT_BLADE_ARRAY 0
346+
#endif
347+
#endif
348+
349+
// Check user-defined array is valid at compile time.
350+
static_assert(
351+
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+
);
355+
#ifdef BLADE_DETECT_PIN
356+
static_assert(
357+
SABERSENSE_DEFAULT_BLADE_ARRAY != 0,
358+
"[Sabersense] ERROR: "
359+
"#define SABERSENSE_DEFAULT_BLADE_ARRAY must be 1 or higher when using Blade Detect."
360+
);
361+
#endif
362+
325363
#ifndef SABERSENSE_DISABLE_SAVE_ARRAY
326364
class SaveArrayStateFile : public ConfigFile {
327365
public:
328366
void iterateVariables(VariableOP *op) override {
329-
CONFIG_VARIABLE2(sabersense_array_index, 0); // Default array if no save file.
367+
// Default array if no save file present...
368+
CONFIG_VARIABLE2(sabersense_array_index, SABERSENSE_DEFAULT_BLADE_ARRAY);
330369
}
331-
int sabersense_array_index; // Stores current array index.
332-
};
370+
int sabersense_array_index; // Stores current array index.
371+
};
333372
#endif
334373
335374
struct SabersenseArraySelector {
336375
static int return_value; // Tracks current array index.
337376
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+
}
338382
return return_value;
339383
}
340384
static void cycle() {

0 commit comments

Comments
 (0)