]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Projects/RelayBoard/RelayBoard.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Projects / RelayBoard / RelayBoard.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 OBinou (obconseil [at] gmail [dot] com)
11 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12
13 Permission to use, copy, modify, distribute, and sell this
14 software and its documentation for any purpose is hereby granted
15 without fee, provided that the above copyright notice appear in
16 all copies and that both that the copyright notice and this
17 permission notice and warranty disclaimer appear in supporting
18 documentation, and that the name of the author not be used in
19 advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
21
22 The author disclaims all warranties with regard to this
23 software, including all implied warranties of merchantability
24 and fitness. In no event shall the author be liable for any
25 special, indirect or consequential damages or any damages
26 whatsoever resulting from loss of use, data or profits, whether
27 in an action of contract, negligence or other tortious action,
28 arising out of or in connection with the use or performance of
29 this software.
30 */
31
32 /** \file
33 *
34 * Main source file for the RelayBoard program. This file contains the main tasks of
35 * the project and is responsible for the initial application hardware configuration.
36 */
37
38 #include "RelayBoard.h"
39
40
41 /** Main program entry point. This routine contains the overall program flow, including initial
42 * setup of all components and the main program loop.
43 */
44 int main(void)
45 {
46 SetupHardware();
47
48 GlobalInterruptEnable();
49
50 for (;;)
51 USB_USBTask();
52 }
53
54 /** Configures the board hardware and chip peripherals for the project's functionality. */
55 void SetupHardware(void)
56 {
57 #if (ARCH == ARCH_AVR8)
58 /* Disable watchdog if enabled by bootloader/fuses */
59 MCUSR &= ~(1 << WDRF);
60 wdt_disable();
61
62 /* Disable clock division */
63 clock_prescale_set(clock_div_1);
64 #endif
65
66 /* Hardware Initialization */
67 USB_Init();
68
69 /* Initialize Relays */
70 DDRC |= ALL_RELAYS;
71 PORTC &= ~ALL_RELAYS;
72 }
73
74 /** Event handler for the library USB Control Request reception event. */
75 void EVENT_USB_Device_ControlRequest(void)
76 {
77 const uint8_t SerialNumber[5] = { 0, 0, 0, 0, 1 };
78 uint8_t ControlData[2] = { 0, 0 };
79
80 switch (USB_ControlRequest.bRequest)
81 {
82 case 0x09:
83 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
84 {
85 LEDs_ToggleLEDs(LEDS_LED1);
86
87 Endpoint_ClearSETUP();
88
89 Endpoint_Read_Control_Stream_LE(ControlData, sizeof(ControlData));
90 Endpoint_ClearIN();
91
92 switch (USB_ControlRequest.wValue)
93 {
94 case 0x303:
95 if (ControlData[1]) PORTC &= ~RELAY1; else PORTC |= RELAY1;
96 break;
97 case 0x306:
98 if (ControlData[1]) PORTC &= ~RELAY2; else PORTC |= RELAY2;
99 break;
100 case 0x309:
101 if (ControlData[1]) PORTC &= ~RELAY3; else PORTC |= RELAY3;
102 break;
103 case 0x30c:
104 if (ControlData[1]) PORTC &= ~RELAY4; else PORTC |= RELAY4;
105 break;
106 }
107 }
108
109 break;
110 case 0x01:
111 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
112 {
113 LEDs_ToggleLEDs(LEDS_LED1);
114
115 Endpoint_ClearSETUP();
116
117 switch (USB_ControlRequest.wValue)
118 {
119 case 0x301:
120 Endpoint_Write_Control_Stream_LE(SerialNumber, sizeof(SerialNumber));
121 break;
122 case 0x303:
123 ControlData[1] = (PORTC & RELAY1) ? 2 : 3;
124 break;
125 case 0x306:
126 ControlData[1] = (PORTC & RELAY2) ? 2 : 3;
127 break;
128 case 0x309:
129 ControlData[1] = (PORTC & RELAY3) ? 2 : 3;
130 break;
131 case 0x30c:
132 ControlData[1] = (PORTC & RELAY4) ? 2 : 3;
133 break;
134 }
135
136 if (ControlData[1])
137 Endpoint_Write_Control_Stream_LE(ControlData, sizeof(ControlData));
138
139 Endpoint_ClearOUT();
140 }
141
142 break;
143 }
144 }
145
Imprint / Impressum