This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
FaceRec_TX510.h
181 lines (164 loc) · 4.73 KB
/
FaceRec_TX510.h
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "esphome.h"
using namespace esphome;
#define SYNCWORD1 0xEF
#define SYNCWORD2 0xAA
// Commands:
#define IDENT 0x12 // Identify
#define REGIS 0x13 // Register User
#define DEL 0x20 // Delete UserID
#define CLR 0x21 // Clear All UserIDs
#define BACKLIT 0xC0 // Backlight control
#define DISPL 0xC1 // Display Control
#define FLSH 0xC2 // Flash control
#define VERS 0x30 // Query Version
#define REBT 0xC3 // Reboot
#define BAUDRT 0x51 // Change Baudrate
#define NUMBERREC 0xC4 // Query number recorded entries
#define TURNON 0x01
#define TURNOFF 0x00
// Baudrates:
#define BAUD9600 0x00
#define BAUD19200 0x01
#define BAUD38400 0x02
#define BAUD57600 0x03
#define BAUD115200 0x04
// Responds from TX510
#define SUCC 0x00 // Successful
#define FAIL 0x01 // Failed
#define ANGL 0x03 // Angle failure
#define FAIL2D 0x06 // 2D failure
#define FAIL3D 0x07 // 3D failure
#define NOMATCH 0x08 // No mactching entry
#define EXIST 0x09 // Entry already exist
class UARTSensor : public Component, public UARTDevice {
public:
UARTSensor(UARTComponent *parent) : UARTDevice(parent) {}
TextSensor* presence_sensor = new TextSensor();
TextSensor* text_userid = new TextSensor();
TextSensor* text_reply = new TextSensor();
void setup() {
}
std::vector<int> bytes;
void loop() override
{
static int MAX_PACKET_SIZE = 45;
static int packetIndex = 0;
static bool receivingData = false;
static byte receivedParity = 0;
while (available()) {
bytes.push_back(read());
if (receivingData) {
if (bytes[packetIndex] == receivedParity && packetIndex > 7) {
processPacket();
receivedParity = 0;
packetIndex = 0;
receivingData = false;
bytes.clear();
} else {
receivedParity += bytes[packetIndex];
packetIndex++;
}
} else if (packetIndex <= MAX_PACKET_SIZE) {
if (bytes[0] == SYNCWORD1 && packetIndex == 0) {
packetIndex = 1;
} else if (bytes[1] == SYNCWORD2 && packetIndex == 1) {
packetIndex++;
receivingData = true;
}
else {
receivingData = false;
packetIndex = 0;
bytes.clear();
}
} else {
packetIndex = 0;
receivedParity = 0;
receivingData = false;
bytes.clear();
}
}
}
void processPacket() {
switch(bytes[7]){
case IDENT:
case REGIS:
case DEL:
case CLR:
case DISPL:
case BACKLIT:
case FLSH:
case REBT:
case BAUDRT:
case NUMBERREC:
switch(bytes[8]){
case SUCC:
id(text_reply).publish_state("Success");
if(bytes[7] == IDENT || bytes[7] == REGIS || bytes[7] == NUMBERREC) {
byte userId;
userId = bytes[9] + bytes[10];
if(bytes[7] != NUMBERREC){
id(text_userid).publish_state(String(userId).c_str());
id(presence_sensor).publish_state("on");
} else {
String sum = String(userId);
String text = "Sum: " + String(userId);
if( sum != "0"){
for(int i = 11; i < bytes.size() - 2; i+=2){
byte id = bytes[i] + bytes[i+1];
text+= ", User ID: " + String(id);
}
}
id(text_reply).publish_state(text.c_str());
}
}
break;
default:
if(bytes[7] == IDENT || bytes[7] == REGIS) {
id(text_userid).publish_state("None");
id(presence_sensor).publish_state("off");
}
switch(bytes[8]){
case FAIL:
if (bytes[7] == DISPL ||
bytes[7] == BACKLIT ||
bytes[7] == FLSH){
id(text_reply).publish_state("Turned OFF");
} else {
id(text_reply).publish_state("Failed");
}
break;
case FAIL2D:
id(text_reply).publish_state("Failed 2D");
break;
case FAIL3D:
id(text_reply).publish_state("Failed 3D");
break;
case ANGL:
id(text_reply).publish_state("Failed Angle");
break;
case NOMATCH:
id(text_reply).publish_state("Failed No Match");
break;
}
break;
}
break;
case VERS:
byte version[20];
int indx = 0;
for(int i = 16; i < bytes.size()-1; i++){
version[indx] = bytes[i];
indx++;
}
String ver = byteArrayToAsciiString(version, indx);
id(text_reply).publish_state(ver.c_str());
}
}
String byteArrayToAsciiString(byte* byteArray, int arraySize) {
String result = "";
for (int i = 0; i < arraySize; i++) {
result += char(byteArray[i]);
}
return result;
}
};