1 /* $Id: main.c,v 1.4 2010/06/30 19:39:28 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--; }
101 if (irstate == IRSTATE_RED) {
102 ledpwm_re = 0xff; ledpwm_gr = 0; ledpwm_bl = 0;
105 irstate = IRSTATE_RED;
108 case 0x05: /* Green */
109 if (irstate == IRSTATE_GREEN) {
110 ledpwm_re = 0; ledpwm_gr = 0xff; ledpwm_bl = 0;
113 irstate = IRSTATE_GREEN;
116 case 0x06: /* Blue */
117 if (irstate == IRSTATE_BLUE) {
118 ledpwm_re = 0; ledpwm_gr = 0; ledpwm_bl = 0xff;
121 irstate = IRSTATE_BLUE;
124 case 0x07: /* White */
125 if (irstate == IRSTATE_WHITE) {
126 ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0xff;
129 irstate = IRSTATE_WHITE;
132 case 0x08: /* Dark orange */
133 ledpwm_re = 0xff; ledpwm_gr = 0x40; ledpwm_bl = 0x00;
136 case 0x0c: /* orange */
137 ledpwm_re = 0xff; ledpwm_gr = 0x80; ledpwm_bl = 0x00;
140 case 0x10: /* bright orange */
141 ledpwm_re = 0xff; ledpwm_gr = 0xc0; ledpwm_bl = 0x00;
144 case 0x14: /* yellow */
145 ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0x00;
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));
154 console_printhex8(i);
155 ledpwm_setled(LEDPWM_REDLED, i);
156 ledpwm_setled(LEDPWM_GREENLED, i+128);
157 ledpwm_setled(LEDPWM_BLUELED, 255-i);