tabby/avr/send.c
2016-06-06 19:42:15 -05:00

156 lines
3.6 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() {
uint16_t i = 0,
b = 0,
sheet_offset = 0;
uint8_t c;
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];
uart_init();
tabby_avr_link_init_master();
sei();
while (1) {
uart_putchar('O', NULL);
c = uart_getchar(NULL);
if (c == 'K') {
break;
}
}
while (1) {
c = uart_getchar(NULL);
switch (i) {
case 0: {
if (c == TABBY_PRINTER_SYNC_1) {
header.data[0] = c;
i++;
b = 0;
}
break;
}
case 1: {
if (c == TABBY_PRINTER_SYNC_2) {
header.data[1] = c;
i++;
} else {
i = 0;
}
break;
}
case 2: {
switch (c) {
case TABBY_PRINTER_PACKET_INIT:
case TABBY_PRINTER_PACKET_CANCEL: {
sheet_offset = 0;
}
case TABBY_PRINTER_PACKET_JOB:
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;
i++;
break;
}
default: {
if (b < header.size) {
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) {
i = 0;
b = 0;
if (header.type == TABBY_PRINTER_PACKET_JOB) {
tabby_avr_printer_send_sheet(sheet,
sheet_offset,
&response);
tabby_avr_printer_job_start(&job, &response);
sheet_offset = 0;
} else if (header.type == TABBY_PRINTER_PACKET_INQUIRY) {
tabby_avr_printer_send_inquiry(&response);
}
uart_putchar(response.device, NULL);
uart_putchar(response.status, NULL);
}
}
}
}
return 0;
}