173 lines
4.4 KiB
C
173 lines
4.4 KiB
C
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include <avr/sleep.h>
|
|
#include <util/delay.h>
|
|
|
|
#include <tabby/printer.h>
|
|
#include <tabby/avr/uart.h>
|
|
#include <tabby/avr/link.h>
|
|
#include <tabby/avr/printer.h>
|
|
#include <tabby/avr.h>
|
|
|
|
int main() {
|
|
tabby_printer_packet header = {
|
|
.size = 0
|
|
};
|
|
|
|
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;
|
|
|
|
uart_init();
|
|
|
|
tabby_avr_link_init_master();
|
|
|
|
while (1) {
|
|
uint8_t c = uart_getchar(NULL);
|
|
|
|
switch (offset_header) {
|
|
case 0: {
|
|
if (c == TABBY_PRINTER_SYNC_1) {
|
|
header.data[0] = c;
|
|
offset_header++;
|
|
|
|
offset_body = 0;
|
|
sum_calc = 0;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 1: {
|
|
if (c == TABBY_PRINTER_SYNC_2) {
|
|
header.data[1] = c;
|
|
offset_header++;
|
|
} else {
|
|
offset_header = 0;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 2: {
|
|
switch (c) {
|
|
case TABBY_PRINTER_PACKET_INIT:
|
|
case TABBY_PRINTER_PACKET_CANCEL: {
|
|
response.status = TABBY_PRINTER_OK;
|
|
offset_sheet = 0;
|
|
}
|
|
|
|
case TABBY_PRINTER_PACKET_JOB:
|
|
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;
|
|
} else {
|
|
offset_header++;
|
|
}
|
|
|
|
sum_calc += c;
|
|
|
|
break;
|
|
}
|
|
|
|
default: {
|
|
if (offset_body < header.size) {
|
|
if (header.type == TABBY_PRINTER_PACKET_JOB) {
|
|
job.data[offset_body] = c;
|
|
} else if (header.type == TABBY_PRINTER_PACKET_DATA) {
|
|
sheet[offset_sheet++] = c;
|
|
}
|
|
|
|
offset_body++;
|
|
|
|
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) {
|
|
tabby_avr_printer_send_sheet(sheet,
|
|
offset_sheet,
|
|
&response);
|
|
|
|
tabby_avr_printer_job_start(&job, &response);
|
|
|
|
offset_sheet = 0;
|
|
} else if (header.type == TABBY_PRINTER_PACKET_INQUIRY) {
|
|
tabby_avr_printer_send_inquiry(&response);
|
|
}
|
|
|
|
respond:
|
|
uart_putchar(response.device, NULL);
|
|
uart_putchar(response.status, NULL);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|