aboutsummaryrefslogtreecommitdiff
path: root/shared/serial_parse.c
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-05-26 15:39:31 +0200
committerlonkaars <loek@pipeframe.xyz>2022-05-26 15:39:31 +0200
commit60f07661602a5dfe8e39b8038964b38bddcb33a5 (patch)
tree0b257acda0797a13cd09e7df2d16a6da0a6aef11 /shared/serial_parse.c
parent333eea840a17d0f8ecf0110d952df2857fea4da0 (diff)
parentf073c9d3848dab915bed4844e9d13684aa5e23eb (diff)
merge dev into master
Diffstat (limited to 'shared/serial_parse.c')
-rw-r--r--shared/serial_parse.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/shared/serial_parse.c b/shared/serial_parse.c
new file mode 100644
index 0000000..89c5809
--- /dev/null
+++ b/shared/serial_parse.c
@@ -0,0 +1,44 @@
+#include <stdbool.h>
+#include <string.h>
+
+#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) {
+ 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;
+
+ static bool attentive = false;
+ static bool listening = false;
+
+ 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 (!listening) return;
+ current_message[current_message_index++] = byte;
+
+ complete_message_length = w2_cmd_sizeof(current_message, current_message_index);
+
+ if (current_message_index == complete_message_length) {
+ w2_cmd_handler(current_message, current_message_index);
+
+ memset(&current_message, 0, W2_SERIAL_READ_BUFFER_SIZE);
+ current_message_index = 0;
+ complete_message_length = 1;
+ attentive = false;
+ listening = false;
+ return;
+ }
+}