CodexPadFrameDecoder Arduino Lib 1.0.0
Loading...
Searching...
No Matches
codex_pad_frame_decoder.cpp
2
3namespace {
4bool HasAxisValueChangedSignificantly(const int16_t prev_value, const int16_t current_value, const uint8_t threshold) {
5 return prev_value != current_value && (current_value == 0 || current_value == 255 || abs(current_value - prev_value) >= threshold);
6}
7} // namespace
8
9CodexPadFrameDecoder::CodexPadFrameDecoder(Stream &stream) : stream_(stream), parser_(sizeof(current_inputs_) + 1, OnFrameReceived, this) {
10}
11
12void CodexPadFrameDecoder::OnFrame(const uint8_t *data) {
13 if (data != nullptr && data[0] == kPayloadType) {
14 memcpy(&current_inputs_, data + 1, sizeof(current_inputs_));
15 }
16}
17void CodexPadFrameDecoder::OnFrameReceived(const uint8_t *data, void *user_data) {
18 const auto *decoder = static_cast<CodexPadFrameDecoder *>(user_data);
19 if (decoder) {
20 decoder->OnFrame(data);
21 }
22}
23
25 prev_inputs_ = current_inputs_;
26
27 while (stream_.available() > 0) {
28 parser_.Feed(stream_.read());
29 }
30}
31
32bool CodexPadFrameDecoder::pressed(const Button button) const {
33 return (prev_inputs_.button_states & static_cast<uint32_t>(button)) == 0 && (current_inputs_.button_states & static_cast<uint32_t>(button)) != 0;
34}
35
36bool CodexPadFrameDecoder::released(const Button button) const {
37 return (prev_inputs_.button_states & static_cast<uint32_t>(button)) != 0 && (current_inputs_.button_states & static_cast<uint32_t>(button)) == 0;
38}
39
40bool CodexPadFrameDecoder::holding(const Button button) const {
41 return (prev_inputs_.button_states & static_cast<uint32_t>(button)) != 0 && (current_inputs_.button_states & static_cast<uint32_t>(button)) != 0;
42}
43
45 return (current_inputs_.button_states & static_cast<uint32_t>(button)) != 0;
46}
47
49 return current_inputs_.button_states;
50}
51
52uint8_t CodexPadFrameDecoder::axis_value(const Axis axis) const {
53 return current_inputs_.axis_values[static_cast<size_t>(axis)];
54}
55
56const uint8_t *CodexPadFrameDecoder::axis_values() const {
57 return current_inputs_.axis_values;
58}
59
60bool CodexPadFrameDecoder::HasAxisValueChanged(const Axis axis, const uint8_t threshold) const {
61 return HasAxisValueChangedSignificantly(
62 prev_inputs_.axis_values[static_cast<size_t>(axis)], current_inputs_.axis_values[static_cast<size_t>(axis)], threshold);
63}
void Update()
Update the decoder state. This function must be called continuously in the main loop to feed and pars...
uint32_t button_states() const
Get all button states, return a 32-bit unsigned integer where each bit represents the state of a spec...
const uint8_t * axis_values() const
Get current values of all analog axes (returns a read-only array pointer).
bool holding(const Button button) const
check if a button is held.
bool HasAxisValueChanged(const Axis axis, const uint8_t threshold) const
check if an axis value has changed.
uint8_t axis_value(const Axis axis) const
Get the raw value (0~255) of a specified joystick axis.
static constexpr uint8_t kPayloadType
Input report type identifier for the gamepad (the first byte of the payload).
bool released(const Button button) const
check if a button is released.
bool pressed(const Button button) const
check if a button is pressed.
Button
Gamepad button mask enumeration.
CodexPadFrameDecoder(Stream &stream)
Constructor.
Axis
Joystick axis index enumeration.
bool button_state(const Button button) const
check if a button is pressed or held.