]> www.fi.muni.cz Git - tinyboard.git/commitdiff
Buttons, status LED, control implemented.
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 1 May 2013 20:36:06 +0000 (22:36 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 1 May 2013 20:36:06 +0000 (22:36 +0200)
projects/step-up/Makefile
projects/step-up/adc.c
projects/step-up/control.c
projects/step-up/lights.h
projects/step-up/main.c
projects/step-up/pattern.c

index 5c5af306f77ef28460b6d055eacc171897f2ec29..b6ca436293d05a834e374cb6e4b72ec6554b47d1 100644 (file)
@@ -1,6 +1,6 @@
 
 PROGRAM=lights
-SRC=main.c logging.c pwm.c adc.c pwmled.c pattern.c
+SRC=main.c logging.c pwm.c adc.c pwmled.c pattern.c buttons.c control.c
 OBJ=$(SRC:.c=.o)
 
 
index cff4527037c0bfe052a6304caeb9a422da9fd1c3..194b00e0b0de070d3ab0369b287560df8f93f37a 100644 (file)
@@ -158,6 +158,7 @@ static void inline adc_based_timer()
        if ((jiffies & 0x0007) == 0) {
                patterns_next_tick();
        }
+       timer_check_buttons();
 }
 
 ISR(ADC_vect) { // IRQ handler
index da59972b135c59c2cb2b5678aa4bb578724a327c..c877c87ae82fc2482551cd9ab4211edbd88a76b7 100644 (file)
 
 #include "lights.h"
 
-static pattern_t panic_pattern[] = {
-       { 3, 1 }, // FIXME: will be 4, but let's be safe while testing
-       { 0, 1 },
-       { 3, 1 },
-       { 0, 1 },
-       { 3, 1 },
-       { 0, 1 },
-       { 3, 1 },
-       { 0, 1 },
-       { 3, 1 },
-       { 0, 1 },
-       { 3, 1 },
-       { 0, 1 },
-       { 3, 1 },
-       { 0, 1 },
-       { 3, 1 },
-       { 0, 1 },
-       { 3, 1 },
-       { 0, 1 },
-       { 3, 1 },
-       { 0, 1 },
-       { 3, 1 },
-       { 0, 1 },
-       PATTERN_END
-};
-
-pattern_t on1_pattern [] = {
+static pattern_t on_pattern [] = {
        { 1, 0x10 },
        PATTERN_END
 };
 
-static pattern_t on2_pattern [] = {
-       { 2, 0x10 },
-       PATTERN_END
-};
-
-static pattern_t on3_pattern [] = {
-       { 3, 0x10 },
-       PATTERN_END
-};
-
-static pattern_t brake_pattern [] = {
-       { 4, 0x2 },
-       { 3, 0x8 },
-       PATTERN_END
-};
-
-static pattern_t normal2_pattern[] = {
-       { 2, 0x1 },
-       { 0, 0x1 },
-       { 2, 0x1 },
-       { 0, 0x8 },
+static pattern_t blink_pattern[] = {
        { 1, 0x1 },
-       { 0, 0x1 },
+       { 0, 0x2 },
        { 1, 0x1 },
        { 0, 0x8 },
-       PATTERN_END
-};
-
-static pattern_t normal3_pattern[] = {
-       { 3, 0x1 },
-       { 0, 0x1 },
-       { 3, 0x1 },
-       { 0, 0x8 },
-       { 1, 0x1 },
-       { 0, 0x1 },
        { 1, 0x1 },
        { 0, 0x8 },
        PATTERN_END
 };
 
-static pattern_t normal4_pattern[] = {
-       { 4, 0x1 },
-       { 0, 0x1 },
-       { 4, 0x1 },
-       { 0, 0x8 },
-       { 1, 0x1 },
-       { 0, 0x1 },
+static pattern_t slow_pattern[] = {
        { 1, 0x1 },
-       { 0, 0x8 },
-       PATTERN_END
-};
-
-static pattern_t slow1_pattern[] = {
-       { 1, 0x01 },
-       { 0, 0x10 },
-       PATTERN_END
-};
-
-static pattern_t slow2_pattern[] = {
-       { 2, 0x01 },
-       { 0, 0x10 },
-       PATTERN_END
-};
-
-static pattern_t slow3_pattern[] = {
-       { 3, 0x01 },
-       { 0, 0x10 },
+       { 0, 0x1F },
        PATTERN_END
 };
 
-static unsigned char dim_mode, towbar_mode, braking;
+static unsigned char light_mode, shutdown_in_progress;
 
 void init_control()
 {
-       dim_mode = 0;
-       towbar_mode = 0;
-       braking = 0;
+       light_mode = 1;
+       shutdown_in_progress = 0;
+       short_press();
 }
 
-void brake_on()
+void long_press_start()
 {
-       braking = 1;
-       gpio_set(0, 1);
-       led_set_pattern(N_PWMLEDS, status_led_pattern_select());
-       led_set_pattern(0, pwmled0_pattern_select());
+       shutdown_in_progress = 1;
+       pattern_reload();
 }
 
-void brake_off()
+void short_press()
 {
-       braking = 0;
-       gpio_set(0, 0);
-       led_set_pattern(N_PWMLEDS, status_led_pattern_select());
-       led_set_pattern(0, pwmled0_pattern_select());
-}
+       if (++light_mode >= 2*N_PWMLED_MODES)
+               light_mode = 0;
 
-void toggle_dim_mode()
-{
-       dim_mode = !dim_mode;
+       pwmled_set_target(light_mode < N_PWMLED_MODES
+               ? light_mode
+               : light_mode - N_PWMLED_MODES);
        pattern_reload();
 }
 
-void set_panic_mode()
+void long_press()
 {
-       if (!dim_mode)
-               led_set_pattern(0, panic_pattern);
-
-       led_set_pattern(1, panic_pattern);
-       led_set_pattern(2, panic_pattern);
-       led_set_pattern(4, panic_pattern);
+       shutdown_in_progress = 0;
+       pattern_reload();
 }
 
-pattern_t *pwmled0_pattern_select()
+pattern_t *pwmled_pattern_select()
 {
-       if (battery_critical)
-               return on1_pattern;
-
-       if (towbar_mode)
+       if (shutdown_in_progress)
                return NULL;
 
-       if (braking)
-               return brake_pattern;
-
-       switch (ambient_zone) {
-       case 0: return dim_mode ? NULL : number_pattern(2, 1);
-       case 1: return dim_mode ? slow1_pattern : normal2_pattern;
-       case 2: return dim_mode ? slow2_pattern : normal3_pattern;
-       case 3:
-       default: return dim_mode ? slow3_pattern : normal4_pattern;
-       }
-}
-
-pattern_t *pwmled1_pattern_select()
-{
-#ifndef TESTING_FW
-       return NULL;
-#else
-       if (battery_critical)
-               return on1_pattern;
-#endif
-
-       if (towbar_mode) {
-               switch (ambient_zone) {
-               case 0:
-               case 1:
-                       return dim_mode ? on2_pattern : on1_pattern;
-               case 2: return dim_mode ? NULL : on2_pattern;
-               case 3:
-               default: return dim_mode ? NULL : on3_pattern;
-               }
+       if (light_mode == 0) {
+               return slow_pattern;
+       } else if (light_mode < N_PWMLED_MODES) {
+               return blink_pattern;
        } else {
-               switch (ambient_zone) {
-               case 0: return dim_mode ? slow1_pattern : normal2_pattern;
-               case 1: return dim_mode ? slow2_pattern : normal3_pattern;
-               case 2: return dim_mode ? NULL : normal4_pattern;
-               case 3:
-               default: return NULL;
-               }
-       }
-}
-
-pattern_t *pwmled2_pattern_select()
-{
-#ifndef TESTING_FW
-       if (battery_critical)
-               return on1_pattern;
-#endif
-
-       switch (ambient_zone) {
-       case 0: return dim_mode ? on2_pattern : on3_pattern;
-       case 1: return dim_mode ? slow1_pattern : normal2_pattern;
-       case 2:
-       case 3:
-       default:
-               return dim_mode ? slow2_pattern : normal3_pattern;
+               return on_pattern;
        }
 }
 
 pattern_t *status_led_pattern_select()
 {
-       if (braking)
-               return on1_pattern;
-
-       if (buttons_setup_in_progress())
-               return buttons_setup_status0_pattern_select();
+       if (shutdown_in_progress)
+               return on_pattern;
 
-       // FIXME: do something sane
-       return number_pattern(battery_gauge(), 0);
+       return number_pattern(light_mode+1, 0);
 }
 
-pattern_t *illumination_led_pattern_select()
+#if 0
+void brake_on()
 {
-       if (battery_critical)
-               return NULL;
-
-       switch (ambient_zone) {
-       case 0: return dim_mode
-               ? number_pattern(1, 1)
-               : on1_pattern;
-       case 1: return dim_mode
-               ? number_pattern(2, 1)
-               : number_pattern(3, 1);
-       case 2: return dim_mode
-               ? number_pattern(1, 0)
-               : number_pattern(2, 0);
-       case 3:
-       default: return dim_mode
-               ? number_pattern(3, 0)
-               : number_pattern(4, 0);
-       }
+       braking = 1;
+       gpio_set(0, 1);
+       led_set_pattern(N_PWMLEDS, status_led_pattern_select());
+       led_set_pattern(0, pwmled0_pattern_select());
 }
 
-pattern_t *laser_pattern_select()
+void brake_off()
 {
-       if (!dim_mode && ambient_zone <= 1)
-               return number_pattern(2, 1);
-       else
-               return NULL;
+       braking = 0;
+       gpio_set(0, 0);
+       led_set_pattern(N_PWMLEDS, status_led_pattern_select());
+       led_set_pattern(0, pwmled0_pattern_select());
 }
+
+#endif
index 64fce03dfb6ccc58f5dc52fae89edb4ef0151e59..9cbc0c957228bd8ffe9da9e0e97662de82f1c4e6 100644 (file)
@@ -42,17 +42,6 @@ void pwmled_adc(uint16_t adcval);
 void pwmled_set_target(unsigned char mode);
 void pwmled_on_off(unsigned char on);
 
-/* gpio.c */
-void init_gpio();
-void susp_gpio();
-void gpio_set(unsigned char n, unsigned char on);
-
-/* ambient.c */
-#define AMBIENT_ADC_SHIFT 0    /* 1 measurement per callback */
-void init_ambient();
-extern volatile unsigned char ambient_zone;
-void ambient_adc(uint16_t adc_val);
-
 /* pattern.c */
 typedef struct {
        unsigned char mode: 3;
@@ -67,16 +56,11 @@ pattern_t *number_pattern(unsigned char num, unsigned char inv);
 void pattern_reload();
 
 /* buttons.c */
-#define MAX_USER_PARAMS 3
 void init_buttons();
 void susp_buttons();
 void timer_check_buttons();
-void button_adc(uint16_t adcval);
-unsigned char get_user_param(unsigned char param);
 unsigned char buttons_wait_for_release();
-unsigned char buttons_setup_in_progress();
-pattern_t *buttons_setup_status0_pattern_select();
-pattern_t *buttons_setup_status1_pattern_select();
+void status_led_on_off(unsigned char on);
 
 /* battery.c */
 extern volatile unsigned char battery_critical;
@@ -85,19 +69,14 @@ void init_battery();
 unsigned char battery_gauge();
 
 /* control.c */
-extern pattern_t on1_pattern[];
-
 void init_control();
+void long_press_start();
+void long_press();
+void short_press();
 void brake_on();
 void brake_off();
-void toggle_dim_mode();
-void set_panic_mode();
-pattern_t *pwmled0_pattern_select();
-pattern_t *pwmled1_pattern_select();
-pattern_t *pwmled2_pattern_select();
+pattern_t *pwmled_pattern_select();
 pattern_t *status_led_pattern_select();
-pattern_t *illumination_led_pattern_select();
-pattern_t *laser_pattern_select();
 
 /* main.c */
 void power_down();
index 20f339e3b79d7305f95cc20cce9af80eef084a27..5b5817a70c42e7d1ef1fe7cc0b40ac408fde7dda 100644 (file)
@@ -17,14 +17,11 @@ static void hw_setup()
        init_pwm();
        init_adc();
 
-       //init_tmr();
-       //init_buttons();
+       init_buttons();
 
        init_pwmled();
-       //init_gpio();
-       //init_ambient();
-       //init_pattern();
-       //init_control();
+       init_pattern();
+       init_control();
 
        set_sleep_mode(SLEEP_MODE_IDLE);
 }
index 693df1df9565664a21f3b370da636027411b7449..261db2c31be4562fb04794d39099005ef48488df 100644 (file)
@@ -3,7 +3,7 @@
 
 #include "lights.h"
 
-#define N_LEDS 1
+#define N_LEDS 2
 static unsigned char led_counters[N_LEDS];
 static pattern_t *led_patterns[N_LEDS];
 
@@ -74,8 +74,9 @@ pattern_t on1_pattern[] = {
 
 static void led_set_mode(unsigned char n, unsigned char mode)
 {
-       if (n == 0) {
-               pwmled_on_off(mode);
+       switch (n) {
+       case 0: pwmled_on_off(mode); break;
+       case 1: status_led_on_off(mode); break;
        }
 }
 
@@ -116,21 +117,9 @@ pattern_t *number_pattern(unsigned char num, unsigned char inv)
 
 static pattern_t *pattern_select(unsigned char n)
 {
-       static unsigned char count;
-       static unsigned char mode;
        switch(n) {
-       case 0:
-               if (++count > 2) {
-                       count = 0;
-                       if (mode == 0) {
-                               mode = 3;
-                       } else {
-                               mode = 0;
-                       }
-
-                       pwmled_set_target(mode);
-               }
-               return number_pattern(mode ? 2 : 3, 0);
+       case 0: return pwmled_pattern_select();
+       case 1: return status_led_pattern_select();
        default: return NULL;
        }
 }
@@ -148,21 +137,7 @@ static void inline pattern_finished(unsigned char n)
        unsigned char i;
 
        led_patterns[n] = NULL;
-
-       if (n == 0) {
-               led_set_pattern(0, pattern_select(0));
-       }
-#if 0
-       } else if (n == 3) {
-               if (!led_patterns[4])
-                       led_set_pattern(4, pattern_select(4));
-       } else if (n == 4) {
-               if (!led_patterns[3])
-                       led_set_pattern(3, pattern_select(3));
-       } else {
-               led_set_pattern(n, pattern_select(n));
-       }
-#endif
+       led_set_pattern(n, pattern_select(n));
 }
 
 void patterns_next_tick()