diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-10-28 14:31:56 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-10-28 14:31:56 +0200 |
commit | babb6dc29a5c4af60292ffad5216317d13e2a685 (patch) | |
tree | d5de3240e0112310c5ffc90ead6935333d5f9f78 | |
parent | 4cd465332087e4ab12709f28fae55df10e1a1154 (diff) |
fix off-by-one error and create macro for switching serial debug print color
-rw-r--r-- | shared/protocol.c | 2 | ||||
-rw-r--r-- | stm32f091/consts.h | 2 | ||||
-rw-r--r-- | stm32f091/esp8266.c | 3 | ||||
-rw-r--r-- | stm32f091/protocol.c | 14 | ||||
-rw-r--r-- | stm32f091/server.c | 8 | ||||
-rw-r--r-- | stm32f091/setup.c | 7 | ||||
-rw-r--r-- | stm32f091/util.h | 4 |
7 files changed, 25 insertions, 15 deletions
diff --git a/shared/protocol.c b/shared/protocol.c index e17a728..8887070 100644 --- a/shared/protocol.c +++ b/shared/protocol.c @@ -142,6 +142,6 @@ unsigned short ws_protocol_get_header_size(ws_s_protocol_res* response) { unsigned short size = 2; // comma and trailing newline if (response->success == WS_PROTOCOL_CMD_RETURN_OK) size += 2; // ok if (response->success == WS_PROTOCOL_CMD_RETURN_ERROR) size += 5; // error - size += ws_log16(response->msg->bytes); // amount of characters for message size (hex) + size += ws_log16(response->msg->bytes) + 1; // amount of characters for message size (hex) return size; }
\ No newline at end of file diff --git a/stm32f091/consts.h b/stm32f091/consts.h index 955ceea..3d2ef5a 100644 --- a/stm32f091/consts.h +++ b/stm32f091/consts.h @@ -6,7 +6,7 @@ #define WS_SERVER_MAX_CHANNELS 4 #define WS_DMA_RX_BUFFER_SIZE 100 -#define WS_DMA_TX_BUFFER_SIZE 100 +#define WS_DMA_TX_BUFFER_SIZE 1024 #define WS_PINOUT_I2C_SDA_PIN GPIO_PIN_9 #define WS_PINOUT_I2C_SDA_PORT GPIOB diff --git a/stm32f091/esp8266.c b/stm32f091/esp8266.c index 754b45b..b896567 100644 --- a/stm32f091/esp8266.c +++ b/stm32f091/esp8266.c @@ -58,8 +58,7 @@ void ws_esp8266_send(uint8_t* data, size_t size) { g_ws_esp8266_dma_tx_buffer[limited_size] = 0x00; #ifdef WS_DBG_PRINT_ESP_OVER_USART2 - uint8_t green[] = { 0x1b, 0x5b, 0x33, 0x32, 0x6d }; - HAL_UART_Transmit(&huart2, green, sizeof(green), 100); + ws_dbg_set_usart2_tty_color(2); HAL_UART_Transmit(&huart2, g_ws_esp8266_dma_tx_buffer, strlen((char*) g_ws_esp8266_dma_tx_buffer), 100); #endif diff --git a/stm32f091/protocol.c b/stm32f091/protocol.c index 21e6527..4d605fc 100644 --- a/stm32f091/protocol.c +++ b/stm32f091/protocol.c @@ -9,27 +9,29 @@ void ws_protocol_res_last_records(ws_s_protocol_parsed_req_cmd* parsed_cmd, ws_s_protocol_res* response, bool send) { static unsigned int record_amount = 0; const char* response_header = "id,temperature,humidity,atmospheric_pressure\n"; - const size_t response_line_size = sizeof("xxxx,xx,xx,xx\n"); + const unsigned int response_line_len = strlen("xxxx,xx,xx,xx\n"); if (!send) { response->success = WS_PROTOCOL_CMD_RETURN_OK; response->csh = true; response->msg = ws_bin_s_alloc(0); response->msg->bytes = 0; - record_amount = WS_MIN(record_amount, ws_backlog_get_record_count()); if (sscanf(parsed_cmd->argv[1], "%u", &record_amount) < 1) response->success = WS_PROTOCOL_CMD_RETURN_ERROR; - else response->msg->bytes = strlen(response_header) + response_line_size * record_amount; + else { + record_amount = WS_MIN(record_amount, ws_backlog_get_record_count()); + response->msg->bytes = strlen(response_header) + response_line_len * record_amount; + } ws_server_req_respond_start(0, response->msg->bytes + ws_protocol_get_header_size(response)); } else { if (response->success == WS_PROTOCOL_CMD_RETURN_ERROR) return; ws_protocol_send_data(response_header, strlen(response_header)); - char line[response_line_size]; + char line[response_line_len + 1]; // + 1 for string terminator for (unsigned int i = 0; i < record_amount; i++) { ws_s_backlog_record* record = ws_backlog_get_last_record(i); sprintf(line, "%04x,%02x,%02x,%02x\n", record->id, record->sens_temperature, record->sens_humidity, record->sens_atm_pressure); - ws_protocol_send_data(line, response_line_size - 1); // remove string terminator + ws_protocol_send_data(line, response_line_len); // remove string terminator } - ws_protocol_send_data("\r\n", 2); // test + // ws_protocol_send_data("\r\n", 2); // test } } diff --git a/stm32f091/server.c b/stm32f091/server.c index 1419da8..6225eae 100644 --- a/stm32f091/server.c +++ b/stm32f091/server.c @@ -8,6 +8,7 @@ #include "server.h" #include "setup.h" #include "consts.h" +#include "util.h" ws_s_server_parser g_ws_server_parser = { .last_response = WS_SERVER_RC_NONE, @@ -48,8 +49,7 @@ void ws_server_req_finish(unsigned int channel, bool ignore) { #define next_few_bytes_are(code) (i + sizeof(code) - 1 < size && strncmp((char*)&data[i], code, sizeof(code) - 1) == 0) void ws_server_req_incoming(uint8_t* data, size_t size) { #ifdef WS_DBG_PRINT_ESP_OVER_USART2 - uint8_t red[] = { 0x1b, 0x5b, 0x33, 0x31, 0x6d }; - HAL_UART_Transmit(&huart2, red, sizeof(red), 100); + ws_dbg_set_usart2_tty_color(1); HAL_UART_Transmit(&huart2, data, size, 100); #endif @@ -128,7 +128,6 @@ void ws_server_req_incoming(uint8_t* data, size_t size) { case WS_SERVER_LM_CIPSEND_LISTENING: { if (next_few_bytes_are("SEND OK") || next_few_bytes_are("ERROR")) { ws_server_req_respond_end(0); - // g_ws_server_parser.mode = WS_SERVER_LM_IDLE; } break; } @@ -153,8 +152,7 @@ void ws_server_buffer_send_append(uint8_t* data, size_t size) { // TODO: refactor this void ws_server_buffer_send_finish() { #ifdef WS_DBG_PRINT_ESP_OVER_USART2 - uint8_t green[] = { 0x1b, 0x5b, 0x33, 0x32, 0x6d }; - HAL_UART_Transmit(&huart2, green, sizeof(green), 100); + ws_dbg_set_usart2_tty_color(2); HAL_UART_Transmit(&huart2, g_ws_esp8266_dma_tx_buffer, g_ws_esp8266_dma_tx_buffer_size, 100); #endif diff --git a/stm32f091/setup.c b/stm32f091/setup.c index 3bf331f..1591649 100644 --- a/stm32f091/setup.c +++ b/stm32f091/setup.c @@ -9,6 +9,7 @@ #include "setup.h" #include "backlog.h" #include "server.h" +#include "util.h" I2C_HandleTypeDef hi2c1 = { .Instance = I2C1, @@ -95,6 +96,12 @@ void ws_io_setup() { ws_backlog_alloc(24 * 60); // ws_backlog_alloc(10); +#ifdef WS_DBG_PRINT_ESP_OVER_USART2 + ws_dbg_set_usart2_tty_color(7); + const char restart_str[] = "\r\n--- stm restart ---\r\n"; + HAL_UART_Transmit(&huart2, restart_str, strlen(restart_str), 100); +#endif + #ifdef WS_ESP8266_WLAN_MAC ws_esp8266_set_mac(); #endif diff --git a/stm32f091/util.h b/stm32f091/util.h index 92f093f..9f5c1ec 100644 --- a/stm32f091/util.h +++ b/stm32f091/util.h @@ -14,3 +14,7 @@ HAL_UART_Transmit(&huart2, (uint8_t*) temp, sizeof(char) * strlen(temp), HAL_MAX_DELAY); \ } +#define ws_dbg_set_usart2_tty_color(color) { \ + uint8_t sgr[] = { 0x1b, 0x5b, 0x33, 0x30 + color, 0x6d }; \ + HAL_UART_Transmit(&huart2, sgr, sizeof(sgr), 100); \ +} |