@@ -25,11 +25,51 @@ class StairwayWipeUsermod : public Usermod {
2525 public:
2626void setup () {
2727 }
28- void loop () {
28+ /* *
29+ * @brief Drives the stairway wipe state machine and reacts to user variables.
30+ *
31+ * @details
32+ * Reads userVar0 (U0) and userVar1 (U1) to control a directional stairway color wipe:
33+ * - U0 = 0: off.
34+ * - U0 = 1: start/keep wipe from local side.
35+ * - U0 = 2: start/keep wipe from opposite side.
36+ * - U0 = 3: toggle mode for direction 1 (becomes 1 when off, 0 when on).
37+ * - U0 = 4: toggle mode for direction 2 (becomes 2 when off, 0 when on).
38+ *
39+ * Manages a small state machine:
40+ * - State 0: idle, will start a wipe.
41+ * - State 1: wiping; transitions to static when wipe completes.
42+ * - State 2: static/hold; transitions to off after U1 seconds if U1 > 0.
43+ * - State 3: prepare to wipe off (or immediately off if off-wipe is disabled).
44+ * - State 4: wiping off; turns fully off when wipe-off completes.
45+ *
46+ * The wipe duration and wipe-off timing are derived from the current effectSpeed. A change
47+ * in trigger side (previousUserVar0 differing from userVar0) forces the usermod to begin
48+ * turning off. When turning on/off the code invokes startWipe() or turnOff() and issues
49+ * color/state update notifications as appropriate.
50+ *
51+ * @note Defining STAIRCASE_WIPE_OFF enables a reverse color-wipe transition when turning off;
52+ * without it the lights fade off immediately.
53+ */
54+ void loop () {
2955 // userVar0 (U0 in HTTP API):
3056 // has to be set to 1 if movement is detected on the PIR that is the same side of the staircase as the ESP8266
3157 // has to be set to 2 if movement is detected on the PIR that is the opposite side
3258 // can be set to 0 if no movement is detected. Otherwise LEDs will turn off after a configurable timeout (userVar1 seconds)
59+ // U0 = 3: Toggle mode for direction 1 (if off, turn on with U0=1; if on, turn off with U0=0)
60+ // U0 = 4: Toggle mode for direction 2 (if off, turn on with U0=2; if on, turn off with U0=0)
61+
62+ // Handle toggle modes U0=3 and U0=4
63+ if (userVar0 == 3 || userVar0 == 4 ) {
64+ if (wipeState == 0 || wipeState == 3 || wipeState == 4 ) {
65+ // Lights are off or turning off, so turn them on
66+ wipeState = 0 ; // Reset state so the state machine starts fresh
67+ userVar0 = (userVar0 == 3 ) ? 1 : 2 ;
68+ } else {
69+ // Lights are on or turning on, so turn them off
70+ userVar0 = 0 ;
71+ }
72+ }
3373
3474 if (userVar0 > 0 )
3575 {
0 commit comments