GamepadCodec Arduino Lib 1.0.1
Loading...
Searching...
No Matches
gamepad_codec_decoder.cpp
Go to the documentation of this file.
2
3#include <string.h>
4
6
7/**
8 * @file gamepad_codec_decoder.cpp
9 */
10
11namespace gamepad::codec {
12Decoder::Decoder(Stream &stream) noexcept : stream_(stream), parser_(kGamepadInputStatePayloadSize) {}
13
14const gamepad::input::Tracker &Decoder::Update() noexcept {
15 input_tracker_.Tick();
16
17 while (stream_.available() > 0) {
18 parser_.Process(stream_.read());
19
20 if (parser_.is_complete()) {
21 OnFrame(parser_.data(), parser_.data_length());
22 }
23 }
24
25 return input_tracker_;
26}
27
28void Decoder::OnFrame(const uint8_t *data, const size_t data_length) {
29 if (data != nullptr && data[0] == static_cast<uint8_t>(DataType::kGamepadInputState) &&
30 data_length == kGamepadInputStatePayloadSize) {
31 input_tracker_.Update(gamepad::input::State::FromBytes(data + 1));
32 }
33}
34} // namespace gamepad::codec
Decoder(Stream &stream) noexcept
Constructor.
const gamepad::input::Tracker & Update() noexcept
Update, need to be called in Loop.
@ kGamepadInputState
Gamepad input state (buttons + axes).