Merge branch 'master' of scm.xan.host:/var/scm/tabby
This commit is contained in:
commit
ee796c04e2
4 changed files with 58 additions and 67 deletions
|
@ -12,7 +12,7 @@ OBJCOPY = $(CROSS)objcopy
|
||||||
OBJCOPY_FLAGS = -S
|
OBJCOPY_FLAGS = -S
|
||||||
|
|
||||||
AVRDUDE = avrdude
|
AVRDUDE = avrdude
|
||||||
AVRDUDE_DEVICE = /dev/ttyACM0
|
AVRDUDE_DEVICE = /dev/cu.usbmodem1421
|
||||||
AVRDUDE_FLAGS = -c arduino -p atmega328p -b 115200 -D -P $(AVRDUDE_DEVICE)
|
AVRDUDE_FLAGS = -c arduino -p atmega328p -b 115200 -D -P $(AVRDUDE_DEVICE)
|
||||||
|
|
||||||
HEADERS_LOCAL =
|
HEADERS_LOCAL =
|
||||||
|
|
12
bin/main.c
12
bin/main.c
|
@ -27,7 +27,8 @@ int main(int argc, char **argv) {
|
||||||
int fd, status;
|
int fd, status;
|
||||||
|
|
||||||
tabby_printer_packet_header header;
|
tabby_printer_packet_header header;
|
||||||
tabby_printer_packet_footer footer;
|
|
||||||
|
uint16_t checksum;
|
||||||
|
|
||||||
uint8_t body[TABBY_PRINTER_MAX_PACKET_SIZE];
|
uint8_t body[TABBY_PRINTER_MAX_PACKET_SIZE];
|
||||||
|
|
||||||
|
@ -43,7 +44,9 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
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) {
|
if (status < 0) {
|
||||||
fprintf(stderr, "%s: %s: %s\n",
|
fprintf(stderr, "%s: %s: %s\n",
|
||||||
|
@ -52,7 +55,10 @@ int main(int argc, char **argv) {
|
||||||
continue;
|
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);
|
tabby_link_close(fd);
|
||||||
|
|
|
@ -31,25 +31,12 @@ typedef struct _tabby_printer_packet_header {
|
||||||
};
|
};
|
||||||
} 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
|
* Methods for communicating with Game Boy as a printer
|
||||||
*/
|
*/
|
||||||
int tabby_printer_packet_recv(int fd, tabby_printer_packet_header *header,
|
int tabby_printer_packet_recv(int fd, tabby_printer_packet_header *header,
|
||||||
void *body,
|
void *body,
|
||||||
tabby_printer_packet_footer *footer);
|
uint16_t *checksum);
|
||||||
|
|
||||||
int tabby_printer_response_send(int fd, uint8_t device, uint8_t status);
|
int tabby_printer_response_send(int fd, uint8_t device, uint8_t status);
|
||||||
|
|
||||||
|
|
|
@ -22,70 +22,68 @@ static uint16_t checksum(tabby_printer_packet_header *header, void *body) {
|
||||||
|
|
||||||
int tabby_printer_packet_recv(int fd, tabby_printer_packet_header *header,
|
int tabby_printer_packet_recv(int fd, tabby_printer_packet_header *header,
|
||||||
void *body,
|
void *body,
|
||||||
tabby_printer_packet_footer *footer) {
|
uint16_t *sum) {
|
||||||
ssize_t len;
|
ssize_t len;
|
||||||
|
|
||||||
size_t i = 0,
|
size_t i = 0,
|
||||||
b = 0,
|
b = 0;
|
||||||
f = 0;
|
|
||||||
|
|
||||||
uint8_t value,
|
uint8_t value;
|
||||||
last = 0;
|
|
||||||
|
|
||||||
int started = 0;
|
|
||||||
|
|
||||||
memset(header, '\0', sizeof(*header));
|
memset(header, '\0', sizeof(*header));
|
||||||
memset(footer, '\0', sizeof(*footer));
|
|
||||||
|
*sum = 0;
|
||||||
|
|
||||||
while ((len = tabby_link_recv(fd, &value, 1)) >= 0) {
|
while ((len = tabby_link_recv(fd, &value, 1)) >= 0) {
|
||||||
if (started) {
|
if (i == 0) {
|
||||||
if (i == 2) {
|
if (value == 0x83) {
|
||||||
header->type = value;
|
header->preamble[0] = value;
|
||||||
} else if (i == 3) {
|
i++;
|
||||||
header->compression = value;
|
} else {
|
||||||
} 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;
|
errno = EIO;
|
||||||
|
|
||||||
goto error_io;
|
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;
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
last = value;
|
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return errno = 0;
|
|
||||||
|
|
||||||
error_io:
|
error_io:
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue