diff --git a/avr/recv.c b/avr/recv.c index 8447356..da9fc95 100644 --- a/avr/recv.c +++ b/avr/recv.c @@ -7,9 +7,9 @@ #include #include -static volatile tabby_printer_packet header; - -static volatile uint16_t i, b; +static volatile uint16_t offset_header = 0, + offset_body = 0, + size_body = 0; /* * SPI byte receipt interrupt vector @@ -20,13 +20,11 @@ ISR(SPI_STC_vect) { uart_putchar(in, NULL); - switch (i) { + switch (offset_header) { case 0: { if (in == TABBY_PRINTER_SYNC_1) { - header.preamble[0] = in; - i++; - - b = 0; + offset_header++; + offset_body = 0; } break; @@ -34,61 +32,54 @@ ISR(SPI_STC_vect) { case 1: { if (in == TABBY_PRINTER_SYNC_2) { - header.preamble[1] = in; - i++; + offset_header++; } else { - i = 0; + offset_header = 0; } break; } - case 2: { - header.type = in; - i++; - - break; - } - + case 2: case 3: { - header.compression = in; - i++; + offset_header++; break; } case 4: { - header.size = in; - i++; + size_body = in; + + offset_header++; break; } case 5: { - header.size |= in << 8; + size_body |= in << 8; - if (header.size > TABBY_PRINTER_PACKET_MAX_SIZE) { - i = 0; + if (size_body > TABBY_PRINTER_PACKET_MAX_SIZE) { + offset_header = 0; } else { - i++; + offset_header++; } break; } default: { - if (b <= header.size) { - b++; - } else if (b == header.size + 1) { - b++; + if (offset_body <= size_body) { + offset_body++; + } else if (offset_body == size_body + 1) { + offset_body++; out = TABBY_PRINTER_DEVICE_ID; - } else if (b == header.size + 2) { - b++; + } else if (offset_body == size_body + 2) { + offset_body++; out = TABBY_PRINTER_OK; - } else if (b == header.size + 3) { - i = 0; + } else if (offset_body == size_body + 3) { + offset_header = 0; } } } diff --git a/avr/send.c b/avr/send.c index b3d1c17..aef40d9 100644 --- a/avr/send.c +++ b/avr/send.c @@ -10,12 +10,6 @@ #include int main() { - uint16_t i = 0, - b = 0, - sheet_offset = 0; - - uint8_t c; - tabby_printer_packet header = { .size = 0 }; @@ -29,22 +23,28 @@ int main() { 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(); - sei(); - while (1) { - c = uart_getchar(NULL); + uint8_t c = uart_getchar(NULL); - switch (i) { + switch (offset_header) { case 0: { if (c == TABBY_PRINTER_SYNC_1) { header.data[0] = c; - i++; + offset_header++; - b = 0; + offset_body = 0; + sum_calc = 0; } break; @@ -53,9 +53,9 @@ int main() { case 1: { if (c == TABBY_PRINTER_SYNC_2) { header.data[1] = c; - i++; + offset_header++; } else { - i = 0; + offset_header = 0; } break; @@ -65,38 +65,45 @@ int main() { switch (c) { case TABBY_PRINTER_PACKET_INIT: 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_DATA: case TABBY_PRINTER_PACKET_INQUIRY: { header.type = c; - i++; + offset_header++; break; } default: { - i = 0; + offset_header = 0; break; } } + sum_calc += c; + break; } case 3: { header.compression = c; - i++; + offset_header++; + + sum_calc += c; break; } case 4: { header.size = c; - i++; + offset_header++; + + sum_calc += c; break; } @@ -106,43 +113,55 @@ int main() { if (tabby_avr_printer_packet_toolarge(header.type, header.size)) { - i = 0; - b = 0; - sheet_offset = 0; + offset_header = 0; + + response.status |= TABBY_PRINTER_FULL; + + goto respond; } else { - i++; + offset_header++; } + sum_calc += c; + break; } default: { - if (b < header.size) { + if (offset_body < header.size) { if (header.type == TABBY_PRINTER_PACKET_JOB) { - job.data[b] = c; + job.data[offset_body] = c; } else if (header.type == TABBY_PRINTER_PACKET_DATA) { - sheet[sheet_offset++] = c; + sheet[offset_sheet++] = c; } - b++; - } else if (b == header.size) { - b++; - } else if (b == header.size + 1) { - i = 0; - b = 0; + offset_body++; - 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, - sheet_offset, + offset_sheet, &response); tabby_avr_printer_job_start(&job, &response); - sheet_offset = 0; + 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); }