Added new Relay Controller Board project (thanks to OBinou).
[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 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 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
28 this software.
29 */
30
31 /** \file
32 *
33 * Main source file for the RelayBoard program. This file contains the main tasks of
34 * the project and is responsible for the initial application hardware configuration.
35 */
36
37 #include "RelayBoard.h"
38
39
40 /** Main program entry point. This routine contains the overall program flow, including initial
41 * setup of all components and the main program loop.
42 */
43 int main(void)
44 {
45 SetupHardware();
46
47 for (;;)
48 USB_USBTask();
49 }
50
51 /** Configures the board hardware and chip peripherals for the project's functionality. */
52 void SetupHardware(void)
53 {
54 /* Disable watchdog if enabled by bootloader/fuses */
55 MCUSR &= ~(1 << WDRF);
56 wdt_disable();
57
58 /* Disable clock division */
59 clock_prescale_set(clock_div_1);
60
61 /* Hardware Initialization */
62 USB_Init();
63
64 /* Initialize Relays */
65 DDRC |= ALL_RELAYS;
66 PORTC |= ALL_RELAYS;
67 }
68
69
70 /** Event handler for the library USB Configuration Changed event. */
71 void EVENT_USB_Device_ConfigurationChanged(void)
72 {
73 USB_Device_EnableSOFEvents();
74 }
75
76 /** Event handler for the library USB Unhandled Control Packet event. */
77 void EVENT_USB_Device_UnhandledControlRequest(void)
78 {
79 const uint8_t serial[5] = { 0, 0, 0, 0, 1 };
80 uint8_t data[2] = { 0, 0 };
81
82 switch (USB_ControlRequest.bRequest)
83 {
84 case 0x09:
85 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
86 {
87 LEDs_ToggleLEDs(LEDS_LED1);
88
89 Endpoint_ClearSETUP();
90
91 Endpoint_Read_Control_Stream_LE(data, sizeof(data));
92 Endpoint_ClearIN();
93
94 switch (USB_ControlRequest.wValue)
95 {
96 case 0x303:
97 if (data[1]) PORTC |= RELAY1; else PORTC &= ~RELAY1; break;
98 case 0x306:
99 if (data[1]) PORTC |= RELAY2; else PORTC &= ~RELAY2; break;
100 case 0x309:
101 if (data[1]) PORTC |= RELAY3; else PORTC &= ~RELAY3; break;
102 case 0x30c:
103 if (data[1]) PORTC |= RELAY4; else PORTC &= ~RELAY4; break;
104 default:
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(serial, sizeof(serial));
121 break;
122 case 0x303:
123 if (PORTC & RELAY1) data[1]=3; else data[1]=2; break;
124 case 0x306:
125 if (PORTC & RELAY2) data[1]=3; else data[1]=2; break;
126 case 0x309:
127 if (PORTC & RELAY3) data[1]=3; else data[1]=2; break;
128 case 0x30c:
129 if (PORTC & RELAY4) data[1]=3; else data[1]=2; break;
130 default:
131 break;
132 }
133
134 if (data[1])
135 Endpoint_Write_Control_Stream_LE(data, sizeof(data));
136
137 Endpoint_ClearOUT();
138 }
139
140 break;
141 }
142 }