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