| 1 | /* $Id: main.c,v 1.8 2010/07/25 20:40:44 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 "ledframing.h" |
| 23 | #include "rfm12.h" |
| 24 | #include "eepromdata.h" |
| 25 | |
| 26 | #define IRSTATE_RED 1 |
| 27 | #define IRSTATE_GREEN 2 |
| 28 | #define IRSTATE_BLUE 3 |
| 29 | #define IRSTATE_WHITE 4 |
| 30 | |
| 31 | int main(void) |
| 32 | { |
| 33 | uint8_t irstate = 0; |
| 34 | |
| 35 | /* Load settings from EEPROM */ |
| 36 | ledpwm_bri = eeprom_read_byte(&ee_defledbrightness); |
| 37 | |
| 38 | /* Initialize stuff */ |
| 39 | console_init(); |
| 40 | timers_init(); |
| 41 | ledpwm_init(); |
| 42 | ledframing_init(); |
| 43 | ircontrol_init(); |
| 44 | rfm12_init(); |
| 45 | |
| 46 | wdt_enable(WDTO_2S); |
| 47 | |
| 48 | /* Prepare sleep mode */ |
| 49 | //set_sleep_mode(SLEEP_MODE_IDLE); |
| 50 | //sleep_enable(); |
| 51 | |
| 52 | /* All set up, enable interrupts and go. */ |
| 53 | sei(); |
| 54 | |
| 55 | while (1) { /* We should never exit */ |
| 56 | wdt_reset(); |
| 57 | uint8_t k = ircontrol_getlastcommand(); |
| 58 | if (k != 0xff) { |
| 59 | switch (k) { |
| 60 | case 0x00: /* "Up" */ |
| 61 | if (irstate == IRSTATE_RED) { |
| 62 | if (ledpwm_re < 0xff) { ledpwm_re++; } |
| 63 | } else if (irstate == IRSTATE_GREEN) { |
| 64 | if (ledpwm_gr < 0xff) { ledpwm_gr++; } |
| 65 | } else if (irstate == IRSTATE_BLUE) { |
| 66 | if (ledpwm_bl < 0xff) { ledpwm_bl++; } |
| 67 | } else { |
| 68 | if (ledpwm_bri < 0xff) { ledpwm_bri++; } |
| 69 | } |
| 70 | break; |
| 71 | case 0x01: /* "Down" */ |
| 72 | if (irstate == IRSTATE_RED) { |
| 73 | if (ledpwm_re > 0) { ledpwm_re--; } |
| 74 | } else if (irstate == IRSTATE_GREEN) { |
| 75 | if (ledpwm_gr > 0) { ledpwm_gr--; } |
| 76 | } else if (irstate == IRSTATE_BLUE) { |
| 77 | if (ledpwm_bl > 0) { ledpwm_bl--; } |
| 78 | } else { |
| 79 | if (ledpwm_bri > 0) { ledpwm_bri--; } |
| 80 | } |
| 81 | break; |
| 82 | case 0x02: /* "Off" */ |
| 83 | ledpwm_bri = 0; |
| 84 | break; |
| 85 | case 0x03: /* "On" */ |
| 86 | ledpwm_bri = 128; |
| 87 | break; |
| 88 | case 0x04: /* Red */ |
| 89 | if (irstate == IRSTATE_RED) { |
| 90 | ledpwm_re = 0xff; ledpwm_gr = 0; ledpwm_bl = 0; |
| 91 | irstate = 0; |
| 92 | } else { |
| 93 | irstate = IRSTATE_RED; |
| 94 | } |
| 95 | break; |
| 96 | case 0x05: /* Green */ |
| 97 | if (irstate == IRSTATE_GREEN) { |
| 98 | ledpwm_re = 0; ledpwm_gr = 0xff; ledpwm_bl = 0; |
| 99 | irstate = 0; |
| 100 | } else { |
| 101 | irstate = IRSTATE_GREEN; |
| 102 | } |
| 103 | break; |
| 104 | case 0x06: /* Blue */ |
| 105 | if (irstate == IRSTATE_BLUE) { |
| 106 | ledpwm_re = 0; ledpwm_gr = 0; ledpwm_bl = 0xff; |
| 107 | irstate = 0; |
| 108 | } else { |
| 109 | irstate = IRSTATE_BLUE; |
| 110 | } |
| 111 | break; |
| 112 | case 0x07: /* White */ |
| 113 | if (irstate == IRSTATE_WHITE) { |
| 114 | ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0xff; |
| 115 | irstate = 0; |
| 116 | } else { |
| 117 | irstate = IRSTATE_WHITE; |
| 118 | } |
| 119 | break; |
| 120 | case 0x08: /* Dark orange */ |
| 121 | ledpwm_re = 0xff; ledpwm_gr = 0x40; ledpwm_bl = 0x00; |
| 122 | irstate = 0; |
| 123 | break; |
| 124 | case 0x09: /* greenish cyan */ |
| 125 | ledpwm_re = 0x00; ledpwm_gr = 0xff; ledpwm_bl = 0x80; |
| 126 | irstate = 0; |
| 127 | break; |
| 128 | case 0x0a: /* dark blue */ |
| 129 | ledpwm_re = 0x20; ledpwm_gr = 0x00; ledpwm_bl = 0xff; |
| 130 | irstate = 0; |
| 131 | break; |
| 132 | case 0x0c: /* orange */ |
| 133 | ledpwm_re = 0xff; ledpwm_gr = 0x80; ledpwm_bl = 0x00; |
| 134 | irstate = 0; |
| 135 | break; |
| 136 | case 0x0d: /* cyan */ |
| 137 | ledpwm_re = 0x00; ledpwm_gr = 0xff; ledpwm_bl = 0xff; |
| 138 | irstate = 0; |
| 139 | break; |
| 140 | case 0x0e: /* dark orchid 2 */ |
| 141 | ledpwm_re = 0x50; ledpwm_gr = 0x00; ledpwm_bl = 0xff; |
| 142 | irstate = 0; |
| 143 | break; |
| 144 | case 0x10: /* bright orange */ |
| 145 | ledpwm_re = 0xff; ledpwm_gr = 0xc0; ledpwm_bl = 0x00; |
| 146 | irstate = 0; |
| 147 | break; |
| 148 | case 0x11: /* blueish cyan */ |
| 149 | ledpwm_re = 0x00; ledpwm_gr = 0xa0; ledpwm_bl = 0xff; |
| 150 | irstate = 0; |
| 151 | break; |
| 152 | case 0x12: /* dark orchid */ |
| 153 | ledpwm_re = 0xc0; ledpwm_gr = 0x00; ledpwm_bl = 0xff; |
| 154 | irstate = 0; |
| 155 | break; |
| 156 | case 0x14: /* yellow */ |
| 157 | ledpwm_re = 0xff; ledpwm_gr = 0xff; ledpwm_bl = 0x00; |
| 158 | irstate = 0; |
| 159 | break; |
| 160 | case 0x15: /* blueerish cyan */ |
| 161 | ledpwm_re = 0x00; ledpwm_gr = 0x60; ledpwm_bl = 0xff; |
| 162 | irstate = 0; |
| 163 | break; |
| 164 | case 0x16: /* violet */ |
| 165 | ledpwm_re = 0xff; ledpwm_gr = 0x00; ledpwm_bl = 0xff; |
| 166 | irstate = 0; |
| 167 | break; |
| 168 | }; |
| 169 | ledpwm_set(ledpwm_re, ledpwm_gr, ledpwm_bl, ledpwm_bri); |
| 170 | } |
| 171 | /* i++; |
| 172 | console_printhex8(i); |
| 173 | ledpwm_setled(LEDPWM_REDLED, i); |
| 174 | ledpwm_setled(LEDPWM_GREENLED, i+128); |
| 175 | ledpwm_setled(LEDPWM_BLUELED, 255-i); |
| 176 | _delay_ms(500); */ |
| 177 | } |
| 178 | } |