Skip to content

Commit 444cd91

Browse files
committed
Detect and send key events
1 parent 9bea838 commit 444cd91

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/drivers/input/input_driver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "input_driver.hpp"
22

33
#include "globals.hpp"
4+
#include "services.hpp"
45

56
namespace xbot::driver::input {
67

@@ -10,6 +11,7 @@ bool Input::Update(bool new_active) {
1011
if (new_active) {
1112
active_since = system::getTimeMicros();
1213
}
14+
input_service.OnInputChanged(*this);
1315
chEvtBroadcastFlags(&mower_events, MowerEvents::INPUTS_CHANGED);
1416
return true;
1517
} else {

src/services/input_service/input_service.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,22 @@ void InputService::SendStatus() {
137137
SendActiveInputs(active_inputs_mask);
138138
CommitTransaction();
139139
}
140+
141+
bool InputService::SendInputEventHelper(Input& input, InputEventType type) {
142+
uint8_t payload[2] = {input.idx, static_cast<uint8_t>(type)};
143+
return SendInputEvent(payload, 2);
144+
}
145+
146+
void InputService::OnInputChanged(Input& input) {
147+
// TODO: This will be called in the middle of the driver's update loop.
148+
// We might want to queue the raw changes and send them at a safe time.
149+
StartTransaction();
150+
if (input.IsActive()) {
151+
SendInputEventHelper(input, InputEventType::ACTIVE);
152+
} else {
153+
SendInputEventHelper(input, InputEventType::INACTIVE);
154+
// TODO: This obviously needs debouncing, more variants and configuration.
155+
SendInputEventHelper(input, input.ActiveDuration() >= 500'000 ? InputEventType::LONG : InputEventType::SHORT);
156+
}
157+
CommitTransaction();
158+
}

src/services/input_service/input_service.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class InputService : public InputServiceBase {
3131
SendStatus();
3232
}
3333

34+
void OnInputChanged(Input& input);
35+
3436
private:
3537
GpioInputDriver gpio_driver_{*this};
3638
WorxInputDriver worx_driver_{*this};
@@ -49,6 +51,7 @@ class InputService : public InputServiceBase {
4951
void OnStop() override;
5052
void OnLoop(uint32_t now_micros, uint32_t last_tick_micros) override;
5153

54+
bool SendInputEventHelper(Input& input, InputEventType type);
5255
void SendStatus();
5356
ServiceSchedule tick_schedule_{*this, 200'000,
5457
XBOT_FUNCTION_FOR_METHOD(InputService, &InputService::SendStatus, this)};

0 commit comments

Comments
 (0)