#include #include // for NULL #include "lights.h" #define N_LEDS 1 static unsigned char led_counters[N_LEDS]; static pattern_t *led_patterns[N_LEDS]; static pattern_t pattern_num[] = { { 0, 0x5 }, { 1, 0x1 }, /* 10 */ { 0, 0x5 }, { 1, 0x1 }, /* 9 */ { 0, 0x5 }, { 1, 0x1 }, /* 8 */ { 0, 0x5 }, { 1, 0x1 }, /* 7 */ { 0, 0x5 }, { 1, 0x1 }, /* 6 */ { 0, 0x5 }, { 1, 0x1 }, /* 5 */ { 0, 0x5 }, { 1, 0x1 }, /* 4 */ { 0, 0x5 }, { 1, 0x1 }, /* 3 */ { 0, 0x5 }, { 1, 0x1 }, /* 2 */ { 0, 0x5 }, { 1, 0x1 }, /* 1 */ { 0, 0xF }, PATTERN_END }; static pattern_t pattern_invnum[] = { { 1, 0x5 }, { 0, 0x1 }, /* 10 */ { 1, 0x5 }, { 0, 0x1 }, /* 9 */ { 1, 0x5 }, { 0, 0x1 }, /* 8 */ { 1, 0x5 }, { 0, 0x1 }, /* 7 */ { 1, 0x5 }, { 0, 0x1 }, /* 6 */ { 1, 0x5 }, { 0, 0x1 }, /* 5 */ { 1, 0x5 }, { 0, 0x1 }, /* 4 */ { 1, 0x5 }, { 0, 0x1 }, /* 3 */ { 1, 0x5 }, { 0, 0x1 }, /* 2 */ { 1, 0x5 }, { 0, 0x1 }, /* 1 */ { 1, 0xF }, PATTERN_END }; pattern_t off_pattern[] = { { 0, 0x1 }, PATTERN_END }; pattern_t on1_pattern[] = { { 1, 1 }, { 0, 2 }, { 1, 1 }, { 0, 8 }, { 1, 1 }, { 0, 8 }, PATTERN_END }; static void led_set_mode(unsigned char n, unsigned char mode) { if (n == 0) { pwmled_on_off(mode); } } void led_set_pattern(unsigned char n, pattern_t *pattern) { if (!pattern) pattern = off_pattern; led_patterns[n] = pattern; led_counters[n] = pattern->duration; led_set_mode(n, pattern->mode); } void init_pattern() { unsigned char i; for (i = 0; i < N_LEDS; i++) led_set_pattern(i, NULL); } pattern_t *number_pattern(unsigned char num, unsigned char inv) { if (num >= 10) num = 10; if (inv) { return pattern_invnum + sizeof(pattern_invnum)/sizeof(pattern_t) - 2 - 2*num; } else { return pattern_num + sizeof(pattern_num)/sizeof(pattern_t) - 2 - 2*num; } } 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); default: return NULL; } } void pattern_reload() { unsigned char i; for (i = 0; i < N_LEDS; i++) led_set_pattern(i, pattern_select(i)); } 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 } void patterns_next_tick() { unsigned char i; for (i = 0; i < N_LEDS; i++) { if (!led_patterns[i]) { pattern_finished(i); continue; } if (--led_counters[i] == 0) { pattern_t *p = led_patterns[i]; p++; if (p->duration == 0) { // END /* Keep the last state, wait for others */ pattern_finished(i); continue; } led_set_pattern(i, p); } } }