Skip to content

Commit e75294d

Browse files
committed
Add a way to inject key presses
1 parent df6230d commit e75294d

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/drivers/input/input_driver.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
namespace xbot::driver::input {
77

8-
bool Input::Update(bool new_active) {
8+
bool Input::Update(bool new_active, uint32_t predate) {
99
if (invert) {
1010
new_active = !new_active;
1111
}
1212
bool expected = !new_active;
1313
if (active.compare_exchange_strong(expected, new_active)) {
14-
const uint32_t now = xbot::service::system::getTimeMicros();
14+
const uint32_t now = xbot::service::system::getTimeMicros() - predate;
1515
if (new_active) {
1616
active_since = now;
1717
}
@@ -22,6 +22,15 @@ bool Input::Update(bool new_active) {
2222
return false;
2323
}
2424

25+
void Input::InjectPress(bool long_press) {
26+
InjectPress(input_service.GetPressDelay(long_press));
27+
}
28+
29+
void Input::InjectPress(uint32_t duration) {
30+
Update(true, duration);
31+
Update(false);
32+
}
33+
2534
void InputDriver::AddInput(Input* input) {
2635
if (inputs_head_ == nullptr) {
2736
inputs_head_ = input;

src/drivers/input/input_driver.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ struct Input {
3333
return active;
3434
}
3535

36-
bool Update(bool new_active);
36+
bool Update(bool new_active, uint32_t predate = 0);
37+
38+
void InjectPress(bool long_press = false);
39+
void InjectPress(uint32_t duration);
3740

3841
uint32_t ActiveDuration(const uint32_t now) const {
3942
return now - active_since;

src/services/input_service/input_service.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class InputService : public InputServiceBase {
3232

3333
etl::pair<uint16_t, uint32_t> GetEmergencyReasons(uint32_t now);
3434

35+
uint32_t GetPressDelay(bool long_press = false) {
36+
return long_press ? LongPressTime.value : DebounceTime.value;
37+
}
38+
3539
private:
3640
MUTEX_DECL(mutex_);
3741

0 commit comments

Comments
 (0)