add UART transmit command

This commit is contained in:
King Kévin 2022-10-13 13:03:51 +02:00
parent 2de071edb2
commit fb2dda3fb3
1 changed files with 31 additions and 0 deletions

31
main.c
View File

@ -124,6 +124,20 @@ void puth(uint8_t h)
putn(h & 0x0f);
}
// ASCII to nibble (0xff if invalid)
static uint8_t a2n(char c)
{
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 0xa;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 0xa;
} else {
return 0xff;
}
}
// set duty cycle of red LED
void led_red(uint16_t bightness)
{
@ -433,6 +447,23 @@ void main(void)
if (uart_c) { // data received over UART
putc(uart_c); // echo back
if ('\r' == uart_c || '\n' == uart_c) { // end of line received
if (uart_used >= 9 && 'T' == uart_cmd[0]) { // transmit command
bool code_valid = true; // verify it it's really a hex string
uint32_t code = 0; // parsed code
for (uint8_t i = 0; i < 8; i++) {
uint32_t n = a2n(uart_cmd[1 + i]);
if (n > 0xf) {
code_valid = false;
break;
} else {
code |= (n << (i * 4));
}
}
if (code_valid) {
nec_transmit(code); // transmit code
puts("\r\ncode transmitted\r\n");
}
}
uart_used = 0; // reset buffer
} else if (uart_used < ARRAY_LENGTH(uart_cmd)) {
uart_cmd[uart_used++] = uart_c;