Wow, Tiffany, refactoring is like, so crazy sexy cool

This commit is contained in:
XANTRONIX Development 2016-06-07 17:05:49 -05:00
parent 06503a3510
commit eb8ac6acb6
2 changed files with 79 additions and 69 deletions

View file

@ -7,9 +7,9 @@
#include <tabby/avr/link.h> #include <tabby/avr/link.h>
#include <tabby/avr.h> #include <tabby/avr.h>
static volatile tabby_printer_packet header; static volatile uint16_t offset_header = 0,
offset_body = 0,
static volatile uint16_t i, b; size_body = 0;
/* /*
* SPI byte receipt interrupt vector * SPI byte receipt interrupt vector
@ -20,13 +20,11 @@ ISR(SPI_STC_vect) {
uart_putchar(in, NULL); uart_putchar(in, NULL);
switch (i) { switch (offset_header) {
case 0: { case 0: {
if (in == TABBY_PRINTER_SYNC_1) { if (in == TABBY_PRINTER_SYNC_1) {
header.preamble[0] = in; offset_header++;
i++; offset_body = 0;
b = 0;
} }
break; break;
@ -34,61 +32,54 @@ ISR(SPI_STC_vect) {
case 1: { case 1: {
if (in == TABBY_PRINTER_SYNC_2) { if (in == TABBY_PRINTER_SYNC_2) {
header.preamble[1] = in; offset_header++;
i++;
} else { } else {
i = 0; offset_header = 0;
} }
break; break;
} }
case 2: { case 2:
header.type = in;
i++;
break;
}
case 3: { case 3: {
header.compression = in; offset_header++;
i++;
break; break;
} }
case 4: { case 4: {
header.size = in; size_body = in;
i++;
offset_header++;
break; break;
} }
case 5: { case 5: {
header.size |= in << 8; size_body |= in << 8;
if (header.size > TABBY_PRINTER_PACKET_MAX_SIZE) { if (size_body > TABBY_PRINTER_PACKET_MAX_SIZE) {
i = 0; offset_header = 0;
} else { } else {
i++; offset_header++;
} }
break; break;
} }
default: { default: {
if (b <= header.size) { if (offset_body <= size_body) {
b++; offset_body++;
} else if (b == header.size + 1) { } else if (offset_body == size_body + 1) {
b++; offset_body++;
out = TABBY_PRINTER_DEVICE_ID; out = TABBY_PRINTER_DEVICE_ID;
} else if (b == header.size + 2) { } else if (offset_body == size_body + 2) {
b++; offset_body++;
out = TABBY_PRINTER_OK; out = TABBY_PRINTER_OK;
} else if (b == header.size + 3) { } else if (offset_body == size_body + 3) {
i = 0; offset_header = 0;
} }
} }
} }

View file

@ -10,12 +10,6 @@
#include <tabby/avr.h> #include <tabby/avr.h>
int main() { int main() {
uint16_t i = 0,
b = 0,
sheet_offset = 0;
uint8_t c;
tabby_printer_packet header = { tabby_printer_packet header = {
.size = 0 .size = 0
}; };
@ -29,22 +23,28 @@ int main() {
uint8_t sheet[TABBY_PRINTER_SHEET_SIZE]; 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(); uart_init();
tabby_avr_link_init_master(); tabby_avr_link_init_master();
sei();
while (1) { while (1) {
c = uart_getchar(NULL); uint8_t c = uart_getchar(NULL);
switch (i) { switch (offset_header) {
case 0: { case 0: {
if (c == TABBY_PRINTER_SYNC_1) { if (c == TABBY_PRINTER_SYNC_1) {
header.data[0] = c; header.data[0] = c;
i++; offset_header++;
b = 0; offset_body = 0;
sum_calc = 0;
} }
break; break;
@ -53,9 +53,9 @@ int main() {
case 1: { case 1: {
if (c == TABBY_PRINTER_SYNC_2) { if (c == TABBY_PRINTER_SYNC_2) {
header.data[1] = c; header.data[1] = c;
i++; offset_header++;
} else { } else {
i = 0; offset_header = 0;
} }
break; break;
@ -65,38 +65,45 @@ int main() {
switch (c) { switch (c) {
case TABBY_PRINTER_PACKET_INIT: case TABBY_PRINTER_PACKET_INIT:
case TABBY_PRINTER_PACKET_CANCEL: { case TABBY_PRINTER_PACKET_CANCEL: {
sheet_offset = 0; response.status = TABBY_PRINTER_OK;
offset_sheet = 0;
} }
case TABBY_PRINTER_PACKET_JOB: case TABBY_PRINTER_PACKET_JOB:
case TABBY_PRINTER_PACKET_DATA: case TABBY_PRINTER_PACKET_DATA:
case TABBY_PRINTER_PACKET_INQUIRY: { case TABBY_PRINTER_PACKET_INQUIRY: {
header.type = c; header.type = c;
i++; offset_header++;
break; break;
} }
default: { default: {
i = 0; offset_header = 0;
break; break;
} }
} }
sum_calc += c;
break; break;
} }
case 3: { case 3: {
header.compression = c; header.compression = c;
i++; offset_header++;
sum_calc += c;
break; break;
} }
case 4: { case 4: {
header.size = c; header.size = c;
i++; offset_header++;
sum_calc += c;
break; break;
} }
@ -106,43 +113,55 @@ int main() {
if (tabby_avr_printer_packet_toolarge(header.type, if (tabby_avr_printer_packet_toolarge(header.type,
header.size)) { header.size)) {
i = 0; offset_header = 0;
b = 0;
sheet_offset = 0; response.status |= TABBY_PRINTER_FULL;
goto respond;
} else { } else {
i++; offset_header++;
} }
sum_calc += c;
break; break;
} }
default: { default: {
if (b < header.size) { if (offset_body < header.size) {
if (header.type == TABBY_PRINTER_PACKET_JOB) { if (header.type == TABBY_PRINTER_PACKET_JOB) {
job.data[b] = c; job.data[offset_body] = c;
} else if (header.type == TABBY_PRINTER_PACKET_DATA) { } else if (header.type == TABBY_PRINTER_PACKET_DATA) {
sheet[sheet_offset++] = c; sheet[offset_sheet++] = c;
} }
b++; offset_body++;
} else if (b == header.size) {
b++;
} else if (b == header.size + 1) {
i = 0;
b = 0;
if (header.type == TABBY_PRINTER_PACKET_JOB) { 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, tabby_avr_printer_send_sheet(sheet,
sheet_offset, offset_sheet,
&response); &response);
tabby_avr_printer_job_start(&job, &response); tabby_avr_printer_job_start(&job, &response);
sheet_offset = 0; offset_sheet = 0;
} else if (header.type == TABBY_PRINTER_PACKET_INQUIRY) { } else if (header.type == TABBY_PRINTER_PACKET_INQUIRY) {
tabby_avr_printer_send_inquiry(&response); tabby_avr_printer_send_inquiry(&response);
} }
respond:
uart_putchar(response.device, NULL); uart_putchar(response.device, NULL);
uart_putchar(response.status, NULL); uart_putchar(response.status, NULL);
} }