Document the Bluetooth ACL layer. Remove unneeded parameters from the signalling...
[pub/USBasp.git] / Projects / RelayBoard / RelayBoard.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 OBinou (obconseil [at] gmail [dot] com)
11 Copyright 2010 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 disclaim 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 for (;;)
49 USB_USBTask();
50 }
51
52 /** Configures the board hardware and chip peripherals for the project's functionality. */
53 void SetupHardware(void)
54 {
55 /* Disable watchdog if enabled by bootloader/fuses */
56 MCUSR &= ~(1 << WDRF);
57 wdt_disable();
58
59 /* Disable clock division */
60 clock_prescale_set(clock_div_1);
61
62 /* Hardware Initialization */
63 USB_Init();
64
65 /* Initialize Relays */
66 DDRC |= ALL_RELAYS;
67 PORTC &= ~ALL_RELAYS;
68 }
69
70
71 /** Event handler for the library USB Configuration Changed event. */
72 void EVENT_USB_Device_ConfigurationChanged(void)
73 {
74 USB_Device_EnableSOFEvents();
75 }
76
77 /** Event handler for the library USB Unhandled Control Packet event. */
78 void EVENT_USB_Device_UnhandledControlRequest(void)
79 {
80 const uint8_t SerialNumber[5] = { 0, 0, 0, 0, 1 };
81 uint8_t ControlData[2] = { 0, 0 };
82
83 switch (USB_ControlRequest.bRequest)
84 {
85 case 0x09:
86 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
87 {
88 LEDs_ToggleLEDs(LEDS_LED1);
89
90 Endpoint_ClearSETUP();
91
92 Endpoint_Read_Control_Stream_LE(ControlData, sizeof(ControlData));
93 Endpoint_ClearIN();
94
95 switch (USB_ControlRequest.wValue)
96 {
97 case 0x303:
98 if (ControlData[1]) PORTC &= ~RELAY1; else PORTC |= RELAY1;
99 break;
100 case 0x306:
101 if (ControlData[1]) PORTC &= ~RELAY2; else PORTC |= RELAY2;
102 break;
103 case 0x309:
104 if (ControlData[1]) PORTC &= ~RELAY3; else PORTC |= RELAY3;
105 break;
106 case 0x30c:
107 if (ControlData[1]) PORTC &= ~RELAY4; else PORTC |= RELAY4;
108 break;
109 }
110 }
111
112 break;
113 case 0x01:
114 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
115 {
116 LEDs_ToggleLEDs(LEDS_LED1);
117
118 Endpoint_ClearSETUP();
119
120 switch (USB_ControlRequest.wValue)
121 {
122 case 0x301:
123 Endpoint_Write_Control_Stream_LE(SerialNumber, sizeof(SerialNumber));
124 break;
125 case 0x303:
126 ControlData[1] = (PORTC & RELAY1) ? 2 : 3;
127 break;
128 case 0x306:
129 ControlData[1] = (PORTC & RELAY2) ? 2 : 3;
130 break;
131 case 0x309:
132 ControlData[1] = (PORTC & RELAY3) ? 2 : 3;
133 break;
134 case 0x30c:
135 ControlData[1] = (PORTC & RELAY4) ? 2 : 3;
136 break;
137 }
138
139 if (ControlData[1])
140 Endpoint_Write_Control_Stream_LE(ControlData, sizeof(ControlData));
141
142 Endpoint_ClearOUT();
143 }
144
145 break;
146 }
147 }