87651e9060f0ff3c812108a3d37f84ae6be5f258
[pub/USBasp.git] / Demos / Host / Incomplete / PrinterHost / Lib / PrinterCommands.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 #include "PrinterCommands.h"
32
33 /** Sends the given data directly to the printer via the data endpoints, for the sending of print commands in printer
34 * languages accepted by the attached printer (e.g. PCL).
35 *
36 * \param[in] PrinterCommands Pointer to a structure containing the commands and length of the data to send
37 *
38 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
39 */
40 uint8_t Printer_SendData(Printer_Data_t* PrinterCommands)
41 {
42 uint8_t ErrorCode;
43
44 Pipe_SelectPipe(PRINTER_DATA_OUT_PIPE);
45 Pipe_Unfreeze();
46
47 if ((ErrorCode = Pipe_Write_Stream_LE(PrinterCommands->Data, PrinterCommands->Length)) != PIPE_RWSTREAM_NoError)
48 return ErrorCode;
49
50 Pipe_ClearOUT();
51 while (!(Pipe_IsOUTReady()));
52
53 Pipe_Freeze();
54
55 return PIPE_RWSTREAM_NoError;
56 }
57
58 /** Issues a Printer class Get Device ID command to the attached device, to retrieve the device ID string (which indicates
59 * the accepted printer languages, the printer's model and other pertinent information).
60 *
61 * \param[out] DeviceIDString Pointer to the destination where the returned string should be stored
62 * \param[in] BufferSize Size in bytes of the allocated buffer for the returned Device ID string
63 *
64 * \return A value from the USB_Host_SendControlErrorCodes_t enum
65 */
66 uint8_t Printer_GetDeviceID(char* DeviceIDString, uint8_t BufferSize)
67 {
68 uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
69 uint16_t DeviceIDStringLength;
70
71 USB_ControlRequest = (USB_Request_Header_t)
72 {
73 bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
74 bRequest: GET_DEVICE_ID,
75 wValue: 0,
76 wIndex: 0,
77 wLength: sizeof(DeviceIDStringLength),
78 };
79
80 if ((ErrorCode = USB_Host_SendControlRequest(&DeviceIDStringLength)) != HOST_SENDCONTROL_Successful)
81 return ErrorCode;
82
83 DeviceIDStringLength = SwapEndian_16(DeviceIDStringLength);
84
85 if (DeviceIDStringLength > BufferSize)
86 DeviceIDStringLength = BufferSize;
87
88 USB_ControlRequest.wLength = (DeviceIDStringLength - 1);
89
90 if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)
91 return ErrorCode;
92
93 DeviceIDString[DeviceIDStringLength] = 0x00;
94
95 return HOST_SENDCONTROL_Successful;
96 }
97
98 /** Issues a Printer class Get Port Status command to the attached device, to retrieve the current status flags of the
99 * printer.
100 *
101 * \param[out] PortStatus Pointer to the destination where the printer's status flag values should be stored
102 *
103 * \return A value from the USB_Host_SendControlErrorCodes_t enum
104 */
105 uint8_t Printer_GetPortStatus(uint8_t* PortStatus)
106 {
107 USB_ControlRequest = (USB_Request_Header_t)
108 {
109 bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
110 bRequest: GET_PORT_STATUS,
111 wValue: 0,
112 wIndex: 0,
113 wLength: sizeof(uint8_t),
114 };
115
116 return USB_Host_SendControlRequest(PortStatus);
117 }
118
119 /** Issues a Printer class Soft Reset command to the attached device, to reset the printer ready for new input without
120 * physically cycling the printer's power.
121 *
122 * \return A value from the USB_Host_SendControlErrorCodes_t enum
123 */
124 uint8_t Printer_SoftReset(void)
125 {
126 USB_ControlRequest = (USB_Request_Header_t)
127 {
128 bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
129 bRequest: SOFT_RESET,
130 wValue: 0,
131 wIndex: 0,
132 wLength: 0,
133 };
134
135 return USB_Host_SendControlRequest(NULL);
136 }
137