-/*\r
- LUFA Library\r
- Copyright (C) Dean Camera, 2009.\r
- \r
- dean [at] fourwalledcubicle [dot] com\r
- www.fourwalledcubicle.com\r
-*/\r
-\r
-/*\r
- Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
- Permission to use, copy, modify, and distribute this software\r
- and its documentation for any purpose and without fee is hereby\r
- granted, provided that the above copyright notice appear in all\r
- copies and that both that the copyright notice and this\r
- permission notice and warranty disclaimer appear in supporting\r
- documentation, and that the name of the author not be used in\r
- advertising or publicity pertaining to distribution of the\r
- software without specific, written prior permission.\r
-\r
- The author disclaim all warranties with regard to this\r
- software, including all implied warranties of merchantability\r
- and fitness. In no event shall the author be liable for any\r
- special, indirect or consequential damages or any damages\r
- whatsoever resulting from loss of use, data or profits, whether\r
- in an action of contract, negligence or other tortious action,\r
- arising out of or in connection with the use or performance of\r
- this software.\r
-*/\r
-\r
-/*\r
- USB Printer host demo application.\r
- \r
- ** NOT CURRENTLY FUNCTIONAL - DO NOT USE **\r
-*/\r
-\r
-#include "PrinterHost.h"\r
-\r
-\r
-int main(void)\r
-{\r
- SetupHardware();\r
-\r
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
-\r
- puts_P(PSTR(ESC_FG_CYAN "Printer Host Demo running.\r\n" ESC_FG_WHITE));\r
- \r
- for (;;)\r
- {\r
- USB_Printer_Host();\r
- USB_USBTask();\r
- }\r
-}\r
-\r
-void SetupHardware(void)\r
-{\r
- /* Disable watchdog if enabled by bootloader/fuses */\r
- MCUSR &= ~(1 << WDRF);\r
- wdt_disable();\r
-\r
- /* Disable clock division */\r
- clock_prescale_set(clock_div_1);\r
-\r
- /* Hardware Initialization */\r
- SerialStream_Init(9600, false);\r
- LEDs_Init();\r
- USB_Init();\r
-}\r
-\r
-void EVENT_USB_DeviceAttached(void)\r
-{\r
- puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));\r
- LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
-}\r
-\r
-void EVENT_USB_DeviceUnattached(void)\r
-{\r
- puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE));\r
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
-}\r
-\r
-void EVENT_USB_HostError(uint8_t ErrorCode)\r
-{\r
- USB_ShutDown();\r
-\r
- puts_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"));\r
- printf_P(PSTR(" -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);\r
-\r
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
- for(;;);\r
-}\r
-\r
-void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)\r
-{\r
- puts_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"));\r
- printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);\r
- printf_P(PSTR(" -- In State %d\r\n" ESC_FG_WHITE), USB_HostState);\r
-\r
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
-}\r
-\r
-void EVENT_USB_DeviceEnumerationComplete(void)\r
-{\r
- LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
-}\r
-\r
-void USB_Printer_Host(void)\r
-{\r
- uint8_t ErrorCode;\r
-\r
- switch (USB_HostState)\r
- {\r
- case HOST_STATE_Addressed:\r
- puts_P(PSTR("Getting Config Data.\r\n"));\r
- \r
- /* Select the control pipe for the request transfer */\r
- Pipe_SelectPipe(PIPE_CONTROLPIPE); \r
- \r
- /* Get and process the configuration descriptor data */\r
- if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)\r
- {\r
- if (ErrorCode == ControlError)\r
- puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));\r
- else\r
- puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));\r
-\r
- printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
- \r
- /* Indicate error via status LEDs */\r
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
-\r
- /* Wait until USB device disconnected */\r
- USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
- break;\r
- }\r
- \r
- /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */\r
- if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)\r
- {\r
- puts_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"));\r
- printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
-\r
- /* Indicate error via status LEDs */\r
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
-\r
- /* Wait until USB device disconnected */\r
- USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
- break;\r
- }\r
- \r
- /* Some printers use alternate settings to determine the communication protocol used - if so, send a SetInterface\r
- * request to switch to the interface alternate setting with the Bidirectional protocol */\r
- if (PrinterAltSetting)\r
- {\r
- USB_ControlRequest = (USB_Request_Header_t)\r
- {\r
- bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),\r
- bRequest: REQ_SetInterface,\r
- wValue: PrinterAltSetting,\r
- wIndex: PrinterInterfaceNumber,\r
- wLength: 0,\r
- };\r
- \r
- if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)\r
- {\r
- puts_P(PSTR(ESC_FG_RED "Control Error (Set Interface).\r\n"));\r
- printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
-\r
- /* Indicate error via status LEDs */\r
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
-\r
- /* Wait until USB device disconnected */\r
- USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
- break; \r
- }\r
- }\r
- \r
- USB_HostState = HOST_STATE_Configured;\r
- break;\r
- case HOST_STATE_Configured:\r
- puts_P(PSTR("Retrieving Device ID...\r\n"));\r
- \r
- char DeviceIDString[256];\r
- if ((ErrorCode = Printer_GetDeviceID(DeviceIDString, sizeof(DeviceIDString))) != HOST_SENDCONTROL_Successful)\r
- {\r
- puts_P(PSTR(ESC_FG_RED "Control Error (Get DeviceID).\r\n"));\r
- printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
-\r
- /* Indicate error via status LEDs */\r
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
-\r
- /* Wait until USB device disconnected */\r
- USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
- break;\r
- }\r
-\r
- printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString);\r
-\r
- puts_P(PSTR("Printer Enumerated.\r\n"));\r
- \r
- USB_HostState = HOST_STATE_Ready;\r
- break;\r
- case HOST_STATE_Ready:\r
- /* Indicate device busy via the status LEDs */\r
- LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
- \r
- Printer_Data_t TestPageData =\r
- {\r
-// "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X",\r
- "\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n",\r
- (sizeof(TestPageData.Data) - 1)\r
- };\r
- \r
- printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), TestPageData.Length);\r
-\r
- if ((ErrorCode = Printer_SendData(&TestPageData)) != PIPE_RWSTREAM_NoError)\r
- {\r
- puts_P(PSTR(ESC_FG_RED "Error Sending Test Page.\r\n"));\r
- printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
-\r
- /* Indicate error via status LEDs */\r
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
-\r
- /* Wait until USB device disconnected */\r
- USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
- break;\r
- }\r
-\r
- puts_P(PSTR("Test Page Sent.\r\n"));\r
- \r
- /* Indicate device no longer busy */\r
- LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
-\r
- USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
- break;\r
- }\r
-}\r