3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
37 #include "RelayBoard.h"
40 /** Main program entry point. This routine contains the overall program flow, including initial
41 * setup of all components and the main program loop.
51 /** Configures the board hardware and chip peripherals for the project's functionality. */
52 void SetupHardware(void)
54 /* Disable watchdog if enabled by bootloader/fuses */
55 MCUSR
&= ~(1 << WDRF
);
58 /* Disable clock division */
59 clock_prescale_set(clock_div_1
);
61 /* Hardware Initialization */
64 /* Initialize Relays */
70 /** Event handler for the library USB Configuration Changed event. */
71 void EVENT_USB_Device_ConfigurationChanged(void)
73 USB_Device_EnableSOFEvents();
76 /** Event handler for the library USB Unhandled Control Packet event. */
77 void EVENT_USB_Device_UnhandledControlRequest(void)
79 const uint8_t serial
[5] = { 0, 0, 0, 0, 1 };
80 uint8_t data
[2] = { 0, 0 };
82 switch (USB_ControlRequest
.bRequest
)
85 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
87 LEDs_ToggleLEDs(LEDS_LED1
);
89 Endpoint_ClearSETUP();
91 Endpoint_Read_Control_Stream_LE(data
, sizeof(data
));
94 switch (USB_ControlRequest
.wValue
)
97 if (data
[1]) PORTC
|= RELAY1
; else PORTC
&= ~RELAY1
; break;
99 if (data
[1]) PORTC
|= RELAY2
; else PORTC
&= ~RELAY2
; break;
101 if (data
[1]) PORTC
|= RELAY3
; else PORTC
&= ~RELAY3
; break;
103 if (data
[1]) PORTC
|= RELAY4
; else PORTC
&= ~RELAY4
; break;
111 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
113 LEDs_ToggleLEDs(LEDS_LED1
);
115 Endpoint_ClearSETUP();
117 switch (USB_ControlRequest
.wValue
)
120 Endpoint_Write_Control_Stream_LE(serial
, sizeof(serial
));
123 if (PORTC
& RELAY1
) data
[1]=3; else data
[1]=2; break;
125 if (PORTC
& RELAY2
) data
[1]=3; else data
[1]=2; break;
127 if (PORTC
& RELAY3
) data
[1]=3; else data
[1]=2; break;
129 if (PORTC
& RELAY4
) data
[1]=3; else data
[1]=2; break;
135 Endpoint_Write_Control_Stream_LE(data
, sizeof(data
));