Skip to content

Commit 905fc78

Browse files
committed
Merge branch 'feature/input-service-improvements' into feature/YF-cover-ui
2 parents aecd253 + 0e1ed14 commit 905fc78

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed

.devcontainer/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
1818
cmake make \
1919
gdb-multiarch && \
2020
ln -s /usr/bin/nm /usr/bin/nm-multiarch && \
21-
ln -s /usr/bin/objdump /usr/bin/objdump-multiarch
21+
ln -s /usr/bin/objdump /usr/bin/objdump-multiarch && \
22+
echo 'source /usr/share/bash-completion/completions/git' >> /home/dev/.bashrc
2223

2324
USER $USERNAME

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"vscode": {
77
"extensions": [
88
"ms-vscode.cpptools-extension-pack",
9-
"marus25.cortex-debug",
9+
"marus25.cortex-debug"
1010
]
1111
}
1212
}

src/drivers/input/input_driver.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,43 @@
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;
17+
input_service.OnInputChanged(*this, true, 0);
18+
} else {
19+
const uint32_t duration = ActiveDuration(now);
20+
if (emergency_reason != 0 && duration >= emergency_delay_ms * 1'000) {
21+
emergency_pending = true;
22+
}
23+
input_service.OnInputChanged(*this, false, duration);
1724
}
18-
input_service.OnInputChanged(*this, new_active, ActiveDuration(now));
1925
chEvtBroadcastFlags(&mower_events, MowerEvents::INPUTS_CHANGED);
2026
return true;
2127
}
2228
return false;
2329
}
2430

31+
bool Input::GetAndClearPendingEmergency() {
32+
bool only_if_pending = true;
33+
return emergency_pending.compare_exchange_strong(only_if_pending, false);
34+
}
35+
36+
void Input::InjectPress(bool long_press) {
37+
InjectPress(input_service.GetPressDelay(long_press));
38+
}
39+
40+
void Input::InjectPress(uint32_t duration) {
41+
Update(true, duration);
42+
Update(false);
43+
}
44+
2545
void InputDriver::AddInput(Input* input) {
2646
if (inputs_head_ == nullptr) {
2747
inputs_head_ = input;

src/drivers/input/input_driver.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@ struct Input {
3333
return active;
3434
}
3535

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

3842
uint32_t ActiveDuration(const uint32_t now) const {
3943
return now - active_since;
4044
}
4145

4246
private:
43-
etl::atomic<bool> active = false;
44-
uint32_t active_since = 0;
45-
Input* next_for_driver_ = nullptr;
47+
etl::atomic<bool> active{false};
48+
etl::atomic<bool> emergency_pending{false};
49+
uint32_t active_since{0};
50+
Input* next_for_driver_{nullptr};
4651

4752
friend class InputDriver;
4853
friend struct InputIterable;

src/services/input_service/input_service.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,13 @@ etl::pair<uint16_t, uint32_t> InputService::GetEmergencyReasons(uint32_t now) {
223223
Lock lk(&mutex_);
224224
uint16_t reasons = 0;
225225
uint32_t block_time = UINT32_MAX;
226-
for (const auto& input : all_inputs_) {
227-
// TODO: What if the input was triggered so briefly that we couldn't observe it?
228-
if (input.emergency_reason == 0 || !input.IsActive()) continue;
229-
if (TimeoutReached(input.ActiveDuration(now), input.emergency_delay_ms * 1'000, block_time)) {
226+
for (auto& input : all_inputs_) {
227+
if (input.emergency_reason == 0) continue;
228+
if (input.GetAndClearPendingEmergency()) {
229+
reasons |= input.emergency_reason;
230+
block_time = 0;
231+
} else if (input.IsActive() &&
232+
TimeoutReached(input.ActiveDuration(now), input.emergency_delay_ms * 1'000, block_time)) {
230233
reasons |= input.emergency_reason;
231234
}
232235
}

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)