WIP6: Fix Button struct continuously reading a long press
This commit is contained in:
parent
08c23d6244
commit
105a387efc
@ -42,7 +42,7 @@ public:
|
||||
|
||||
Button() = delete;
|
||||
explicit Button(const gpio& sw)
|
||||
: sw(sw)
|
||||
: sw(sw), was_pressed_(sw.read())
|
||||
{}
|
||||
|
||||
void update()
|
||||
@ -59,7 +59,15 @@ public:
|
||||
else if (!is_pressed && was_pressed_)
|
||||
{
|
||||
if (time_held > TIMEOUT_SHORT_PRESS && time_held <= TIMEOUT_LONG_PRESS)
|
||||
{
|
||||
new_state = ButtonPress::SHORT_PRESS;
|
||||
}
|
||||
|
||||
if (state_exhausted_)
|
||||
{
|
||||
current_state_ = ButtonPress::NOT_PRESSED;
|
||||
state_exhausted_ = false;
|
||||
}
|
||||
|
||||
time_pressed_down_ = 0;
|
||||
}
|
||||
@ -70,13 +78,25 @@ public:
|
||||
}
|
||||
|
||||
was_pressed_ = is_pressed;
|
||||
|
||||
if (!state_exhausted_)
|
||||
current_state_ = new_state;
|
||||
}
|
||||
|
||||
[[nodiscard]] ButtonPress getState() const
|
||||
{
|
||||
if (!state_exhausted_)
|
||||
{
|
||||
if (current_state_ != ButtonPress::NOT_PRESSED && current_state_ != ButtonPress::SHORT_PRESS)
|
||||
state_exhausted_ = true;
|
||||
|
||||
return current_state_;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ButtonPress::NOT_PRESSED;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef SKULLC_WITH_CORO
|
||||
skullc::coro::Task<ButtonPress> await_press()
|
||||
@ -96,9 +116,10 @@ public:
|
||||
#endif
|
||||
|
||||
private:
|
||||
bool was_pressed_ = false;
|
||||
bool was_pressed_;
|
||||
std::uint32_t time_pressed_down_ = 0;
|
||||
ButtonPress current_state_ = ButtonPress::NOT_PRESSED;
|
||||
mutable bool state_exhausted_ = false;
|
||||
};
|
||||
|
||||
}// namespace Peripherals
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user