Skip to content

Commit af7245d

Browse files
committed
Dynamically register input drivers
1 parent 444cd91 commit af7245d

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

robots/include/worx_robot.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
#define WORX_ROBOT_HPP
33

44
#include <drivers/charger/bq_2576/bq_2576.hpp>
5+
#include <drivers/input/gpio_input_driver.hpp>
6+
#include <drivers/input/worx_input_driver.hpp>
7+
#include <services.hpp>
58

69
#include "robot.hpp"
710

11+
using namespace xbot::driver::input;
12+
813
class WorxRobot : public MowerRobot {
914
public:
1015
void InitPlatform() override;
@@ -29,6 +34,8 @@ class WorxRobot : public MowerRobot {
2934

3035
private:
3136
BQ2576 charger_{};
37+
GpioInputDriver gpio_driver_{input_service};
38+
WorxInputDriver worx_driver_{input_service};
3239
};
3340

3441
#endif // WORX_ROBOT_HPP

robots/src/worx_robot.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ void WorxRobot::InitPlatform() {
77
InitMotors();
88
charger_.setI2C(&I2CD1);
99
power_service.SetDriver(&charger_);
10+
input_service.RegisterInputDriver("gpio", &gpio_driver_);
11+
input_service.RegisterInputDriver("worx", &worx_driver_);
1012
}
1113

1214
bool WorxRobot::IsHardwareSupported() {

src/services/input_service/input_service.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <xbot-service/Lock.hpp>
66

7+
#include "../../drivers/input/gpio_input_driver.hpp"
78
#include "../../globals.hpp"
89
#include "../../json_stream.hpp"
910

@@ -121,7 +122,8 @@ void InputService::OnStop() {
121122
void InputService::OnLoop(uint32_t, uint32_t) {
122123
eventmask_t events = chEvtGetAndClearEvents(Events::ids_to_mask({Events::GPIO_TRIGGERED}));
123124
if (events & EVENT_MASK(Events::GPIO_TRIGGERED)) {
124-
gpio_driver_.tick();
125+
auto* gpio_driver = static_cast<GpioInputDriver*>(drivers_.find("gpio")->second);
126+
gpio_driver->tick();
125127
}
126128
}
127129

src/services/input_service/input_service.hpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
#include <InputServiceBase.hpp>
99

10-
#include "drivers/input/gpio_input_driver.hpp"
11-
#include "drivers/input/worx_input_driver.hpp"
10+
#include "../../drivers/input/input_driver.hpp"
1211

1312
using namespace xbot::driver::input;
1413
using namespace xbot::service;
@@ -23,6 +22,10 @@ class InputService : public InputServiceBase {
2322

2423
mutex_t mutex_;
2524

25+
void RegisterInputDriver(const char* id, InputDriver* driver) {
26+
drivers_.emplace(id, driver);
27+
}
28+
2629
const etl::ivector<Input*>& GetAllInputs() const {
2730
return all_inputs_;
2831
}
@@ -34,13 +37,7 @@ class InputService : public InputServiceBase {
3437
void OnInputChanged(Input& input);
3538

3639
private:
37-
GpioInputDriver gpio_driver_{*this};
38-
WorxInputDriver worx_driver_{*this};
39-
40-
const etl::flat_map<etl::string<4>, InputDriver*, 2> drivers_ = {
41-
{"gpio", &gpio_driver_},
42-
{"worx", &worx_driver_},
43-
};
40+
etl::flat_map<etl::string<4>, InputDriver*, 2> drivers_;
4441

4542
// Must not have more than 64 inputs due to the size of various bitmasks.
4643
etl::vector<Input*, 30> all_inputs_;

0 commit comments

Comments
 (0)