PrinterCommands.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2017.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Printer Device commands, to send/receive data to and from an attached USB
  29. * printer, and to send and receive Printer Class control requests.
  30. */
  31. #include "PrinterCommands.h"
  32. /** Sends the given data directly to the printer via the data endpoints, for the sending of print commands in printer
  33. * languages accepted by the attached printer (e.g. PCL).
  34. *
  35. * \param[in] PrinterCommands Pointer to the data to send to the attached printer
  36. * \param[in] CommandSize Size of the data to send to the attached printer
  37. *
  38. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
  39. */
  40. uint8_t Printer_SendData(const void* const PrinterCommands,
  41. const uint16_t CommandSize)
  42. {
  43. uint8_t ErrorCode;
  44. Pipe_SelectPipe(PRINTER_DATA_OUT_PIPE);
  45. Pipe_Unfreeze();
  46. if ((ErrorCode = Pipe_Write_Stream_LE(PrinterCommands, CommandSize, NULL)) != PIPE_RWSTREAM_NoError)
  47. return ErrorCode;
  48. Pipe_ClearOUT();
  49. Pipe_WaitUntilReady();
  50. Pipe_Freeze();
  51. return PIPE_RWSTREAM_NoError;
  52. }
  53. /** Issues a Printer class Get Device ID command to the attached device, to retrieve the device ID string (which indicates
  54. * the accepted printer languages, the printer's model and other pertinent information).
  55. *
  56. * \param[out] DeviceIDString Pointer to the destination where the returned string should be stored
  57. * \param[in] BufferSize Size in bytes of the allocated buffer for the returned Device ID string
  58. *
  59. * \return A value from the USB_Host_SendControlErrorCodes_t enum
  60. */
  61. uint8_t Printer_GetDeviceID(char* DeviceIDString,
  62. const uint16_t BufferSize)
  63. {
  64. uint8_t ErrorCode;
  65. uint16_t DeviceIDStringLength = 0;
  66. USB_ControlRequest = (USB_Request_Header_t)
  67. {
  68. .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
  69. .bRequest = PRNT_REQ_GetDeviceID,
  70. .wValue = 0,
  71. .wIndex = PrinterInterfaceNumber,
  72. .wLength = sizeof(DeviceIDStringLength),
  73. };
  74. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  75. if ((ErrorCode = USB_Host_SendControlRequest(&DeviceIDStringLength)) != HOST_SENDCONTROL_Successful)
  76. return ErrorCode;
  77. if (!(DeviceIDStringLength))
  78. {
  79. DeviceIDString[0] = 0x00;
  80. return HOST_SENDCONTROL_Successful;
  81. }
  82. DeviceIDStringLength = SwapEndian_16(DeviceIDStringLength);
  83. if (DeviceIDStringLength > BufferSize)
  84. DeviceIDStringLength = BufferSize;
  85. USB_ControlRequest.wLength = DeviceIDStringLength;
  86. if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)
  87. return ErrorCode;
  88. /* Move string back two characters to remove the string length value from the start of the array */
  89. memmove(&DeviceIDString[0], &DeviceIDString[2], DeviceIDStringLength - 2);
  90. DeviceIDString[DeviceIDStringLength - 2] = 0x00;
  91. return HOST_SENDCONTROL_Successful;
  92. }
  93. /** Issues a Printer class Get Port Status command to the attached device, to retrieve the current status flags of the
  94. * printer.
  95. *
  96. * \param[out] PortStatus Pointer to the destination where the printer's status flag values should be stored
  97. *
  98. * \return A value from the USB_Host_SendControlErrorCodes_t enum
  99. */
  100. uint8_t Printer_GetPortStatus(uint8_t* const PortStatus)
  101. {
  102. USB_ControlRequest = (USB_Request_Header_t)
  103. {
  104. .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
  105. .bRequest = PRNT_REQ_GetPortStatus,
  106. .wValue = 0,
  107. .wIndex = PrinterInterfaceNumber,
  108. .wLength = sizeof(uint8_t),
  109. };
  110. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  111. return USB_Host_SendControlRequest(PortStatus);
  112. }
  113. /** Issues a Printer class Soft Reset command to the attached device, to reset the printer ready for new input without
  114. * physically cycling the printer's power.
  115. *
  116. * \return A value from the USB_Host_SendControlErrorCodes_t enum
  117. */
  118. uint8_t Printer_SoftReset(void)
  119. {
  120. USB_ControlRequest = (USB_Request_Header_t)
  121. {
  122. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  123. .bRequest = PRNT_REQ_SoftReset,
  124. .wValue = 0,
  125. .wIndex = PrinterInterfaceNumber,
  126. .wLength = 0,
  127. };
  128. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  129. return USB_Host_SendControlRequest(NULL);
  130. }