#include "lights.h"
-static unsigned char led_counters[N_LEDS];
-static pattern_t *led_patterns[N_LEDS];
-
-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, 0x10 },
- { 0, 0x10 },
- 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, 0xF },
- PATTERN_END
-};
+#define N_LEDS 2
+static unsigned char bits_left[N_LEDS];
+static unsigned char *pattern_data[N_LEDS];
+static unsigned char current_bit[N_LEDS];
-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
+static unsigned char off_pattern[] = {
+ 0b00000000,
};
-pattern_t off_pattern[] = {
- { 0, 0x1 },
- PATTERN_END
-};
-
-static void led_set_mode(unsigned char n, unsigned char mode)
+void led_set_pattern(unsigned char led, unsigned char bits_len,
+ unsigned char bits_start,
+ unsigned char *data)
{
- if (n < N_PWMLEDS) {
- pwmled_set_mode(n, mode);
- } else if (n < N_LEDS) {
- gpio_set(n - N_PWMLEDS, mode);
+ if (!bits_len) {
+ led_set_pattern(led, 8, 0, off_pattern);
+ } else {
+ bits_left[led] = bits_len-1;
+ current_bit[led] = bits_start;
+ pattern_data[led] = data;
}
}
-void led_set_pattern(unsigned char n, pattern_t *pattern)
+void led_set_number_pattern(unsigned char led, unsigned char num,
+ unsigned char inv)
{
- 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);
+ static unsigned char pattern_num[] = {
+ 0b00100000, /* 10 */
+ 0b10000010, /* 8,9 */
+ 0b00001000, /* 7 */
+ 0b00100000, /* 6 */
+ 0b10000010, /* 4,5 */
+ 0b00001000, /* 3 */
+ 0b00100000, /* 2 */
+ 0b10000000, /* 1 */
+ 0b00000000,
+ };
+
+ static unsigned char pattern_invnum[] = {
+ 0b11011111, /* 10 */
+ 0b01111101, /* 8,9 */
+ 0b11110111, /* 7 */
+ 0b11011111, /* 6 */
+ 0b01111101, /* 4,5 */
+ 0b11110111, /* 3 */
+ 0b11011111, /* 2 */
+ 0b01111111, /* 1 */
+ 0b11111111,
+ };
+
+ unsigned char bits_len, bits_remaining;
+
+ if (num > 10)
+ num = 10;
- led_set_pattern(N_PWMLEDS+1, boot_pattern);
+ bits_len = 6*num + 12;
+ bits_remaining = 9*8 - bits_len;
+ log_byte(0x88);
+ log_byte(bits_len);
+ log_byte(bits_remaining & 7);
+ log_byte(bits_remaining >> 3);
+ log_flush();
+ led_set_pattern(led, bits_len, bits_remaining & 7,
+ inv ? pattern_invnum + (bits_remaining >> 3)
+ : pattern_num + (bits_remaining >> 3));
}
-pattern_t *number_pattern(unsigned char num, unsigned char inv)
+static void inline pattern_select(unsigned char n)
{
- 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;
+ switch(n) {
+ case 0: pwmled_pattern_select(n);
+ break;
+ case 1: status_led_pattern_select(n);
+ break;
}
}
-static pattern_t *pattern_select(unsigned char n)
+static void inline led_set_mode(unsigned char n, unsigned char mode)
{
- switch(n) {
- case 0: return pwmled0_pattern_select();
- case 1: return pwmled1_pattern_select();
- case 2: return pwmled2_pattern_select();
- case 3: return status_led_pattern_select();
- case 4: return illumination_led_pattern_select();
- case 6: return laser_pattern_select();
- default: return NULL;
+ switch (n) {
+ case 0: pwmled_on_off(mode); break;
+ case 1: status_led_on_off(mode); log_byte(mode); log_flush(); break;
}
}
-void pattern_reload()
+void patterns_next_tick()
{
unsigned char i;
- for (i = 0; i < N_LEDS; i++)
- led_set_pattern(i, pattern_select(i));
+ for (i = 0; i < N_LEDS; i++) {
+ if (!bits_left[i]) {
+ pattern_select(i);
+ } else {
+ bits_left[i]--;
+ current_bit[i]++;
+ if (current_bit[i] >= 8) {
+ current_bit[i] = 0;
+ pattern_data[i]++;
+ }
+ }
+
+ led_set_mode(i, *(pattern_data[i]) & (1 << (7 - current_bit[i]))
+ ? 1 : 0);
+ }
}
-static void inline pattern_finished(unsigned char n)
+void init_pattern()
{
unsigned char i;
- led_patterns[n] = NULL;
-
- if (n < N_PWMLEDS) {
- for (i = 0; i < N_PWMLEDS; i++)
- if (led_patterns[i])
- return;
-
- /* all pwmleds finished; restart them */
- for (i = 0; i < N_PWMLEDS; i++)
- led_set_pattern(i, pattern_select(i));
- } 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));
- }
+ for (i = 0; i < N_LEDS; i++)
+ led_set_pattern(i, 0, 0, NULL);
}
-void patterns_next_tick()
+void pattern_reload()
{
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);
- }
+ for (i = 0; i < N_LEDS; i++)
+ bits_left[i] = 0;
- }
+ patterns_next_tick();
}