]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Demos/Host/LowLevel/PrinterHost/Lib/PrinterCommands.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Demos / Host / LowLevel / PrinterHost / Lib / PrinterCommands.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 disclaims 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 /** \file
32 *
33 * Printer Device commands, to send/receive data to and from an attached USB
34 * printer, and to send and receive Printer Class control requests.
35 */
36
37 #include "PrinterCommands.h"
38
39 /** Sends the given data directly to the printer via the data endpoints, for the sending of print commands in printer
40 * languages accepted by the attached printer (e.g. PCL).
41 *
42 * \param[in] PrinterCommands Pointer to the data to send to the attached printer
43 * \param[in] CommandSize Size of the data to send to the attached printer
44 *
45 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
46 */
47 uint8_t Printer_SendData(const void* const PrinterCommands,
48 const uint16_t CommandSize)
49 {
50 uint8_t ErrorCode;
51
52 Pipe_SelectPipe(PRINTER_DATA_OUT_PIPE);
53 Pipe_Unfreeze();
54
55 if ((ErrorCode = Pipe_Write_Stream_LE(PrinterCommands, CommandSize, NULL)) != PIPE_RWSTREAM_NoError)
56 return ErrorCode;
57
58 Pipe_ClearOUT();
59
60 Pipe_WaitUntilReady();
61
62 Pipe_Freeze();
63
64 return PIPE_RWSTREAM_NoError;
65 }
66
67 /** Issues a Printer class Get Device ID command to the attached device, to retrieve the device ID string (which indicates
68 * the accepted printer languages, the printer's model and other pertinent information).
69 *
70 * \param[out] DeviceIDString Pointer to the destination where the returned string should be stored
71 * \param[in] BufferSize Size in bytes of the allocated buffer for the returned Device ID string
72 *
73 * \return A value from the USB_Host_SendControlErrorCodes_t enum
74 */
75 uint8_t Printer_GetDeviceID(char* DeviceIDString,
76 const uint16_t BufferSize)
77 {
78 uint8_t ErrorCode;
79 uint16_t DeviceIDStringLength = 0;
80
81 USB_ControlRequest = (USB_Request_Header_t)
82 {
83 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
84 .bRequest = PRNT_REQ_GetDeviceID,
85 .wValue = 0,
86 .wIndex = PrinterInterfaceNumber,
87 .wLength = sizeof(DeviceIDStringLength),
88 };
89
90 Pipe_SelectPipe(PIPE_CONTROLPIPE);
91
92 if ((ErrorCode = USB_Host_SendControlRequest(&DeviceIDStringLength)) != HOST_SENDCONTROL_Successful)
93 return ErrorCode;
94
95 if (!(DeviceIDStringLength))
96 {
97 DeviceIDString[0] = 0x00;
98 return HOST_SENDCONTROL_Successful;
99 }
100
101 DeviceIDStringLength = SwapEndian_16(DeviceIDStringLength);
102
103 if (DeviceIDStringLength > BufferSize)
104 DeviceIDStringLength = BufferSize;
105
106 USB_ControlRequest.wLength = DeviceIDStringLength;
107
108 if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)
109 return ErrorCode;
110
111 /* Move string back two characters to remove the string length value from the start of the array */
112 memmove(&DeviceIDString[0], &DeviceIDString[2], DeviceIDStringLength - 2);
113
114 DeviceIDString[DeviceIDStringLength - 2] = 0x00;
115
116 return HOST_SENDCONTROL_Successful;
117 }
118
119 /** Issues a Printer class Get Port Status command to the attached device, to retrieve the current status flags of the
120 * printer.
121 *
122 * \param[out] PortStatus Pointer to the destination where the printer's status flag values should be stored
123 *
124 * \return A value from the USB_Host_SendControlErrorCodes_t enum
125 */
126 uint8_t Printer_GetPortStatus(uint8_t* const PortStatus)
127 {
128 USB_ControlRequest = (USB_Request_Header_t)
129 {
130 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
131 .bRequest = PRNT_REQ_GetPortStatus,
132 .wValue = 0,
133 .wIndex = PrinterInterfaceNumber,
134 .wLength = sizeof(uint8_t),
135 };
136
137 Pipe_SelectPipe(PIPE_CONTROLPIPE);
138
139 return USB_Host_SendControlRequest(PortStatus);
140 }
141
142 /** Issues a Printer class Soft Reset command to the attached device, to reset the printer ready for new input without
143 * physically cycling the printer's power.
144 *
145 * \return A value from the USB_Host_SendControlErrorCodes_t enum
146 */
147 uint8_t Printer_SoftReset(void)
148 {
149 USB_ControlRequest = (USB_Request_Header_t)
150 {
151 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
152 .bRequest = PRNT_REQ_SoftReset,
153 .wValue = 0,
154 .wIndex = PrinterInterfaceNumber,
155 .wLength = 0,
156 };
157
158 Pipe_SelectPipe(PIPE_CONTROLPIPE);
159
160 return USB_Host_SendControlRequest(NULL);
161 }
162
Imprint / Impressum