X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=bike-lights.git;a=blobdiff_plain;f=firmware%2Fcontrol.c;h=1ac813a62a0f408ec8f9abfb70a783a0988da53e;hp=384edc87123994e84d52f75c045401c7f70a1fd1;hb=1d5dfb4d273a61877eb5937f93fbd266b782bc7a;hpb=9812e0e491f8582ea322138b440054f19cd1f792 diff --git a/firmware/control.c b/firmware/control.c index 384edc8..1ac813a 100644 --- a/firmware/control.c +++ b/firmware/control.c @@ -4,106 +4,147 @@ #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 }, + { 3, D_1 }, // FIXME: will be 4, but let's be safe while testing + { 0, D_1 }, + { 3, D_1 }, + { 0, D_1 }, + { 3, D_1 }, + { 0, D_1 }, + { 3, D_1 }, + { 0, D_1 }, + { 3, D_1 }, + { 0, D_1 }, + { 3, D_1 }, + { 0, D_1 }, + { 3, D_1 }, + { 0, D_1 }, + { 3, D_1 }, + { 0, D_1 }, + { 3, D_1 }, + { 0, D_1 }, + { 3, D_1 }, + { 0, D_1 }, + { 3, D_1 }, + { 0, D_1 }, PATTERN_END }; pattern_t on1_pattern [] = { - { 1, 0x10 }, + { 1, D_8 }, PATTERN_END }; static pattern_t on2_pattern [] = { - { 2, 0x10 }, + { 2, D_8 }, PATTERN_END }; static pattern_t on3_pattern [] = { - { 3, 0x10 }, + { 3, D_8 }, + PATTERN_END +}; + +static pattern_t brake_pattern [] = { + { 4, D_2 }, + { 3, D_8 }, PATTERN_END }; static pattern_t normal2_pattern[] = { - { 2, 0x1 }, - { 0, 0x1 }, - { 2, 0x1 }, - { 0, 0x8 }, - { 1, 0x1 }, - { 0, 0x1 }, - { 1, 0x1 }, - { 0, 0x8 }, + { 2, D_1 }, + { 0, D_1 }, + { 2, D_1 }, + { 0, D_8 }, + { 1, D_1 }, + { 0, D_1 }, + { 1, D_1 }, + { 0, D_8 }, PATTERN_END }; static pattern_t normal3_pattern[] = { - { 3, 0x1 }, - { 0, 0x1 }, - { 3, 0x1 }, - { 0, 0x8 }, - { 1, 0x1 }, - { 0, 0x1 }, - { 1, 0x1 }, - { 0, 0x8 }, + { 3, D_1 }, + { 0, D_1 }, + { 3, D_1 }, + { 0, D_8 }, + { 1, D_1 }, + { 0, D_1 }, + { 1, D_1 }, + { 0, D_8 }, PATTERN_END }; static pattern_t normal4_pattern[] = { - { 4, 0x1 }, - { 0, 0x1 }, - { 4, 0x1 }, - { 0, 0x8 }, - { 1, 0x1 }, - { 0, 0x1 }, - { 1, 0x1 }, - { 0, 0x8 }, + { 4, D_1 }, + { 0, D_1 }, + { 4, D_1 }, + { 0, D_8 }, + { 1, D_1 }, + { 0, D_1 }, + { 1, D_1 }, + { 0, D_8 }, PATTERN_END }; static pattern_t slow1_pattern[] = { - { 1, 0x01 }, - { 0, 0x10 }, + { 1, D_1 }, + { 0, D_13 }, PATTERN_END }; static pattern_t slow2_pattern[] = { - { 2, 0x01 }, - { 0, 0x10 }, + { 2, D_1 }, + { 0, D_13 }, PATTERN_END }; static pattern_t slow3_pattern[] = { - { 3, 0x01 }, - { 0, 0x10 }, + { 3, D_1 }, + { 0, D_13 }, + PATTERN_END +}; + +static pattern_t tmp1_pattern[] = { + { PWM_PAT(3, 0, 0), D_1 }, + { PWM_PAT(0, 0, 1), D_1 }, + { PWM_PAT(3, 0, 0), D_1 }, + { PWM_PAT(0, 0, 1), D_1 }, + { PWM_PAT(0, 0, 0), D_3 }, + { PWM_PAT(0, 1, 0), D_1 }, + { PWM_PAT(0, 0, 0), D_5 }, + { PWM_PAT(1, 0, 0), D_1 }, + { PWM_PAT(0, 0, 3), D_1 }, + { PWM_PAT(1, 0, 0), D_1 }, + { PWM_PAT(0, 0, 3), D_1 }, + { PWM_PAT(0, 0, 0), D_5 }, + { PWM_PAT(0, 1, 0), D_1 }, + { PWM_PAT(0, 0, 0), D_3 }, PATTERN_END }; -static unsigned char dim_mode, towbar_mode; +static unsigned char dim_mode, towbar_mode, braking; void init_control() { dim_mode = 0; towbar_mode = 0; + braking = 0; +} + +void brake_on() +{ + braking = 1; + gpio_set(0, 1); + led_set_pattern(N_STATUS_LED, status_led_pattern_select()); + led_set_pattern(0, pwmled_pattern_select()); +} + +void brake_off() +{ + braking = 0; + gpio_set(0, 0); + led_set_pattern(N_STATUS_LED, status_led_pattern_select()); + led_set_pattern(0, pwmled_pattern_select()); } void toggle_dim_mode() @@ -114,87 +155,48 @@ void toggle_dim_mode() void set_panic_mode() { - 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); + led_set_pattern(0, panic_pattern); + led_set_pattern(N_ILLUM_LED, panic_pattern); } -pattern_t *pwmled0_pattern_select() +pattern_t *pwmled_pattern_select() { - // TODO: battery critical + return tmp1_pattern; + + if (battery_critical) + return on1_pattern; if (towbar_mode) return NULL; - switch (ambient_zone) { - case 0: return dim_mode ? NULL : on3_pattern; - case 1: return dim_mode ? NULL : normal3_pattern; - case 2: return dim_mode ? slow3_pattern : normal3_pattern; - case 3: - default: return dim_mode ? slow3_pattern : normal4_pattern; - } -} - -pattern_t *pwmled1_pattern_select() -{ - // TODO: battery critical - -#ifndef TESTING_FW - return NULL; -#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; - } - } -} + if (braking) + return brake_pattern; -pattern_t *pwmled2_pattern_select() -{ - // TODO: battery critical - -#ifndef TESTING_FW - return on3_pattern; -#endif switch (ambient_zone) { - case 0: return dim_mode ? on2_pattern : on3_pattern; + case 0: return dim_mode ? NULL : number_pattern(2, 1); case 1: return dim_mode ? slow1_pattern : normal2_pattern; - case 2: + case 2: return dim_mode ? slow2_pattern : normal3_pattern; case 3: - default: - return dim_mode ? slow2_pattern : normal3_pattern; + default: return dim_mode ? slow3_pattern : normal4_pattern; } } pattern_t *status_led_pattern_select() { + if (braking) + return on1_pattern; + if (buttons_setup_in_progress()) return buttons_setup_status0_pattern_select(); // FIXME: do something sane - return number_pattern(1 + ambient_zone, 0); + return number_pattern(battery_gauge(), 0); } pattern_t *illumination_led_pattern_select() { - // todo: battery critical + if (battery_critical) + return NULL; switch (ambient_zone) { case 0: return dim_mode @@ -213,3 +215,10 @@ pattern_t *illumination_led_pattern_select() } } +pattern_t *laser_pattern_select() +{ + if (!dim_mode && ambient_zone <= 1) + return number_pattern(2, 1); + else + return NULL; +}