@@ -143,30 +143,17 @@ void updateEmergency() {
143
143
}
144
144
uint8_t last_emergency = status_message.emergency_bitmask & LL_EMERGENCY_BIT_LATCH;
145
145
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
152
152
uint8_t emergency_state = 0 ;
153
153
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
170
157
if (button_emergency_started == 0 ) {
171
158
button_emergency_started = millis ();
172
159
}
@@ -175,15 +162,25 @@ void updateEmergency() {
175
162
button_emergency_started = 0 ;
176
163
}
177
164
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 ;
183
179
}
184
180
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
187
184
if (tilt_emergency_started == 0 ) {
188
185
tilt_emergency_started = millis ();
189
186
}
@@ -192,18 +189,9 @@ void updateEmergency() {
192
189
tilt_emergency_started = 0 ;
193
190
}
194
191
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);
207
195
}
208
196
209
197
if (emergency_state || emergency_latch) {
@@ -214,8 +202,7 @@ void updateEmergency() {
214
202
status_message.emergency_bitmask = emergency_state;
215
203
216
204
// 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)) {
219
206
sendMessage (&status_message, sizeof (struct ll_status ));
220
207
221
208
// Update UI instantly
0 commit comments