Skip to content

Commit edcdf3b

Browse files
committed
Connect YardForce CoverUI to InputService
1 parent 905fc78 commit edcdf3b

File tree

11 files changed

+88
-16
lines changed

11 files changed

+88
-16
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE
8888
src/drivers/input/input_driver.cpp
8989
src/drivers/input/gpio_input_driver.cpp
9090
src/drivers/input/worx_input_driver.cpp
91+
src/drivers/input/yard_force_input_driver.cpp
9192
$<$<CONFIG:Debug>:src/drivers/input/simulated_input_driver.cpp>
9293
# YardForce Cover UI Driver
9394
src/drivers/ui/YardForceCoverUI/yard_force_cover_ui_driver.cpp

robots/src/yardforce_robot.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ void YardForceRobot::InitPlatform() {
66
InitMotors();
77
charger_.setI2C(&I2CD1);
88
power_service.SetDriver(&charger_);
9+
input_service.RegisterInputDriver("yardforce", &cover_ui_driver_.GetInputDriver());
910
cover_ui_driver_.Start(&UARTD7);
1011
}
1112

services

src/drivers/input/gpio_input_driver.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef GPIO_INPUT_DRIVER_HPP
22
#define GPIO_INPUT_DRIVER_HPP
33

4-
#include <etl/vector.h>
54
#include <hal.h>
65

76
#include "input_driver.hpp"

src/drivers/input/input_driver.hpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace xbot::driver::input {
1212
struct Input {
1313
enum { VIRTUAL = 255 };
14+
enum class Type : uint8_t { BUTTON, HALL };
1415

1516
// Configuration
1617
uint8_t idx;
@@ -26,6 +27,18 @@ struct Input {
2627
struct {
2728
uint8_t bit;
2829
} worx;
30+
31+
struct {
32+
Input::Type type;
33+
union {
34+
struct {
35+
uint8_t id;
36+
} button;
37+
struct {
38+
uint8_t bit;
39+
} hall;
40+
};
41+
} yardforce;
2942
};
3043

3144
// State
@@ -86,20 +99,23 @@ class InputDriver {
8699
public:
87100
virtual ~InputDriver() = default;
88101
explicit InputDriver() = default;
102+
89103
void AddInput(Input* input);
90104
void ClearInputs();
105+
InputIterable Inputs() {
106+
return InputIterable{inputs_head_};
107+
}
108+
91109
virtual bool OnInputConfigValue(lwjson_stream_parser_t* jsp, const char* key, lwjson_stream_type_t type,
92110
Input& input) = 0;
111+
93112
virtual bool OnStart() {
94113
return true;
95114
};
96115
virtual void OnStop(){};
97116

98-
protected:
117+
private:
99118
Input* inputs_head_ = nullptr;
100-
InputIterable Inputs() {
101-
return InputIterable{inputs_head_};
102-
}
103119
};
104120
} // namespace xbot::driver::input
105121

src/drivers/input/simulated_input_driver.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef SIMULATED_INPUT_DRIVER_HPP
22
#define SIMULATED_INPUT_DRIVER_HPP
33

4-
#include <etl/vector.h>
5-
64
#include "input_driver.hpp"
75

86
namespace xbot::driver::input {

src/drivers/input/worx_input_driver.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef WORX_INPUT_DRIVER_HPP
22
#define WORX_INPUT_DRIVER_HPP
33

4-
#include <etl/vector.h>
54
#include <hal.h>
65

76
#include <services.hpp>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "yard_force_input_driver.hpp"
2+
3+
#include <ulog.h>
4+
5+
#include <json_stream.hpp>
6+
7+
namespace xbot::driver::input {
8+
9+
bool YardForceInputDriver::OnInputConfigValue(lwjson_stream_parser_t *jsp, const char *key, lwjson_stream_type_t type,
10+
Input &input) {
11+
// TODO: We probably want to take strings here and map them to the ID.
12+
if (strcmp(key, "button") == 0) {
13+
input.yardforce.type = Input::Type::BUTTON;
14+
return JsonGetNumber(jsp, type, input.yardforce.button.id);
15+
} else if (strcmp(key, "hall") == 0) {
16+
input.yardforce.type = Input::Type::HALL;
17+
return JsonGetNumber(jsp, type, input.yardforce.hall.bit);
18+
}
19+
ULOG_ERROR("Unknown attribute \"%s\"", key);
20+
return false;
21+
}
22+
23+
} // namespace xbot::driver::input
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef YARDFORCE_INPUT_DRIVER_HPP
2+
#define YARDFORCE_INPUT_DRIVER_HPP
3+
4+
#include <services.hpp>
5+
6+
#include "input_driver.hpp"
7+
8+
namespace xbot::driver::input {
9+
class YardForceInputDriver : public InputDriver {
10+
public:
11+
explicit YardForceInputDriver() = default;
12+
bool OnInputConfigValue(lwjson_stream_parser_t *jsp, const char *key, lwjson_stream_type_t type,
13+
Input &input) override;
14+
};
15+
} // namespace xbot::driver::input
16+
17+
#endif // YARDFORCE_INPUT_DRIVER_HPP

src/drivers/ui/YardForceCoverUI/yard_force_cover_ui_driver.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
#include <sys/unistd.h>
44
#include <ulog.h>
55

6+
#include <json_stream.hpp>
67
#include <services.hpp>
78

89
#include "COBS.h"
910
#include "ui_board.h"
1011

12+
#define IS_BIT_SET(x, bit) ((x & (1 << bit)) != 0)
13+
1114
static constexpr uint8_t EVT_PACKET_RECEIVED = 1;
1215

1316
void YardForceCoverUIDriver::Start(UARTDriver *uart) {
@@ -203,12 +206,22 @@ void YardForceCoverUIDriver::ProcessPacket() {
203206
if (encode_decode_buf_[0] == Get_Version && size == sizeof(struct msg_get_version)) {
204207
board_found_ = true;
205208
} else if (encode_decode_buf_[0] == Get_Button && size == sizeof(struct msg_event_button)) {
206-
// TODO: Send an event to the InputService.
207-
// msg_event_button *msg = (struct msg_event_button *)encode_decode_buf_;
208-
} /* else if (encode_decode_buf_[0] == Get_Emergency && size == sizeof(struct msg_event_emergency)) {
209-
struct msg_event_emergency *msg = (struct msg_event_emergency *)encode_decode_buf_;
210-
stock_ui_emergency_state = msg->state;
211-
} else if (encode_decode_buf_[0] == Get_Rain && size == sizeof(struct msg_event_rain)) {
209+
msg_event_button *msg = (struct msg_event_button *)encode_decode_buf_;
210+
for (auto &input : input_driver_.Inputs()) {
211+
if (input.yardforce.type == Input::Type::BUTTON && input.yardforce.button.id == msg->button_id) {
212+
const bool long_press = msg->press_duration >= 1;
213+
input.InjectPress(long_press);
214+
break;
215+
}
216+
}
217+
} else if (encode_decode_buf_[0] == Get_Emergency && size == sizeof(struct msg_event_emergency)) {
218+
msg_event_emergency *msg = (struct msg_event_emergency *)encode_decode_buf_;
219+
for (auto &input : input_driver_.Inputs()) {
220+
if (input.yardforce.type == Input::Type::HALL) {
221+
input.Update(IS_BIT_SET(msg->state, input.yardforce.hall.bit));
222+
}
223+
}
224+
} /* else if (encode_decode_buf_[0] == Get_Rain && size == sizeof(struct msg_event_rain)) {
212225
struct msg_event_rain *msg = (struct msg_event_rain *)encode_decode_buf_;
213226
stock_ui_rain = (msg->value < llhl_config.rain_threshold);
214227
} else if (encode_decode_buf_[0] == Get_Subscribe && size == sizeof(struct msg_event_subscribe)) {

0 commit comments

Comments
 (0)