tabby/avr/send.c

191 lines
3.6 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>
2016-06-05 12:37:05 -05:00
#include <tabby/avr.h>
2016-06-01 22:30:12 -05:00
2016-06-05 16:45:04 -05:00
static tabby_printer_packet header;
static tabby_printer_response response;
2016-06-05 15:06:57 -05:00
static uint8_t body[TABBY_PRINTER_PACKET_MAX_SIZE];
static uint8_t sheet[TABBY_PRINTER_SHEET_SIZE];
2016-06-05 16:45:04 -05:00
static uint16_t sum;
static void spi_init() {
2016-06-05 12:37:05 -05:00
SC_OUTPUT();
SO_OUTPUT();
SI_INPUT();
SI_PULLUP();
SC_HIGH();
SO_LOW();
}
static uint8_t spi_send_byte(uint8_t value) {
2016-06-04 19:11:06 -05:00
uint8_t i, ret = 0;
for (i=0; i<8; i++) {
2016-06-05 12:37:05 -05:00
SC_LOW();
2016-06-04 01:45:57 -05:00
if (value & 0x80) {
2016-06-05 12:37:05 -05:00
SO_HIGH();
} else {
2016-06-05 12:37:05 -05:00
SO_LOW();
}
2016-06-05 12:37:05 -05:00
_delay_us(SPI_PULSE_USEC);
2016-06-04 19:11:06 -05:00
ret <<= 1;
value <<= 1;
2016-06-05 12:37:05 -05:00
if (SI_IS_HIGH()) {
ret |= 1;
}
2016-06-01 22:30:12 -05:00
2016-06-05 12:37:05 -05:00
SC_HIGH();
2016-06-04 22:12:14 -05:00
2016-06-05 12:37:05 -05:00
_delay_us(SPI_PULSE_USEC);
2016-06-01 22:30:12 -05:00
}
return ret;
}
static void spi_send_packet() {
int i;
for (i=0; i<sizeof(header); i++) {
(void)spi_send_byte(header.data[i]);
2016-06-01 22:30:12 -05:00
}
for (i=0; i<header.size; i++) {
(void)spi_send_byte(body[i]);
}
2016-06-01 22:30:12 -05:00
(void)spi_send_byte( sum & 0x00ff);
(void)spi_send_byte((sum & 0xff00) >> 8);
response.device = spi_send_byte(0);
response.status = spi_send_byte(0);
}
2016-06-01 22:30:12 -05:00
int main() {
2016-06-04 19:11:06 -05:00
uint16_t i = 0,
2016-06-05 12:37:05 -05:00
b = 0;
uint8_t c;
2016-06-01 22:30:12 -05:00
uart_init();
spi_init();
2016-06-01 22:30:12 -05:00
sei();
2016-06-04 19:11:06 -05:00
while (1) {
uart_putchar('O', NULL);
c = uart_getchar(NULL);
if (c == 'K') {
break;
}
}
2016-06-01 22:30:12 -05:00
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_JOB:
case TABBY_PRINTER_PACKET_DATA:
case TABBY_PRINTER_PACKET_CANCEL:
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) {
body[b++] = c;
} else if (b == header.size) {
sum = c;
b++;
} else if (b == header.size + 1) {
sum |= c << 8;
2016-06-02 23:32:38 -05:00
i = 0;
b = 0;
spi_send_packet();
uart_putchar(response.device, NULL);
uart_putchar(response.status, NULL);
}
}
}
2016-06-01 22:30:12 -05:00
}
return 0;
}