From 17faa7fef36a251a8f28266a3792c48702bc3f51 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 5 Jul 2011 14:05:24 +0000 Subject: color temperature --- kernel/dmx.c | 11 ++-- kernel/dmx/Makefile | 3 +- kernel/rgb.c | 186 ++++++++++++++++++++++++++++++++++++++++------------ kernel/tasks.h | 3 + kernel/uart.c | 21 +++--- 5 files changed, 167 insertions(+), 57 deletions(-) (limited to 'kernel') diff --git a/kernel/dmx.c b/kernel/dmx.c index 8a1094e..4d93135 100644 --- a/kernel/dmx.c +++ b/kernel/dmx.c @@ -38,11 +38,12 @@ main() semaphore(0, 1); - task(heartbeat, STACK, MSEC(0), MSEC(750), 0); - task(rgb, STACK, MSEC(0), MSEC(10), &rgbargs); - task(pwm, STACK, MSEC(0), MSEC(10), &pwmargs[0]); - task(pwm, STACK, MSEC(0), MSEC(10), &pwmargs[1]); - task(pwm, STACK, MSEC(0), MSEC(10), &pwmargs[2]); + task(heartbeat, STACK, USEC(0), MSEC(750), 0); + task(rgb, STACK, USEC(10), MSEC(10), &rgbargs); + task(pwm, STACK, USEC(20), USEC(2500), &pwmargs[0]); + task(pwm, STACK, USEC(30), USEC(2500), &pwmargs[1]); + task(pwm, STACK, USEC(50), USEC(2500), &pwmargs[2]); + task(cmd, STACK, USEC(70), MSEC(10), &rgbargs); for (;;); diff --git a/kernel/dmx/Makefile b/kernel/dmx/Makefile index bd3276f..a77e610 100644 --- a/kernel/dmx/Makefile +++ b/kernel/dmx/Makefile @@ -7,9 +7,10 @@ STACK= 64 TASKS= 8 SEMAPHORES= 8 BAUD= 9600 +USE_2X= 1 PROG= dmx -SRCS= dmx.c uart.c heartbeat.c rgb.c hsv.c +SRCS= dmx.c uart.c heartbeat.c rgb.c hsv.c cmd.c NOMAN= .include diff --git a/kernel/rgb.c b/kernel/rgb.c index fbe42f7..794d266 100644 --- a/kernel/rgb.c +++ b/kernel/rgb.c @@ -23,85 +23,185 @@ void hsv(uint8_t *, uint8_t *, uint8_t *, uint16_t, uint8_t, uint8_t); -// uint8_t factor[] = { 1, 2, 3, 4, 6, 8, 12, 16, 23, 32, 45, 64, 90, 128, 180, 255 }; -uint8_t factor[] PROGMEM = { - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, - 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 5, 5, 5, 5, 5, 5, - 5, 5, 6, 6, 6, 6, 6, 6, - 6, 6, 7, 7, 7, 7, 7, 7, - 8, 8, 8, 8, 8, 9, 9, 9, - 9, 9, 10, 10, 10, 10, 11, 11, - 11, 11, 12, 12, 12, 12, 13, 13, - 13, 13, 14, 14, 14, 15, 15, 15, - 16, 16, 17, 17, 17, 18, 18, 18, - 19, 19, 20, 20, 21, 21, 22, 22, - 23, 23, 24, 24, 25, 25, 26, 26, - 27, 27, 28, 29, 29, 30, 31, 31, - 32, 33, 34, 34, 35, 36, 37, 37, - 38, 39, 40, 41, 42, 43, 44, 45, - 46, 47, 48, 49, 50, 51, 52, 53, - 54, 55, 57, 58, 59, 61, 62, 63, - 65, 66, 68, 69, 71, 72, 74, 75, - 77, 79, 80, 82, 84, 86, 88, 90, - 92, 94, 96, 98, 100, 102, 104, 107, - 109, 111, 114, 116, 119, 122, 124, 127, - 130, 133, 136, 139, 142, 145, 148, 151, - 154, 158, 161, 165, 168, 172, 176, 180, - 184, 188, 192, 196, 200, 205, 209, 214, - 219, 223, 228, 233, 238, 244, 249, 255, +/* 1/sqrt(2) timing scala in usec */ +uint16_t factor[] PROGMEM = { + 30, 30, 31, 31, 32, 32, 33, 34, + 34, 35, 35, 36, 37, 37, 38, 39, + 39, 40, 41, 41, 42, 43, 44, 44, + 45, 46, 47, 48, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 68, + 69, 70, 71, 72, 74, 75, 76, 78, + 79, 80, 82, 83, 85, 86, 88, 89, + 91, 92, 94, 96, 97, 99, 101, 103, + 104, 106, 108, 110, 112, 114, 116, 118, + 120, 122, 124, 126, 129, 131, 133, 136, + 138, 140, 143, 145, 148, 150, 153, 156, + 158, 161, 164, 167, 170, 173, 176, 179, + 182, 185, 189, 192, 195, 199, 202, 206, + 209, 213, 217, 220, 224, 228, 232, 236, + 240, 245, 249, 253, 258, 262, 267, 272, + 276, 281, 286, 291, 296, 301, 307, 312, + 317, 323, 329, 334, 340, 346, 352, 358, + 365, 371, 378, 384, 391, 398, 405, 412, + 419, 426, 434, 441, 449, 457, 465, 473, + 481, 490, 498, 507, 516, 525, 534, 544, + 553, 563, 573, 583, 593, 603, 614, 625, + 635, 647, 658, 669, 681, 693, 705, 717, + 730, 743, 756, 769, 782, 796, 810, 824, + 839, 853, 868, 883, 899, 915, 931, 947, + 963, 980, 997, 1015, 1033, 1051, 1069, 1088, + 1107, 1126, 1146, 1166, 1186, 1207, 1228, 1250, + 1271, 1294, 1316, 1339, 1363, 1386, 1411, 1435, + 1460, 1486, 1512, 1538, 1565, 1593, 1621, 1649, + 1678, 1707, 1737, 1767, 1798, 1830, 1862, 1894, + 1927, 1961, 1995, 2030, 2066, 2102, 2138, 2176, + 2214, 2253, 2292, 2332, 2373, 2414, 2457, 2500, +}; + +struct bb { + uint8_t color, r, g, b; +} bb[] PROGMEM = { + { 10, 0xff, 0x38, 0x00 }, + { 11, 0xff, 0x47, 0x00 }, + { 12, 0xff, 0x53, 0x00 }, + { 13, 0xff, 0x5d, 0x00 }, + { 14, 0xff, 0x65, 0x00 }, + { 15, 0xff, 0x6d, 0x00 }, + { 16, 0xff, 0x73, 0x00 }, + { 17, 0xff, 0x79, 0x00 }, + { 18, 0xff, 0x7e, 0x00 }, + { 19, 0xff, 0x83, 0x00 }, + { 20, 0xff, 0x89, 0x12 }, + { 21, 0xff, 0x8e, 0x21 }, + { 22, 0xff, 0x93, 0x2c }, + { 23, 0xff, 0x98, 0x36 }, + { 24, 0xff, 0x9d, 0x3f }, + { 25, 0xff, 0xa1, 0x48 }, + { 26, 0xff, 0xa5, 0x4f }, + { 27, 0xff, 0xa9, 0x57 }, + { 28, 0xff, 0xad, 0x5e }, + { 29, 0xff, 0xb1, 0x65 }, + { 30, 0xff, 0xb4, 0x6b }, + { 31, 0xff, 0xb8, 0x72 }, + { 32, 0xff, 0xbb, 0x78 }, + { 33, 0xff, 0xbe, 0x7e }, + { 34, 0xff, 0xc1, 0x84 }, + { 35, 0xff, 0xc4, 0x89 }, + { 36, 0xff, 0xc7, 0x8f }, + { 37, 0xff, 0xc9, 0x94 }, + { 38, 0xff, 0xcc, 0x99 }, + { 39, 0xff, 0xce, 0x9f }, + { 40, 0xff, 0xd1, 0xa3 }, + { 41, 0xff, 0xd3, 0xa8 }, + { 42, 0xff, 0xd5, 0xad }, + { 43, 0xff, 0xd7, 0xb1 }, + { 44, 0xff, 0xd9, 0xb6 }, + { 45, 0xff, 0xdb, 0xba }, + { 46, 0xff, 0xdd, 0xbe }, + { 47, 0xff, 0xdf, 0xc2 }, + { 48, 0xff, 0xe1, 0xc6 }, + { 49, 0xff, 0xe3, 0xca }, + { 50, 0xff, 0xe4, 0xce }, + { 51, 0xff, 0xe6, 0xd2 }, + { 52, 0xff, 0xe8, 0xd5 }, + { 53, 0xff, 0xe9, 0xd9 }, + { 54, 0xff, 0xeb, 0xdc }, + { 55, 0xff, 0xec, 0xe0 }, + { 56, 0xff, 0xee, 0xe3 }, + { 57, 0xff, 0xef, 0xe6 }, + { 58, 0xff, 0xf0, 0xe9 }, + { 59, 0xff, 0xf2, 0xec }, + { 60, 0xff, 0xf3, 0xef }, + { 61, 0xff, 0xf4, 0xf2 }, + { 62, 0xff, 0xf5, 0xf5 }, + { 63, 0xff, 0xf6, 0xf8 }, + { 64, 0xff, 0xf8, 0xfb }, + { 65, 0xff, 0xf9, 0xfd }, + { 66, 0xfe, 0xf9, 0xff }, + { 67, 0xfc, 0xf7, 0xff }, + { 68, 0xf9, 0xf6, 0xff }, + { 69, 0xf7, 0xf5, 0xff }, + { 70, 0xf5, 0xf3, 0xff }, + { 71, 0xf3, 0xf2, 0xff }, + { 72, 0xf0, 0xf1, 0xff }, + { 73, 0xef, 0xf0, 0xff }, + { 74, 0xed, 0xef, 0xff }, + { 75, 0xeb, 0xee, 0xff }, + { 76, 0xe9, 0xed, 0xff }, + { 77, 0xe7, 0xec, 0xff }, + { 78, 0xe6, 0xeb, 0xff }, + { 79, 0xe4, 0xea, 0xff }, + { 80, 0xe3, 0xe9, 0xff }, + { 81, 0xe1, 0xe8, 0xff }, + { 82, 0xe0, 0xe7, 0xff }, + { 83, 0xde, 0xe6, 0xff }, + { 84, 0xdd, 0xe6, 0xff }, + { 85, 0xdc, 0xe5, 0xff }, + { 86, 0xda, 0xe4, 0xff }, + { 87, 0xd9, 0xe3, 0xff }, + { 88, 0xd8, 0xe3, 0xff }, + { 89, 0xd7, 0xe2, 0xff }, + { 90, 0xd6, 0xe1, 0xff }, }; void rgb(void *arg) { struct rgbarg *a = (struct rgbarg *)arg; - uint32_t d = deadline(); - uint32_t r = release(); uint16_t i = 0; uint8_t v; for (;;) { +#if 0 i = (i + 1) % 360; + #if 0 v = i % 120; v = (v < 60) ? 255 - 2 * v : 15 + 2 * v; + #else + v = 255; + #endif hsv(a->r, a->g, a->b, i, 255, v); - - r = d; - d += MSEC(28); - update(r, d); + period(MSEC(83)); +#endif + i = (i + 1) % 162; + if (i > 80) + v = 161 - i; + else + v = i; + *a->r = pgm_read_byte(&bb[v].r); + *a->g = pgm_read_byte(&bb[v].g); + *a->b = pgm_read_byte(&bb[v].b); + period(MSEC(370)); } } void pwm(void *arg) { -#define SCALE 6 struct pwmarg *a = (struct pwmarg *)arg; + uint16_t on, off, maxval;; uint32_t d = deadline(); uint32_t r = release(); DDRB |= _BV(a->pin); PORTB &= ~_BV(a->pin); + maxval = pgm_read_word(&factor[255]); for (;;) { + on = pgm_read_word(&factor[*a->value]); + off = maxval - on; + if (*a->value > 0) { PORTB |= _BV(a->pin); - d = r += USEC(pgm_read_byte(&factor[*a->value]) << SCALE); + r = d += USEC(on); update(r, d); } if (*a->value < 255) { PORTB &= ~_BV(a->pin); - d = r += USEC(pgm_read_byte(&factor[(255 - *a->value)]) << SCALE); + r = d += USEC(off); update(r, d); } } diff --git a/kernel/tasks.h b/kernel/tasks.h index 6e6ea43..9c0288f 100644 --- a/kernel/tasks.h +++ b/kernel/tasks.h @@ -67,6 +67,8 @@ struct ppmarg { }; void init_uart(void); +char uart_getchar(void); +void uart_putchar(char); void heartbeat(void *); void rgb(void *); @@ -74,5 +76,6 @@ void pwm(void *); void lcd(void *); void adc(void *); void ppm(void *); +void cmd(void *); #endif diff --git a/kernel/uart.c b/kernel/uart.c index 0726a9b..9163e0b 100644 --- a/kernel/uart.c +++ b/kernel/uart.c @@ -40,7 +40,12 @@ uart_getchar(void) { char c; +#if BLOCKING loop_until_bit_is_set(UCSRA, RXC); +#else + if (bit_is_clear(UCSRA, RXC)) + return 0; +#endif if (UCSRA & _BV(FE)) return -2; /* EOF */ @@ -48,13 +53,12 @@ uart_getchar(void) return -1; /* ERR */ c = UDR; + uart_putchar(c); /* ECHO */ + switch (c) { case '\r': c = '\n'; break; - case '\n': - uart_putchar(c); - break; case '\t': c = ' '; break; @@ -65,6 +69,7 @@ uart_getchar(void) return c; } +#if USE_RXCIE ISR(SIG_UART_RECV) { uint8_t c = UDR; @@ -99,16 +104,16 @@ ISR(SIG_UART_DATA) UDR = r ? '0' + r : '.'; } +#endif void init_uart(void) { - UCSRB = _BV(RXCIE) | _BV(RXEN) | _BV(TXEN); + UCSRB = _BV(RXEN) | _BV(TXEN); +#if USE_RXCIE + UCSRB |= _BV(RXCIE); +#endif UBRRH = UBRRH_VALUE; UBRRL = UBRRL_VALUE; -#if USE_2X - UCSRA |= _BV(U2X); -#else UCSRA &= ~_BV(U2X); -#endif } -- cgit v1.2.3