ddf1553f |
1 | /* $Id: main.c,v 1.4 2010/06/30 19:39:28 simimeie Exp $ |
2d3643ee |
2 | * Main file for the HaWo moodlight. |
3 | * This is the main file that glues it all together. It also contains all |
4 | * functionality that is too small to require an extra file. |
5 | * (C) Michael "Fox" Meier 2010 |
6 | */ |
7 | |
8 | #include <avr/io.h> |
9 | #include <avr/interrupt.h> |
10 | #include <avr/wdt.h> |
11 | #include <avr/eeprom.h> |
12 | #include <avr/power.h> |
13 | #include <avr/sleep.h> |
14 | #include <stdio.h> |
15 | #include <string.h> |
16 | #include <util/delay.h> |
17 | |
18 | #include "console.h" |
17aea8ef |
19 | #include "ledpwm.h" |
20 | #include "ircontrol.h" |
ed7a0d87 |
21 | #include "timers.h" |
2d3643ee |
22 | /* #include "eepromdata.h" */ |
23 | |
24 | uint8_t mcusr_mirror __attribute__ ((section (".noinit"))); |
25 | |
26 | /* This is needed to recover from a watchdog reset, as the watchdog |
27 | * stays active after the reset. |
28 | * The variable is just to make the reason of the last reset accessible |
29 | * later. */ |
30 | void get_mcusr(void) __attribute__((naked)) __attribute__((section(".init3"))); |
31 | void get_mcusr(void) { |
32 | mcusr_mirror = MCUSR; |
33 | MCUSR = 0; |
34 | wdt_disable(); |
35 | } |
36 | |
ddf1553f |
37 | #define IRSTATE_RED 1 |
38 | #define IRSTATE_GREEN 2 |
39 | #define IRSTATE_BLUE 3 |
40 | #define IRSTATE_WHITE 4 |
41 | |
2d3643ee |
42 | int main(void) |
43 | { |
ddf1553f |
44 | uint8_t irstate = 0; |
2d3643ee |
45 | |
46 | /* Load settings from EEPROM */ |
47 | #if 0 |
48 | something = eeprom_read_byte(&ee_something); |
49 | #endif /* LEDMODULE */ |
50 | |
51 | /* Initialize stuff */ |
52 | console_init(); |
ed7a0d87 |
53 | timers_init(); |
17aea8ef |
54 | ledpwm_init(); |
55 | ircontrol_init(); |
2d3643ee |
56 | |
57 | wdt_enable(WDTO_2S); |
58 | |
59 | /* Prepare sleep mode */ |
60 | //set_sleep_mode(SLEEP_MODE_IDLE); |
61 | //sleep_enable(); |
62 | |
63 | /* All set up, enable interrupts and go. */ |
64 | sei(); |
65 | |
66 | if (mcusr_mirror & _BV(WDRF)) { |
67 | console_printpgm_P(PSTR("NOTE: last reset was from Watchdog Timer.")); |
68 | } |
69 | if (mcusr_mirror & _BV(BORF)) { |
70 | console_printpgm_P(PSTR("NOTE: last reset was from Brownout.")); |
71 | } |
72 | |
73 | while (1) { /* We should never exit */ |
74 | wdt_reset(); |
ddf1553f |
75 | uint8_t k = ircontrol_getlastcommand(); |
76 | if (k != 0xff) { |
77 | switch (k) { |
78 | case 0x00: /* "Up" */ |
79 | if (irstate == IRSTATE_RED) { |
80 | if (ledpwm_re < 0xff) { ledpwm_re++; } |
81 | } else if (irstate == IRSTATE_GREEN) { |
82 | if (ledpwm_gr < 0xff) { ledpwm_gr++; } |
83 | } else if (irstate == IRSTATE_BLUE) { |
84 | if (ledpwm_bl < 0xff) { ledpwm_bl++; } |
85 | } else { |
86 | if (ledpwm_bri < 0xff) { ledpwm_bri++; } |
87 | } |
88 | break; |
89 | case 0x01: /* "Down" */ |
90 | if (irstate == IRSTATE_RED) { |
91 | if (ledpwm_re > 0) { ledpwm_re--; } |
92 | } else if (irstate == IRSTATE_GREEN) { |
93 | if (ledpwm_gr > 0) { ledpwm_gr--; } |
94 | } else if (irstate == IRSTATE_BLUE) { |
95 | if (ledpwm_bl > 0) { ledpwm_bl--; } |
96 | } else { |
97 | if (ledpwm_bri > 0) { ledpwm_bri--; } |
98 | } |
99 | break; |
100 | case 0x04: /* Red */ |
101 | if (irstate == IRSTATE_RED) { |
102 | ledpwm_re = 0xff; ledpwm_gr = 0; ledpwm_bl = 0; |
103 | irstate = 0; |
104 | } else { |
105 | irstate = IRSTATE_RED; |
106 | } |
107 | break; |
108 | case 0x05: /* Green */ |
109 | if (irstate == IRSTATE_GREEN) { |
110 | ledpwm_re = 0; ledpwm_gr = 0xff; ledpwm_bl = 0; |
111 | irstate = 0; |
112 | } else { |
113 | irstate = IRSTATE_GREEN; |
114 | } |
115 | break; |
116 | case 0x06: /* Blue */ |
117 | if (irstate == IRSTATE_BLUE) { |
118 | ledpwm_re = 0; ledpwm_gr = 0; ledpwm_bl = 0xff; |
119 | irstate = 0; |
120 | } else { |
121 | irstate = IRSTATE_BLUE; |
122 | } |
123 | break; |
124 | case 0x07: /* White */ |
125 | if (irstate == IRSTATE_WHITE) { |
126 | ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0xff; |
127 | irstate = 0; |
128 | } else { |
129 | irstate = IRSTATE_WHITE; |
130 | } |
131 | break; |
132 | case 0x08: /* Dark orange */ |
133 | ledpwm_re = 0xff; ledpwm_gr = 0x40; ledpwm_bl = 0x00; |
134 | irstate = 0; |
135 | break; |
136 | case 0x0c: /* orange */ |
137 | ledpwm_re = 0xff; ledpwm_gr = 0x80; ledpwm_bl = 0x00; |
138 | irstate = 0; |
139 | break; |
140 | case 0x10: /* bright orange */ |
141 | ledpwm_re = 0xff; ledpwm_gr = 0xc0; ledpwm_bl = 0x00; |
142 | irstate = 0; |
143 | break; |
144 | case 0x14: /* yellow */ |
145 | ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0x00; |
146 | irstate = 0; |
147 | break; |
148 | }; |
149 | ledpwm_setled(LEDPWM_REDLED, (((uint16_t)ledpwm_re * ledpwm_bri) / 255)); |
150 | ledpwm_setled(LEDPWM_GREENLED, (((uint16_t)ledpwm_gr * ledpwm_bri) / 255)); |
151 | ledpwm_setled(LEDPWM_BLUELED, (((uint16_t)ledpwm_bl * ledpwm_bri) / 255)); |
152 | } |
17aea8ef |
153 | /* i++; |
154 | console_printhex8(i); |
155 | ledpwm_setled(LEDPWM_REDLED, i); |
156 | ledpwm_setled(LEDPWM_GREENLED, i+128); |
157 | ledpwm_setled(LEDPWM_BLUELED, 255-i); |
158 | _delay_ms(500); */ |
2d3643ee |
159 | } |
160 | } |