GamepadCodec Arduino Lib 1.0.1
Loading...
Searching...
No Matches
gamepad_codec_decoder.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef _GAMEPAD_CODEC_DECODER_H_
4#define _GAMEPAD_CODEC_DECODER_H_
5
6#include <Stream.h>
7#include <stdint.h>
8
9#include "gamepad_input_tracker.h"
10#include "robust_frame.h"
11
12/**
13 * @file gamepad_codec_decoder.h
14 */
15
16namespace gamepad::codec {
17
18/**
19 * @~English
20 * @brief Decoder class responsible for parsing gamepad input frames from a byte stream and updating the state tracker.
21 * @details Binds to a Stream (e.g., Serial) object. When Update() is called, it reads all available data, uses
22 * RobustFrame::Parser to synchronize frame boundaries, identifies the payload type (0x01), deserializes data into
23 * gamepad::input::State, and finally updates the internal Tracker.
24 */
25/**
26 * @~Chinese
27 * @brief 解码器类,负责从字节流中解析手柄输入帧并更新状态追踪器。
28 * @details 该类绑定一个 Stream(如 Serial)对象,在调用 Update()时会读取流中所有可用数据,通过 RobustFrame::Parser
29 * 同步帧边界,识别有效载荷类型,并将数据反序列化为 gamepad::input::State,最终更新内部 Tracker。
30 */
31class Decoder {
32 public:
33 /**
34 * @~English
35 * @brief Constructor.
36 * @param[in] stream Reference to a Stream object for reading data byte stream.
37 */
38 /**
39 * @~Chinese
40 * @brief 构造函数。
41 * @param[in] stream 用于读取数据字节流的 Stream 对象引用。
42 */
43 Decoder(Stream &stream) noexcept;
44
45 /**
46 * @~English
47 * @brief Update, need to be called in Loop
48 */
49 /**
50 * @~Chinese
51 * @brief 更新,需要在Loop中不断调用
52 */
53 const gamepad::input::Tracker &Update() noexcept;
54
55 /**
56 * @~English
57 * @brief Get the input tracker object
58 * @details This function returns a reference to the internal input tracker object.
59 * @return Input tracker object
60 */
61 /**
62 * @~Chinese
63 * @brief 获取输入跟踪器对象
64 * @details 此函数返回内部输入跟踪器对象的引用。
65 * @return 输入跟踪器对象
66 */
67 const gamepad::input::Tracker &input_tracker() const noexcept { return input_tracker_; }
68
69 private:
70 Decoder(const Decoder &) = delete;
71 Decoder &operator=(const Decoder &) = delete;
72
73 void OnFrame(const uint8_t *data, size_t data_length);
74
75 Stream &stream_;
76 robust_frame::Parser parser_;
77 gamepad::input::Tracker input_tracker_;
78};
79
80} // namespace gamepad::codec
81
82#endif
Decoder class responsible for parsing gamepad input frames from a byte stream and updating the state ...
Decoder(Stream &stream) noexcept
Constructor.
const gamepad::input::Tracker & input_tracker() const noexcept
Get the input tracker object.
const gamepad::input::Tracker & Update() noexcept
Update, need to be called in Loop.