-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathwavinAhc9000.cpp
More file actions
212 lines (185 loc) · 7.08 KB
/
Copy pathwavinAhc9000.cpp
File metadata and controls
212 lines (185 loc) · 7.08 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "wavinAhc9000.h"
#include "esphome/core/log.h"
namespace esphome {
namespace wavinAhc9000 {
static const char *TAG = "wavin";
static const uint8_t MODBUS_READ_REGISTER = 0x43;
static const uint8_t MODBUS_WRITE_REGISTER = 0x44;
static const uint8_t MODBUS_WRITE_MASKED_REGISTER = 0x45;
static const uint8_t CATEGORY_MAIN = 0x00;
static const uint8_t CATEGORY_ELEMENTS = 0x01;
static const uint8_t CATEGORY_PACKED_DATA = 0x02;
static const uint8_t CATEGORY_CHANNELS = 0x03;
static const uint8_t PACKED_DATA_MANUAL_TEMPERATURE = 0x00;
static const uint8_t PACKED_DATA_CONFIGURATION = 0x07;
static const uint8_t PRIMARY_ELEMENT_MASK = 0x3f;
static const uint8_t ALL_TP_LOST_MASK = 0x02;
static const uint8_t CHANNEL_OUTP_ON = 0x10;
static const uint8_t MODE_MASK = 0x07;
void WavinAhc9000::add_temp_callback(int channel, std::function<void(float)> &&callback) {
temp_callbacks_[channel].add(std::move(callback));
}
void WavinAhc9000::add_bat_level_callback(int channel, std::function<void(float)> &&callback) {
bat_level_callbacks_[channel].add(std::move(callback));
}
void WavinAhc9000::add_target_temp_callback(int channel, std::function<void(float)> &&callback) {
target_temp_callbacks_[channel].add(std::move(callback));
}
void WavinAhc9000::add_mode_callback(int channel, std::function<void(int)> &&callback) {
mode_callbacks_[channel].add(std::move(callback));
}
void WavinAhc9000::add_output_callback(int channel, std::function<void(bool)> &&callback) {
output_callbacks_[channel].add(std::move(callback));
}
void WavinAhc9000::set_target_temp(int channel, float temperature) {
set_temp_.push_back(temperature);
temp_channel_.push_back(channel);
}
void WavinAhc9000::send_read_(uint8_t function_code, uint16_t arg1, uint16_t arg2) {
// Wavin custom read: [function][arg1 hi][arg1 lo][arg2 hi][arg2 lo].
// send_pdu() prepends the device address and appends the CRC.
const uint8_t pdu[5] = {function_code, (uint8_t) (arg1 >> 8), (uint8_t) (arg1 & 0xff),
(uint8_t) (arg2 >> 8), (uint8_t) (arg2 & 0xff)};
this->send_pdu(std::span<const uint8_t>(pdu, sizeof(pdu)));
}
void WavinAhc9000::on_modbus_data(const std::vector<uint8_t> &data) {
this->waiting_ = false;
// The modbus core already stripped the address and the function code, so `data`
// is [byte_count][payload...]. Skip the byte-count byte to reach the payload.
if (data.size() < 2) {
ESP_LOGW(TAG, "Invalid modbus response - too short");
return;
}
std::vector<uint8_t> payload(data.begin() + 1, data.end());
float temperature;
switch (state_) {
case 0:
temperature = ((payload[0] << 8) + payload[1]) / 10.0;
ESP_LOGD(TAG, "Confirmed target temperature channel %i: %.1f", channel_ + 1, temperature);
channel_ = -1;
break;
case 1:
handle_channel_data_(payload);
break;
case 2:
handle_element_data_(payload);
break;
case 3:
handle_target_temp_data_(payload);
break;
case 4:
handle_mode_data_(payload);
break;
}
}
bool WavinAhc9000::on_modbus_no_response() {
this->waiting_ = false;
if (state_ == 0) {
ESP_LOGD(TAG, "Timeout on set temperature on channel %d", channel_ + 1);
channel_ = -1;
} else {
ESP_LOGD(TAG, "Timeout on channel %d, state %d", channel_ + 1, state_);
state_ = 4; // skip remaining reads for this channel
}
return false; // do not ask the hub to retry
}
void WavinAhc9000::handle_channel_data_(const std::vector<uint8_t> &data) {
element_ = (data[5] & PRIMARY_ELEMENT_MASK) - 1;
if (element_ == -1) { // this channel isn't used
ESP_LOGV(TAG, "Channel %d isn't used", channel_ + 1);
state_ = 4;
return;
}
if (data[0] & ALL_TP_LOST_MASK) {
ESP_LOGD(TAG, "All TP lost for channel %d", channel_ + 1);
state_++; // skip temp and bat data
}
bool output_on = data[1] & CHANNEL_OUTP_ON;
ESP_LOGD(TAG, "Status channel %i: %s",channel_ + 1, ONOFF(output_on));
output_callbacks_[channel_].call(output_on);
}
void WavinAhc9000::handle_element_data_(const std::vector<uint8_t> &data) {
float temperature = ((data[0] << 8) + data[1]) / 10.0;
int battery = data[13] * 10;
ESP_LOGD(TAG, "Temperature channel %i: %.1f", channel_ + 1, temperature);
ESP_LOGD(TAG, "Battery channel %i: %i", channel_ + 1, battery);
temp_callbacks_[channel_].call(temperature);
bat_level_callbacks_[channel_].call(battery);
}
void WavinAhc9000::handle_target_temp_data_(const std::vector<uint8_t> &data) {
float temperature = ((data[0] << 8) + data[1]) / 10.0;
ESP_LOGD(TAG, "Target temperature channel %i: %.1f", channel_ + 1, temperature);
target_temp_callbacks_[channel_].call(temperature);
}
void WavinAhc9000::handle_mode_data_(const std::vector<uint8_t> &data) {
int mode = data[0] & MODE_MASK;
ESP_LOGD(TAG, "Mode channel %i: %d",channel_ + 1, mode );
mode_callbacks_[channel_].call(mode);
}
void WavinAhc9000::loop() {
// One transaction in flight at a time; on_modbus_data()/on_modbus_no_response() clear this.
if (this->waiting_)
return;
if (set_temp_.size() && (channel_ < 0)) {
int temperature = ((roundf(set_temp_.front() * 2.0) / 2) * 10);
ESP_LOGV(TAG, "Rounded to nearest half for channel %d: %d", channel_ + 1, temperature);
set_temp_.erase(set_temp_.begin());
channel_ = temp_channel_.front();
temp_channel_.erase(temp_channel_.begin());
ESP_LOGV(TAG, "Setting temperature for channel %d: %d", channel_ + 1, temperature);
// Wavin custom write: [function][category][index][channel][count=1][value hi][value lo].
const uint8_t pdu[7] = {MODBUS_WRITE_REGISTER, CATEGORY_PACKED_DATA, PACKED_DATA_MANUAL_TEMPERATURE,
(uint8_t) channel_, 1, (uint8_t) (temperature >> 8), (uint8_t) (temperature & 0xff)};
this->send_pdu(std::span<const uint8_t>(pdu, sizeof(pdu)));
this->waiting_ = true;
return;
}
if (start_scan_) {
start_scan_ = false;
channel_ = 0;
// Find first configured channel
while (channel_ < 16 && !used_channels_[channel_]) {
channel_++;
}
if (channel_ >= 16) {
channel_ = -1;
return;
}
}
if (channel_ < 0)
return;
if (++state_ > 4) {
// Move to next configured channel
do {
channel_++;
} while (channel_ < 16 && !used_channels_[channel_]);
if (channel_ >= 16) {
state_ = 0;
channel_ = -1;
return;
}
state_ = 1;
}
ESP_LOGV(TAG, "Sending for channel %d, state %d", channel_ + 1, state_);
switch(state_) {
case 1:
send_read_(MODBUS_READ_REGISTER, (CATEGORY_CHANNELS << 8) + 0, (channel_ << 8) + 3);
break;
case 2:
ESP_LOGV(TAG, "Reading data for element %d", element_);
send_read_(MODBUS_READ_REGISTER, (CATEGORY_ELEMENTS << 8) + 4, (element_ << 8) + 7);
break;
case 3:
send_read_(MODBUS_READ_REGISTER, (CATEGORY_PACKED_DATA << 8) + PACKED_DATA_MANUAL_TEMPERATURE, (channel_ << 8) + 1);
break;
case 4:
send_read_(MODBUS_READ_REGISTER, (CATEGORY_PACKED_DATA << 8) + PACKED_DATA_CONFIGURATION, (channel_ << 8) + 1);
break;
}
this->waiting_ = true;
}
void WavinAhc9000::update() {
start_scan_ = true;
}
} // wavinAhc9000
} // esphome