X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=pattern.c;h=9999749fd2fe794952bbc16d83cc6f8704486636;hb=66dbf8c04a632ad64f68aad6c775af6a5f5b12e8;hp=386f9df9938ea959f3ca83a2f3b6e5b6ee46830c;hpb=4b93ea55987c25fcce022403a505ca1749dc329c;p=bike-lights.git diff --git a/pattern.c b/pattern.c index 386f9df..9999749 100644 --- a/pattern.c +++ b/pattern.c @@ -1,3 +1,5 @@ +#include + #include "lights.h" typedef struct { @@ -10,16 +12,71 @@ static pattern_t *led_patterns[N_LEDS]; #define PATTERN_END { 0, 0 } pattern_t off_pattern[] = { - { 0, 5 }, + { 0, 0x1F }, PATTERN_END }; pattern_t blink_pattern[] = { - { 1, 5 }, - { 0, 5 }, + { 1, 0x4 }, + { 0, 0x8 }, + PATTERN_END +}; + +pattern_t mode1_pattern[] = { + { 1, 0x1 }, + { 0, 0x1 }, + { 4, 0x1 }, + { 0, 0x1 }, + { 1, 0x1 }, PATTERN_END }; +pattern_t boot_pattern[] = { + { 1, 0x6 }, + { 0, 0x6 }, + { 1, 0x3 }, + { 0, 0x3 }, + { 1, 0x2 }, + { 0, 0x2 }, + { 1, 0x1 }, + { 0, 0x1 }, + { 1, 0x1 }, + { 0, 0x1 }, + { 1, 0x1 }, + { 0, 0x1 }, + { 1, 0x1 }, + { 0, 0x1 }, + { 1, 0x10 }, + { 0, 0x10 }, + PATTERN_END +}; + +pattern_t pattern_num[] = { + { 1, 0x1 }, /* 10 */ + { 0, 0x4 }, + { 1, 0x1 }, /* 9 */ + { 0, 0x4 }, + { 1, 0x1 }, /* 8 */ + { 0, 0x4 }, + { 1, 0x1 }, /* 7 */ + { 0, 0x4 }, + { 1, 0x1 }, /* 6 */ + { 0, 0x4 }, + { 1, 0x1 }, /* 5 */ + { 0, 0x4 }, + { 1, 0x1 }, /* 4 */ + { 0, 0x4 }, + { 1, 0x1 }, /* 3 */ + { 0, 0x4 }, + { 1, 0x1 }, /* 2 */ + { 0, 0x4 }, + { 1, 0x1 }, /* 1 */ + { 0, 0x1F }, + PATTERN_END +}; + +static unsigned char test_running; + void pattern_init() { unsigned char i; @@ -28,6 +85,54 @@ void pattern_init() led_counters[i] = 0; led_patterns[i] = off_pattern; } + led_patterns[N_PWMLEDS+1] = boot_pattern; + led_counters[N_PWMLEDS+1] = boot_pattern->duration; + gpio_set(GPIO_LED2, 1); + test_running = 0; +} + +static pattern_t *number_pattern(unsigned char num) +{ + if (num >= 9) + num = 9; + + return pattern_num + sizeof(pattern_num)/sizeof(pattern_t) + - 1 - 2*num; +} + +static inline pattern_t *pattern_select(unsigned char n) +{ + if (n < N_PWMLEDS && !pwmled_enabled(n)) + return off_pattern; // Don't mess with non-enabled LEDs + + if (n == 2) { + if (test_running) { + log_byte(0xF7); + log_flush(); + return off_pattern; + } else { + test_running = 1; + log_byte(0xF6); + return mode1_pattern; + } + } + return number_pattern(1+ambient_zone); +} + +static void led_set_mode(unsigned char n, unsigned char mode) +{ + if (n < N_PWMLEDS) { + pwmled_set_mode(n, mode); + } else if (n < N_LEDS) { + gpio_set(n - N_PWMLEDS, mode); + } +} + +static void inline led_set_level(unsigned char n, unsigned char level) +{ + if (n == N_PWMLEDS) { + gpio_set(GPIO_LED2, 1); + } } void patterns_next_tick() @@ -41,11 +146,7 @@ void patterns_next_tick() led_patterns[i] = pattern_select(i); } led_counters[i] = led_patterns[i]->duration; - if (led_patterns[i]->mode == 0) { - led_off(i); - } else { - led_set_level(i, led_patterns[i]->mode - 1); - } + led_set_mode(i, led_patterns[i]->mode); } led_counters[i]--;