-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesk_height_sensor.h
183 lines (165 loc) · 4.28 KB
/
desk_height_sensor.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
182
183
#include "esphome.h"
#include <bitset>
class DeskHeightSensor : public Component, public UARTDevice, public Sensor
{
public:
DeskHeightSensor(UARTComponent *parent) : UARTDevice(parent) {}
float value = 0;
float lastPublished = -1;
unsigned long history[5];
int msg_len = 0;
unsigned long msg_type;
bool valid = false;
float get_setup_priority() const override { return esphome::setup_priority::DATA; }
int hex_to_int(byte s)
{
std::bitset<8> b(s);
if (b[0] && b[1] && b[2] && b[3] && b[4] && b[5] && !b[6])
{
return 0;
}
if (not b[0] && b[1] && b[2] && !b[3] && !b[4] && !b[5] && !b[6])
{
return 1;
}
if (b[0] && b[1] && !b[2] && b[3] && b[4] && !b[5] && b[6])
{
return 2;
}
if (b[0] && b[1] && b[2] && b[3] && !b[4] && !b[5] && b[6])
{
return 3;
}
if (not b[0] && b[1] && b[2] && !b[3] && !b[4] && b[5] && b[6])
{
return 4;
}
if (b[0] && !b[1] && b[2] && b[3] && !b[4] && b[5] && b[6])
{
return 5;
}
if (b[0] && !b[1] && b[2] && b[3] && b[4] && b[5] && b[6])
{
return 6;
}
if (b[0] && b[1] && b[2] && !b[3] && !b[4] && !b[5] && !b[6])
{
return 7;
}
if (b[0] && b[1] && b[2] && b[3] && b[4] && b[5] && b[6])
{
return 8;
}
if (b[0] && b[1] && b[2] && b[3] && !b[4] && b[5] && b[6])
{
return 9;
}
if (!b[0] && !b[1] && !b[2] && !b[3] && !b[4] && !b[5] && b[6])
{
return 10;
}
return 0;
}
bool is_decimal(byte b)
{
return (b & 0x80) == 0x80;
}
void setup() override
{
// nothing to do here
}
void loop() override
{
while (available() > 0)
{
byte incomingByte = read();
// ESP_LOGD("DEBUG", "Incoming byte is: %08x", incomingByte);
// First byte, start of a packet
if (incomingByte == 0x9b)
{
// Reset message length
msg_len = 0;
valid = false;
}
// Second byte defines the message length
if (history[0] == 0x9b)
{
msg_len = (int)incomingByte;
}
// Third byte is message type
if (history[1] == 0x9b)
{
msg_type = incomingByte;
}
// Fourth byte is first height digit, if msg type 0x12 & msg len 7
if (history[2] == 0x9b)
{
if (msg_type == 0x12 && (msg_len == 7 || msg_len == 10))
{
// Empty height
if (incomingByte == 0)
{
// ESP_LOGD("DEBUG", "Height 1 is EMPTY -> 0x%02x", incomingByte);
// deskSerial.write(command_wakeup, sizeof(command_wakeup));
}
else if (hex_to_int(incomingByte) == 0)
{
// ESP_LOGD("DEBUG", "Invalid height 1 -> 0x%02x", incomingByte);
// deskSerial.write(command_wakeup, sizeof(command_wakeup));
}
else
{
valid = true;
// ESP_LOGD("DEBUG", "Height 1 is: 0x%02x", incomingByte);
}
}
}
// Fifth byte is second height digit
if (history[3] == 0x9b)
{
if (valid == true)
{
// ESP_LOGD("DEBUG", "Height 2 is: 0x%02x", incomingByte);
}
}
// Sixth byte is third height digit
if (history[4] == 0x9b)
{
if (valid == true)
{
int height1 = hex_to_int(history[1]) * 100;
int height2 = hex_to_int(history[0]) * 10;
int height3 = hex_to_int(incomingByte);
if (height2 == 100) // check if 'number' is a hyphen, return value 10 multiplied by 10
{
}
else
{
float finalHeight = height1 + height2 + height3;
if (is_decimal(history[0]))
{
finalHeight = finalHeight / 10;
}
value = finalHeight;
// ESP_LOGD("DeskHeightSensor", "Current height is: %f", finalHeight);
}
}
}
// Save byte buffer to history arrary
history[4] = history[3];
history[3] = history[2];
history[2] = history[1];
history[1] = history[0];
history[0] = incomingByte;
// End byte
if (incomingByte == 0x9d)
{
if (value && value != lastPublished)
{
publish_state(value);
lastPublished = value;
}
}
}
}
};