some more colors on the remote interpreted
[moodlight.git] / main.c
1 /* $Id: main.c,v 1.5 2010/07/01 23:56:19 simimeie Exp $
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"
19 #include "ledpwm.h"
20 #include "ircontrol.h"
21 #include "timers.h"
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
37 #define IRSTATE_RED       1
38 #define IRSTATE_GREEN     2
39 #define IRSTATE_BLUE      3
40 #define IRSTATE_WHITE     4
41
42 int main(void)
43 {
44   uint8_t irstate = 0;
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();
53   timers_init();
54   ledpwm_init();
55   ircontrol_init();
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();
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 0x02: /* "Off" */
101                  ledpwm_bri = 0;
102                  break;
103       case 0x03: /* "On" */
104                  ledpwm_bri = 128;
105                  break;
106       case 0x04: /* Red */
107                  if (irstate == IRSTATE_RED) {
108                    ledpwm_re = 0xff; ledpwm_gr = 0; ledpwm_bl = 0;
109                    irstate = 0;
110                  } else {
111                    irstate = IRSTATE_RED;
112                  }
113                  break;
114       case 0x05: /* Green */
115                  if (irstate == IRSTATE_GREEN) {
116                    ledpwm_re = 0; ledpwm_gr = 0xff; ledpwm_bl = 0;
117                    irstate = 0;
118                  } else {
119                    irstate = IRSTATE_GREEN;
120                  }
121                  break;
122       case 0x06: /* Blue */
123                  if (irstate == IRSTATE_BLUE) {
124                    ledpwm_re = 0; ledpwm_gr = 0; ledpwm_bl = 0xff;
125                    irstate = 0;
126                  } else {
127                    irstate = IRSTATE_BLUE;
128                  }
129                  break;
130       case 0x07: /* White */
131                  if (irstate == IRSTATE_WHITE) {
132                    ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0xff;
133                    irstate = 0;
134                  } else {
135                    irstate = IRSTATE_WHITE;
136                  }
137                  break;
138       case 0x08: /* Dark orange */
139                  ledpwm_re = 0xff; ledpwm_gr = 0x40; ledpwm_bl = 0x00;
140                  irstate = 0;
141                  break;
142       case 0x09: /* greenish cyan */
143                  ledpwm_re = 0x00; ledpwm_gr = 0xff; ledpwm_bl = 0x80;
144                  irstate = 0;
145                  break;
146       case 0x0c: /* orange */
147                  ledpwm_re = 0xff; ledpwm_gr = 0x80; ledpwm_bl = 0x00;
148                  irstate = 0;
149                  break;
150       case 0x0d: /* cyan */
151                  ledpwm_re = 0x00; ledpwm_gr = 0xff; ledpwm_bl = 0xff;
152                  irstate = 0;
153                  break;
154       case 0x10: /* bright orange */
155                  ledpwm_re = 0xff; ledpwm_gr = 0xc0; ledpwm_bl = 0x00;
156                  irstate = 0;
157                  break;
158       case 0x11: /* blueish cyan */
159                  ledpwm_re = 0x00; ledpwm_gr = 0xa0; ledpwm_bl = 0xff;
160                  irstate = 0;
161                  break;
162       case 0x14: /* yellow */
163                  ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0x00;
164                  irstate = 0;
165                  break;
166       case 0x15: /* blueerish cyan */
167                  ledpwm_re = 0x00; ledpwm_gr = 0x60; ledpwm_bl = 0xff;
168                  irstate = 0;
169                  break;
170       };
171       ledpwm_setled(LEDPWM_REDLED,   (((uint16_t)ledpwm_re * ledpwm_bri) / 255));
172       ledpwm_setled(LEDPWM_GREENLED, (((uint16_t)ledpwm_gr * ledpwm_bri) / 255));
173       ledpwm_setled(LEDPWM_BLUELED,  (((uint16_t)ledpwm_bl * ledpwm_bri) / 255));
174     }
175     /* i++;
176     console_printhex8(i);
177     ledpwm_setled(LEDPWM_REDLED, i);
178     ledpwm_setled(LEDPWM_GREENLED, i+128);
179     ledpwm_setled(LEDPWM_BLUELED, 255-i);
180     _delay_ms(500); */
181   }
182 }
This page took 0.050127 seconds and 3 git commands to generate.