#include "LEDNotifier.h"
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
{
.Config =
{
.ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
.DataINEndpoint =
{
.Address = CDC_TX_EPADDR,
.Size = CDC_TXRX_EPSIZE,
.Banks = 1,
},
.DataOUTEndpoint =
{
.Address = CDC_RX_EPADDR,
.Size = CDC_TXRX_EPSIZE,
.Banks = 1,
},
.NotificationEndpoint =
{
.Address = CDC_NOTIFICATION_EPADDR,
.Size = CDC_NOTIFICATION_EPSIZE,
.Banks = 1,
},
},
};
static volatile uint8_t SoftPWM_Count;
static volatile uint8_t SoftPWM_Channel1_Duty;
static volatile uint8_t SoftPWM_Channel2_Duty;
static volatile uint8_t SoftPWM_Channel3_Duty;
static FILE USBSerialStream;
ISR(TIMER0_COMPA_vect, ISR_BLOCK)
{
uint8_t LEDMask = LEDS_ALL_LEDS;
if (++SoftPWM_Count == 0b00011111)
SoftPWM_Count = 0;
if (SoftPWM_Count >= SoftPWM_Channel1_Duty)
LEDMask &= ~LEDS_LED1;
if (SoftPWM_Count >= SoftPWM_Channel2_Duty)
LEDMask &= ~LEDS_LED2;
if (SoftPWM_Count >= SoftPWM_Channel3_Duty)
LEDMask &= ~LEDS_LED3;
LEDs_SetAllLEDs(LEDMask);
}
int main(void)
{
SetupHardware();
CDC_Device_CreateBlockingStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
GlobalInterruptEnable();
for (;;)
{
uint8_t ColourUpdate = fgetc(&USBSerialStream);
uint8_t Channel = (ColourUpdate & 0b11100000);
uint8_t Duty = (ColourUpdate & 0b00011111);
if (Channel & (1 << 5))
SoftPWM_Channel1_Duty = Duty;
if (Channel & (1 << 6))
SoftPWM_Channel2_Duty = Duty;
if (Channel & (1 << 7))
SoftPWM_Channel3_Duty = Duty;
fputc(ColourUpdate, &USBSerialStream);
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();
}
}
void SetupHardware(void)
{
#if (ARCH == ARCH_AVR8)
MCUSR &= ~(1 << WDRF);
wdt_disable();
clock_prescale_set(clock_div_1);
#endif
LEDs_Init();
USB_Init();
OCR0A = 100;
TCCR0A = (1 << WGM01);
TCCR0B = (1 << CS00);
TIMSK0 = (1 << OCIE0A);
}
void EVENT_USB_Device_ConfigurationChanged(void)
{
CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
}
void EVENT_USB_Device_ControlRequest(void)
{
CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
}