Update copyrights for 2018.
[pub/USBasp.git] / Projects / LEDNotifier / LEDNotifier.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2018.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
21 The author disclaims 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
28 this software.
29 */
30
31 /** \file
32 *
33 * Main source file for the LEDNotfier project. This file contains the main tasks of
34 * the project and is responsible for the initial application hardware configuration.
35 */
36
37 #include "LEDNotifier.h"
38
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.
42 */
43 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
44 {
45 .Config =
46 {
47 .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
48 .DataINEndpoint =
49 {
50 .Address = CDC_TX_EPADDR,
51 .Size = CDC_TXRX_EPSIZE,
52 .Banks = 1,
53 },
54 .DataOUTEndpoint =
55 {
56 .Address = CDC_RX_EPADDR,
57 .Size = CDC_TXRX_EPSIZE,
58 .Banks = 1,
59 },
60 .NotificationEndpoint =
61 {
62 .Address = CDC_NOTIFICATION_EPADDR,
63 .Size = CDC_NOTIFICATION_EPSIZE,
64 .Banks = 1,
65 },
66 },
67 };
68
69 /** Counter for the software PWM. */
70 static volatile uint8_t SoftPWM_Count;
71
72 /** Duty cycle for the first software PWM channel. */
73 static volatile uint8_t SoftPWM_Channel1_Duty;
74
75 /** Duty cycle for the second software PWM channel. */
76 static volatile uint8_t SoftPWM_Channel2_Duty;
77
78 /** Duty cycle for the third software PWM channel. */
79 static volatile uint8_t SoftPWM_Channel3_Duty;
80
81 /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
82 * used like any regular character stream in the C APIs.
83 */
84 static FILE USBSerialStream;
85
86
87 /** Interrupt handler for managing the software PWM channels for the LEDs */
88 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
89 {
90 uint8_t LEDMask = LEDS_ALL_LEDS;
91
92 if (++SoftPWM_Count == 0b00011111)
93 SoftPWM_Count = 0;
94
95 if (SoftPWM_Count >= SoftPWM_Channel1_Duty)
96 LEDMask &= ~LEDS_LED1;
97
98 if (SoftPWM_Count >= SoftPWM_Channel2_Duty)
99 LEDMask &= ~LEDS_LED2;
100
101 if (SoftPWM_Count >= SoftPWM_Channel3_Duty)
102 LEDMask &= ~LEDS_LED3;
103
104 LEDs_SetAllLEDs(LEDMask);
105 }
106
107 /** Main program entry point. This routine contains the overall program flow, including initial
108 * setup of all components and the main program loop.
109 */
110 int main(void)
111 {
112 SetupHardware();
113
114 /* Create a regular blocking character stream for the interface so that it can be used with the stdio.h functions */
115 CDC_Device_CreateBlockingStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
116
117 GlobalInterruptEnable();
118
119 for (;;)
120 {
121 /* Read in next LED colour command from the host */
122 uint8_t ColourUpdate = fgetc(&USBSerialStream);
123
124 /* Top 3 bits select the LED, bottom 5 control the brightness */
125 uint8_t Channel = (ColourUpdate & 0b11100000);
126 uint8_t Duty = (ColourUpdate & 0b00011111);
127
128 if (Channel & (1 << 5))
129 SoftPWM_Channel1_Duty = Duty;
130
131 if (Channel & (1 << 6))
132 SoftPWM_Channel2_Duty = Duty;
133
134 if (Channel & (1 << 7))
135 SoftPWM_Channel3_Duty = Duty;
136
137 fputc(&USBSerialStream, ColourUpdate);
138
139 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
140 USB_USBTask();
141 }
142 }
143
144 /** Configures the board hardware and chip peripherals for the demo's functionality. */
145 void SetupHardware(void)
146 {
147 #if (ARCH == ARCH_AVR8)
148 /* Disable watchdog if enabled by bootloader/fuses */
149 MCUSR &= ~(1 << WDRF);
150 wdt_disable();
151
152 /* Disable clock division */
153 clock_prescale_set(clock_div_1);
154 #endif
155
156 /* Hardware Initialization */
157 LEDs_Init();
158 USB_Init();
159
160 /* Timer Initialization */
161 OCR0A = 100;
162 TCCR0A = (1 << WGM01);
163 TCCR0B = (1 << CS00);
164 TIMSK0 = (1 << OCIE0A);
165 }
166
167 /** Event handler for the library USB Configuration Changed event. */
168 void EVENT_USB_Device_ConfigurationChanged(void)
169 {
170 CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
171 }
172
173 /** Event handler for the library USB Control Request reception event. */
174 void EVENT_USB_Device_ControlRequest(void)
175 {
176 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
177 }
178