summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-05-26 18:56:41 +0200
committerlonkaars <loek@pipeframe.xyz>2022-05-26 18:56:41 +0200
commite75c48a4d79a838844aab071c81b2879aab2b5ee (patch)
tree175599e4df47cad9cb655080f3cbd0029e16973f
parent515ea1b4db5528a08deed32463f77cd03a7f7139 (diff)
check naming rules and moved some things around
-rw-r--r--robot/io.c20
-rw-r--r--robot/io.h49
-rw-r--r--robot/mode_dirc.c7
-rw-r--r--robot/setup.c4
-rw-r--r--shared/protocol.h53
5 files changed, 75 insertions, 58 deletions
diff --git a/robot/io.c b/robot/io.c
index f98c87a..8b34aba 100644
--- a/robot/io.c
+++ b/robot/io.c
@@ -1,9 +1,11 @@
-#include "io.h"
+#include <string.h>
+
#include "../shared/consts.h"
+#include "io.h"
#include "modes.h"
#include "orangutan_shim.h"
-w2_io_all g_w2_io;
+w2_s_io_all g_w2_io;
void w2_io_main() {
g_w2_io.button[0].pressed = get_single_debounced_button_press(BUTTON_A);
@@ -11,11 +13,11 @@ void w2_io_main() {
g_w2_io.button[2].pressed = get_single_debounced_button_press(BUTTON_C);
g_w2_io.button[3].pressed = get_single_debounced_button_press(TOP_BUTTON);
g_w2_io.button[4].pressed = get_single_debounced_button_press(BOTTOM_BUTTON);
+
unsigned int sensor_values[5];
qtr_read(sensor_values, QTR_EMITTERS_ON);
- for (int i = 0; i < 5; i++) {
- g_w2_io.qtr[i].range = sensor_values[i];
- }
+ for (int i = 0; i < 5; i++) g_w2_io.qtr[i].range = sensor_values[i];
+
// TODO average voltage over mutiple samples sensor
g_w2_io.battery.charge_level = analog_read(W2_BATTERY_PIN);
g_w2_io.front_distance.detection = analog_read(W2_FRONT_SENSOR_PIN);
@@ -24,6 +26,10 @@ void w2_io_main() {
set_motors(g_w2_io.motor_left.speed, g_w2_io.motor_right.speed);
red_led(g_w2_io.led_red.on);
green_led(g_w2_io.led_green.on);
- print(g_w2_io.lcd.text);
-};
+ // TODO don't do this every cycle
+ char text_copy[17];
+ memcpy((char *)text_copy, g_w2_io.lcd.text, 16);
+ text_copy[16] = 0x00;
+ print(text_copy);
+};
diff --git a/robot/io.h b/robot/io.h
index ce67c73..3d91799 100644
--- a/robot/io.h
+++ b/robot/io.h
@@ -1,48 +1,9 @@
#pragma once
-#include <stdbool.h>
-#include <stdint.h>
-#include <stdlib.h>
-void w2_io_main();
-
-typedef struct {
- bool pressed;
-} w2_s_i_push;
-
-typedef struct {
- uint16_t range;
-} w2_s_i_contrast;
-
-typedef struct {
- uint16_t detection;
-} w2_s_i_distance;
-
-typedef struct {
- uint16_t charge_level;
-} w2_s_i_battery;
+#include "../shared/protocol.h"
-typedef struct {
- int speed;
-} w2_s_o_motor;
-
-typedef struct {
- bool on;
-} w2_s_o_led;
-
-typedef struct {
- char text[17]; // 16 chars + '\0'
-} w2_s_o_display;
-
-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;
+/** i/o module main */
+void w2_io_main();
- 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_io_all;
+/** global struct containing all i/o */
+extern w2_s_io_all g_w2_io;
diff --git a/robot/mode_dirc.c b/robot/mode_dirc.c
index 0bbf3cb..9ed9fef 100644
--- a/robot/mode_dirc.c
+++ b/robot/mode_dirc.c
@@ -1,7 +1,10 @@
#include "mode_dirc.h"
-#include "orangutan_shim.h"
+#include "io.h"
int16_t g_w2_mode_dirc_motor_l = 0;
int16_t g_w2_mode_dirc_motor_r = 0;
-void w2_mode_dirc() { set_motors(g_w2_mode_dirc_motor_l, g_w2_mode_dirc_motor_r); }
+void w2_mode_dirc() {
+ g_w2_io.motor_left.speed = g_w2_mode_dirc_motor_l;
+ g_w2_io.motor_right.speed = g_w2_mode_dirc_motor_r;
+}
diff --git a/robot/setup.c b/robot/setup.c
index 06c8fa4..2c426a3 100644
--- a/robot/setup.c
+++ b/robot/setup.c
@@ -16,10 +16,6 @@ void w2_setup_main() {
// check endianness
g_w2_endianness = *_ptest;
- // reset underside leds
- red_led(0);
- green_led(0);
-
// clear lcd
clear();
diff --git a/shared/protocol.h b/shared/protocol.h
index 057f67d..b997ec6 100644
--- a/shared/protocol.h
+++ b/shared/protocol.h
@@ -1,5 +1,6 @@
#pragma once
+#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
@@ -48,6 +49,56 @@ 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 {
+ int 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;
@@ -134,7 +185,7 @@ typedef struct {
typedef struct {
uint8_t opcode;
- // TODO: sensor data
+ w2_s_io_all io;
} w2_s_cmd_sens_tx;
typedef struct {