tabby/avr/send.c

155 lines
3.7 KiB
C
Raw 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() {
uint16_t i = 0,
b = 0,
sheet_offset = 0;
2016-06-05 12:37:05 -05:00
uint8_t c;
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];
2016-06-01 22:30:12 -05:00
uart_init();
tabby_avr_link_init_master();
2016-06-01 22:30:12 -05:00
sei();
while (1) {
c = uart_getchar(NULL);
switch (i) {
case 0: {
2016-06-05 12:37:05 -05:00
if (c == TABBY_PRINTER_SYNC_1) {
header.data[0] = c;
i++;
b = 0;
}
break;
}
case 1: {
2016-06-05 12:37:05 -05:00
if (c == TABBY_PRINTER_SYNC_2) {
header.data[1] = c;
i++;
} else {
i = 0;
}
break;
}
case 2: {
switch (c) {
2016-06-05 12:37:05 -05:00
case TABBY_PRINTER_PACKET_INIT:
case TABBY_PRINTER_PACKET_CANCEL: {
sheet_offset = 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;
i++;
break;
}
default: {
i = 0;
break;
}
}
break;
}
case 3: {
header.compression = c;
i++;
break;
}
case 4: {
header.size = c;
i++;
break;
}
case 5: {
header.size |= c << 8;
if (tabby_avr_printer_packet_toolarge(header.type,
header.size)) {
i = 0;
b = 0;
sheet_offset = 0;
2016-06-06 20:46:25 -05:00
} else {
i++;
}
break;
}
default: {
if (b < header.size) {
2016-06-06 01:42:17 -05:00
if (header.type == TABBY_PRINTER_PACKET_JOB) {
job.data[b] = c;
} else if (header.type == TABBY_PRINTER_PACKET_DATA) {
sheet[sheet_offset++] = c;
}
b++;
} else if (b == header.size) {
b++;
} else if (b == header.size + 1) {
2016-06-02 23:32:38 -05:00
i = 0;
b = 0;
2016-06-06 01:42:17 -05:00
if (header.type == TABBY_PRINTER_PACKET_JOB) {
tabby_avr_printer_send_sheet(sheet,
sheet_offset,
&response);
tabby_avr_printer_job_start(&job, &response);
2016-06-06 01:42:17 -05:00
sheet_offset = 0;
} else if (header.type == TABBY_PRINTER_PACKET_INQUIRY) {
tabby_avr_printer_send_inquiry(&response);
}
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;
}