From: Jan "Yenya" Kasprzak Date: Tue, 4 Jun 2013 16:04:35 +0000 (+0200) Subject: patterns: fibonacci-scaled step duration X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=29b27abe6adedf08f1067a76c8785cd8fe5d3296;p=bike-lights.git patterns: fibonacci-scaled step duration We have only three bits for pattern duration now, use Fibonacci-compressed values: instead of 0-7, use 0, 1, 2, 3, 5, 8, 13, and 21. --- diff --git a/firmware/control.c b/firmware/control.c index a90bb71..a219f19 100644 --- a/firmware/control.c +++ b/firmware/control.c @@ -4,106 +4,103 @@ #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, 0x7 }, + { 1, D_8 }, PATTERN_END }; static pattern_t on2_pattern [] = { - { 2, 0x7 }, + { 2, D_8 }, PATTERN_END }; static pattern_t on3_pattern [] = { - { 3, 0x7 }, + { 3, D_8 }, PATTERN_END }; static pattern_t brake_pattern [] = { - { 4, 0x2 }, - { 3, 0x7 }, + { 4, D_2 }, + { 3, D_8 }, PATTERN_END }; static pattern_t normal2_pattern[] = { - { 2, 0x1 }, - { 0, 0x1 }, - { 2, 0x1 }, - { 0, 0x7 }, - { 1, 0x1 }, - { 0, 0x1 }, - { 1, 0x1 }, - { 0, 0x7 }, + { 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, 0x7 }, - { 1, 0x1 }, - { 0, 0x1 }, - { 1, 0x1 }, - { 0, 0x7 }, + { 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, 0x7 }, - { 1, 0x1 }, - { 0, 0x1 }, - { 1, 0x1 }, - { 0, 0x7 }, + { 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, 0x7 }, - { 0, 0x7 }, + { 1, D_1 }, + { 0, D_13 }, PATTERN_END }; static pattern_t slow2_pattern[] = { - { 2, 0x01 }, - { 0, 0x7 }, - { 0, 0x7 }, + { 2, D_1 }, + { 0, D_13 }, PATTERN_END }; static pattern_t slow3_pattern[] = { - { 3, 0x01 }, - { 0, 0x7 }, - { 0, 0x7 }, + { 3, D_1 }, + { 0, D_13 }, PATTERN_END }; diff --git a/firmware/lights.h b/firmware/lights.h index f317606..5d1b6e5 100644 --- a/firmware/lights.h +++ b/firmware/lights.h @@ -74,10 +74,18 @@ void ambient_adc(uint16_t adc_val); /* pattern.c */ typedef struct { unsigned char mode: 5; - unsigned char duration: 3; + unsigned char duration_fib: 3; } pattern_t; #define PATTERN_END { 0, 0 } +#define D_1 1 +#define D_2 2 +#define D_3 3 +#define D_5 4 +#define D_8 5 +#define D_13 6 +#define D_21 7 + void init_pattern(); void patterns_next_tick(); void led_set_pattern(unsigned char led, pattern_t *pattern); diff --git a/firmware/pattern.c b/firmware/pattern.c index 77f026a..3354c6f 100644 --- a/firmware/pattern.c +++ b/firmware/pattern.c @@ -6,80 +6,82 @@ static unsigned char led_counters[N_LEDS]; static pattern_t *led_patterns[N_LEDS]; +static unsigned char fibonacci[8] = { + 0, 1, 2, 3, 5, 8, 13, 21, +}; + static 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, 0x7 }, - { 0, 0x7 }, + { 1, D_5 }, + { 0, D_5 }, + { 1, D_3 }, + { 0, D_3 }, + { 1, D_2 }, + { 0, D_2 }, + { 1, D_1 }, + { 0, D_1 }, + { 1, D_1 }, + { 0, D_1 }, + { 1, D_1 }, + { 0, D_1 }, + { 1, D_1 }, + { 0, D_1 }, + { 1, D_8 }, + { 0, D_8 }, PATTERN_END }; 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, 0x7 }, - { 0, 0x7 }, + { 0, D_5 }, + { 1, D_1 }, /* 10 */ + { 0, D_5 }, + { 1, D_1 }, /* 9 */ + { 0, D_5 }, + { 1, D_1 }, /* 8 */ + { 0, D_5 }, + { 1, D_1 }, /* 7 */ + { 0, D_5 }, + { 1, D_1 }, /* 6 */ + { 0, D_5 }, + { 1, D_1 }, /* 5 */ + { 0, D_5 }, + { 1, D_1 }, /* 4 */ + { 0, D_5 }, + { 1, D_1 }, /* 3 */ + { 0, D_5 }, + { 1, D_1 }, /* 2 */ + { 0, D_5 }, + { 1, D_1 }, /* 1 */ + { 0, D_13 }, 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, 0x7 }, - { 1, 0x7 }, + { 1, D_5 }, + { 0, D_1 }, /* 10 */ + { 1, D_5 }, + { 0, D_1 }, /* 9 */ + { 1, D_5 }, + { 0, D_1 }, /* 8 */ + { 1, D_5 }, + { 0, D_1 }, /* 7 */ + { 1, D_5 }, + { 0, D_1 }, /* 6 */ + { 1, D_5 }, + { 0, D_1 }, /* 5 */ + { 1, D_5 }, + { 0, D_1 }, /* 4 */ + { 1, D_5 }, + { 0, D_1 }, /* 3 */ + { 1, D_5 }, + { 0, D_1 }, /* 2 */ + { 1, D_5 }, + { 0, D_1 }, /* 1 */ + { 1, D_13 }, PATTERN_END }; pattern_t off_pattern[] = { - { 0, 0x1 }, + { 0, D_1 }, PATTERN_END }; @@ -99,7 +101,8 @@ void led_set_pattern(unsigned char n, pattern_t *pattern) led_patterns[n] = pattern; - led_counters[n] = pattern->duration; + led_counters[n] = fibonacci[pattern->duration_fib]; + led_set_mode(n, pattern->mode); } @@ -188,7 +191,7 @@ void patterns_next_tick() if (--led_counters[i] == 0) { pattern_t *p = led_patterns[i]; p++; - if (p->duration == 0) { // END + if (p->duration_fib == 0) { // END /* Keep the last state, wait for others */ pattern_finished(i); continue;