CodexPadFrameDecoder Arduino Lib 1.0.0

Loading...
Searching...
No Matches
codex_pad_frame_decoder.cpp
1#include "codex_pad_frame_decoder.h"
2
3 namespace {
4 bool 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
9 CodexPadFrameDecoder::CodexPadFrameDecoder(Stream &stream) : stream_(stream), parser_(sizeof(current_inputs_) + 1, OnFrameReceived, this) {
10}
11
12 void CodexPadFrameDecoder::OnFrame(const uint8_t *data) {
13 if (data != nullptr && data[0] == kPayloadType) {
14 memcpy(&current_inputs_, data + 1, sizeof(current_inputs_));
15 }
16}
17 void 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
24 void CodexPadFrameDecoder::Update() {
25 prev_inputs_ = current_inputs_;
26
27 while (stream_.available() > 0) {
28 parser_.Feed(stream_.read());
29 }
30}
31
32 bool 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
36 bool 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
40 bool 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
44 bool CodexPadFrameDecoder::button_state(const Button button) const {
45 return (current_inputs_.button_states & static_cast<uint32_t>(button)) != 0;
46}
47
48uint32_t CodexPadFrameDecoder::button_states() const {
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
56 const uint8_t *CodexPadFrameDecoder::axis_values() const {
57 return current_inputs_.axis_values;
58}
59
60 bool 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}
CodexPadFrameDecoder::Update
void Update()
Update the decoder state. This function must be called continuously in the main loop to feed and pars...
Definition codex_pad_frame_decoder.cpp:24
CodexPadFrameDecoder::button_states
uint32_t button_states() const
Get all button states, return a 32-bit unsigned integer where each bit represents the state of a spec...
Definition codex_pad_frame_decoder.cpp:48
CodexPadFrameDecoder::axis_values
const uint8_t * axis_values() const
Get current values of all analog axes (returns a read-only array pointer).
Definition codex_pad_frame_decoder.cpp:56
CodexPadFrameDecoder::holding
bool holding(const Button button) const
check if a button is held.
Definition codex_pad_frame_decoder.cpp:40
CodexPadFrameDecoder::HasAxisValueChanged
bool HasAxisValueChanged(const Axis axis, const uint8_t threshold) const
check if an axis value has changed.
Definition codex_pad_frame_decoder.cpp:60
CodexPadFrameDecoder::axis_value
uint8_t axis_value(const Axis axis) const
Get the raw value (0~255) of a specified joystick axis.
Definition codex_pad_frame_decoder.cpp:52
CodexPadFrameDecoder::kPayloadType
static constexpr uint8_t kPayloadType
Input report type identifier for the gamepad (the first byte of the payload).
Definition codex_pad_frame_decoder.h:59
CodexPadFrameDecoder::released
bool released(const Button button) const
check if a button is released.
Definition codex_pad_frame_decoder.cpp:36
CodexPadFrameDecoder::pressed
bool pressed(const Button button) const
check if a button is pressed.
Definition codex_pad_frame_decoder.cpp:32
CodexPadFrameDecoder::Button
Button
Gamepad button mask enumeration.
Definition codex_pad_frame_decoder.h:71
CodexPadFrameDecoder::CodexPadFrameDecoder
CodexPadFrameDecoder(Stream &stream)
Constructor.
Definition codex_pad_frame_decoder.cpp:9
CodexPadFrameDecoder::Axis
Axis
Joystick axis index enumeration.
Definition codex_pad_frame_decoder.h:253
CodexPadFrameDecoder::button_state
bool button_state(const Button button) const
check if a button is pressed or held.
Definition codex_pad_frame_decoder.cpp:44
codex_pad_frame_decoder.h