tabby/avr/printer.c
2016-06-06 20:28:19 -05:00

116 lines
3.3 KiB
C

#include <stdlib.h>
#include <tabby/avr/link.h>
#include <tabby/avr/printer.h>
int tabby_avr_printer_packet_toolarge(uint8_t type, uint16_t size) {
switch (type) {
case TABBY_PRINTER_PACKET_JOB: {
if (size > sizeof(tabby_printer_job)) {
return 1;
}
}
case TABBY_PRINTER_PACKET_DATA: {
if (size > TABBY_PRINTER_PACKET_MAX_SIZE) {
return 1;
}
}
}
return 0;
}
static uint16_t checksum(tabby_printer_packet *header, uint8_t *body) {
uint16_t sum = 0;
int i;
for (i=2; i<sizeof(*header); i++) {
sum += header->data[i];
}
for (i=0; i<header->size; i++) {
sum += body[i];
}
return sum;
}
void tabby_avr_printer_send_packet(uint8_t type,
uint8_t *body,
uint16_t size,
tabby_printer_response *response) {
tabby_printer_packet header = {
.preamble = { TABBY_PRINTER_SYNC_1, TABBY_PRINTER_SYNC_2 },
.type = type,
.compression = TABBY_PRINTER_COMPRESSION_NONE,
.size = size
};
uint16_t sum = checksum(&header, body);
int i;
for (i=0; i<sizeof(header); i++) {
(void)tabby_avr_link_send_byte(header.data[i]);
}
for (i=0; i<size; i++) {
(void)tabby_avr_link_send_byte(body[i]);
}
(void)tabby_avr_link_send_byte( sum & 0x00ff);
(void)tabby_avr_link_send_byte((sum & 0xff00) >> 8);
response->device = tabby_avr_link_send_byte(0);
response->status = tabby_avr_link_send_byte(0);
}
void tabby_avr_printer_init(tabby_printer_response *response) {
tabby_avr_printer_send_packet(TABBY_PRINTER_PACKET_INIT,
NULL,
0,
response);
}
void tabby_avr_printer_send_inquiry(tabby_printer_response *response) {
tabby_avr_printer_send_packet(TABBY_PRINTER_PACKET_INQUIRY,
NULL,
0,
response);
}
void tabby_avr_printer_job_start(tabby_printer_job *job,
tabby_printer_response *response) {
tabby_avr_printer_send_packet(TABBY_PRINTER_PACKET_JOB,
job->data,
sizeof(*job),
response);
}
void tabby_avr_printer_send_data(uint8_t *data,
uint16_t size,
tabby_printer_response *response) {
tabby_avr_printer_send_packet(TABBY_PRINTER_PACKET_DATA,
data,
size,
response);
}
void tabby_avr_printer_send_sheet(uint8_t *sheet,
uint16_t size,
tabby_printer_response *response) {
int i;
tabby_avr_printer_init(response);
for (i=0; i<size; i+=TABBY_PRINTER_BAND_SIZE) {
tabby_avr_printer_send_data(sheet + i,
TABBY_PRINTER_BAND_SIZE,
response);
}
tabby_avr_printer_send_data(NULL, 0, response);
}