3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  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. 
  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 
  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. 
  37 #include "PrinterCommands.h" 
  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). 
  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 
  45  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum 
  47 uint8_t Printer_SendData(void* PrinterCommands
, uint16_t CommandSize
) 
  51         Pipe_SelectPipe(PRINTER_DATA_OUT_PIPE
); 
  54         if ((ErrorCode 
= Pipe_Write_Stream_LE(PrinterCommands
, CommandSize
)) != PIPE_RWSTREAM_NoError
) 
  58         while (!(Pipe_IsOUTReady())) 
  60                 if (USB_HostState 
== HOST_STATE_Unattached
) 
  61                   return PIPE_RWSTREAM_DeviceDisconnected
; 
  66         return PIPE_RWSTREAM_NoError
; 
  69 /** Issues a Printer class Get Device ID command to the attached device, to retrieve the device ID string (which indicates 
  70  *  the accepted printer languages, the printer's model and other pertinent information). 
  72  *  \param[out] DeviceIDString Pointer to the destination where the returned string should be stored 
  73  *  \param[in] BufferSize  Size in bytes of the allocated buffer for the returned Device ID string 
  75  *  \return A value from the USB_Host_SendControlErrorCodes_t enum 
  77 uint8_t Printer_GetDeviceID(char* DeviceIDString
, uint16_t BufferSize
) 
  79         uint8_t  ErrorCode 
= HOST_SENDCONTROL_Successful
; 
  80         uint16_t DeviceIDStringLength 
= 0; 
  82         USB_ControlRequest 
= (USB_Request_Header_t
) 
  84                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
  85                         .bRequest      
= REQ_GetDeviceID
, 
  88                         .wLength       
= sizeof(DeviceIDStringLength
), 
  91         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
  93         if ((ErrorCode 
= USB_Host_SendControlRequest(&DeviceIDStringLength
)) != HOST_SENDCONTROL_Successful
) 
  96         if (!(DeviceIDStringLength
)) 
  98                 DeviceIDString
[0] = 0x00; 
  99                 return HOST_SENDCONTROL_Successful
; 
 102         DeviceIDStringLength 
= SwapEndian_16(DeviceIDStringLength
); 
 104         if (DeviceIDStringLength 
> BufferSize
) 
 105           DeviceIDStringLength 
= BufferSize
; 
 107         USB_ControlRequest
.wLength 
= DeviceIDStringLength
; 
 109         if ((ErrorCode 
= USB_Host_SendControlRequest(DeviceIDString
)) != HOST_SENDCONTROL_Successful
) 
 112         /* Move string back two characters to remove the string length value from the start of the array */ 
 113         memmove(&DeviceIDString
[0], &DeviceIDString
[2], DeviceIDStringLength 
- 2); 
 115         DeviceIDString
[DeviceIDStringLength 
- 2] = 0x00; 
 117         return HOST_SENDCONTROL_Successful
; 
 120 /** Issues a Printer class Get Port Status command to the attached device, to retrieve the current status flags of the 
 123  *  \param[out] PortStatus  Pointer to the destination where the printer's status flag values should be stored 
 125  *  \return A value from the USB_Host_SendControlErrorCodes_t enum 
 127 uint8_t Printer_GetPortStatus(uint8_t* PortStatus
) 
 129         USB_ControlRequest 
= (USB_Request_Header_t
) 
 131                         bmRequestType
: (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 132                         bRequest
:      REQ_GetPortStatus
, 
 135                         wLength
:       sizeof(uint8_t), 
 138         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 140         return USB_Host_SendControlRequest(PortStatus
); 
 143 /** Issues a Printer class Soft Reset command to the attached device, to reset the printer ready for new input without 
 144  *  physically cycling the printer's power. 
 146  *  \return A value from the USB_Host_SendControlErrorCodes_t enum 
 148 uint8_t Printer_SoftReset(void) 
 150         USB_ControlRequest 
= (USB_Request_Header_t
) 
 152                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 153                         .bRequest      
= REQ_SoftReset
, 
 159         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 161         return USB_Host_SendControlRequest(NULL
);