@@ -143,30 +143,17 @@ void updateEmergency() {
143143 }
144144 uint8_t last_emergency = status_message.emergency_bitmask & LL_EMERGENCY_BIT_LATCH;
145145
146- // Mask the emergency bits. 2x Lift sensor, 2x Emergency Button
147- bool emergency1 = !gpio_get (PIN_EMERGENCY_1) | (stock_ui_emergency_state & Emergency_state::Emergency_lift1);
148- bool emergency2 = !gpio_get (PIN_EMERGENCY_2) | (stock_ui_emergency_state & Emergency_state::Emergency_lift2);
149- bool emergency3 = !gpio_get (PIN_EMERGENCY_3) | (stock_ui_emergency_state & Emergency_state::Emergency_stop1);
150- bool emergency4 = !gpio_get (PIN_EMERGENCY_4) | (stock_ui_emergency_state & Emergency_state::Emergency_stop2);
151-
146+ // Read & assign emergencies in the same manner as in ll_status.emergency_bitmask
147+ uint8_t emergency_read = !gpio_get (PIN_EMERGENCY_3) << 1 | // Stop1
148+ !gpio_get (PIN_EMERGENCY_4) << 2 | // Stop2
149+ !gpio_get (PIN_EMERGENCY_1) << 3 | // Lift1
150+ !gpio_get (PIN_EMERGENCY_2) << 4 | // Lift2
151+ stock_ui_emergency_state; // OR with StockUI emergency
152152 uint8_t emergency_state = 0 ;
153153
154- bool is_tilted = emergency1 || emergency2;
155- bool is_lifted = emergency1 && emergency2;
156- bool stop_pressed = emergency3 || emergency4;
157-
158- if (is_lifted) {
159- // We just lifted, store the timestamp
160- if (lift_emergency_started == 0 ) {
161- lift_emergency_started = millis ();
162- }
163- } else {
164- // Not lifted, reset the time
165- lift_emergency_started = 0 ;
166- }
167-
168- if (stop_pressed) {
169- // We just pressed, store the timestamp
154+ // Handle emergency "Stop" buttons
155+ if (emergency_read && LL_EMERGENCY_BITS_STOP) {
156+ // If we just pressed, store the timestamp
170157 if (button_emergency_started == 0 ) {
171158 button_emergency_started = millis ();
172159 }
@@ -175,15 +162,25 @@ void updateEmergency() {
175162 button_emergency_started = 0 ;
176163 }
177164
178- if (LIFT_EMERGENCY_MILLIS > 0 && lift_emergency_started > 0 && (millis () - lift_emergency_started) >= LIFT_EMERGENCY_MILLIS) {
179- if (emergency1)
180- emergency_state |= LL_EMERGENCY_BIT_LIFT1;
181- if (emergency2)
182- emergency_state |= LL_EMERGENCY_BIT_LIFT2;
165+ if (button_emergency_started > 0 && (millis () - button_emergency_started) >= BUTTON_EMERGENCY_MILLIS)
166+ {
167+ emergency_state |= (emergency_read & LL_EMERGENCY_BITS_STOP);
168+ }
169+
170+ // Handle lifted (both wheels are lifted)
171+ if ((emergency_read & LL_EMERGENCY_BITS_LIFT) == LL_EMERGENCY_BITS_LIFT) {
172+ // If we just lifted, store the timestamp
173+ if (lift_emergency_started == 0 ) {
174+ lift_emergency_started = millis ();
175+ }
176+ } else {
177+ // Not lifted, reset the time
178+ lift_emergency_started = 0 ;
183179 }
184180
185- if (is_tilted) {
186- // We just tilted, store the timestamp
181+ // Handle tilted (one wheel is lifted)
182+ if (emergency_read & LL_EMERGENCY_BITS_LIFT) {
183+ // If we just tilted, store the timestamp
187184 if (tilt_emergency_started == 0 ) {
188185 tilt_emergency_started = millis ();
189186 }
@@ -192,18 +189,9 @@ void updateEmergency() {
192189 tilt_emergency_started = 0 ;
193190 }
194191
195- if (TILT_EMERGENCY_MILLIS > 0 && tilt_emergency_started > 0 && (millis () - tilt_emergency_started) >= TILT_EMERGENCY_MILLIS) {
196- if (emergency1)
197- emergency_state |= LL_EMERGENCY_BIT_LIFT1;
198- if (emergency2)
199- emergency_state |= LL_EMERGENCY_BIT_LIFT2;
200- }
201- if (button_emergency_started > 0 && (millis () - button_emergency_started) >= BUTTON_EMERGENCY_MILLIS)
202- {
203- if (emergency3)
204- emergency_state |= LL_EMERGENCY_BIT_STOP1;
205- if (emergency4)
206- emergency_state |= LL_EMERGENCY_BIT_STOP2;
192+ if ((LIFT_EMERGENCY_MILLIS > 0 && lift_emergency_started > 0 && (millis () - lift_emergency_started) >= LIFT_EMERGENCY_MILLIS) ||
193+ (TILT_EMERGENCY_MILLIS > 0 && tilt_emergency_started > 0 && (millis () - tilt_emergency_started) >= TILT_EMERGENCY_MILLIS)) {
194+ emergency_state |= (emergency_read & LL_EMERGENCY_BITS_LIFT);
207195 }
208196
209197 if (emergency_state || emergency_latch) {
@@ -214,8 +202,7 @@ void updateEmergency() {
214202 status_message.emergency_bitmask = emergency_state;
215203
216204 // If it's a new emergency, instantly send the message. This is to not spam the channel during emergencies.
217- if (last_emergency != (emergency_state & LL_EMERGENCY_BIT_LATCH))
218- {
205+ if (last_emergency != (emergency_state & LL_EMERGENCY_BIT_LATCH)) {
219206 sendMessage (&status_message, sizeof (struct ll_status ));
220207
221208 // Update UI instantly
0 commit comments