From 4c035b14f1349828384946523987f009c9966690 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Mon, 20 Jul 2009 03:07:27 +0000 Subject: [PATCH] Cleaner solution to the PrinterHost data send routine problem of embedded NULLs - use a special structure to hold the data string plus the length in bytes of the data. --- .../Incomplete/PrinterHost/Lib/PrinterCommands.c | 6 +++--- .../Incomplete/PrinterHost/Lib/PrinterCommands.h | 9 ++++++++- Demos/Host/Incomplete/PrinterHost/PrinterHost.c | 14 +++++++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.c b/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.c index 03d3070947..87651e9060 100644 --- a/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.c +++ b/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.c @@ -33,18 +33,18 @@ /** Sends the given data directly to the printer via the data endpoints, for the sending of print commands in printer * languages accepted by the attached printer (e.g. PCL). * - * \param[in] PrinterCommands Pointer to the input buffer containing the printer data to send + * \param[in] PrinterCommands Pointer to a structure containing the commands and length of the data to send * * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum */ -uint8_t Printer_SendData(char* PrinterCommands, uint16_t DataLength) +uint8_t Printer_SendData(Printer_Data_t* PrinterCommands) { uint8_t ErrorCode; Pipe_SelectPipe(PRINTER_DATA_OUT_PIPE); Pipe_Unfreeze(); - if ((ErrorCode = Pipe_Write_Stream_LE(PrinterCommands, DataLength)) != PIPE_RWSTREAM_NoError) + if ((ErrorCode = Pipe_Write_Stream_LE(PrinterCommands->Data, PrinterCommands->Length)) != PIPE_RWSTREAM_NoError) return ErrorCode; Pipe_ClearOUT(); diff --git a/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.h b/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.h index b296d86f60..9ba6d4d5de 100644 --- a/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.h +++ b/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.h @@ -53,8 +53,15 @@ /** Pipe number of the Printer data OUT pipe */ #define PRINTER_DATA_OUT_PIPE 2 + /* Type Defines: */ + typedef struct + { + char* Data; + uint16_t Length; + } Printer_Data_t; + /* Function Prototypes: */ - uint8_t Printer_SendData(char* PrinterCommands, uint16_t DataLength); + uint8_t Printer_SendData(Printer_Data_t* PrinterCommands); uint8_t Printer_GetDeviceID(char* DeviceIDString, uint8_t BufferSize); uint8_t Printer_GetPortStatus(uint8_t* PortStatus); uint8_t Printer_SoftReset(void); diff --git a/Demos/Host/Incomplete/PrinterHost/PrinterHost.c b/Demos/Host/Incomplete/PrinterHost/PrinterHost.c index bff5e20205..9eb05abd80 100644 --- a/Demos/Host/Incomplete/PrinterHost/PrinterHost.c +++ b/Demos/Host/Incomplete/PrinterHost/PrinterHost.c @@ -204,12 +204,16 @@ void USB_Printer_Host(void) /* Indicate device busy via the status LEDs */ LEDs_SetAllLEDs(LEDMASK_USB_BUSY); - char PCL_Test_Page[] = "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X"; -// char ESCP2_Test_Page[] = "\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n"; - - printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), (sizeof(PCL_Test_Page) - 1)); + Printer_Data_t TestPageData = + { + "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X", +// "\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n", + (sizeof(TestPageData.Data) - 1) + }; + + printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), TestPageData.Length); - if ((ErrorCode = Printer_SendData(PCL_Test_Page, (sizeof(PCL_Test_Page) - 1))) != PIPE_RWSTREAM_NoError) + if ((ErrorCode = Printer_SendData(&TestPageData)) != PIPE_RWSTREAM_NoError) { puts_P(PSTR(ESC_FG_RED "Error Sending Test Page.\r\n")); printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);