| 1 | /* $Id: main.c,v 1.6 2010/07/10 07:32:08 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 0x0a: /* dark blue */ |
| 147 | ledpwm_re = 0x20; ledpwm_gr = 0x00; ledpwm_bl = 0xff; |
| 148 | irstate = 0; |
| 149 | break; |
| 150 | case 0x0c: /* orange */ |
| 151 | ledpwm_re = 0xff; ledpwm_gr = 0x80; ledpwm_bl = 0x00; |
| 152 | irstate = 0; |
| 153 | break; |
| 154 | case 0x0d: /* cyan */ |
| 155 | ledpwm_re = 0x00; ledpwm_gr = 0xff; ledpwm_bl = 0xff; |
| 156 | irstate = 0; |
| 157 | break; |
| 158 | case 0x0e: /* dark orchid 2 */ |
| 159 | ledpwm_re = 0x50; ledpwm_gr = 0x00; ledpwm_bl = 0xff; |
| 160 | irstate = 0; |
| 161 | break; |
| 162 | case 0x10: /* bright orange */ |
| 163 | ledpwm_re = 0xff; ledpwm_gr = 0xc0; ledpwm_bl = 0x00; |
| 164 | irstate = 0; |
| 165 | break; |
| 166 | case 0x11: /* blueish cyan */ |
| 167 | ledpwm_re = 0x00; ledpwm_gr = 0xa0; ledpwm_bl = 0xff; |
| 168 | irstate = 0; |
| 169 | break; |
| 170 | case 0x12: /* dark orchid */ |
| 171 | ledpwm_re = 0xc0; ledpwm_gr = 0x00; ledpwm_bl = 0xff; |
| 172 | irstate = 0; |
| 173 | break; |
| 174 | case 0x14: /* yellow */ |
| 175 | ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0x00; |
| 176 | irstate = 0; |
| 177 | break; |
| 178 | case 0x15: /* blueerish cyan */ |
| 179 | ledpwm_re = 0x00; ledpwm_gr = 0x60; ledpwm_bl = 0xff; |
| 180 | irstate = 0; |
| 181 | break; |
| 182 | case 0x16: /* violet */ |
| 183 | ledpwm_re = 0xff; ledpwm_gr = 0x00; ledpwm_bl = 0xff; |
| 184 | irstate = 0; |
| 185 | break; |
| 186 | }; |
| 187 | ledpwm_setled(LEDPWM_REDLED, (((uint16_t)ledpwm_re * ledpwm_bri) / 255)); |
| 188 | ledpwm_setled(LEDPWM_GREENLED, (((uint16_t)ledpwm_gr * ledpwm_bri) / 255)); |
| 189 | ledpwm_setled(LEDPWM_BLUELED, (((uint16_t)ledpwm_bl * ledpwm_bri) / 255)); |
| 190 | } |
| 191 | /* i++; |
| 192 | console_printhex8(i); |
| 193 | ledpwm_setled(LEDPWM_REDLED, i); |
| 194 | ledpwm_setled(LEDPWM_GREENLED, i+128); |
| 195 | ledpwm_setled(LEDPWM_BLUELED, 255-i); |
| 196 | _delay_ms(500); */ |
| 197 | } |
| 198 | } |