Cut superfluous code down to the bone
This commit is contained in:
parent
0e37c57a3a
commit
eba12edc65
5 changed files with 6 additions and 75 deletions
27
avr/main.c
27
avr/main.c
|
@ -16,28 +16,11 @@ static volatile tabby_printer_packet_header header = {
|
||||||
.size = 0
|
.size = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
static volatile uint8_t body[648];
|
|
||||||
|
|
||||||
static volatile uint16_t sum = 0;
|
static volatile uint16_t sum = 0;
|
||||||
|
|
||||||
static volatile uint16_t i = 0,
|
static volatile uint16_t i = 0,
|
||||||
b = 0;
|
b = 0;
|
||||||
|
|
||||||
static inline uint16_t checksum() {
|
|
||||||
uint16_t ret = header.data[2]
|
|
||||||
+ header.data[3]
|
|
||||||
+ header.data[4]
|
|
||||||
+ header.data[5];
|
|
||||||
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
for (i=0; i<header.size; i++) {
|
|
||||||
ret += ((uint8_t *)body)[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* SPI byte receipt interrupt vector
|
* SPI byte receipt interrupt vector
|
||||||
*/
|
*/
|
||||||
|
@ -107,18 +90,14 @@ ISR(SPI_STC_vect) {
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
if (b < header.size) {
|
if (b < header.size) {
|
||||||
body[b++] = value;
|
b++;
|
||||||
} else if (b == header.size) {
|
|
||||||
sum >>= 8;
|
|
||||||
sum |= value << 8;
|
|
||||||
|
|
||||||
|
SPDR = 0;
|
||||||
|
} else if (b == header.size) {
|
||||||
b++;
|
b++;
|
||||||
|
|
||||||
SPDR = 0;
|
SPDR = 0;
|
||||||
} else if (b == header.size + 1) {
|
} else if (b == header.size + 1) {
|
||||||
sum >>= 8;
|
|
||||||
sum |= value << 8;
|
|
||||||
|
|
||||||
b++;
|
b++;
|
||||||
|
|
||||||
SPDR = 0x81;
|
SPDR = 0x81;
|
||||||
|
|
|
@ -44,8 +44,6 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
uint8_t value;
|
|
||||||
|
|
||||||
status = tabby_printer_packet_recv(fd, &header, &body, &checksum);
|
status = tabby_printer_packet_recv(fd, &header, &body, &checksum);
|
||||||
|
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
|
@ -64,9 +62,6 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_link_send:
|
|
||||||
tabby_link_close(fd);
|
|
||||||
|
|
||||||
error_link_open:
|
error_link_open:
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,12 +17,4 @@ int tabby_link_open(const char *dev);
|
||||||
|
|
||||||
int tabby_link_close(int fd);
|
int tabby_link_close(int fd);
|
||||||
|
|
||||||
ssize_t tabby_link_send(int fd, void *buf, uint16_t len);
|
|
||||||
|
|
||||||
ssize_t tabby_link_recv(int fd, void *buf, uint16_t len);
|
|
||||||
|
|
||||||
int tabby_link_set_clock_source(int fd, tabby_clock_source source);
|
|
||||||
|
|
||||||
int tabby_link_set_clock_speed(int fd, tabby_clock_speed speed);
|
|
||||||
|
|
||||||
#endif /* _TABBY_LINK_H */
|
#endif /* _TABBY_LINK_H */
|
||||||
|
|
37
src/link.c
37
src/link.c
|
@ -11,7 +11,7 @@ int tabby_link_open(const char *dev) {
|
||||||
goto error_open;
|
goto error_open;
|
||||||
}
|
}
|
||||||
|
|
||||||
attr.c_cflag = CS8 | CREAD;
|
attr.c_cflag = CS8 | CREAD;
|
||||||
attr.c_ispeed = TABBY_LINK_BAUD;
|
attr.c_ispeed = TABBY_LINK_BAUD;
|
||||||
attr.c_ospeed = TABBY_LINK_BAUD;
|
attr.c_ospeed = TABBY_LINK_BAUD;
|
||||||
attr.c_iflag = IGNPAR;
|
attr.c_iflag = IGNPAR;
|
||||||
|
@ -37,38 +37,3 @@ error_open:
|
||||||
int tabby_link_close(int fd) {
|
int tabby_link_close(int fd) {
|
||||||
return close(fd);
|
return close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t tabby_link_send(int fd, void *buf, uint16_t len) {
|
|
||||||
uint8_t header[3] = {
|
|
||||||
TABBY_COMMAND_SEND, (len & 0xff00) >> 8, len & 0xff
|
|
||||||
};
|
|
||||||
|
|
||||||
if (write(fd, &header, sizeof(header)) < 0) {
|
|
||||||
goto error_write_header;
|
|
||||||
}
|
|
||||||
|
|
||||||
return write(fd, buf, (size_t)len);
|
|
||||||
|
|
||||||
error_write_header:
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t tabby_link_recv(int fd, void *buf, uint16_t len) {
|
|
||||||
return read(fd, buf, (size_t)len);
|
|
||||||
}
|
|
||||||
|
|
||||||
int tabby_link_set_clock_source(int fd, tabby_clock_source source) {
|
|
||||||
uint8_t packet[2] = {
|
|
||||||
TABBY_COMMAND_CLOCK_SOURCE, source
|
|
||||||
};
|
|
||||||
|
|
||||||
return write(fd, &packet, sizeof(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
int tabby_link_set_clock_speed(int fd, tabby_clock_speed speed) {
|
|
||||||
uint8_t packet[2] = {
|
|
||||||
TABBY_COMMAND_CLOCK_SPEED, speed
|
|
||||||
};
|
|
||||||
|
|
||||||
return write(fd, &packet, sizeof(packet));
|
|
||||||
}
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ int tabby_printer_packet_recv(int fd, tabby_printer_packet_header *header,
|
||||||
|
|
||||||
*sum = 0;
|
*sum = 0;
|
||||||
|
|
||||||
while ((len = tabby_link_recv(fd, &value, 1)) >= 0) {
|
while ((len = read(fd, &value, 1)) >= 0) {
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
if (value == 0x88) {
|
if (value == 0x88) {
|
||||||
header->preamble[0] = value;
|
header->preamble[0] = value;
|
||||||
|
@ -91,5 +91,5 @@ int tabby_printer_response_send(int fd, uint8_t device, uint8_t status) {
|
||||||
device, status
|
device, status
|
||||||
};
|
};
|
||||||
|
|
||||||
return tabby_link_send(fd, &response, sizeof(response));
|
return write(fd, &response, sizeof(response));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue