diff options
Diffstat (limited to 'shared')
-rw-r--r-- | shared/bin.h | 3 | ||||
-rw-r--r-- | shared/bool.h | 9 | ||||
-rw-r--r-- | shared/consts.h | 24 | ||||
-rw-r--r-- | shared/errors.h | 4 | ||||
-rw-r--r-- | shared/io.h | 59 | ||||
-rw-r--r-- | shared/makefile | 4 | ||||
-rw-r--r-- | shared/os.mk | 12 | ||||
-rw-r--r-- | shared/protocol.c | 19 | ||||
-rw-r--r-- | shared/protocol.h | 60 | ||||
-rw-r--r-- | shared/semver.h | 2 | ||||
-rw-r--r-- | shared/serial_parse.c | 23 | ||||
-rw-r--r-- | shared/serial_parse.h | 13 | ||||
-rw-r--r-- | shared/util.h | 2 |
13 files changed, 137 insertions, 97 deletions
diff --git a/shared/bin.h b/shared/bin.h index af3a249..48485c8 100644 --- a/shared/bin.h +++ b/shared/bin.h @@ -1,4 +1,7 @@ #pragma once + +/** @file bin.h */ + /** * helper file for binary data * diff --git a/shared/bool.h b/shared/bool.h new file mode 100644 index 0000000..9ea97f7 --- /dev/null +++ b/shared/bool.h @@ -0,0 +1,9 @@ +#pragma once + +#include <stdint.h> + +/** @file bool.h */ + +typedef uint8_t bool; +#define false 0 /* NOLINT */ +#define true 1 /* NOLINT */ diff --git a/shared/consts.h b/shared/consts.h index c65e50f..cdd96b3 100644 --- a/shared/consts.h +++ b/shared/consts.h @@ -1,28 +1,38 @@ #pragma once +/** @file consts.h */ + #ifndef W2_BUILD_STR // is defined by CFLAGS += -DW2_BUILD_STR in makefile #define W2_BUILD_STR ("????????") #endif -/** max logic module execution time in milliseconds */ -#define W2_MAX_MODULE_CYCLE_MS (20) +#if !defined W2_HOST_WIN32 && !defined W2_HOST_LINUX +#define W2_HOST_UNKNOWN +#warning "host operating system unknown" +#endif + /** 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 */ #define W2_BATTERY_PIN 6 /** side-facing distance sensor pinout */ -#define W2_SIDE_SENSOR_PIN 7
\ No newline at end of file +#define W2_SIDE_SENSOR_PIN 7 diff --git a/shared/errors.h b/shared/errors.h index ac8e95f..344a506 100644 --- a/shared/errors.h +++ b/shared/errors.h @@ -1,5 +1,7 @@ #pragma once +/** @file errors.h */ + #include <stdint.h> #define W2_E_TYPE_MASK (0b11 << 6) @@ -43,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/io.h b/shared/io.h new file mode 100644 index 0000000..584ad1e --- /dev/null +++ b/shared/io.h @@ -0,0 +1,59 @@ +#pragma once + +#include <stdio.h> + +#include "bool.h" + +#pragma pack(push, 1) + +/** momentary button input struct */ +typedef struct { + bool pressed; +} w2_s_i_push; + +/** qtr contrast sensor input struct */ +typedef struct { + uint16_t range; +} w2_s_i_contrast; + +/** distance sensor input struct */ +typedef struct { + uint16_t detection; +} w2_s_i_distance; + +/** battery input struct */ +typedef struct { + uint16_t charge_level; +} w2_s_i_battery; + +/** motor output struct */ +typedef struct { + int16_t speed; +} w2_s_o_motor; + +/** underside led output struct */ +typedef struct { + bool on; +} w2_s_o_led; + +/** lcd output struct */ +typedef struct { + char text[16]; +} w2_s_o_display; + +/** struct containing all i/o */ +typedef struct { + w2_s_i_push button[5]; + w2_s_i_contrast qtr[5]; + w2_s_i_distance front_distance; + w2_s_i_distance side_distance; + w2_s_i_battery battery; + + w2_s_o_motor motor_left; + w2_s_o_motor motor_right; + w2_s_o_led led_red; + w2_s_o_led led_green; + w2_s_o_display lcd; +} w2_s_io_all; + +#pragma pack(pop) diff --git a/shared/makefile b/shared/makefile index cfdf8ac..e5a6ff6 100644 --- a/shared/makefile +++ b/shared/makefile @@ -1,5 +1,9 @@ SOURCES += $(wildcard ../shared/*.c) HEADERS += $(wildcard ../shared/*.h) +# debug build info string +BUILD_STR=$(shell git update-index -q --refresh; git describe --tags --dirty='*' --broken='x' | cut -c1-20) +CFLAGS += -DW2_BUILD_STR=\"$(BUILD_STR)\" + clean:: rm -f ../shared/*.o diff --git a/shared/os.mk b/shared/os.mk new file mode 100644 index 0000000..e4ede42 --- /dev/null +++ b/shared/os.mk @@ -0,0 +1,12 @@ +# os info
+OS=$(strip $(shell uname -o))
+ifeq ($(OS),GNU/Linux)
+CFLAGS += -DW2_HOST_LINUX
+LINUX := true
+TARGET := a.out
+endif
+ifeq ($(OS),Msys)
+CFLAGS += -DW2_HOST_WIN32
+WIN32 := true
+TARGET := a.exe
+endif
\ No newline at end of file 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); -} diff --git a/shared/protocol.h b/shared/protocol.h index 7aa5e9f..93e53f4 100644 --- a/shared/protocol.h +++ b/shared/protocol.h @@ -1,14 +1,14 @@ #pragma once +/** @file protocol.h */ + #include <stdint.h> #include <stdlib.h> #include "bin.h" +#include "bool.h" #include "consts.h" - -typedef uint8_t bool; -#define false 0 /* NOLINT */ -#define true 1 /* NOLINT */ +#include "io.h" #define W2_SERIAL_START_BYTE 0xff @@ -52,56 +52,6 @@ typedef enum { #pragma pack(push, 1) -/** momentary button input struct */ -typedef struct { - bool pressed; -} w2_s_i_push; - -/** qtr contrast sensor input struct */ -typedef struct { - uint16_t range; -} w2_s_i_contrast; - -/** distance sensor input struct */ -typedef struct { - uint16_t detection; -} w2_s_i_distance; - -/** battery input struct */ -typedef struct { - uint16_t charge_level; -} w2_s_i_battery; - -/** motor output struct */ -typedef struct { - int16_t speed; -} w2_s_o_motor; - -/** underside led output struct */ -typedef struct { - bool on; -} w2_s_o_led; - -/** lcd output struct */ -typedef struct { - char text[16]; -} w2_s_o_display; - -/** struct containing all i/o */ -typedef struct { - w2_s_i_push button[5]; - w2_s_i_contrast qtr[5]; - w2_s_i_distance front_distance; - w2_s_i_distance side_distance; - w2_s_i_battery battery; - - w2_s_o_motor motor_left; - w2_s_o_motor motor_right; - w2_s_o_led led_red; - w2_s_o_led led_green; - w2_s_o_display lcd; -} w2_s_io_all; - typedef struct { uint8_t opcode; uint8_t id; @@ -164,6 +114,8 @@ typedef struct { uint8_t status; } w2_s_cmd_bomd_tx; +#define W2_CMD_SRES_RX_TYPE_REINITGS 0 +#define W2_CMD_SRES_RX_TYPE_PREVMODE 1 typedef struct { uint8_t opcode; uint8_t type; diff --git a/shared/semver.h b/shared/semver.h index a99ae61..6a1da79 100644 --- a/shared/semver.h +++ b/shared/semver.h @@ -1,5 +1,7 @@ #pragma once +/** @file semver.h */ + #include <stdint.h> typedef struct { 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 a1c8fb9..0816ea1 100644 --- a/shared/serial_parse.h +++ b/shared/serial_parse.h @@ -1,8 +1,17 @@ #pragma once +/** @file serial_parse.h */ + #include <stdint.h> +#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); diff --git a/shared/util.h b/shared/util.h index 9e4d8ac..465e043 100644 --- a/shared/util.h +++ b/shared/util.h @@ -1,5 +1,7 @@ #pragma once +/** @file util.h */ + #define W2_MIN(a, b) (((a) < (b)) ? (a) : (b)) #define W2_MAX(a, b) (((a) > (b)) ? (a) : (b)) #define W2_RANGE(min, val, max) W2_MIN(max, W2_MAX(val, min)) |