Hi @ABasi.2
So far, no example is provided to cover Printer class. CubeMX imports Middleware files and leaves it to developer to tailor their application in app_usbx_host.c/h. Here is a suggestion to implement functions to initialize and communicate with the printer.
/*
* ux_host_printer.c
*
*/
#include "ux_host_printer.h"
#include "ux_api.h"
typedef struct
{
UX_HOST_CLASS_PRINTER *pPrinterClass;
} PrinterData_t;
static PrinterData_t sgrPrinterData;
bool ux_host_printer_IsPrinterConnected(void)
{
bool bReturn = false;
if (sgrPrinterData.pPrinterClass != NULL)
{
bReturn = true;
}
else
{
/* No printer connected */
}
return bReturn;
}
bool ux_host_printer_Send(uint8_t *_pData, uint32_t _iLength)
{
bool bReturn = false;
uint32_t iSend;
if ((sgrPrinterData.pPrinterClass != NULL) && (_pData != NULL))
{
if (ux_host_class_printer_write(sgrPrinterData.pPrinterClass, _pData, _iLength, &iSend) == UX_SUCCESS)
{
if (iSend == _iLength)
{
bReturn = true;
}
else
{
/* Not all data has been sent to the printer */
}
}
else
{
/* Failed to send data to the printer */
}
}
else
{
/* No printer available or invalid parameter */
}
return bReturn;
}
bool ux_host_printer_Receive(uint8_t *_pData, uint32_t *_pReceivedLength, uint32_t _iRequestedLength)
{
bool bReturn = false;
if ((sgrPrinterData.pPrinterClass != NULL) && (_pData != NULL) && (_pReceivedLength != NULL) && (_iRequestedLength > 0uL))
{
if (ux_host_class_printer_read(sgrPrinterData.pPrinterClass, _pData, _iRequestedLength, _pReceivedLength) == UX_SUCCESS)
{
bReturn = true;
}
else
{
/* Failed to read data from printer */
}
}
else
{
/* No printer available or invalid parameter */
}
return bReturn;
}
void ux_host_printer_AddPrinter(UX_HOST_CLASS_PRINTER *_pPrinterClass)
{
if (_pPrinterClass != NULL)
{
sgrPrinterData.pPrinterClass = _pPrinterClass;
}
else
{
/* Invalid parameter */
}
}
void ux_host_printer_RemovePrinter(void)
{
sgrPrinterData.pPrinterClass = NULL;
}
Then, ensure your header file declares the necessary functions.
/*
* ux_host_printer.h
*
*/
#include <stdbool.h>
#include <stdint.h>
#include "ux_api.h"
#include "ux_host_class_printer.h"
bool ux_host_printer_IsPrinterConnected(void);
bool ux_host_printer_Send(uint8_t *_pData, uint32_t _iLength);
bool ux_host_printer_Receive(uint8_t *_pData, uint32_t *_pReceivedLength, uint32_t _iRequestedLength);
void ux_host_printer_AddPrinter(UX_HOST_CLASS_PRINTER *_pPrinterClass);
void ux_host_printer_RemovePrinter(void);
When ux_host_event_callback is called in app_usbx_host.c, handle device insertion and removal as follows:
switch (event)
{
case UX_DEVICE_INSERTION:
/* USER CODE BEGIN UX_DEVICE_INSERTION */
if (current_class -> ux_host_class_entry_function == ux_host_class_printer_entry)
{
ux_host_printer_AddPrinter((UX_HOST_CLASS_PRINTER *)current_instance);
}
/* USER CODE END UX_DEVICE_INSERTION */
break;
case UX_DEVICE_REMOVAL:
/* USER CODE BEGIN UX_DEVICE_REMOVAL */
if (current_class -> ux_host_class_entry_function == ux_host_class_printer_entry)
{
ux_host_printer_RemovePrinter();
}
else
{
/* Another device is removed */
}
/* USER CODE END UX_DEVICE_REMOVAL */
break;
In MX_USBX_Host_Init, initialize host printer class after /* USER CODE BEGIN MX_USBX_Host_Init1 */
/* Initialize the host printer class */
if (ux_host_stack_class_register(_ux_system_host_class_printer_name,
ux_host_class_printer_entry) != UX_SUCCESS)
{
/* USER CODE BEGIN USBX_HOST_STORAGE_REGISTER_ERROR */
return UX_ERROR;
/* USER CODE END USBX_HOST_STORAGE_REGISTER_ERROR */
}
I hope this helps! An internal ticket 208191 is submitted to CubeMX to add ux_host_printer.c and ux_host_printer.h in CubeMX generated code.