X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=tinyboard.git;a=blobdiff_plain;f=projects%2Fstep-up%2Fcontrol.c;h=4ffb71cf631310b98de786e785b9e02dccf7092c;hp=da59972b135c59c2cb2b5678aa4bb578724a327c;hb=HEAD;hpb=004b6f8d1c4e73883032f74e20d581dd080a386e diff --git a/projects/step-up/control.c b/projects/step-up/control.c index da59972..4ffb71c 100644 --- a/projects/step-up/control.c +++ b/projects/step-up/control.c @@ -3,252 +3,167 @@ #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 +static unsigned char on_pattern[] = { + /* 8 bits */ + 0b11111111, }; -pattern_t on1_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 }, - { 1, 0x1 }, - { 0, 0x1 }, - { 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 }, - { 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 +#define IVA + +static unsigned char blink_pattern[] = { +#ifdef IVA + /* ../...-/..-/.../-.-/.-// */ + /* 137 bits, 19.7% on */ + 0b10000100, + 0b00000000, + 0b10000100, + 0b00100001, + 0b11000000, + 0b00001000, + 0b01000011, + 0b10000000, + 0b00010000, + 0b10000100, + 0b00000000, + 0b11100001, + 0b00001110, + 0b00000000, + 0b01000011, + 0b10000000, + 0b00000000, + 0b00000000, +#else /* FILIP */ + /* ..-./../.-../../.--.// */ + /* 124 bits, 19.4% on */ + 0b10000100, + 0b00111000, + 0b01000000, + 0b00001000, + 0b01000000, + 0b00001000, + 0b01110000, + 0b10000100, + 0b00000000, + 0b10000100, + 0b00000000, + 0b10000111, + 0b00001110, + 0b00010000, + 0b00000000, + 0b00000000, +#endif }; -static pattern_t slow3_pattern[] = { - { 3, 0x01 }, - { 0, 0x10 }, - PATTERN_END +static unsigned char slow_pattern[] = { + /* 24 bits */ + 0b00010000, + 0b00000000, + 0b00000000, }; -static unsigned char dim_mode, towbar_mode, braking; - -void init_control() +static unsigned char light_mode; +static union { + unsigned char errors; + struct { + unsigned char shutdown_in_progress : 1; + unsigned char pwmled_error : 1; + unsigned char battery_low : 1; + }; +} e; + +void set_error(unsigned char err) { - dim_mode = 0; - towbar_mode = 0; - braking = 0; + switch(err) { + case ERR_BATTERY: + e.battery_low = 1; + pwmled_set_target(0); + pattern_reload(); + break; + case ERR_PWMLED: + e.pwmled_error = 1; + break; + } } -void brake_on() +void init_control() { - braking = 1; - gpio_set(0, 1); - led_set_pattern(N_PWMLEDS, status_led_pattern_select()); - led_set_pattern(0, pwmled0_pattern_select()); + light_mode = 1; + e.errors = 0; + short_press(); } -void brake_off() +void long_press_start() { - braking = 0; - gpio_set(0, 0); - led_set_pattern(N_PWMLEDS, status_led_pattern_select()); - led_set_pattern(0, pwmled0_pattern_select()); -} - -void toggle_dim_mode() -{ - dim_mode = !dim_mode; + e.shutdown_in_progress = 1; pattern_reload(); } -void set_panic_mode() +void short_press() { - if (!dim_mode) - led_set_pattern(0, panic_pattern); + if (e.battery_low) + return; - led_set_pattern(1, panic_pattern); - led_set_pattern(2, panic_pattern); - led_set_pattern(4, panic_pattern); + if (++light_mode >= 2*N_PWMLED_MODES) + light_mode = 0; + + pwmled_set_target(light_mode < N_PWMLED_MODES + ? light_mode + : light_mode - N_PWMLED_MODES); + pattern_reload(); } -pattern_t *pwmled0_pattern_select() +void long_press() { - if (battery_critical) - return on1_pattern; - - if (towbar_mode) - 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; - } + power_down(); } -pattern_t *pwmled1_pattern_select() +void pwmled_pattern_select(unsigned char led) { -#ifndef TESTING_FW - return NULL; -#else - if (battery_critical) - return on1_pattern; + if (e.shutdown_in_progress) { + led_set_pattern(led, 0, 0, NULL); + } else if (e.battery_low) { + led_set_pattern(led, 24, 0, slow_pattern); + } else if (light_mode == 0) { + led_set_pattern(led, 24, 0, slow_pattern); + } else if (light_mode < N_PWMLED_MODES) { +#ifdef IVA + led_set_pattern(led, 137, 0, blink_pattern); +#else /* FILIP */ + led_set_pattern(led, 124, 0, blink_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; - } } 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; - } + led_set_pattern(led, 8, 0, on_pattern); } } -pattern_t *pwmled2_pattern_select() +void status_led_pattern_select(unsigned char led) { -#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; + if (e.shutdown_in_progress) { + led_set_pattern(led, 8, 0, on_pattern); + } else if (e.pwmled_error) { + led_set_number_pattern(led, 1, 1); + } else if (e.battery_low) { + led_set_number_pattern(led, 1, 1); + } else { + led_set_number_pattern(led, battery_gauge(), 0); } } -pattern_t *status_led_pattern_select() +#if 0 +void brake_on() { - if (braking) - return on1_pattern; - - if (buttons_setup_in_progress()) - return buttons_setup_status0_pattern_select(); - - // FIXME: do something sane - return number_pattern(battery_gauge(), 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 *illumination_led_pattern_select() +void brake_off() { - 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 = 0; + gpio_set(0, 0); + led_set_pattern(N_PWMLEDS, status_led_pattern_select()); + led_set_pattern(0, pwmled0_pattern_select()); } -pattern_t *laser_pattern_select() -{ - if (!dim_mode && ambient_zone <= 1) - return number_pattern(2, 1); - else - return NULL; -} +#endif