From 3bbe41a04b6053a3a3a902384850cfcbd38b0686 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 27 May 2022 11:40:05 +0200 Subject: move things around and add todo's to robot readme --- robot/readme.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'robot/readme.md') diff --git a/robot/readme.md b/robot/readme.md index b27c06f..25e730f 100644 --- a/robot/readme.md +++ b/robot/readme.md @@ -72,15 +72,15 @@ what they're supposed to do: this list will probably get updated from time to time: -- modules shouldn't create any global state variables, they should use `static` - variables instead. -- modules are run cyclically, so they shouldn't take more than +- logic modules shouldn't create any global state variables if possible, they + should use `static` variables instead +- logic modules are run cyclically, so they shouldn't take more than `W2_MAX_MODULE_CYCLE_MS` to execute (this is an arbitrary number, and may be - changed). + changed) - documentation comments should follow the [javadoc-style doxygen format](https://wiki.scilab.org/Doxygen%20documentation%20Examples) and be - placed in header (.h) files if possible. this only applies to public members - (e.g. no local variables or module-internal code). + placed in header (.h) files. this only applies to public members (e.g. no + local variables or module-internal code) - code style is mostly handled by `clang-format` and `clang-tidy`, but you should still follow these naming conventions (`` indicate placeholders): @@ -93,15 +93,15 @@ this list will probably get updated from time to time: |enum|`w2_e_`|`w2_e_errorcodes`; `w2_e_serial_commands`| this again only applies to public members. local variables should still have - short descriptive names, but shouldn't be prefixed with `w2_*`. + short descriptive names, but shouldn't be prefixed with `w2_*` - arbitrary numbers should be aliased to `#define` statements or `enum`s if - part of a series. + part of a series - general constants should be placed in `consts.h` - don't import `` directly, instead use `"orangutan_shim.h"` to keep code compatible with the simulator - don't use ``, instead use `"../shared/protocol.h"`. this makes sure that `bool` values are equal to `uint8_t`. they're functionally - identical. + identical ## todo @@ -119,6 +119,8 @@ global todo: placing the robot and pressing a button (maybe make this a seperate mode) - [ ] create labeled timer functions like nodejs `console.time()` and `console.timeEnd()` (use for serial read timeout constraint) +- [ ] `serial_parse` doesn't properly handle escaped `0xff` bytes in listen + mode ### hypervisor -- cgit v1.2.3 From 05318790dcbd6714a2adb3532e902a56a6638ca0 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 27 May 2022 12:16:53 +0200 Subject: edit robot readme --- robot/readme.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'robot/readme.md') diff --git a/robot/readme.md b/robot/readme.md index 25e730f..0c8f626 100644 --- a/robot/readme.md +++ b/robot/readme.md @@ -39,10 +39,15 @@ organizational and form more of a software 'skeleton', while the 'maze' and ┌────────┴───────┐┌─────────┴────────┐┌────────┴─────────┐┌─────┴──────┐ │ Error handling ││ I/O Read & Write ││ PC communication ││ Mode logic │ └────────────────┘└──────────────────┘└──────────────────┘└─────┬──────┘ - ┌──────────┬──────────────┬───────────────┤ - ┌───┴──┐┌──────┴────┐┌────────┴───────┐┌──────┴──────┐ - *modes* -> │ Maze ││ Warehouse ││ Emergency stop ││ Calibration │ - └──────┘└───────────┘└────────────────┘└─────────────┘ + │ + Maze ─┤ + Warehouse ─┤ + Emergency stop ─┤ + *modes* -> Line finding ─┤ + Charge station ─┤ + Direct control ─┤ + Wet floor ─┤ + Sensor calibration ─┘ ``` this diagram roughly describes how different parts of the robot software are @@ -64,7 +69,7 @@ what they're supposed to do: |emergency stop |`mode_halt `|may 31|Fiona| stops all execution until emergency mode is reset by software or user| |line finding |`mode_lcal `|may 31|Fiona| find line by turning on own axis if lost| |charge station |`mode_chrg `|may 31|Fiona| go to the charging station transition in the grid, and continue until a black circle is found| -|direct control |`mode_dirc `|may 31|Loek| respond to [DIRC](../protocol.md#DIRC) commands| +|direct control |`mode_dirc `|done|Loek| respond to [DIRC](../protocol.md#DIRC) commands| |wet floor |`mode_spin `|may 31|Fiona| spin uncontrollably (simulating wet floor??)| |sensor calibration|`mode_scal `|may 31|Jorn & Abdullaahi| calibrate underside uv sensors| -- cgit v1.2.3 From 4c4d045329c4a149bae0b53952c39c14243e1870 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 27 May 2022 12:53:42 +0200 Subject: throw error on noisy serial channel --- robot/- | 0 robot/hypervisor.c | 21 +++++++++++++++------ robot/hypervisor.h | 8 ++++++++ robot/readme.md | 2 +- robot/sercomm.c | 21 +++++++++++++++++++++ robot/sim.c | 4 ++-- robot/sim.h | 6 ++++++ shared/consts.h | 15 +++++++++------ shared/errors.h | 2 -- shared/protocol.c | 19 ------------------- 10 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 robot/- (limited to 'robot/readme.md') diff --git a/robot/- b/robot/- new file mode 100644 index 0000000..e69de29 diff --git a/robot/hypervisor.c b/robot/hypervisor.c index 0baa406..1fd3ac2 100644 --- a/robot/hypervisor.c +++ b/robot/hypervisor.c @@ -6,12 +6,13 @@ #include "orangutan_shim.h" #include "sercomm.h" -uint64_t g_w2_hypervisor_cycles = 0; -uint64_t g_w2_hypervisor_uptime_ms = 0; -unsigned long g_w2_hypervisor_ema_sercomm_ms = 0; -unsigned long g_w2_hypervisor_ema_errcatch_ms = 0; -unsigned long g_w2_hypervisor_ema_io_ms = 0; -unsigned long g_w2_hypervisor_ema_mode_ms = 0; +uint64_t g_w2_hypervisor_cycles = 0; +uint64_t g_w2_hypervisor_uptime_ms = 0; +unsigned long g_w2_hypervisor_ema_sercomm_ms = 0; +unsigned long g_w2_hypervisor_ema_errcatch_ms = 0; +unsigned long g_w2_hypervisor_ema_io_ms = 0; +unsigned long g_w2_hypervisor_ema_mode_ms = 0; +uint64_t g_w2_hypervisor_timers[W2_HYPERVISOR_TIMER_COUNT] = {0}; void w2_hypervisor_main() { #ifdef W2_SIM @@ -51,3 +52,11 @@ void w2_hypervisor_main() { g_w2_hypervisor_cycles++; } + +void w2_hypervisor_time_start(uint8_t label) { + g_w2_hypervisor_timers[label] = g_w2_hypervisor_uptime_ms; +} + +uint64_t w2_hypervisor_time_end(uint8_t label) { + return g_w2_hypervisor_uptime_ms - g_w2_hypervisor_timers[label]; +} diff --git a/robot/hypervisor.h b/robot/hypervisor.h index b5dc0ab..589d324 100644 --- a/robot/hypervisor.h +++ b/robot/hypervisor.h @@ -4,6 +4,9 @@ #include +/** amount of parallel timers */ +#define W2_HYPERVISOR_TIMER_COUNT (1) + extern uint64_t g_w2_hypervisor_cycles; extern uint64_t g_w2_hypervisor_uptime_ms; @@ -18,3 +21,8 @@ extern unsigned long g_w2_hypervisor_ema_mode_ms; * stores global variables and controls when other modules run */ void w2_hypervisor_main(); + +/** start timer with label `label` */ +void w2_hypervisor_time_start(uint8_t label); +/** stop timer with label `label` */ +uint64_t w2_hypervisor_time_end(uint8_t label); diff --git a/robot/readme.md b/robot/readme.md index 0c8f626..168be02 100644 --- a/robot/readme.md +++ b/robot/readme.md @@ -43,7 +43,7 @@ organizational and form more of a software 'skeleton', while the 'maze' and Maze ─┤ Warehouse ─┤ Emergency stop ─┤ - *modes* -> Line finding ─┤ + *logic modes* -> Line finding ─┤ Charge station ─┤ Direct control ─┤ Wet floor ─┤ diff --git a/robot/sercomm.c b/robot/sercomm.c index 2736030..83d6419 100644 --- a/robot/sercomm.c +++ b/robot/sercomm.c @@ -3,6 +3,7 @@ #include "../shared/bin.h" #include "../shared/serial_parse.h" +#include "errcatch.h" #include "hypervisor.h" #include "io.h" #include "mode_dirc.h" @@ -51,6 +52,26 @@ void w2_sercomm_append_msg(w2_s_bin *data) { g_w2_sercomm_index = next_index; } +void w2_cmd_handler(uint8_t data[W2_SERIAL_READ_BUFFER_SIZE], uint8_t data_length) { + w2_s_bin *copy = w2_bin_s_alloc(data_length, data); + void (*handler)(w2_s_bin *) = g_w2_cmd_handlers[data[0]]; + + if (handler == NULL) { +#ifdef W2_SIM + // TODO throw warning + simwarn("unknown serial message with code 0x%02x\n", data[0]); +#endif + w2_errcatch_throw(W2_E_WARN_SERIAL_NOISY); + } else { +#ifdef W2_SIM + w2_sim_print_serial(copy); +#endif + handler(copy); + } + + free(copy); +} + void w2_cmd_ping_rx(w2_s_bin *data) { w2_s_cmd_ping_rx *message = malloc(w2_cmd_sizeof(data->data, data->bytes)); memcpy(message, data->data, data->bytes); diff --git a/robot/sim.c b/robot/sim.c index ba9aa4a..4efdc4d 100644 --- a/robot/sim.c +++ b/robot/sim.c @@ -105,7 +105,7 @@ void serial_send(char* message, unsigned int length) { } void serial_receive_ring(char* buffer, unsigned char size) { - simprintfunc("serial_receive_ring", "0x%016llx, %u", (uint64_t) buffer, size); + simprintfunc("serial_receive_ring", PTR_FMT ", %u", (uint64_t) buffer, size); } unsigned char serial_get_received_bytes() { @@ -170,7 +170,7 @@ unsigned char get_single_debounced_button_press(unsigned char buttons) { } void qtr_read(unsigned int* sensor_values, unsigned char read_mode) { - simprintfunc("qtr_read", "0x%016llx, %s", (uint64_t) sensor_values, read_mode == QTR_EMITTERS_ON ? "QTR_EMITTERS_ON" : "???"); + simprintfunc("qtr_read", PTR_FMT ", %s", (uint64_t) sensor_values, read_mode == QTR_EMITTERS_ON ? "QTR_EMITTERS_ON" : "???"); sensor_values[0] = 0; sensor_values[1] = 0; sensor_values[2] = 0; diff --git a/robot/sim.h b/robot/sim.h index 76b57f8..aa587cd 100644 --- a/robot/sim.h +++ b/robot/sim.h @@ -54,6 +54,12 @@ extern bool g_w2_sim_headless; COL_CYN name COL_RST "(" COL_YEL fmt COL_RST ")\n", ##__VA_ARGS__); } #define simwarn(message, ...) if (DBG_ENABLE_SIMWARN) { simprintf(COL_YEL "[WARN] " COL_RST message, ##__VA_ARGS__); } #define siminfo(message, ...) if (DBG_ENABLE_SIMINFO) { simprintf(COL_MAG "[INFO] " COL_RST message, ##__VA_ARGS__); } +#ifdef W2_HOST_LINUX +#define PTR_FMT "0x%016lx" +#endif +#ifdef W2_HOST_WIN32 +#define PTR_FMT "0x%016llx" +#endif #define BUTTON_A 0 #define BUTTON_B 1 diff --git a/shared/consts.h b/shared/consts.h index 50b16b9..cdd96b3 100644 --- a/shared/consts.h +++ b/shared/consts.h @@ -12,21 +12,24 @@ #warning "host operating system unknown" #endif -/** max logic module execution time in milliseconds */ -#define W2_MAX_MODULE_CYCLE_MS (20) /** serial baud rate (bit/s) */ #define W2_SERIAL_BAUD (9600) +/** size of input (receive) buffer (in bytes) */ +#define W2_SERIAL_READ_BUFFER_SIZE (255) + /** size of the error handling buffer (in errors, not bytes) */ #define W2_ERROR_BUFFER_SIZE (16) /** size of the serial communication buffer (in messages, not bytes) */ #define W2_SERCOMM_BUFFER_SIZE (16) -/** size of input (receive) buffer (in bytes) */ -#define W2_SERIAL_READ_BUFFER_SIZE (255) -/** exponential moving average new measurement weight (double 0-1) */ -#define W2_EMA_WEIGHT (0.10) /** size of mode history buffer */ #define W2_MODE_HISTORY_BUFFER_SIZE (4) +/** max logic module execution time in milliseconds */ +#define W2_MAX_MODULE_CYCLE_MS (20) + +/** exponential moving average new measurement weight (double 0-1) */ +#define W2_EMA_WEIGHT (0.10) + /** front-facing distance sensor pinout */ #define W2_FRONT_SENSOR_PIN 5 /** battery voltage sensor pinout */ diff --git a/shared/errors.h b/shared/errors.h index 4c9f7c8..344a506 100644 --- a/shared/errors.h +++ b/shared/errors.h @@ -45,8 +45,6 @@ typedef enum { W2_E_WARN_SERCOMM_BUFFER_FULL = 0x06 | W2_E_TYPE_WARN, /** semver minor version doesn't match */ W2_E_WARN_VERSION_INCOMPATIBLE = 0x07 | W2_E_TYPE_WARN, - /** serial byte took to long to receive */ - W2_E_WARN_SERIAL_TIMEOUT = 0x08 | W2_E_TYPE_WARN, /** unknown message encountered (noisy channel?) */ W2_E_WARN_SERIAL_NOISY = 0x09 | W2_E_TYPE_WARN, /** mode history index out of bounds */ diff --git a/shared/protocol.c b/shared/protocol.c index fcc9fa4..9be1d31 100644 --- a/shared/protocol.c +++ b/shared/protocol.c @@ -74,22 +74,3 @@ size_t w2_cmd_expt_tx_sizeof(w2_s_bin *data) { size_t w2_cmd_mcfg_rx_sizeof(w2_s_bin *data) { return W2_DYN_MEMBER_SIZEOF(w2_s_cmd_mcfg_rx, 3, w2_s_cmd_mcfg_feature); } - -void w2_cmd_handler(uint8_t data[W2_SERIAL_READ_BUFFER_SIZE], uint8_t data_length) { - w2_s_bin *copy = w2_bin_s_alloc(data_length, data); - void (*handler)(w2_s_bin *) = g_w2_cmd_handlers[data[0]]; - - if (handler == NULL) { -#ifdef W2_SIM - // TODO throw warning - simwarn("unknown serial message with code 0x%02x\n", data[0]); -#endif - } else { -#ifdef W2_SIM - w2_sim_print_serial(copy); -#endif - handler(copy); - } - - free(copy); -} -- cgit v1.2.3 From def257fcd9769d3572dbf5bdd076e4ce470fc8ec Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 27 May 2022 13:49:35 +0200 Subject: remove windows sim compatibility and fix 0xff byte handling --- robot/readme.md | 16 ++++++---------- robot/sercomm.c | 15 ++++++++++----- robot/sim.c | 35 +++-------------------------------- robot/sim.h | 19 ------------------- robot/tests/dirc.bin | Bin 6 -> 7 bytes robot/tests/ping.bin | Bin 3 -> 4 bytes shared/serial_parse.c | 23 ++++++++++------------- shared/serial_parse.h | 11 +++++++++-- 8 files changed, 38 insertions(+), 81 deletions(-) (limited to 'robot/readme.md') diff --git a/robot/readme.md b/robot/readme.md index 168be02..e6ab294 100644 --- a/robot/readme.md +++ b/robot/readme.md @@ -112,19 +112,15 @@ this list will probably get updated from time to time: global todo: -- [ ] start robot in calibration mode - [ ] assume robot starts in maze -- [ ] 'crosswalk' transition detection in seperate file (used by grid and maze - mode) +- [ ] 'crosswalk' transition detection and line following in seperate file + (used by grid, maze, and charge mode) - [ ] client software architecture -- [x] mode 'return' buffer -- [x] clear global timer at start of cycle instead of just for mode selection - module (for last ping time measurement) -- [ ] calibrate (line-detecting) light sensors in setup.c, or manually by - placing the robot and pressing a button (maybe make this a seperate mode) -- [ ] create labeled timer functions like nodejs `console.time()` and +- [ ] enter mode_scal by placing the robot on a white surface and pressing a + button +- [x] create labeled timer functions like nodejs `console.time()` and `console.timeEnd()` (use for serial read timeout constraint) -- [ ] `serial_parse` doesn't properly handle escaped `0xff` bytes in listen +- [x] `serial_parse` doesn't properly handle escaped `0xff` bytes in listen mode ### hypervisor diff --git a/robot/sercomm.c b/robot/sercomm.c index 83d6419..e0ea39d 100644 --- a/robot/sercomm.c +++ b/robot/sercomm.c @@ -26,16 +26,21 @@ void w2_sercomm_main() { #endif // read and parse data while (serial_get_received_bytes() != g_w2_serial_buffer_index) { - w2_serial_parse(g_w2_serial_buffer[g_w2_serial_buffer_index]); + if (!w2_serial_parse(g_w2_serial_buffer[g_w2_serial_buffer_index])) + w2_errcatch_throw(W2_E_WARN_SERIAL_NOISY); g_w2_serial_buffer_index = (g_w2_serial_buffer_index + 1) % W2_SERIAL_READ_BUFFER_SIZE; } // send data while (g_w2_sercomm_offset != g_w2_sercomm_index) { - w2_s_bin *data = g_w2_sercomm_buffer[g_w2_sercomm_offset]; - char *data_cast = malloc(data->bytes); - memcpy(data_cast, data->data, data->bytes); - serial_send(data_cast, data->bytes); + w2_s_bin *data = g_w2_sercomm_buffer[g_w2_sercomm_offset]; +#ifdef W2_SIM + w2_sim_print_serial(data); +#endif + for (uint8_t i = 0; i < data->bytes; i++) { + uint8_t byte = data->data[i]; + byte == 0xff ? serial_send("\xff\xff", 2) : serial_send((char *)&byte, 1); + } g_w2_sercomm_offset = (g_w2_sercomm_offset + 1) % W2_SERCOMM_BUFFER_SIZE; } } diff --git a/robot/sim.c b/robot/sim.c index 4efdc4d..6283694 100644 --- a/robot/sim.c +++ b/robot/sim.c @@ -3,17 +3,7 @@ #include #include #include - -#ifdef W2_HOST_LINUX #include -#endif - -#ifdef W2_HOST_WIN32 -// garbage os compatibility -#include -#include -#define STDIN_FILENO 0 -#endif #include "sim.h" #include "../shared/consts.h" @@ -21,12 +11,7 @@ #include "sercomm.h" #include "errcatch.h" -#ifdef W2_HOST_LINUX struct timespec reference_time; // NOLINT -#endif -#ifdef W2_HOST_WIN32 -DWORD reference_time; // NOLINT -#endif bool g_w2_sim_headless = false; static const char* const W2_CMD_NAMES[] = { @@ -96,16 +81,11 @@ void serial_send(char* message, unsigned int length) { return; } - if (DBG_ENABLE_PRINTFUNC) simprintfunc("serial_send", ", %u", length); - - if (!DBG_ENABLE_SERIAL) return; - w2_s_bin *bin = w2_bin_s_alloc(length, (uint8_t*) message); - w2_sim_print_serial(bin); - free(bin); + if (DBG_ENABLE_PRINTFUNC) simprintfunc("serial_send", "<%u byte%s>", length, length == 1 ? "" : "s"); } void serial_receive_ring(char* buffer, unsigned char size) { - simprintfunc("serial_receive_ring", PTR_FMT ", %u", (uint64_t) buffer, size); + simprintfunc("serial_receive_ring", "0x%016lx, %u", (uint64_t) buffer, size); } unsigned char serial_get_received_bytes() { @@ -118,21 +98,12 @@ void w2_sim_setup(int argc, char **argv) { g_w2_sim_headless = true; // disable echo and enable raw mode -#ifdef W2_HOST_LINUX struct termios term; tcgetattr(STDIN_FILENO, &term); term.c_lflag &= ~(ECHO | ICANON); term.c_cc[VTIME] = 0; term.c_cc[VMIN] = 0; tcsetattr(STDIN_FILENO, 0, &term); -#endif -#ifdef W2_HOST_WIN32 - DWORD mode; - HANDLE console = GetStdHandle(STD_INPUT_HANDLE); - - GetConsoleMode(console, &mode); - SetConsoleMode(console, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT)); -#endif // debug error // w2_errcatch_throw(W2_E_WARN_BATTERY_LOW); @@ -170,7 +141,7 @@ unsigned char get_single_debounced_button_press(unsigned char buttons) { } void qtr_read(unsigned int* sensor_values, unsigned char read_mode) { - simprintfunc("qtr_read", PTR_FMT ", %s", (uint64_t) sensor_values, read_mode == QTR_EMITTERS_ON ? "QTR_EMITTERS_ON" : "???"); + simprintfunc("qtr_read", "0x%016lx, %s", (uint64_t) sensor_values, read_mode == QTR_EMITTERS_ON ? "QTR_EMITTERS_ON" : "???"); sensor_values[0] = 0; sensor_values[1] = 0; sensor_values[2] = 0; diff --git a/robot/sim.h b/robot/sim.h index aa587cd..249414d 100644 --- a/robot/sim.h +++ b/robot/sim.h @@ -24,7 +24,6 @@ extern bool g_w2_sim_headless; #define DBG_BYTES_PER_LINE 16 // debug colors -#ifdef W2_HOST_LINUX #define COL_BLK "\e[0;30m" #define COL_RED "\e[0;31m" #define COL_GRN "\e[0;32m" @@ -34,18 +33,6 @@ extern bool g_w2_sim_headless; #define COL_CYN "\e[0;36m" #define COL_WHT "\e[0;37m" #define COL_RST "\e[0m" -#endif -#ifdef W2_HOST_WIN32 -#define COL_BLK "" -#define COL_RED "" -#define COL_GRN "" -#define COL_YEL "" -#define COL_BLU "" -#define COL_MAG "" -#define COL_CYN "" -#define COL_WHT "" -#define COL_RST "" -#endif // debug stdout print macros #define simprintf(message, ...) if (!g_w2_sim_headless) printf(COL_RED "[SIM] " COL_RST message, ##__VA_ARGS__) @@ -54,12 +41,6 @@ extern bool g_w2_sim_headless; COL_CYN name COL_RST "(" COL_YEL fmt COL_RST ")\n", ##__VA_ARGS__); } #define simwarn(message, ...) if (DBG_ENABLE_SIMWARN) { simprintf(COL_YEL "[WARN] " COL_RST message, ##__VA_ARGS__); } #define siminfo(message, ...) if (DBG_ENABLE_SIMINFO) { simprintf(COL_MAG "[INFO] " COL_RST message, ##__VA_ARGS__); } -#ifdef W2_HOST_LINUX -#define PTR_FMT "0x%016lx" -#endif -#ifdef W2_HOST_WIN32 -#define PTR_FMT "0x%016llx" -#endif #define BUTTON_A 0 #define BUTTON_B 1 diff --git a/robot/tests/dirc.bin b/robot/tests/dirc.bin index 1aea35c..8d141e8 100644 Binary files a/robot/tests/dirc.bin and b/robot/tests/dirc.bin differ diff --git a/robot/tests/ping.bin b/robot/tests/ping.bin index 456804e..2bbe45e 100644 Binary files a/robot/tests/ping.bin and b/robot/tests/ping.bin differ diff --git a/shared/serial_parse.c b/shared/serial_parse.c index 3bc8e3b..b1b4f50 100644 --- a/shared/serial_parse.c +++ b/shared/serial_parse.c @@ -3,8 +3,7 @@ #include "consts.h" #include "serial_parse.h" -// TODO: give this function last time of byte, and measure if >5ms, throw warning -void w2_serial_parse(uint8_t byte) { +bool w2_serial_parse(uint8_t byte) { static uint8_t current_message[W2_SERIAL_READ_BUFFER_SIZE] = {0}; static uint8_t current_message_index = 0; static uint8_t complete_message_length = 2; @@ -14,18 +13,14 @@ void w2_serial_parse(uint8_t byte) { if (byte == W2_SERIAL_START_BYTE) { attentive = !attentive; - // if (attentive && listening) { - // current_message[current_message_index++] = byte; - // } - } else { - // activate listen after non-0xff byte after 0xff - if (attentive && !listening) { - attentive = false; - listening = true; - } + if (attentive && listening) return W2_SERIAL_READ_SUCCESS; + } else if (attentive) { + attentive = false; + listening = !listening; + if (!listening) return W2_SERIAL_READ_FAILURE; } - if (!listening) return; + if (!listening) return W2_SERIAL_READ_SUCCESS; current_message[current_message_index++] = byte; complete_message_length = w2_cmd_sizeof(current_message, current_message_index); @@ -38,6 +33,8 @@ void w2_serial_parse(uint8_t byte) { complete_message_length = 1; attentive = false; listening = false; - return; + return W2_SERIAL_READ_SUCCESS; } + + return W2_SERIAL_READ_SUCCESS; } diff --git a/shared/serial_parse.h b/shared/serial_parse.h index 03c420f..0816ea1 100644 --- a/shared/serial_parse.h +++ b/shared/serial_parse.h @@ -4,7 +4,14 @@ #include +#include "bool.h" #include "protocol.h" -/** parse serial data byte by byte */ -void w2_serial_parse(uint8_t byte); +#define W2_SERIAL_READ_SUCCESS true +#define W2_SERIAL_READ_FAILURE false + +/** + * parse serial data byte by byte + * @return true if read success, false if read fails + */ +bool w2_serial_parse(uint8_t byte); -- cgit v1.2.3