Enhance the HotmailNotifier project so that the LEDs' individual brightness can be...
[pub/USBasp.git] / Projects / HotmailNotifier / HotmailNotifier.c
index 3d467e5..5a7ebd5 100644 (file)
@@ -60,6 +60,39 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
                        },\r
        };\r
 \r
+/** Counter for the software PWM */\r
+static volatile uint8_t SoftPWM_Count;\r
+\r
+/** Duty cycle for the first software PWM channel */\r
+static volatile uint8_t SoftPWM_Channel1_Duty;\r
+\r
+/** Duty cycle for the second software PWM channel */\r
+static volatile uint8_t SoftPWM_Channel2_Duty;\r
+\r
+/** Duty cycle for the third software PWM channel */\r
+static volatile uint8_t SoftPWM_Channel3_Duty;\r
+\r
+\r
+/** Interrupt handler for managing the software PWM channels for the LEDs */\r
+ISR(TIMER0_COMPA_vect, ISR_BLOCK)\r
+{\r
+       uint8_t LEDMask = LEDS_ALL_LEDS;\r
+\r
+       if (++SoftPWM_Count == 0x1F)\r
+         SoftPWM_Count = 0;\r
+\r
+       if (SoftPWM_Count >= SoftPWM_Channel1_Duty)\r
+         LEDMask &= ~LEDS_LED1;\r
+\r
+       if (SoftPWM_Count >= SoftPWM_Channel2_Duty)\r
+         LEDMask &= ~LEDS_LED2;\r
+\r
+       if (SoftPWM_Count >= SoftPWM_Channel3_Duty)\r
+         LEDMask &= ~LEDS_LED3;\r
+         \r
+       LEDs_SetAllLEDs(LEDMask);\r
+}\r
+\r
 /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be\r
  *  used like any regular character stream in the C APIs\r
  */\r
@@ -77,11 +110,21 @@ int main(void)
 \r
        for (;;)\r
        {\r
-               /* Read next character - if a '1' turn on red led, if a '0' turn on green LED */\r
-               if (fgetc(&USBSerialStream) == '1')\r
-                 LEDs_SetAllLEDs(LEDS_LED3);\r
-               else\r
-                 LEDs_SetAllLEDs(LEDS_LED2);\r
+               /* Read in next LED colour command from the host */\r
+               uint8_t ColorUpdate = fgetc(&USBSerialStream);\r
+               \r
+               /* Top 3 bits select the LED, bottom three control the brightness */\r
+               uint8_t Channel = (ColorUpdate & 0b11100000);\r
+               uint8_t Duty    = (ColorUpdate & 0b00011111);\r
+               \r
+               if (Channel & (1 << 5))\r
+                 SoftPWM_Channel1_Duty = Duty;\r
+               \r
+               if (Channel & (1 << 6))\r
+                 SoftPWM_Channel2_Duty = Duty;\r
+
+               if (Channel & (1 << 7))\r
+                 SoftPWM_Channel3_Duty = Duty;\r
 \r
                CDC_Device_USBTask(&VirtualSerial_CDC_Interface);\r
                USB_USBTask();\r
@@ -101,15 +144,18 @@ void SetupHardware(void)
        /* Hardware Initialization */\r
        LEDs_Init();\r
        USB_Init();\r
+       \r
+       /* Timer Initialization */\r
+       OCR0A  = 100;\r
+       TCCR0A = (1 << WGM01);\r
+       TCCR0B = (1 << CS00);\r
+       TIMSK0 = (1 << OCIE0A);\r
 }\r
 \r
 /** Event handler for the library USB Configuration Changed event. */\r
 void EVENT_USB_Device_ConfigurationChanged(void)\r
 {\r
-       LEDs_SetAllLEDs(LEDS_ALL_LEDS);\r
-\r
-       if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))\r
-               LEDs_SetAllLEDs(LEDS_LED1 | LEDS_LED3);\r
+       CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);\r
 }\r
 \r
 /** Event handler for the library USB Unhandled Control Request event. */\r