Add partial project documentation to the incomplete PrinterHost demo.
[pub/USBasp.git] / Demos / Host / Incomplete / PrinterHost / PrinterHost.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 /*
32 USB Printer host demo application.
33
34 ** NOT CURRENTLY FUNCTIONAL - DO NOT USE **
35 */
36
37 #include "PrinterHost.h"
38
39
40 int main(void)
41 {
42 SetupHardware();
43
44 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
45
46 puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
47 "Printer Host Demo running.\r\n" ESC_INVERSE_OFF));
48
49 for (;;)
50 {
51 USB_Printer_Host();
52 USB_USBTask();
53 }
54 }
55
56 void SetupHardware(void)
57 {
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
65 /* Hardware Initialization */
66 SerialStream_Init(9600, false);
67 LEDs_Init();
68 USB_Init();
69 }
70
71 void EVENT_USB_DeviceAttached(void)
72 {
73 puts_P(PSTR("Device Attached.\r\n"));
74 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
75 }
76
77 void EVENT_USB_DeviceUnattached(void)
78 {
79 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
80 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
81 }
82
83 void EVENT_USB_HostError(uint8_t ErrorCode)
84 {
85 USB_ShutDown();
86
87 puts_P(PSTR(ESC_BG_RED "Host Mode Error\r\n"));
88 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
89
90 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
91 for(;;);
92 }
93
94 void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)
95 {
96 puts_P(PSTR(ESC_BG_RED "Dev Enum Error\r\n"));
97 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
98 printf_P(PSTR(" -- In State %d\r\n"), USB_HostState);
99
100 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
101 }
102
103 void EVENT_USB_DeviceEnumerationComplete(void)
104 {
105 LEDs_SetAllLEDs(LEDMASK_USB_READY);
106 }
107
108 void USB_Printer_Host(void)
109 {
110 uint8_t ErrorCode;
111
112 switch (USB_HostState)
113 {
114 case HOST_STATE_Addressed:
115 puts_P(PSTR("Getting Config Data.\r\n"));
116
117 /* Select the control pipe for the request transfer */
118 Pipe_SelectPipe(PIPE_CONTROLPIPE);
119
120 /* Get and process the configuration descriptor data */
121 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
122 {
123 if (ErrorCode == ControlError)
124 puts_P(PSTR("Control Error (Get Configuration).\r\n"));
125 else
126 puts_P(PSTR("Invalid Device.\r\n"));
127
128 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
129
130 /* Indicate error via status LEDs */
131 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
132
133 /* Wait until USB device disconnected */
134 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
135 break;
136 }
137
138 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
139 if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
140 {
141 puts_P(PSTR("Control Error (Set Configuration).\r\n"));
142 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
143
144 /* Indicate error via status LEDs */
145 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
146
147 /* Wait until USB device disconnected */
148 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
149 break;
150 }
151
152 /* Some printers use alternate settings to determine the communication protocol used - if so, send a SetInterface
153 * request to switch to the interface alternate setting with the Bidirectional protocol */
154 if (PrinterAltSetting)
155 {
156 USB_ControlRequest = (USB_Request_Header_t)
157 {
158 bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),
159 bRequest: REQ_SetInterface,
160 wValue: PrinterAltSetting,
161 wIndex: PrinterInterfaceNumber,
162 wLength: 0,
163 };
164
165 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
166 {
167 puts_P(PSTR("Control Error (Set Interface).\r\n"));
168 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
169
170 /* Indicate error via status LEDs */
171 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
172
173 /* Wait until USB device disconnected */
174 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
175 break;
176 }
177 }
178
179 USB_HostState = HOST_STATE_Configured;
180 break;
181 case HOST_STATE_Configured:
182 puts_P(PSTR("Retrieving Device ID...\r\n"));
183
184 char DeviceIDString[128];
185 if ((ErrorCode = Printer_GetDeviceID(DeviceIDString, sizeof(DeviceIDString))) != HOST_SENDCONTROL_Successful)
186 {
187 puts_P(PSTR("Control Error (Get DeviceID).\r\n"));
188 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
189
190 /* Indicate error via status LEDs */
191 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
192
193 /* Wait until USB device disconnected */
194 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
195 break;
196 }
197
198 printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString);
199
200 puts_P(PSTR("Printer Enumerated.\r\n"));
201
202 USB_HostState = HOST_STATE_Ready;
203 break;
204 case HOST_STATE_Ready:
205 /* Indicate device busy via the status LEDs */
206 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
207
208 char PCL_Test_Page[] = "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X";
209 // char ESCP2_Test_Page[] = "\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n";
210
211 printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), strlen(PCL_Test_Page));
212
213 if ((ErrorCode = Printer_SendData(PCL_Test_Page)) != PIPE_RWSTREAM_NoError)
214 {
215 puts_P(PSTR("Error Sending Test Page.\r\n"));
216 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
217
218 /* Indicate error via status LEDs */
219 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
220
221 /* Wait until USB device disconnected */
222 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
223 break;
224 }
225
226 puts_P(PSTR("Test Page Sent, Waiting for Pipe...\r\n"));
227
228 /* Indicate device no longer busy */
229 LEDs_SetAllLEDs(LEDMASK_USB_READY);
230
231 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
232 break;
233 }
234 }