3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this  
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in  
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting  
  17   documentation, and that the name of the author not be used in  
  18   advertising or publicity pertaining to distribution of the  
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  Main source file for the LEDNotfier project. This file contains the main tasks of 
  34  *  the demo and is responsible for the initial application hardware configuration. 
  37 #include "LEDNotifier.h" 
  39 /** LUFA CDC Class driver interface configuration and state information. This structure is 
  40  *  passed to all CDC Class driver functions, so that multiple instances of the same class 
  41  *  within a device can be differentiated from one another. 
  43 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface 
= 
  47                                 .ControlInterfaceNumber         
= 0, 
  49                                 .DataINEndpointNumber           
= CDC_TX_EPNUM
, 
  50                                 .DataINEndpointSize             
= CDC_TXRX_EPSIZE
, 
  51                                 .DataINEndpointDoubleBank       
= false, 
  53                                 .DataOUTEndpointNumber          
= CDC_RX_EPNUM
, 
  54                                 .DataOUTEndpointSize            
= CDC_TXRX_EPSIZE
, 
  55                                 .DataOUTEndpointDoubleBank      
= false, 
  57                                 .NotificationEndpointNumber     
= CDC_NOTIFICATION_EPNUM
, 
  58                                 .NotificationEndpointSize       
= CDC_NOTIFICATION_EPSIZE
, 
  59                                 .NotificationEndpointDoubleBank 
= false, 
  63 /** Counter for the software PWM */ 
  64 static volatile uint8_t SoftPWM_Count
; 
  66 /** Duty cycle for the first software PWM channel */ 
  67 static volatile uint8_t SoftPWM_Channel1_Duty
; 
  69 /** Duty cycle for the second software PWM channel */ 
  70 static volatile uint8_t SoftPWM_Channel2_Duty
; 
  72 /** Duty cycle for the third software PWM channel */ 
  73 static volatile uint8_t SoftPWM_Channel3_Duty
; 
  76 /** Interrupt handler for managing the software PWM channels for the LEDs */ 
  77 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
) 
  79         uint8_t LEDMask 
= LEDS_ALL_LEDS
; 
  81         if (++SoftPWM_Count 
== 0b00011111
) 
  84         if (SoftPWM_Count 
>= SoftPWM_Channel1_Duty
) 
  85           LEDMask 
&= ~LEDS_LED1
; 
  87         if (SoftPWM_Count 
>= SoftPWM_Channel2_Duty
) 
  88           LEDMask 
&= ~LEDS_LED2
; 
  90         if (SoftPWM_Count 
>= SoftPWM_Channel3_Duty
) 
  91           LEDMask 
&= ~LEDS_LED3
; 
  93         LEDs_SetAllLEDs(LEDMask
); 
  96 /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be 
  97  *  used like any regular character stream in the C APIs 
  99 static FILE USBSerialStream
; 
 101 /** Main program entry point. This routine contains the overall program flow, including initial 
 102  *  setup of all components and the main program loop. 
 108         /* Create a regular blocking character stream for the interface so that it can be used with the stdio.h functions */ 
 109         CDC_Device_CreateBlockingStream(&VirtualSerial_CDC_Interface
, &USBSerialStream
); 
 115                 /* Read in next LED colour command from the host */ 
 116                 uint8_t ColorUpdate 
= fgetc(&USBSerialStream
); 
 118                 /* Top 3 bits select the LED, bottom 5 control the brightness */ 
 119                 uint8_t Channel 
= (ColorUpdate 
& 0b11100000
); 
 120                 uint8_t Duty    
= (ColorUpdate 
& 0b00011111
); 
 122                 if (Channel 
& (1 << 5)) 
 123                   SoftPWM_Channel1_Duty 
= Duty
; 
 125                 if (Channel 
& (1 << 6)) 
 126                   SoftPWM_Channel2_Duty 
= Duty
; 
 128                 if (Channel 
& (1 << 7)) 
 129                   SoftPWM_Channel3_Duty 
= Duty
; 
 131                 CDC_Device_USBTask(&VirtualSerial_CDC_Interface
); 
 136 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
 137 void SetupHardware(void) 
 139         /* Disable watchdog if enabled by bootloader/fuses */ 
 140         MCUSR 
&= ~(1 << WDRF
); 
 143         /* Disable clock division */ 
 144         clock_prescale_set(clock_div_1
); 
 146         /* Hardware Initialization */ 
 150         /* Timer Initialization */ 
 152         TCCR0A 
= (1 << WGM01
); 
 153         TCCR0B 
= (1 << CS00
); 
 154         TIMSK0 
= (1 << OCIE0A
); 
 157 /** Event handler for the library USB Configuration Changed event. */ 
 158 void EVENT_USB_Device_ConfigurationChanged(void) 
 160         CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface
); 
 163 /** Event handler for the library USB Unhandled Control Request event. */ 
 164 void EVENT_USB_Device_UnhandledControlRequest(void) 
 166         CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface
);