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