2016-06-06 19:42:15 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <tabby/avr/link.h>
|
|
|
|
#include <tabby/avr/printer.h>
|
|
|
|
|
2016-06-06 20:28:19 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:42:15 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:34:17 -05:00
|
|
|
void tabby_avr_printer_packet_send(uint8_t type,
|
2016-06-06 19:42:15 -05:00
|
|
|
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) {
|
2016-06-10 17:34:17 -05:00
|
|
|
tabby_avr_printer_packet_send(TABBY_PRINTER_PACKET_INIT,
|
2016-06-06 19:42:15 -05:00
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
response);
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:34:17 -05:00
|
|
|
void tabby_avr_printer_inquiry_send(tabby_printer_response *response) {
|
|
|
|
tabby_avr_printer_packet_send(TABBY_PRINTER_PACKET_INQUIRY,
|
2016-06-06 19:42:15 -05:00
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
response);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tabby_avr_printer_job_start(tabby_printer_job *job,
|
|
|
|
tabby_printer_response *response) {
|
2016-06-10 17:34:17 -05:00
|
|
|
tabby_avr_printer_packet_send(TABBY_PRINTER_PACKET_JOB,
|
2016-06-06 19:42:15 -05:00
|
|
|
job->data,
|
|
|
|
sizeof(*job),
|
|
|
|
response);
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:34:17 -05:00
|
|
|
void tabby_avr_printer_data_send(uint8_t *data,
|
2016-06-06 19:42:15 -05:00
|
|
|
uint16_t size,
|
|
|
|
tabby_printer_response *response) {
|
2016-06-10 17:34:17 -05:00
|
|
|
tabby_avr_printer_packet_send(TABBY_PRINTER_PACKET_DATA,
|
2016-06-06 19:42:15 -05:00
|
|
|
data,
|
|
|
|
size,
|
|
|
|
response);
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:34:17 -05:00
|
|
|
void tabby_avr_printer_sheet_send(uint8_t *sheet,
|
2016-06-06 19:42:15 -05:00
|
|
|
uint16_t size,
|
|
|
|
tabby_printer_response *response) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
tabby_avr_printer_init(response);
|
|
|
|
|
|
|
|
for (i=0; i<size; i+=TABBY_PRINTER_BAND_SIZE) {
|
2016-06-10 17:34:17 -05:00
|
|
|
tabby_avr_printer_data_send(sheet + i,
|
2016-06-06 19:42:15 -05:00
|
|
|
TABBY_PRINTER_BAND_SIZE,
|
|
|
|
response);
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:34:17 -05:00
|
|
|
tabby_avr_printer_data_send(NULL, 0, response);
|
2016-06-06 19:42:15 -05:00
|
|
|
}
|
|
|
|
|