]> www.fi.muni.cz Git - bike-lights.git/commitdiff
buttons.c: handle brake sensor failure gracefully
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 28 May 2013 20:45:56 +0000 (22:45 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 28 May 2013 20:45:56 +0000 (22:45 +0200)
A common failure mode is that the magnet slips and does not provide
the signal, so it appears as if constantly braking. Avoid this situation
by allowing continuous braking of at most 16 seconds. After that, report
brake off, and wait for the real "brake off" signall from the Hall sensor.

firmware/buttons.c

index 80ecce603c65503ad940ad05b6edab7b98bb099e..20d28a58915658b84dcfa723d66511743a9cfd46 100644 (file)
@@ -189,30 +189,47 @@ static void handle_button(unsigned char button, unsigned char cur,
 
 static void handle_brake(unsigned char cur, unsigned char prev)
 {
+       uint16_t duration;
+
        if (cur && !prev) {                   // --- just pressed ---
                button_start[2] = jiffies;
+               return;
        } else if (!cur && prev) {            // --- just released ---
                button_start[2] = jiffies;
-       } else {                              // --- no change ---
-               uint16_t duration = jiffies - button_start[2];
-
-               if (duration > 6) {
-                       if (cur) {
-                               if (button_state.brake_working
-                                       && !button_state.brake_reported) {
-                                       button_state.brake_reported = 1;
-                                       brake_on();
-                               }
-                       } else {
-                               button_state.brake_working = 1;
-                               if (button_state.brake_reported) {
-                                       button_state.brake_reported = 0;
+               return;
+       }
+                                             // --- no change ---
+       duration = jiffies - button_start[2];
+
+       if (duration <= 3)
+               return;
+
+       if (cur) {
+               if (button_state.brake_working) {
+                       static unsigned int brake_time;
+                       if (button_state.brake_reported) {
+                               if (brake_time) {
+                                       brake_time--;
+                               } else {
                                        brake_off();
+                                       button_state.brake_working = 0;
+                                       button_state.brake_reported = 0;
                                }
+                       } else {
+                               button_state.brake_reported = 1;
+                               brake_on();
+                               brake_time = 255; // avoid longer than ~16s
                        }
-                       button_start[2] = jiffies - 7; // avoid overflow
+               }
+       } else {
+               button_state.brake_working = 1;
+               if (button_state.brake_reported) {
+                       button_state.brake_reported = 0;
+                       brake_off();
                }
        }
+
+       button_start[2] = jiffies - 7; // avoid overflow
 }
 
 void timer_check_buttons()