status LED visual feedback
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 30 Jan 2014 22:59:58 +0000 (23:59 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 30 Jan 2014 22:59:58 +0000 (23:59 +0100)
firmware/main.c

index 9a04150..afaa557 100644 (file)
@@ -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();
                }
        }
 }