From: Jan "Yenya" Kasprzak Date: Thu, 30 Jan 2014 22:59:58 +0000 (+0100) Subject: status LED visual feedback X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=heater.git;a=commitdiff_plain;h=b3d4cd3651f0bc5d6da19815a63471ea46835c64;hp=36f60161fac194572a272bdb0239a01fcf2b801b status LED visual feedback --- diff --git a/firmware/main.c b/firmware/main.c index 9a04150..afaa557 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -7,7 +7,9 @@ #include "logging.h" -static unsigned char pwm = 1; +#define N_STEPS 5 +static unsigned char steps[] = { 60, 85, 121, 171, 242 }; +static unsigned char intensity = 0; static void timer_init() { @@ -19,6 +21,11 @@ static void timer_init() // TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz GTCCR = _BV(COM1B1) | _BV(PWM1B); OCR1C = 255; + OCR1B = 0; +} + +static void set_pwm(unsigned char pwm) +{ OCR1B = pwm; } @@ -47,6 +54,11 @@ static void status_led_off() PORTB &= ~_BV(PB2); } +static unsigned char status_led_is_on() +{ + return PORTB & _BV(PB2) ? 1 : 0; +} + static void buttons_init() { DDRB &= ~(_BV(PB0) | _BV(PB1)); // set as input @@ -160,9 +172,8 @@ static void power_down() static void button_one_pressed() { - if (pwm > 1) { - pwm >>= 1; - OCR1B = pwm; + if (intensity > 0) { + set_pwm(steps[--intensity]); } else { power_down(); } @@ -170,9 +181,8 @@ static void button_one_pressed() static void button_two_pressed() { - if (pwm < 0x80) { - pwm <<= 1; - OCR1B = pwm; + if (intensity < N_STEPS-1) { + set_pwm(steps[++intensity]); } } @@ -207,6 +217,28 @@ static void timer_check_buttons() button_state = newstate; } +static unsigned char blink_on_time, blink_off_time, n_blinks; +static unsigned char blink_counter; + +static void timer_blink() +{ + if (blink_counter) { + blink_counter--; + } else if (status_led_is_on()) { + status_led_off(); + blink_counter = blink_off_time; + } else if (n_blinks) { + --n_blinks; + status_led_on(); + blink_counter = blink_on_time; + } else { + n_blinks = intensity + 1; + blink_on_time = 0; + blink_off_time = 2; + blink_counter = 10; + } +} + int main() { log_init(); @@ -239,6 +271,7 @@ int main() if (wdt_timer_fired) { wdt_timer_fired = 0; timer_check_buttons(); + timer_blink(); } } }