MatrixMiniR4 1.1.5
Matrix Mini R4 Arduino Library API Documentation
Loading...
Searching...
No Matches
MiniR4SmartCamReader.h
Go to the documentation of this file.
1
6#ifndef MINIR4_SMART_CAM_READER
7#define MINIR4_SMART_CAM_READER
8
9#include <Arduino.h>
10
19{
20private:
21public:
24
25 bool hasReceivedValidData = false;
26
30 void Begin(void) { Serial1.begin(115200); }
31
46 int SmartCamReader(unsigned int* data, unsigned int timeout = 500)
47 {
48 byte length, check; // 声明局部变量
49 byte data_buffer[40]; // 数据缓冲区
50 unsigned int system_time; // 系统时间
51
52 system_time = millis(); // 获取当前系统时间
53 // 等待至少两个字节的数据或超时
54 while ((millis() - system_time < timeout) && (Serial1.available() < 2));
55
56 if (Serial1.available() >= 2) { // 如果至少有两个字节的数据
57 if (Serial1.read() == 0xAA) { // 检查头字节
58 length = Serial1.read(); // 读取数据长度
59 check = 0xAA ^ length; // 初始化校验位
60
61 system_time = millis(); // 重置系统时间
62 // 等待足够多的数据或超时
63 while ((millis() - system_time < timeout) &&
64 (Serial1.available() < length * 2 + 1)) {}
65
66 if (Serial1.available() >= length * 2 + 1) { // 如果数据足够
67 for (unsigned char n = 0; n < length * 2; n++) {
68 data_buffer[n] = Serial1.read(); // 读取数据到缓冲区
69 check = check ^ data_buffer[n]; // 更新校验位
70 }
71 if (check == Serial1.read()) { // 如果校验成功
72 while (Serial1.available()) { // 清空缓冲区
73 Serial1.read();
74 }
75 for (unsigned char n = 0; n < length; n++) { // 解码数据
76 data[n] = (data_buffer[n * 2 + 1] << 8) + data_buffer[n * 2];
77 }
79 return length; // 返回数据长度
80 } else {
82 return -3; // 校验失败
83 }
84 } else {
86 return -2; // 数据不完整
87 }
88 }
89 } else {
91 Serial1.write("start");
92 }
93 return -1; // 超时或没有数据
94 }
95 }
96};
97
98#endif
A class for reading data from mVision camera.
bool hasReceivedValidData
Flag to indicate if valid data has been received.
int SmartCamReader(unsigned int *data, unsigned int timeout=500)
Reads data from the smart camera.
void Begin(void)
Initializes the serial communication with the smart camera.