Keyboard firmwares for Atmel AVR and Cortex-M
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PrinterClassDevice.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 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. * \brief Device mode driver for the library USB Printer Class driver.
  28. *
  29. * Device mode driver for the library USB Printer Class driver.
  30. *
  31. * \note This file should not be included directly. It is automatically included as needed by the USB module driver
  32. * dispatch header located in LUFA/Drivers/USB.h.
  33. */
  34. /** \ingroup Group_USBClassPrinter
  35. * \defgroup Group_USBClassPrinterDevice Printer Class Device Mode Driver
  36. *
  37. * \section Sec_USBClassPrinterDevice_Dependencies Module Source Dependencies
  38. * The following files must be built with any user project that uses this module:
  39. * - LUFA/Drivers/USB/Class/Device/PrinterClassDevice.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
  40. *
  41. * \section Sec_USBClassPrinterDevice_ModDescription Module Description
  42. * Device Mode USB Class driver framework interface, for the Printer USB Class driver.
  43. *
  44. * @{
  45. */
  46. #ifndef _PRINTER_CLASS_DEVICE_H_
  47. #define _PRINTER_CLASS_DEVICE_H_
  48. /* Includes: */
  49. #include "../../USB.h"
  50. #include "../Common/PrinterClassCommon.h"
  51. #include <stdio.h>
  52. /* Enable C linkage for C++ Compilers: */
  53. #if defined(__cplusplus)
  54. extern "C" {
  55. #endif
  56. /* Preprocessor Checks: */
  57. #if !defined(__INCLUDE_FROM_PRINTER_DRIVER)
  58. #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
  59. #endif
  60. /* Public Interface - May be used in end-application: */
  61. /* Type Defines: */
  62. /** \brief Printer Class Device Mode Configuration and State Structure.
  63. *
  64. * Class state structure. An instance of this structure should be made for each Printer interface
  65. * within the user application, and passed to each of the Printer class driver functions as the
  66. * PRNTInterfaceInfo parameter. This stores each Printer interface's configuration and state information.
  67. */
  68. typedef struct
  69. {
  70. struct
  71. {
  72. uint8_t InterfaceNumber; /**< Interface number of the Printer interface within the device. */
  73. USB_Endpoint_Table_t DataINEndpoint; /**< Data IN endpoint configuration table. */
  74. USB_Endpoint_Table_t DataOUTEndpoint; /**< Data OUT endpoint configuration table. */
  75. char* IEEE1284String; /**< IEEE 1284 identification string, sent to the host during enumeration
  76. * to identify the printer model, manufacturer and other characteristics.
  77. */
  78. } Config; /**< Config data for the USB class interface within the device. All elements in this section
  79. * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
  80. */
  81. struct
  82. {
  83. uint8_t PortStatus; /**< Current status of the Printer virtual port, a collection of \c PRNT_PORTSTATUS_*
  84. * bitmask values.
  85. */
  86. volatile bool IsPrinterReset; /**< Flag indicating that the host has requested that the Printer interface be reset
  87. * and that all current Mass Storage operations should immediately abort.
  88. */
  89. } State; /**< State data for the USB class interface within the device. All elements in this section
  90. * are reset to their defaults when the interface is enumerated.
  91. */
  92. } USB_ClassInfo_PRNT_Device_t;
  93. /* Function Prototypes: */
  94. /** Configures the endpoints of a given Printer interface, ready for use. This should be linked to the library
  95. * \ref EVENT_USB_Device_ConfigurationChanged() event so that the endpoints are configured when the configuration containing
  96. * the given Printer interface is selected.
  97. *
  98. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  99. *
  100. * \return Boolean \c true if the endpoints were successfully configured, \c false otherwise.
  101. */
  102. bool PRNT_Device_ConfigureEndpoints(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  103. /** Processes incoming control requests from the host, that are directed to the given Printer class interface. This should be
  104. * linked to the library \ref EVENT_USB_Device_ControlRequest() event.
  105. *
  106. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  107. */
  108. void PRNT_Device_ProcessControlRequest(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  109. /** General management task for a given Printer class interface, required for the correct operation of the interface. This should
  110. * be called frequently in the main program loop, before the master USB management task \ref USB_USBTask().
  111. *
  112. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  113. */
  114. void PRNT_Device_USBTask(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  115. /** Printer class driver event for a soft reset request on a Printer interface. This event fires each time the host
  116. * requests a reset of the printer interface's internal state, and may be hooked in the user program by declaring a
  117. * handler function with the same name and parameters listed here.
  118. *
  119. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  120. */
  121. void EVENT_PRNT_Device_SoftReset(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  122. /** Sends a given data buffer to the attached USB host, if connected. If a host is not connected when the function is
  123. * called, the string is discarded. Bytes will be queued for transmission to the host until either the endpoint bank
  124. * becomes full, or the \ref PRNT_Device_Flush() function is called to flush the pending data to the host. This allows
  125. * for multiple bytes to be packed into a single endpoint packet, increasing data throughput.
  126. *
  127. * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
  128. * the call will fail.
  129. *
  130. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  131. * \param[in] Buffer Pointer to a buffer containing the data to send to the device.
  132. * \param[in] Length Length of the data to send to the host.
  133. *
  134. * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
  135. */
  136. uint8_t PRNT_Device_SendData(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
  137. const void* const Buffer,
  138. const uint16_t Length) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  139. /** Sends a given null terminated string to the attached USB host, if connected. If a host is not connected when
  140. * the function is called, the string is discarded. Bytes will be queued for transmission to the host until either
  141. * the endpoint bank becomes full, or the \ref PRNT_Device_Flush() function is called to flush the pending data to
  142. * the host. This allows for multiple bytes to be packed into a single endpoint packet, increasing data throughput.
  143. *
  144. * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
  145. * the call will fail.
  146. *
  147. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  148. * \param[in] String Pointer to the null terminated string to send to the host.
  149. *
  150. * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
  151. */
  152. uint8_t PRNT_Device_SendString(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
  153. const char* const String) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  154. /** Sends a given byte to the attached USB host, if connected. If a host is not connected when the function is called, the
  155. * byte is discarded. Bytes will be queued for transmission to the host until either the endpoint bank becomes full, or the
  156. * \ref PRNT_Device_Flush() function is called to flush the pending data to the host. This allows for multiple bytes to be
  157. * packed into a single endpoint packet, increasing data throughput.
  158. *
  159. * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
  160. * the call will fail.
  161. *
  162. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  163. * \param[in] Data Byte of data to send to the host.
  164. *
  165. * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
  166. */
  167. uint8_t PRNT_Device_SendByte(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
  168. const uint8_t Data) ATTR_NON_NULL_PTR_ARG(1);
  169. /** Determines the number of bytes received by the Printer interface from the host, waiting to be read. This indicates the number
  170. * of bytes in the OUT endpoint bank only, and thus the number of calls to \ref PRNT_Device_ReceiveByte() which are guaranteed to
  171. * succeed immediately. If multiple bytes are to be received, they should be buffered by the user application, as the endpoint
  172. * bank will not be released back to the USB controller until all bytes are read.
  173. *
  174. * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
  175. * the call will fail.
  176. *
  177. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  178. *
  179. * \return Total number of buffered bytes received from the host.
  180. */
  181. uint16_t PRNT_Device_BytesReceived(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  182. /** Reads a byte of data from the host. If no data is waiting to be read of if a USB host is not connected, the function
  183. * returns a negative value. The \ref PRNT_Device_BytesReceived() function may be queried in advance to determine how many
  184. * bytes are currently buffered in the Printer interface's data receive endpoint bank, and thus how many repeated calls to this
  185. * function which are guaranteed to succeed.
  186. *
  187. * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
  188. * the call will fail.
  189. *
  190. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  191. *
  192. * \return Next received byte from the host, or a negative value if no data received.
  193. */
  194. int16_t PRNT_Device_ReceiveByte(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  195. /** Flushes any data waiting to be sent, ensuring that the send buffer is cleared.
  196. *
  197. * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
  198. * the call will fail.
  199. *
  200. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  201. *
  202. * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
  203. */
  204. uint8_t PRNT_Device_Flush(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  205. #if defined(FDEV_SETUP_STREAM) || defined(__DOXYGEN__)
  206. /** Creates a standard character stream for the given Printer Device instance so that it can be used with all the regular
  207. * functions in the standard <stdio.h> library that accept a \c FILE stream as a destination (e.g. \c fprintf()). The created
  208. * stream is bidirectional and can be used for both input and output functions.
  209. *
  210. * Reading data from this stream is non-blocking, i.e. in most instances, complete strings cannot be read in by a single
  211. * fetch, as the endpoint will not be ready at some point in the transmission, aborting the transfer. However, this may
  212. * be used when the read data is processed byte-per-bye (via \c getc()) or when the user application will implement its own
  213. * line buffering.
  214. *
  215. * \note The created stream can be given as \c stdout if desired to direct the standard output from all \c <stdio.h> functions
  216. * to the given Printer interface.
  217. * \n\n
  218. *
  219. * \note This function is not available on all microcontroller architectures.
  220. *
  221. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  222. * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed.
  223. */
  224. void PRNT_Device_CreateStream(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
  225. FILE* const Stream) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  226. /** Identical to \ref PRNT_Device_CreateStream(), except that reads are blocking until the calling stream function terminates
  227. * the transfer. While blocking, the USB and Printer service tasks are called repeatedly to maintain USB communications.
  228. *
  229. * \note This function is not available on all microcontroller architectures.
  230. *
  231. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
  232. * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed.
  233. */
  234. void PRNT_Device_CreateBlockingStream(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
  235. FILE* const Stream) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  236. #endif
  237. /* Private Interface - For use in library only: */
  238. #if !defined(__DOXYGEN__)
  239. /* Function Prototypes: */
  240. #if defined(__INCLUDE_FROM_PRINTER_DEVICE_C)
  241. #if defined(FDEV_SETUP_STREAM)
  242. static int PRNT_Device_putchar(char c,
  243. FILE* Stream) ATTR_NON_NULL_PTR_ARG(2);
  244. static int PRNT_Device_getchar(FILE* Stream) ATTR_NON_NULL_PTR_ARG(1);
  245. static int PRNT_Device_getchar_Blocking(FILE* Stream) ATTR_NON_NULL_PTR_ARG(1);
  246. #endif
  247. void PRNT_Device_Event_Stub(void) ATTR_CONST;
  248. void EVENT_PRNT_Device_SoftReset(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo)
  249. ATTR_WEAK ATTR_NON_NULL_PTR_ARG(1) ATTR_ALIAS(PRNT_Device_Event_Stub);
  250. #endif
  251. #endif
  252. /* Disable C linkage for C++ Compilers: */
  253. #if defined(__cplusplus)
  254. }
  255. #endif
  256. #endif
  257. /** @} */