-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvedirect.cpp
112 lines (98 loc) · 3.38 KB
/
vedirect.cpp
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
/*
esphome vedirect component
Copyright (C) 2021 [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdarg.h>
#include "vedirect.h"
static const char *TAG = "vedirect";
static const char *HEX_LABEL = "HEX";
static void static_log_e(const char* module, const char* error) {
esp_log_printf_(ESPHOME_LOG_LEVEL_DEBUG, module, 0, "%s", error);
}
static void static_hex_cb(const char* buffer, int size, void* obj) {
((VEDirectComponent*)obj)->hexCallback(buffer, size);
}
VEDirectComponent::VEDirectComponent(UARTComponent* parent)
: UARTDevice(parent),
nb_sensors(0),
hexSensorIndex(-1),
hexAsyncMute(false)
{
vedfh.setErrorHandler(&static_log_e);
}
void VEDirectComponent::_addSensor(const char* label, Sensor* ss, bool is_text) {
if (strcmp(label, HEX_LABEL)==0) {
hexSensorIndex=nb_sensors;
vedfh.addHexCallback(&static_hex_cb, (void*)this);
ESP_LOGD(TAG,"recorded callback for HEX sensor, index=%d",hexSensorIndex);
}
VESensor* s=new VESensor;
s->_label=strdup(label);
s->_sensor=ss;
s->_is_text=is_text;
ve_sensors[nb_sensors++]=s;
ESP_LOGD(TAG,"recorded %s sensor for label %s", s->is_text()?"text":"numeric", s->label());
}
void VEDirectComponent::addSensor(const char* label, TextSensor* ss) {
this->_addSensor(label,(Sensor*)ss,true);
}
void VEDirectComponent::addSensor(const char* label, Sensor* ss) {
this->_addSensor(label,ss,false);
}
void VEDirectComponent::ReadVEData() {
while (available()) {
vedfh.rxData(read());
}
yield();
}
void VEDirectComponent::loop() {
if (available())
ReadVEData();
}
void VEDirectComponent::update() {
int skip[max_sensors]={};
for (int i=0; i<vedfh.veEnd; i++) {
for (int j=0; j<nb_sensors; j++) {
if (skip[j]==0 && strcmp(ve_sensors[j]->label(),vedfh.veName[i])==0) {
skip[j]=1;
VESensor* s=ve_sensors[j];
Sensor* ss=s->sensor();
if (s->is_text()) {
TextSensor* ts=(TextSensor*)ss;
ts->publish_state(vedfh.veValue[i]);
//ESP_LOGD(TAG,"published state %s for text sensor %s", vedfh.veValue[i], s->label());
}
else {
float val=atof(vedfh.veValue[i]);
ss->publish_state(val);
//ESP_LOGD(TAG,"published state %f for numeric sensor %s", val, s->label());
}
break;
}
}
}
}
void VEDirectComponent::hexCallback(const char* buffer, int size) {
if (hexSensorIndex!=-1 && (buffer[1]!='A' || hexAsyncMute==false)) {
char tmpdata[size+1];
memcpy(tmpdata,buffer,size);
tmpdata[size]=0; // end of string
((TextSensor*)(ve_sensors[hexSensorIndex]->sensor()))->publish_state(tmpdata);
ESP_LOGD(TAG,"hex callback received, published hex frame %s", tmpdata);
}
}
void VEDirectComponent::sendHexCommand(const char* command) {
ESP_LOGD(TAG,"send hex command %s", command);
write_str(command);
}