tabby/avr/print.c

174 lines
4.4 KiB
C
Raw Permalink Normal View History

2016-06-01 22:30:12 -05:00
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
2016-06-01 22:30:12 -05:00
#include <util/delay.h>
#include <tabby/printer.h>
#include <tabby/avr/uart.h>
#include <tabby/avr/link.h>
#include <tabby/avr/printer.h>
2016-06-05 12:37:05 -05:00
#include <tabby/avr.h>
2016-06-01 22:30:12 -05:00
int main() {
tabby_printer_packet header = {
.size = 0
};
2016-06-06 01:42:17 -05:00
tabby_printer_response response = {
.device = TABBY_PRINTER_DEVICE_ID,
.status = TABBY_PRINTER_OK
};
tabby_printer_job job;
uint8_t sheet[TABBY_PRINTER_SHEET_SIZE];
uint16_t offset_header = 0,
offset_body = 0,
offset_sheet = 0;
uint16_t sum_calc = 0,
sum_footer = 0;
2016-06-01 22:30:12 -05:00
uart_init();
tabby_avr_link_init_master();
2016-06-01 22:30:12 -05:00
while (1) {
uint8_t c = uart_getchar(NULL);
switch (offset_header) {
case 0: {
2016-06-05 12:37:05 -05:00
if (c == TABBY_PRINTER_SYNC_1) {
header.data[0] = c;
offset_header++;
offset_body = 0;
sum_calc = 0;
}
break;
}
case 1: {
2016-06-05 12:37:05 -05:00
if (c == TABBY_PRINTER_SYNC_2) {
header.data[1] = c;
offset_header++;
} else {
offset_header = 0;
}
break;
}
case 2: {
switch (c) {
2016-06-05 12:37:05 -05:00
case TABBY_PRINTER_PACKET_INIT:
case TABBY_PRINTER_PACKET_CANCEL: {
response.status = TABBY_PRINTER_OK;
offset_sheet = 0;
}
case TABBY_PRINTER_PACKET_JOB:
2016-06-05 12:37:05 -05:00
case TABBY_PRINTER_PACKET_DATA:
case TABBY_PRINTER_PACKET_INQUIRY: {
header.type = c;
offset_header++;
break;
}
default: {
offset_header = 0;
break;
}
}
sum_calc += c;
break;
}
case 3: {
header.compression = c;
offset_header++;
sum_calc += c;
break;
}
case 4: {
header.size = c;
offset_header++;
sum_calc += c;
break;
}
case 5: {
header.size |= c << 8;
if (tabby_avr_printer_packet_toolarge(header.type,
header.size)) {
offset_header = 0;
response.status |= TABBY_PRINTER_FULL;
goto respond;
2016-06-06 20:46:25 -05:00
} else {
offset_header++;
}
sum_calc += c;
break;
}
default: {
if (offset_body < header.size) {
2016-06-06 01:42:17 -05:00
if (header.type == TABBY_PRINTER_PACKET_JOB) {
job.data[offset_body] = c;
2016-06-06 01:42:17 -05:00
} else if (header.type == TABBY_PRINTER_PACKET_DATA) {
sheet[offset_sheet++] = c;
2016-06-06 01:42:17 -05:00
}
offset_body++;
2016-06-02 23:32:38 -05:00
sum_calc += c;
} else if (offset_body == header.size) {
sum_footer = c;
offset_body++;
} else if (offset_body == header.size + 1) {
sum_footer |= c << 8;
offset_header = 0;
if (sum_footer != sum_calc) {
response.status |= TABBY_PRINTER_SUM;
} else if (header.type == TABBY_PRINTER_PACKET_JOB) {
2016-06-10 17:34:17 -05:00
tabby_avr_printer_sheet_send(sheet,
offset_sheet,
&response);
tabby_avr_printer_job_start(&job, &response);
2016-06-06 01:42:17 -05:00
offset_sheet = 0;
2016-06-06 01:42:17 -05:00
} else if (header.type == TABBY_PRINTER_PACKET_INQUIRY) {
2016-06-10 17:34:17 -05:00
tabby_avr_printer_inquiry_send(&response);
}
respond:
2016-06-06 01:42:17 -05:00
uart_putchar(response.device, NULL);
uart_putchar(response.status, NULL);
}
}
}
2016-06-01 22:30:12 -05:00
}
return 0;
}