diff --git a/ex02/include/uart.h b/ex02/include/uart.h index ed6eb55..f775645 100644 --- a/ex02/include/uart.h +++ b/ex02/include/uart.h @@ -12,6 +12,9 @@ void uart_send_u8(uint8_t val); void uart_send_u16(uint16_t val); void uart_send_u32(uint32_t val); + +void uart_send_i16(int16_t val); + void uart_send_u8_hex(uint8_t val); void uart_send_u16_hex(uint16_t val); diff --git a/ex02/src/main.c b/ex02/src/main.c index af73b64..b93ab7b 100644 --- a/ex02/src/main.c +++ b/ex02/src/main.c @@ -42,6 +42,7 @@ typedef struct command { union cmd_type { uint32_t u32; uint16_t u16; + int16_t i16; char c32[32]; } args; } command; @@ -63,7 +64,7 @@ typedef struct node { char magic[3]; // RED char tag[33]; // BLUE uint32_t id; // GREEN - uint16_t priority; // YELLOW + int16_t priority; // YELLOW uint16_t crc; // PURPLE } node; @@ -217,8 +218,11 @@ bool parse_command(char* str, command* cmd) { return uart_sendstring("SET_PRIO failed space \r\n"), true; while (str[i] == ' ') i++; - uint16_t value = 0; - bool seen = false; + int32_t value = 0; + bool seen = false; + bool neg = false; + if (str[i] == '-') + (neg = true, i++); for (; (str[i] >= '0' && str[i] <= '9'); i++) { seen = true; // overflow ? what is that ?! @@ -233,7 +237,7 @@ bool parse_command(char* str, command* cmd) { } cmd->ty = SET_PRIO; - cmd->args.u16 = value; + cmd->args.i16 = neg ? (int16_t)-value : (int16_t)value; return false; } CMD_CHECK(str, "SET_TAG") { @@ -469,7 +473,7 @@ int main(void) { uart_sendstring("Integrity check Good\r\n"); uart_sendstring("ID: "), uart_send_u32(node.id), uart_sendstring("\r\n"); - uart_sendstring("Priority: "), uart_send_u16(node.priority), + uart_sendstring("Priority: "), uart_send_i16(node.priority), uart_sendstring("\r\n"); uart_sendstring("TAG: \""), uart_sendstring(c), uart_sendstring("\"\r\n"); uart_sendstring("SLOT: "), uart_send_u8(current_slot), uart_sendstring("\r\n"); @@ -486,7 +490,7 @@ int main(void) { case SET_PRIO: { if (!read_node(current_slot, &node)) init_default_node(&node); - node.priority = cmd.args.u16; + node.priority = cmd.args.i16; node_set_crc(&node); write_node_reallocating(¤t_slot, &node); break; diff --git a/ex02/src/uart.c b/ex02/src/uart.c index 079a743..bb9f1b1 100644 --- a/ex02/src/uart.c +++ b/ex02/src/uart.c @@ -88,6 +88,18 @@ void uart_send_u16(uint16_t val) { uart_sendstring(buf); } +void uart_send_i16(int16_t val) { + if (val == 0) + return uart_tx('0'); + if (val == -32768) + return uart_sendstring("-32768"); + if (val < 0) { + uart_tx('-'); + val = -val; + } + uart_send_u16(val); +} + void uart_send_u32(uint32_t val) { if (val == 0) return uart_tx('0');