Skip to content

Commit 5c10c61

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

File tree

11 files changed

+66
-13
lines changed

11 files changed

+66
-13
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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ struct Input {
2626
struct {
2727
uint8_t bit;
2828
} worx;
29+
30+
struct {
31+
uint8_t button_id;
32+
} yardforce;
2933
};
3034

3135
// State
@@ -86,20 +90,23 @@ class InputDriver {
8690
public:
8791
virtual ~InputDriver() = default;
8892
explicit InputDriver() = default;
93+
8994
void AddInput(Input* input);
9095
void ClearInputs();
96+
InputIterable Inputs() {
97+
return InputIterable{inputs_head_};
98+
}
99+
91100
virtual bool OnInputConfigValue(lwjson_stream_parser_t* jsp, const char* key, lwjson_stream_type_t type,
92101
Input& input) = 0;
102+
93103
virtual bool OnStart() {
94104
return true;
95105
};
96-
virtual void OnStop(){};
106+
virtual void OnStop() {};
97107

98-
protected:
108+
private:
99109
Input* inputs_head_ = nullptr;
100-
InputIterable Inputs() {
101-
return InputIterable{inputs_head_};
102-
}
103110
};
104111
} // namespace xbot::driver::input
105112

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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
if (strcmp(key, "button") == 0) {
12+
// TODO: We probably want to take a string here and map it to the ID.
13+
return JsonGetNumber(jsp, type, input.yardforce.button_id);
14+
}
15+
ULOG_ERROR("Unknown attribute \"%s\"", key);
16+
return false;
17+
}
18+
19+
} // 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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <sys/unistd.h>
44
#include <ulog.h>
55

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

89
#include "COBS.h"
@@ -203,8 +204,14 @@ void YardForceCoverUIDriver::ProcessPacket() {
203204
if (encode_decode_buf_[0] == Get_Version && size == sizeof(struct msg_get_version)) {
204205
board_found_ = true;
205206
} 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_;
207+
msg_event_button *msg = (struct msg_event_button *)encode_decode_buf_;
208+
for (auto &input : input_driver_.Inputs()) {
209+
if (input.yardforce.button_id == msg->button_id) {
210+
const bool long_press = msg->press_duration >= 1;
211+
input.InjectPress(long_press);
212+
break;
213+
}
214+
}
208215
} /* else if (encode_decode_buf_[0] == Get_Emergency && size == sizeof(struct msg_event_emergency)) {
209216
struct msg_event_emergency *msg = (struct msg_event_emergency *)encode_decode_buf_;
210217
stock_ui_emergency_state = msg->state;

0 commit comments

Comments
 (0)