like yeah, whatever. a better state machine
This commit is contained in:
parent
ffa6882870
commit
c34a79a9fd
4 changed files with 58 additions and 67 deletions
|
@ -12,7 +12,7 @@ OBJCOPY = $(CROSS)objcopy
|
|||
OBJCOPY_FLAGS = -S
|
||||
|
||||
AVRDUDE = avrdude
|
||||
AVRDUDE_DEVICE = /dev/cu.usbmodem1411
|
||||
AVRDUDE_DEVICE = /dev/cu.usbmodem1421
|
||||
AVRDUDE_FLAGS = -c arduino -p atmega328p -b 115200 -D -P $(AVRDUDE_DEVICE)
|
||||
|
||||
HEADERS_LOCAL =
|
||||
|
|
12
bin/main.c
12
bin/main.c
|
@ -27,7 +27,8 @@ int main(int argc, char **argv) {
|
|||
int fd, status;
|
||||
|
||||
tabby_printer_packet_header header;
|
||||
tabby_printer_packet_footer footer;
|
||||
|
||||
uint16_t checksum;
|
||||
|
||||
uint8_t body[TABBY_PRINTER_MAX_PACKET_SIZE];
|
||||
|
||||
|
@ -43,7 +44,9 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
|
||||
while (1) {
|
||||
status = tabby_printer_packet_recv(fd, &header, &body, &footer);
|
||||
uint8_t value;
|
||||
|
||||
status = tabby_printer_packet_recv(fd, &header, &body, &checksum);
|
||||
|
||||
if (status < 0) {
|
||||
fprintf(stderr, "%s: %s: %s\n",
|
||||
|
@ -52,7 +55,10 @@ int main(int argc, char **argv) {
|
|||
continue;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Got a packet\n");
|
||||
value = 0x81;
|
||||
tabby_link_send(fd, &value, 1);
|
||||
value = 0x00;
|
||||
tabby_link_send(fd, &value, 1);
|
||||
}
|
||||
|
||||
tabby_link_close(fd);
|
||||
|
|
|
@ -31,25 +31,12 @@ typedef struct _tabby_printer_packet_header {
|
|||
};
|
||||
} tabby_printer_packet_header;
|
||||
|
||||
typedef struct _tabby_printer_packet_footer {
|
||||
union {
|
||||
struct {
|
||||
uint16_t checksum;
|
||||
|
||||
uint8_t peripheral,
|
||||
status;
|
||||
};
|
||||
|
||||
uint8_t data[4];
|
||||
};
|
||||
} tabby_printer_packet_footer;
|
||||
|
||||
/*
|
||||
* Methods for communicating with Game Boy as a printer
|
||||
*/
|
||||
int tabby_printer_packet_recv(int fd, tabby_printer_packet_header *header,
|
||||
void *body,
|
||||
tabby_printer_packet_footer *footer);
|
||||
uint16_t *checksum);
|
||||
|
||||
int tabby_printer_response_send(int fd, uint8_t device, uint8_t status);
|
||||
|
||||
|
|
|
@ -22,69 +22,67 @@ static uint16_t checksum(tabby_printer_packet_header *header, void *body) {
|
|||
|
||||
int tabby_printer_packet_recv(int fd, tabby_printer_packet_header *header,
|
||||
void *body,
|
||||
tabby_printer_packet_footer *footer) {
|
||||
uint16_t *sum) {
|
||||
ssize_t len;
|
||||
|
||||
size_t i = 0,
|
||||
b = 0,
|
||||
f = 0;
|
||||
b = 0;
|
||||
|
||||
uint8_t value,
|
||||
last;
|
||||
|
||||
int started = 0;
|
||||
uint8_t value;
|
||||
|
||||
memset(header, '\0', sizeof(*header));
|
||||
memset(footer, '\0', sizeof(*footer));
|
||||
|
||||
*sum = 0;
|
||||
|
||||
while ((len = tabby_link_recv(fd, &value, 1)) >= 0) {
|
||||
if (started) {
|
||||
if (i == 2) {
|
||||
if (i == 0) {
|
||||
if (value == 0x83) {
|
||||
header->preamble[0] = value;
|
||||
i++;
|
||||
} else {
|
||||
errno = EIO;
|
||||
|
||||
goto error_io;
|
||||
}
|
||||
} else if (i == 1) {
|
||||
if (value == 0x33) {
|
||||
header->preamble[1] = value;
|
||||
i++;
|
||||
} else {
|
||||
errno = EIO;
|
||||
i = 0;
|
||||
goto error_io;
|
||||
}
|
||||
} else if (i == 2) {
|
||||
header->type = value;
|
||||
i++;
|
||||
} else if (i == 3) {
|
||||
header->compression = value;
|
||||
} else if (i == 4 || i == 5) {
|
||||
header->size <<= 8;
|
||||
header->size |= value;
|
||||
} else if (i > 5 && b < header->size) {
|
||||
((uint8_t *)body)[b++] = value;
|
||||
} else if (b >= header->size && f < 2) {
|
||||
footer->checksum <<= 8;
|
||||
footer->checksum |= value;
|
||||
|
||||
f++;
|
||||
} else if (f >= 2) {
|
||||
footer->data[f++] = value;
|
||||
|
||||
if (f == sizeof(*footer)) {
|
||||
if (checksum(header, body) != footer->checksum) {
|
||||
errno = EIO;
|
||||
|
||||
goto error_io;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (last == 0x88 && value == 0x33) {
|
||||
started = 1;
|
||||
i = 2;
|
||||
b = 0;
|
||||
f = 0;
|
||||
} else if (i == PACKET_RECV_ERROR_THRESHOLD) {
|
||||
errno = EIO;
|
||||
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
|
||||
last = value;
|
||||
|
||||
i++;
|
||||
} else if (i == 4) {
|
||||
header->size = value;
|
||||
i++;
|
||||
} else if (i == 5) {
|
||||
header->size |= value << 8;
|
||||
i++;
|
||||
b = 0;
|
||||
} else if (b < header->size) {
|
||||
((uint8_t *)body)[b++] = value;
|
||||
} else if (b == header->size) {
|
||||
*sum = value;
|
||||
b++;
|
||||
} else if (b == header->size + 1) {
|
||||
*sum |= value << 8;
|
||||
|
||||
if (checksum(header, body) != *sum) {
|
||||
errno = EIO;
|
||||
|
||||
goto error_io;
|
||||
}
|
||||
|
||||
return errno = 0;
|
||||
}
|
||||
}
|
||||
|
||||
error_io:
|
||||
return -errno;
|
||||
|
|
Loading…
Add table
Reference in a new issue