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
9 #include <avr/interrupt.h>
11 #include <avr/eeprom.h>
12 #include <avr/power.h>
13 #include <avr/sleep.h>
16 #include <util/delay.h>
20 #include "ircontrol.h"
22 /* #include "eepromdata.h" */
24 uint8_t mcusr_mirror __attribute__ ((section (".noinit")));
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
30 void get_mcusr(void) __attribute__((naked)) __attribute__((section(".init3")));
31 void get_mcusr(void) {
38 #define IRSTATE_GREEN 2
39 #define IRSTATE_BLUE 3
40 #define IRSTATE_WHITE 4
46 /* Load settings from EEPROM */
48 something = eeprom_read_byte(&ee_something);
49 #endif /* LEDMODULE */
51 /* Initialize stuff */
59 /* Prepare sleep mode */
60 //set_sleep_mode(SLEEP_MODE_IDLE);
63 /* All set up, enable interrupts and go. */
66 if (mcusr_mirror & _BV(WDRF)) {
67 console_printpgm_P(PSTR("NOTE: last reset was from Watchdog Timer."));
69 if (mcusr_mirror & _BV(BORF)) {
70 console_printpgm_P(PSTR("NOTE: last reset was from Brownout."));
73 while (1) { /* We should never exit */
75 uint8_t k = ircontrol_getlastcommand();
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++; }
86 if (ledpwm_bri < 0xff) { ledpwm_bri++; }
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--; }
97 if (ledpwm_bri > 0) { ledpwm_bri--; }
100 case 0x02: /* "Off" */
103 case 0x03: /* "On" */
107 if (irstate == IRSTATE_RED) {
108 ledpwm_re = 0xff; ledpwm_gr = 0; ledpwm_bl = 0;
111 irstate = IRSTATE_RED;
114 case 0x05: /* Green */
115 if (irstate == IRSTATE_GREEN) {
116 ledpwm_re = 0; ledpwm_gr = 0xff; ledpwm_bl = 0;
119 irstate = IRSTATE_GREEN;
122 case 0x06: /* Blue */
123 if (irstate == IRSTATE_BLUE) {
124 ledpwm_re = 0; ledpwm_gr = 0; ledpwm_bl = 0xff;
127 irstate = IRSTATE_BLUE;
130 case 0x07: /* White */
131 if (irstate == IRSTATE_WHITE) {
132 ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0xff;
135 irstate = IRSTATE_WHITE;
138 case 0x08: /* Dark orange */
139 ledpwm_re = 0xff; ledpwm_gr = 0x40; ledpwm_bl = 0x00;
142 case 0x09: /* greenish cyan */
143 ledpwm_re = 0x00; ledpwm_gr = 0xff; ledpwm_bl = 0x80;
146 case 0x0c: /* orange */
147 ledpwm_re = 0xff; ledpwm_gr = 0x80; ledpwm_bl = 0x00;
150 case 0x0d: /* cyan */
151 ledpwm_re = 0x00; ledpwm_gr = 0xff; ledpwm_bl = 0xff;
154 case 0x10: /* bright orange */
155 ledpwm_re = 0xff; ledpwm_gr = 0xc0; ledpwm_bl = 0x00;
158 case 0x11: /* blueish cyan */
159 ledpwm_re = 0x00; ledpwm_gr = 0xa0; ledpwm_bl = 0xff;
162 case 0x14: /* yellow */
163 ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0x00;
166 case 0x15: /* blueerish cyan */
167 ledpwm_re = 0x00; ledpwm_gr = 0x60; ledpwm_bl = 0xff;
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));
176 console_printhex8(i);
177 ledpwm_setled(LEDPWM_REDLED, i);
178 ledpwm_setled(LEDPWM_GREENLED, i+128);
179 ledpwm_setled(LEDPWM_BLUELED, 255-i);