From: Jan "Yenya" Kasprzak Date: Tue, 28 May 2013 20:45:56 +0000 (+0200) Subject: buttons.c: handle brake sensor failure gracefully X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=bike-lights.git;a=commitdiff_plain;h=67fee443a9e9465923ad7e7704eb9000cba49cdd buttons.c: handle brake sensor failure gracefully 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. --- diff --git a/firmware/buttons.c b/firmware/buttons.c index 80ecce6..20d28a5 100644 --- a/firmware/buttons.c +++ b/firmware/buttons.c @@ -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()