57 lines
2.3 KiB
C++
57 lines
2.3 KiB
C++
//
|
||
// 触觉传感器串口协议高层解析工具(中文注释版)。
|
||
// 注意:文件使用了 C++17 的 std::optional 与 inline constexpr,
|
||
// 可提供编译期常量与安全的可空返回值。
|
||
//
|
||
|
||
#pragma once
|
||
|
||
#include "../cpdecoder.hh"
|
||
|
||
#include <cstdint> // 协议字段均以固定宽度字节表示
|
||
#include <optional> // std::optional:可能解析失败时返回空
|
||
#include <vector>
|
||
|
||
namespace ffmsep::tactile {
|
||
|
||
inline constexpr std::uint8_t kStartByte = 0x3A; // 帧起始符(冒号)
|
||
inline constexpr std::uint8_t kEndByteFirst = 0x0D; // 帧结束符(回车)
|
||
inline constexpr std::uint8_t kEndByteSecond = 0x0A; // 帧结束符(换行)
|
||
|
||
// 功能码枚举:使用 enum class 提升类型安全,避免与其他数值混用。
|
||
enum class FunctionCode : std::uint8_t {
|
||
Unknown = 0x00, // 未知功能,解析失败或未初始化
|
||
ReadMatrix = 0x01, // 读取整块矩阵 AD 值
|
||
ReadSingle = 0x02, // 读取单点 AD 值
|
||
ReadTemperature = 0x03,// 读取温度数据
|
||
SetDeviceId = 0x51, // 修改设备编号
|
||
SetMatrixSize = 0x52, // 修改矩阵尺寸(长边/短边)
|
||
CalibrationMode = 0x53 // 进入校准模式
|
||
};
|
||
|
||
struct MatrixSize {
|
||
std::uint8_t long_edge = 0; // 矩阵长边尺寸
|
||
std::uint8_t short_edge = 0; // 矩阵短边尺寸
|
||
};
|
||
|
||
struct TactileFrame {
|
||
std::uint8_t device_address = 0; // 设备地址(1~255)
|
||
FunctionCode function = FunctionCode::Unknown; // 功能码
|
||
std::uint8_t data_length = 0; // 数据域长度
|
||
std::vector<std::uint8_t> payload; // 数据域内容(按协议解析)
|
||
};
|
||
|
||
// 将底层 CPFrame 解析为结构化的 TactileFrame。
|
||
std::optional<TactileFrame> parse_frame(const CPFrame& frame);
|
||
// 将数据域按小端 16 位压力值数组解析。
|
||
std::vector<std::uint16_t> parse_pressure_values(const TactileFrame& frame);
|
||
// 解析矩阵尺寸(用于读/写矩阵尺寸的命令)。
|
||
std::optional<MatrixSize> parse_matrix_size_payload(const TactileFrame& frame);
|
||
// 解析矩阵坐标(复用尺寸结构,分别表示长边/短边索引)。
|
||
std::optional<MatrixSize> parse_matrix_coordinate_payload(const TactileFrame& frame);
|
||
|
||
const CPCodec* tactile_codec();
|
||
void register_tactile_codec();
|
||
|
||
} // namespace ffmsep::tactile
|