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"
39 uint8_t PrinterProtocol
;
46 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
48 puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
49 "Printer Host Demo running.\r\n" ESC_INVERSE_OFF
));
58 void SetupHardware(void)
60 /* Disable watchdog if enabled by bootloader/fuses */
61 MCUSR
&= ~(1 << WDRF
);
64 /* Disable clock division */
65 clock_prescale_set(clock_div_1
);
67 /* Hardware Initialization */
68 SerialStream_Init(9600, false);
73 void EVENT_USB_DeviceAttached(void)
75 puts_P(PSTR("Device Attached.\r\n"));
76 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
79 void EVENT_USB_DeviceUnattached(void)
81 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
82 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
85 void EVENT_USB_HostError(uint8_t ErrorCode
)
89 puts_P(PSTR(ESC_BG_RED
"Host Mode Error\r\n"));
90 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
92 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
96 void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode
, uint8_t SubErrorCode
)
98 puts_P(PSTR(ESC_BG_RED
"Dev Enum Error\r\n"));
99 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
100 printf_P(PSTR(" -- In State %d\r\n"), USB_HostState
);
102 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
105 void EVENT_USB_DeviceEnumerationComplete(void)
107 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
110 void USB_Printer_Host(void)
114 switch (USB_HostState
)
116 case HOST_STATE_Addressed
:
117 puts_P(PSTR("Getting Config Data.\r\n"));
119 /* Select the control pipe for the request transfer */
120 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
122 /* Get and process the configuration descriptor data */
123 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
125 if (ErrorCode
== ControlError
)
126 puts_P(PSTR("Control Error (Get Configuration).\r\n"));
128 puts_P(PSTR("Invalid Device.\r\n"));
130 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
132 /* Indicate error via status LEDs */
133 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
135 /* Wait until USB device disconnected */
136 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
140 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
141 if ((ErrorCode
= USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful
)
143 puts_P(PSTR("Control Error (Set Configuration).\r\n"));
144 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
146 /* Indicate error via status LEDs */
147 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
149 /* Wait until USB device disconnected */
150 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
154 USB_HostState
= HOST_STATE_Configured
;
156 case HOST_STATE_Configured
:
157 printf_P(PSTR("Printer Protocol: %d\r\n"), PrinterProtocol
);
159 puts_P(PSTR("Retrieving Device ID...\r\n"));
161 Device_ID_String_t DeviceIDString
;
162 if ((ErrorCode
= Printer_GetDeviceID(&DeviceIDString
)) != HOST_SENDCONTROL_Successful
)
164 puts_P(PSTR("Control Error (Get DeviceID).\r\n"));
165 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
167 /* Indicate error via status LEDs */
168 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
170 /* Wait until USB device disconnected */
171 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
175 printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString
.String
);
177 puts_P(PSTR("Printer Enumerated.\r\n"));
179 USB_HostState
= HOST_STATE_Ready
;
181 case HOST_STATE_Ready
:
182 /* Indicate device busy via the status LEDs */
183 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
185 //--------------------------------------------------------------
186 #define TEST_TEXT_PAGE "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X"
187 // #define TEST_TEXT_PAGE "\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n"
188 #define PAGE_SIZE (sizeof(TEST_TEXT_PAGE) - 1)
190 Pipe_SelectPipe(PRINTER_DATA_OUT_PIPE
);
193 puts_P(PSTR("Waiting for Printer to Become Ready...\r\n"));
195 while (!(Pipe_IsReadWriteAllowed()));
197 printf_P(PSTR("Printer Write Allowed, Sending Page (%d bytes)...\r\n"), PAGE_SIZE
);
199 Pipe_Write_Stream_LE(TEST_TEXT_PAGE
, PAGE_SIZE
);
202 puts_P(PSTR("Page Sent, Waiting for Pipe...\r\n"));
204 while (!(Pipe_IsReadWriteAllowed()));
207 puts_P(PSTR("Pipe Frozen.\r\n"));
208 //--------------------------------------------------------------
210 /* Indicate device no longer busy */
211 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
213 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;