From 6dd486d5f3ac642bc348fdcd89c33b86e8158637 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sat, 4 Jun 2016 20:33:10 -0500 Subject: [PATCH] Implement stuff to print like, a thing --- include/tabby/printer.h | 49 +++++++++++++++++++++++ src/printer.c | 89 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/include/tabby/printer.h b/include/tabby/printer.h index 12243a2..c108bec 100644 --- a/include/tabby/printer.h +++ b/include/tabby/printer.h @@ -3,6 +3,12 @@ #define TABBY_PRINTER_MAX_PACKET_SIZE 640 +#define TABBY_PRINTER_BAND_SIZE 640 +#define TABBY_PRINTER_SHEET_BANDS 9 + +#define TABBY_PRINTER_SHEET_SIZE \ + (TABBY_PRINTER_SHEET_BANDS * TABBY_PRINTER_BAND_SIZE) + typedef enum { TABBY_PRINTER_LOWBAT = (1 << 7), TABBY_PRINTER_ER2 = (1 << 6), @@ -14,6 +20,19 @@ typedef enum { TABBY_PRINTER_SUM = (1 << 0) } tabby_printer_status; +typedef enum { + TABBY_PRINTER_PACKET_INIT = 0x01, + TABBY_PRINTER_PACKET_JOB = 0x02, + TABBY_PRINTER_PACKET_DATA = 0x04, + TABBY_PRINTER_PACKET_CANCEL = 0x08, + TABBY_PRINTER_PACKET_INQUIRY = 0x0f +} tabby_printer_packet_type; + +typedef enum { + TABBY_PRINTER_COMPRESSION_NONE = 0x00, + TABBY_PRINTER_COMPRESSION_RLE = 0x01 +} tabby_printer_compression; + /* * Data definitions for packets sent from Game Boy to printer */ @@ -31,6 +50,15 @@ typedef struct _tabby_printer_packet { }; } tabby_printer_packet; +typedef struct _tabby_printer_job_packet { + tabby_printer_packet header; + + uint8_t sheets, + linefeeds, + palette, + density; +} tabby_printer_job_packet; + typedef struct _tabby_printer_response { uint8_t device; uint8_t status; @@ -53,4 +81,25 @@ int tabby_printer_packet_send(int fd, void *body, tabby_printer_response *response); +int tabby_printer_init(int fd, + tabby_printer_response *response); + +int tabby_printer_send_inquiry(int fd, + tabby_printer_response *response); + +int tabby_printer_job_start(int fd, uint8_t sheets, + uint8_t linefeeds, + uint8_t palette, + uint8_t density, + tabby_printer_response *response); + +int tabby_printer_job_finish(int fd, + tabby_printer_response *response); + +int tabby_printer_job_cancel(int fd, + tabby_printer_response *response); + +int tabby_printer_send_sheet(int fd, void *data, + tabby_printer_response *response); + #endif /* _TABBY_PRINTER_H */ diff --git a/src/printer.c b/src/printer.c index 550eb93..9395288 100644 --- a/src/printer.c +++ b/src/printer.c @@ -136,3 +136,92 @@ int tabby_printer_packet_send(int fd, error_io: return -1; } + +static void init_header(tabby_printer_packet *header, + tabby_printer_packet_type type, size_t size) { + header->preamble[0] = 0x88; + header->preamble[1] = 0x33; + header->type = type; + header->compression = TABBY_PRINTER_COMPRESSION_NONE; + header->size = (uint16_t)size; +} + +int tabby_printer_init(int fd, + tabby_printer_response *response) { + tabby_printer_packet header; + + init_header(&header, TABBY_PRINTER_PACKET_INIT, 0); + + return tabby_printer_packet_send(fd, &header, NULL, response); +} + +int tabby_printer_send_inquiry(int fd, + tabby_printer_response *response) { + tabby_printer_packet header; + + init_header(&header, TABBY_PRINTER_PACKET_INQUIRY, 0); + + return tabby_printer_packet_send(fd, &header, NULL, response); +} + +int tabby_printer_job_start(int fd, uint8_t sheets, + uint8_t linefeeds, + uint8_t palette, + uint8_t density, + tabby_printer_response *response) { + tabby_printer_packet header; + + uint8_t body[4] = { + sheets, linefeeds, palette, density + }; + + init_header(&header, TABBY_PRINTER_PACKET_INQUIRY, sizeof(body)); + + return tabby_printer_packet_send(fd, &header, &body, response); +} + +int tabby_printer_job_finish(int fd, + tabby_printer_response *response) { + tabby_printer_packet header; + + init_header(&header, TABBY_PRINTER_PACKET_DATA, 0); + + return tabby_printer_packet_send(fd, &header, NULL, response); +} + +int tabby_printer_job_cancel(int fd, + tabby_printer_response *response) { + tabby_printer_packet header; + + init_header(&header, TABBY_PRINTER_PACKET_CANCEL, 0); + + return tabby_printer_packet_send(fd, &header, NULL, response); +} + +int tabby_printer_send_sheet(int fd, void *data, + tabby_printer_response *response) { + int i; + size_t offset = 0; + + for (i=0; i