3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
32 USB Printer host demo application.
34 ** NOT CURRENTLY FUNCTIONAL - DO NOT USE **
37 #include "PrinterHost.h"
44 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
46 puts_P(PSTR(ESC_FG_CYAN
"Printer Host Demo running.\r\n" ESC_FG_WHITE
));
55 void SetupHardware(void)
57 /* Disable watchdog if enabled by bootloader/fuses */
58 MCUSR
&= ~(1 << WDRF
);
61 /* Disable clock division */
62 clock_prescale_set(clock_div_1
);
64 /* Hardware Initialization */
65 SerialStream_Init(9600, false);
70 void EVENT_USB_DeviceAttached(void)
72 puts_P(PSTR(ESC_FG_GREEN
"Device Attached.\r\n" ESC_FG_WHITE
));
73 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
76 void EVENT_USB_DeviceUnattached(void)
78 puts_P(PSTR(ESC_FG_GREEN
"\r\nDevice Unattached.\r\n" ESC_FG_WHITE
));
79 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
82 void EVENT_USB_HostError(uint8_t ErrorCode
)
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
);
89 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
93 void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode
, uint8_t SubErrorCode
)
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
);
99 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
102 void EVENT_USB_DeviceEnumerationComplete(void)
104 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
107 void USB_Printer_Host(void)
111 switch (USB_HostState
)
113 case HOST_STATE_Addressed
:
114 puts_P(PSTR("Getting Config Data.\r\n"));
116 /* Select the control pipe for the request transfer */
117 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
119 /* Get and process the configuration descriptor data */
120 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
122 if (ErrorCode
== ControlError
)
123 puts_P(PSTR(ESC_FG_RED
"Control Error (Get Configuration).\r\n"));
125 puts_P(PSTR(ESC_FG_RED
"Invalid Device.\r\n"));
127 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
129 /* Indicate error via status LEDs */
130 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
132 /* Wait until USB device disconnected */
133 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
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
)
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
);
143 /* Indicate error via status LEDs */
144 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
146 /* Wait until USB device disconnected */
147 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
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
)
155 USB_ControlRequest
= (USB_Request_Header_t
)
157 bmRequestType
: (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
158 bRequest
: REQ_SetInterface
,
159 wValue
: PrinterAltSetting
,
160 wIndex
: PrinterInterfaceNumber
,
164 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
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
);
169 /* Indicate error via status LEDs */
170 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
172 /* Wait until USB device disconnected */
173 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
178 USB_HostState
= HOST_STATE_Configured
;
180 case HOST_STATE_Configured
:
181 puts_P(PSTR("Retrieving Device ID...\r\n"));
183 char DeviceIDString
[256];
184 if ((ErrorCode
= Printer_GetDeviceID(DeviceIDString
, sizeof(DeviceIDString
))) != HOST_SENDCONTROL_Successful
)
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
);
189 /* Indicate error via status LEDs */
190 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
192 /* Wait until USB device disconnected */
193 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
197 printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString
);
199 puts_P(PSTR("Printer Enumerated.\r\n"));
201 USB_HostState
= HOST_STATE_Ready
;
203 case HOST_STATE_Ready
:
204 /* Indicate device busy via the status LEDs */
205 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
207 Printer_Data_t TestPageData
=
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)
214 printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), TestPageData
.Length
);
216 if ((ErrorCode
= Printer_SendData(&TestPageData
)) != PIPE_RWSTREAM_NoError
)
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
);
221 /* Indicate error via status LEDs */
222 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
224 /* Wait until USB device disconnected */
225 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
229 puts_P(PSTR("Test Page Sent.\r\n"));
231 /* Indicate device no longer busy */
232 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
234 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;