-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialUltra.h
More file actions
108 lines (86 loc) · 2.99 KB
/
SerialUltra.h
File metadata and controls
108 lines (86 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// Created by mijiao on 23-2-21.
//
#ifndef SERIALULTRA_SERIALULTRA_H
#define SERIALULTRA_SERIALULTRA_H
#include <iostream>
#include "serialib.h"
#include "MessageProcessor/OutputMessageProcessor.h"
#include "MessageProcessor/InputMessageProcessor.h"
#define MessageData struct __attribute__((packed))
namespace su {
template<typename Head, typename Tail>
class SerialUltra {
private:
std::shared_ptr<serialib> pSerial;
mp::OutputMessageProcessor<Head, Tail> OMP;
mp::InputMessageProcessor<Head, Tail> IMP;
public:
SerialUltra() : pSerial(std::make_shared<serialib>()), OMP(pSerial), IMP(pSerial) {}
SerialUltra(const std::string& device, int baud) : pSerial(std::make_shared<serialib>()), OMP(pSerial),
IMP(pSerial) {
int x;
if ((x = open(device, baud) )== 1) {
IMP.start();
} else {
std::cout << "[ERROR]"<<x<<"open Failed!" << std::endl;
}
}
bool status() {
return pSerial->isDeviceOpen();
}
char open(const std::string& device, int baud) {
if (!pSerial->isDeviceOpen()) {
char ret = pSerial->openDevice(device.c_str(), baud);
if (ret == 1) {
IMP.start();
}
return ret;
} else {
return 0;
}
}
void close() {
IMP.stop();
pSerial->closeDevice();
}
void spinOnce() {
IMP.spinOnce();
}
void spin(bool background = false) {
IMP.spin(background);
}
bool setMaxSize(size_t _maxSize) {
IMP.setMaxSize(_maxSize);
}
void registerChecker(std::function<int(const Head&)> checker) {
IMP.registerChecker(checker);
}
void registerChecker(std::function<int(const Tail&, const uint8_t*, int)> checker) {
IMP.registerChecker(checker);
}
void setGetLength(std::function<size_t(const Head&)> _getLength) {
IMP.setGetLength(_getLength);
}
void setGetId(std::function<int(const Head&)> _getID) {
IMP.setGetId(_getID);
}
template<typename functionType>
void registerCallBack(size_t id, functionType callback) {
IMP.registerCallBack(id, callback);
}
template<typename functionType>
void registerHeadPreprocessor(functionType headPreprocessor) {
OMP.registerHeadPreprocessor(headPreprocessor);
}
template<typename functionType>
void registerTailPreprocessor(functionType tailPreprocessor) {
OMP.registerTailPreprocessor(tailPreprocessor);
}
template<typename Data>
int write(Head head, Data data, Tail tail) {
return OMP.write(head, data, tail);
}
};
}
#endif //SERIALULTRA_SERIALULTRA_H