Fixed PrinterHost demo Printer_GetDeviceID() routine not removing the Device ID strin...
[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_FG_CYAN "Printer Host Demo running.\r\n" ESC_FG_WHITE));
47
48 for (;;)
49 {
50 USB_Printer_Host();
51 USB_USBTask();
52 }
53 }
54
55 void SetupHardware(void)
56 {
57 /* Disable watchdog if enabled by bootloader/fuses */
58 MCUSR &= ~(1 << WDRF);
59 wdt_disable();
60
61 /* Disable clock division */
62 clock_prescale_set(clock_div_1);
63
64 /* Hardware Initialization */
65 SerialStream_Init(9600, false);
66 LEDs_Init();
67 USB_Init();
68 }
69
70 void EVENT_USB_DeviceAttached(void)
71 {
72 puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
73 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
74 }
75
76 void EVENT_USB_DeviceUnattached(void)
77 {
78 puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE));
79 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
80 }
81
82 void EVENT_USB_HostError(uint8_t ErrorCode)
83 {
84 USB_ShutDown();
85
86 puts_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"));
87 printf_P(PSTR(" -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
88
89 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
90 for(;;);
91 }
92
93 void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)
94 {
95 puts_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"));
96 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
97 printf_P(PSTR(" -- In State %d\r\n" ESC_FG_WHITE), USB_HostState);
98
99 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
100 }
101
102 void EVENT_USB_DeviceEnumerationComplete(void)
103 {
104 LEDs_SetAllLEDs(LEDMASK_USB_READY);
105 }
106
107 void USB_Printer_Host(void)
108 {
109 uint8_t ErrorCode;
110
111 switch (USB_HostState)
112 {
113 case HOST_STATE_Addressed:
114 puts_P(PSTR("Getting Config Data.\r\n"));
115
116 /* Select the control pipe for the request transfer */
117 Pipe_SelectPipe(PIPE_CONTROLPIPE);
118
119 /* Get and process the configuration descriptor data */
120 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
121 {
122 if (ErrorCode == ControlError)
123 puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
124 else
125 puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
126
127 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
128
129 /* Indicate error via status LEDs */
130 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
131
132 /* Wait until USB device disconnected */
133 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
134 break;
135 }
136
137 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
138 if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
139 {
140 puts_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"));
141 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
142
143 /* Indicate error via status LEDs */
144 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
145
146 /* Wait until USB device disconnected */
147 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
148 break;
149 }
150
151 /* Some printers use alternate settings to determine the communication protocol used - if so, send a SetInterface
152 * request to switch to the interface alternate setting with the Bidirectional protocol */
153 if (PrinterAltSetting)
154 {
155 USB_ControlRequest = (USB_Request_Header_t)
156 {
157 bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),
158 bRequest: REQ_SetInterface,
159 wValue: PrinterAltSetting,
160 wIndex: PrinterInterfaceNumber,
161 wLength: 0,
162 };
163
164 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
165 {
166 puts_P(PSTR(ESC_FG_RED "Control Error (Set Interface).\r\n"));
167 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
168
169 /* Indicate error via status LEDs */
170 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
171
172 /* Wait until USB device disconnected */
173 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
174 break;
175 }
176 }
177
178 USB_HostState = HOST_STATE_Configured;
179 break;
180 case HOST_STATE_Configured:
181 puts_P(PSTR("Retrieving Device ID...\r\n"));
182
183 char DeviceIDString[256];
184 if ((ErrorCode = Printer_GetDeviceID(DeviceIDString, sizeof(DeviceIDString))) != HOST_SENDCONTROL_Successful)
185 {
186 puts_P(PSTR(ESC_FG_RED "Control Error (Get DeviceID).\r\n"));
187 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
188
189 /* Indicate error via status LEDs */
190 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
191
192 /* Wait until USB device disconnected */
193 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
194 break;
195 }
196
197 printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString);
198
199 puts_P(PSTR("Printer Enumerated.\r\n"));
200
201 USB_HostState = HOST_STATE_Ready;
202 break;
203 case HOST_STATE_Ready:
204 /* Indicate device busy via the status LEDs */
205 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
206
207 Printer_Data_t TestPageData =
208 {
209 // "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X",
210 "\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n",
211 (sizeof(TestPageData.Data) - 1)
212 };
213
214 printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), TestPageData.Length);
215
216 if ((ErrorCode = Printer_SendData(&TestPageData)) != PIPE_RWSTREAM_NoError)
217 {
218 puts_P(PSTR(ESC_FG_RED "Error Sending Test Page.\r\n"));
219 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
220
221 /* Indicate error via status LEDs */
222 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
223
224 /* Wait until USB device disconnected */
225 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
226 break;
227 }
228
229 puts_P(PSTR("Test Page Sent.\r\n"));
230
231 /* Indicate device no longer busy */
232 LEDs_SetAllLEDs(LEDMASK_USB_READY);
233
234 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
235 break;
236 }
237 }