-
Notifications
You must be signed in to change notification settings - Fork 25
/
zehnder.cpp
653 lines (526 loc) · 21.5 KB
/
zehnder.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#include "zehnder.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
namespace esphome {
namespace zehnder {
#define MAX_TRANSMIT_TIME 2000
static const char *const TAG = "zehnder";
typedef struct __attribute__((packed)) {
uint32_t networkId;
} RfPayloadNetworkJoinOpen;
typedef struct __attribute__((packed)) {
uint32_t networkId;
} RfPayloadNetworkJoinRequest;
typedef struct __attribute__((packed)) {
uint32_t networkId;
} RfPayloadNetworkJoinAck;
typedef struct __attribute__((packed)) {
uint8_t speed;
uint8_t voltage;
uint8_t timer;
} RfPayloadFanSettings;
typedef struct __attribute__((packed)) {
uint8_t speed;
} RfPayloadFanSetSpeed;
typedef struct __attribute__((packed)) {
uint8_t speed;
uint8_t timer;
} RfPayloadFanSetTimer;
typedef struct __attribute__((packed)) {
uint8_t rx_type; // 0x00 RX Type
uint8_t rx_id; // 0x01 RX ID
uint8_t tx_type; // 0x02 TX Type
uint8_t tx_id; // 0x03 TX ID
uint8_t ttl; // 0x04 Time-To-Live
uint8_t command; // 0x05 Frame type
uint8_t parameter_count; // 0x06 Number of parameters
union {
uint8_t parameters[9]; // 0x07 - 0x0F Depends on command
RfPayloadFanSetSpeed setSpeed; // Command 0x02
RfPayloadFanSetTimer setTimer; // Command 0x03
RfPayloadNetworkJoinRequest networkJoinRequest; // Command 0x04
RfPayloadNetworkJoinOpen networkJoinOpen; // Command 0x06
RfPayloadFanSettings fanSettings; // Command 0x07
RfPayloadNetworkJoinAck networkJoinAck; // Command 0x0C
} payload;
} RfFrame;
ZehnderRF::ZehnderRF(void) {}
fan::FanTraits ZehnderRF::get_traits() { return fan::FanTraits(false, true, false, this->speed_count_); }
void ZehnderRF::control(const fan::FanCall &call) {
if (call.get_state().has_value()) {
this->state = *call.get_state();
ESP_LOGD(TAG, "Control has state: %u", this->state);
}
if (call.get_speed().has_value()) {
this->speed = *call.get_speed();
ESP_LOGD(TAG, "Control has speed: %u", this->speed);
}
switch (this->state_) {
case StateIdle:
// Set speed
this->setSpeed(this->state ? this->speed : 0x00, 0);
this->lastFanQuery_ = millis(); // Update time
break;
default:
break;
}
this->publish_state();
}
void ZehnderRF::setup() {
ESP_LOGCONFIG(TAG, "ZEHNDER '%s':", this->get_name().c_str());
// Clear config
memset(&this->config_, 0, sizeof(Config));
uint32_t hash = fnv1_hash("zehnderrf");
this->pref_ = global_preferences->make_preference<Config>(hash, true);
if (this->pref_.load(&this->config_)) {
ESP_LOGD(TAG, "Config load ok");
}
// Set nRF905 config
nrf905::Config rfConfig;
rfConfig = this->rf_->getConfig();
rfConfig.band = true;
rfConfig.channel = 118;
// // CRC 16
rfConfig.crc_enable = true;
rfConfig.crc_bits = 16;
// // TX power 10
rfConfig.tx_power = 10;
// // RX power normal
rfConfig.rx_power = nrf905::PowerNormal;
rfConfig.rx_address = 0x89816EA9; // ZEHNDER_NETWORK_LINK_ID;
rfConfig.rx_address_width = 4;
rfConfig.rx_payload_width = 16;
rfConfig.tx_address_width = 4;
rfConfig.tx_payload_width = 16;
rfConfig.xtal_frequency = 16000000; // defaults for now
rfConfig.clkOutFrequency = nrf905::ClkOut500000;
rfConfig.clkOutEnable = false;
// Write config back
this->rf_->updateConfig(&rfConfig);
this->rf_->writeTxAddress(0x89816EA9);
this->speed_count_ = 4;
this->rf_->setOnTxReady([this](void) {
ESP_LOGD(TAG, "Tx Ready");
if (this->rfState_ == RfStateTxBusy) {
if (this->retries_ >= 0) {
this->msgSendTime_ = millis();
this->rfState_ = RfStateRxWait;
} else {
this->rfState_ = RfStateIdle;
}
}
});
this->rf_->setOnRxComplete([this](const uint8_t *const pData, const uint8_t dataLength) {
ESP_LOGV(TAG, "Received frame");
this->rfHandleReceived(pData, dataLength);
});
}
void ZehnderRF::dump_config(void) {
ESP_LOGCONFIG(TAG, "Zehnder Fan config:");
ESP_LOGCONFIG(TAG, " Polling interval %u", this->interval_);
ESP_LOGCONFIG(TAG, " Fan networkId 0x%08X", this->config_.fan_networkId);
ESP_LOGCONFIG(TAG, " Fan my device type 0x%02X", this->config_.fan_my_device_type);
ESP_LOGCONFIG(TAG, " Fan my device id 0x%02X", this->config_.fan_my_device_id);
ESP_LOGCONFIG(TAG, " Fan main_unit type 0x%02X", this->config_.fan_main_unit_type);
ESP_LOGCONFIG(TAG, " Fan main unit id 0x%02X", this->config_.fan_main_unit_id);
}
void ZehnderRF::loop(void) {
uint8_t deviceId;
nrf905::Config rfConfig;
// Run RF handler
this->rfHandler();
switch (this->state_) {
case StateStartup:
// Wait until started up
if (millis() > 15000) {
// Discovery?
if ((this->config_.fan_networkId == 0x00000000) || (this->config_.fan_my_device_type == 0) ||
(this->config_.fan_my_device_id == 0) || (this->config_.fan_main_unit_type == 0) ||
(this->config_.fan_main_unit_id == 0)) {
ESP_LOGD(TAG, "Invalid config, start paring");
this->state_ = StateStartDiscovery;
} else {
ESP_LOGD(TAG, "Config data valid, start polling");
rfConfig = this->rf_->getConfig();
rfConfig.rx_address = this->config_.fan_networkId;
this->rf_->updateConfig(&rfConfig);
this->rf_->writeTxAddress(this->config_.fan_networkId);
// Start with query
this->queryDevice();
}
}
break;
case StateStartDiscovery:
deviceId = this->createDeviceID();
this->discoveryStart(deviceId);
// For now just set TX
break;
case StateIdle:
if (newSetting == true) {
this->setSpeed(newSpeed, newTimer);
} else {
if ((millis() - this->lastFanQuery_) > this->interval_) {
this->queryDevice();
}
}
break;
case StateWaitSetSpeedConfirm:
if (this->rfState_ == RfStateIdle) {
// When done, return to idle
this->state_ = StateIdle;
}
default:
break;
}
}
void ZehnderRF::rfHandleReceived(const uint8_t *const pData, const uint8_t dataLength) {
const RfFrame *const pResponse = (RfFrame *) pData;
RfFrame *const pTxFrame = (RfFrame *) this->_txFrame; // frame helper
nrf905::Config rfConfig;
ESP_LOGD(TAG, "Current state: 0x%02X", this->state_);
switch (this->state_) {
case StateDiscoveryWaitForLinkRequest:
ESP_LOGD(TAG, "DiscoverStateWaitForLinkRequest");
switch (pResponse->command) {
case FAN_NETWORK_JOIN_OPEN: // Received linking request from main unit
ESP_LOGD(TAG, "Discovery: Found unit type 0x%02X (%s) with ID 0x%02X on network 0x%08X", pResponse->tx_type,
pResponse->tx_type == FAN_TYPE_MAIN_UNIT ? "Main" : "?", pResponse->tx_id,
pResponse->payload.networkJoinOpen.networkId);
this->rfComplete();
(void) memset(this->_txFrame, 0, FAN_FRAMESIZE); // Clear frame data
// Found a main unit, so send a join request
pTxFrame->rx_type = FAN_TYPE_MAIN_UNIT; // Set type to main unit
pTxFrame->rx_id = pResponse->tx_id; // Set ID to the ID of the main unit
pTxFrame->tx_type = this->config_.fan_my_device_type;
pTxFrame->tx_id = this->config_.fan_my_device_id;
pTxFrame->ttl = FAN_TTL;
pTxFrame->command = FAN_NETWORK_JOIN_REQUEST; // Request to connect to network
pTxFrame->parameter_count = sizeof(RfPayloadNetworkJoinOpen);
// Request to connect to the received network ID
pTxFrame->payload.networkJoinRequest.networkId = pResponse->payload.networkJoinOpen.networkId;
// Store for later
this->config_.fan_networkId = pResponse->payload.networkJoinOpen.networkId;
this->config_.fan_main_unit_type = pResponse->tx_type;
this->config_.fan_main_unit_id = pResponse->tx_id;
// Update address
rfConfig = this->rf_->getConfig();
rfConfig.rx_address = pResponse->payload.networkJoinOpen.networkId;
this->rf_->updateConfig(&rfConfig, NULL);
this->rf_->writeTxAddress(pResponse->payload.networkJoinOpen.networkId, NULL);
// Send response frame
this->startTransmit(this->_txFrame, FAN_TX_RETRIES, [this]() {
ESP_LOGW(TAG, "Query Timeout");
this->state_ = StateStartDiscovery;
});
this->state_ = StateDiscoveryWaitForJoinResponse;
break;
default:
ESP_LOGD(TAG, "Discovery: Received unknown frame type 0x%02X from ID 0x%02X", pResponse->command,
pResponse->tx_id);
break;
}
break;
case StateDiscoveryWaitForJoinResponse:
ESP_LOGD(TAG, "DiscoverStateWaitForJoinResponse");
switch (pResponse->command) {
case FAN_FRAME_0B:
if ((pResponse->rx_type == this->config_.fan_my_device_type) &&
(pResponse->rx_id == this->config_.fan_my_device_id) &&
(pResponse->tx_type == this->config_.fan_main_unit_type) &&
(pResponse->tx_id == this->config_.fan_main_unit_id)) {
ESP_LOGD(TAG, "Discovery: Link successful to unit with ID 0x%02X on network 0x%08X", pResponse->tx_id,
this->config_.fan_networkId);
this->rfComplete();
(void) memset(this->_txFrame, 0, FAN_FRAMESIZE); // Clear frame data
pTxFrame->rx_type = FAN_TYPE_MAIN_UNIT; // Set type to main unit
pTxFrame->rx_id = pResponse->tx_id; // Set ID to the ID of the main unit
pTxFrame->tx_type = this->config_.fan_my_device_type;
pTxFrame->tx_id = this->config_.fan_my_device_id;
pTxFrame->ttl = FAN_TTL;
pTxFrame->command = FAN_FRAME_0B; // 0x0B acknowledge link successful
pTxFrame->parameter_count = 0x00; // No parameters
// Send response frame
this->startTransmit(this->_txFrame, FAN_TX_RETRIES, [this]() {
ESP_LOGW(TAG, "Query Timeout");
this->state_ = StateStartDiscovery;
});
this->state_ = StateDiscoveryJoinComplete;
} else {
ESP_LOGE(TAG, "Discovery: Received unknown link success from ID 0x%02X on network 0x%08X", pResponse->tx_id,
this->config_.fan_networkId);
}
break;
default:
ESP_LOGE(TAG, "Discovery: Received unknown frame type 0x%02X from ID 0x%02X", pResponse->command,
pResponse->tx_id);
break;
}
break;
case StateDiscoveryJoinComplete:
ESP_LOGD(TAG, "StateDiscoveryJoinComplete");
switch (pResponse->command) {
case FAN_TYPE_QUERY_NETWORK:
if ((pResponse->rx_type == this->config_.fan_main_unit_type) &&
(pResponse->rx_id == this->config_.fan_main_unit_id) &&
(pResponse->tx_type == this->config_.fan_main_unit_type) &&
(pResponse->tx_id == this->config_.fan_main_unit_id)) {
ESP_LOGD(TAG, "Discovery: received network join success 0x0D");
this->rfComplete();
ESP_LOGD(TAG, "Saving pairing config");
this->pref_.save(&this->config_);
this->state_ = StateIdle;
} else {
ESP_LOGW(TAG, "Unexpected frame join reponse from Type 0x%02X ID 0x%02X", pResponse->tx_type,
pResponse->tx_id);
}
break;
default:
ESP_LOGE(TAG, "Discovery: Received unknown frame type 0x%02X from ID 0x%02X on network 0x%08X",
pResponse->command, pResponse->tx_id, this->config_.fan_networkId);
break;
}
break;
case StateWaitQueryResponse:
if ((pResponse->rx_type == this->config_.fan_my_device_type) && // If type
(pResponse->rx_id == this->config_.fan_my_device_id)) { // and id match, it is for us
switch (pResponse->command) {
case FAN_TYPE_FAN_SETTINGS:
ESP_LOGD(TAG, "Received fan settings; speed: 0x%02X voltage: %i timer: %i",
pResponse->payload.fanSettings.speed, pResponse->payload.fanSettings.voltage,
pResponse->payload.fanSettings.timer);
this->rfComplete();
this->state = pResponse->payload.fanSettings.speed > 0;
this->speed = pResponse->payload.fanSettings.speed;
this->publish_state();
this->state_ = StateIdle;
break;
default:
ESP_LOGD(TAG, "Received unexpected frame; type 0x%02X from ID 0x%02X", pResponse->command,
pResponse->tx_id);
break;
}
} else {
ESP_LOGD(TAG, "Received frame from unknown device; type 0x%02X from ID 0x%02X type 0x%02X", pResponse->command,
pResponse->tx_id, pResponse->tx_type);
}
break;
case StateWaitSetSpeedResponse:
if ((pResponse->rx_type == this->config_.fan_my_device_type) && // If type
(pResponse->rx_id == this->config_.fan_my_device_id)) { // and id match, it is for us
switch (pResponse->command) {
case FAN_TYPE_FAN_SETTINGS:
ESP_LOGD(TAG, "Received fan settings; speed: 0x%02X voltage: %i timer: %i",
pResponse->payload.fanSettings.speed, pResponse->payload.fanSettings.voltage,
pResponse->payload.fanSettings.timer);
this->rfComplete();
this->rfComplete();
(void) memset(this->_txFrame, 0, FAN_FRAMESIZE); // Clear frame data
pTxFrame->rx_type = this->config_.fan_main_unit_type; // Set type to main unit
pTxFrame->rx_id = this->config_.fan_main_unit_id; // Set ID to the ID of the main unit
pTxFrame->tx_type = this->config_.fan_my_device_type;
pTxFrame->tx_id = this->config_.fan_my_device_id;
pTxFrame->ttl = FAN_TTL;
pTxFrame->command = FAN_FRAME_SETSPEED_REPLY; // 0x0B acknowledge link successful
pTxFrame->parameter_count = 0x03; // 3 parameters
pTxFrame->payload.parameters[0] = 0x54;
pTxFrame->payload.parameters[1] = 0x03;
pTxFrame->payload.parameters[2] = 0x20;
// Send response frame
this->startTransmit(this->_txFrame, -1, NULL);
this->state_ = StateWaitSetSpeedConfirm;
break;
case FAN_FRAME_SETSPEED_REPLY:
case FAN_FRAME_SETVOLTAGE_REPLY:
// this->rfComplete();
// this->state_ = StateIdle;
break;
default:
ESP_LOGD(TAG, "Received unexpected frame; type 0x%02X from ID 0x%02X", pResponse->command,
pResponse->tx_id);
break;
}
} else {
ESP_LOGD(TAG, "Received frame from unknown device; type 0x%02X from ID 0x%02X type 0x%02X", pResponse->command,
pResponse->tx_id, pResponse->tx_type);
}
break;
default:
ESP_LOGD(TAG, "Received frame from unknown device in unknown state; type 0x%02X from ID 0x%02X type 0x%02X",
pResponse->command, pResponse->tx_id, pResponse->tx_type);
break;
}
}
static uint8_t minmax(const uint8_t value, const uint8_t min, const uint8_t max) {
if (value <= min) {
return min;
} else if (value >= max) {
return max;
} else {
return value;
}
}
uint8_t ZehnderRF::createDeviceID(void) {
uint8_t random = (uint8_t) random_uint32();
// Generate random device_id; don't use 0x00 and 0xFF
// TODO: there's a 1 in 255 chance that the generated ID matches the ID of the main unit. Decide how to deal
// withthis (some sort of ping discovery?)
return minmax(random, 1, 0xFE);
}
void ZehnderRF::queryDevice(void) {
RfFrame *const pFrame = (RfFrame *) this->_txFrame; // frame helper
ESP_LOGD(TAG, "Query device");
this->lastFanQuery_ = millis(); // Update time
// Clear frame data
(void) memset(this->_txFrame, 0, FAN_FRAMESIZE);
// Build frame
pFrame->rx_type = this->config_.fan_main_unit_type;
pFrame->rx_id = this->config_.fan_main_unit_id;
pFrame->tx_type = this->config_.fan_my_device_type;
pFrame->tx_id = this->config_.fan_my_device_id;
pFrame->ttl = FAN_TTL;
pFrame->command = FAN_TYPE_QUERY_DEVICE;
pFrame->parameter_count = 0x00; // No parameters
this->startTransmit(this->_txFrame, FAN_TX_RETRIES, [this]() {
ESP_LOGW(TAG, "Query Timeout");
this->state_ = StateIdle;
});
this->state_ = StateWaitQueryResponse;
}
void ZehnderRF::setSpeed(const uint8_t paramSpeed, const uint8_t paramTimer) {
RfFrame *const pFrame = (RfFrame *) this->_txFrame; // frame helper
uint8_t speed = paramSpeed;
uint8_t timer = paramTimer;
if (speed > this->speed_count_) {
ESP_LOGW(TAG, "Requested speed too high (%u)", speed);
speed = this->speed_count_;
}
ESP_LOGD(TAG, "Set speed: 0x%02X; Timer %u minutes", speed, timer);
if (this->state_ == StateIdle) {
(void) memset(this->_txFrame, 0, FAN_FRAMESIZE); // Clear frame data
// Build frame
pFrame->rx_type = this->config_.fan_main_unit_type;
pFrame->rx_id = this->config_.fan_main_unit_id;
pFrame->tx_type = this->config_.fan_my_device_type;
pFrame->tx_id = this->config_.fan_my_device_id;
pFrame->ttl = FAN_TTL;
if (timer == 0) {
pFrame->command = FAN_FRAME_SETSPEED;
pFrame->parameter_count = sizeof(RfPayloadFanSetSpeed);
pFrame->payload.setSpeed.speed = speed;
} else {
pFrame->command = FAN_FRAME_SETTIMER;
pFrame->parameter_count = sizeof(RfPayloadFanSetTimer);
pFrame->payload.setTimer.speed = speed;
pFrame->payload.setTimer.timer = timer;
}
this->startTransmit(this->_txFrame, FAN_TX_RETRIES, [this]() {
ESP_LOGW(TAG, "Set speed timeout");
this->state_ = StateIdle;
});
newSetting = false;
this->state_ = StateWaitSetSpeedResponse;
} else {
ESP_LOGD(TAG, "Invalid state, I'm trying later again");
newSpeed = speed;
newTimer = timer;
newSetting = true;
}
}
void ZehnderRF::discoveryStart(const uint8_t deviceId) {
RfFrame *const pFrame = (RfFrame *) this->_txFrame; // frame helper
nrf905::Config rfConfig;
ESP_LOGD(TAG, "Start discovery with ID %u", deviceId);
this->config_.fan_my_device_type = FAN_TYPE_REMOTE_CONTROL;
this->config_.fan_my_device_id = deviceId;
// Build frame
(void) memset(this->_txFrame, 0, FAN_FRAMESIZE); // Clear frame data
// Set payload, available for linking
pFrame->rx_type = 0x04;
pFrame->rx_id = 0x00;
pFrame->tx_type = this->config_.fan_my_device_type;
pFrame->tx_id = this->config_.fan_my_device_id;
pFrame->ttl = FAN_TTL;
pFrame->command = FAN_NETWORK_JOIN_ACK;
pFrame->parameter_count = sizeof(RfPayloadNetworkJoinAck);
pFrame->payload.networkJoinAck.networkId = NETWORK_LINK_ID;
// Set RX and TX address
rfConfig = this->rf_->getConfig();
rfConfig.rx_address = NETWORK_LINK_ID;
this->rf_->updateConfig(&rfConfig, NULL);
this->rf_->writeTxAddress(NETWORK_LINK_ID, NULL);
this->startTransmit(this->_txFrame, FAN_TX_RETRIES, [this]() {
ESP_LOGW(TAG, "Start discovery timeout");
this->state_ = StateStartDiscovery;
});
// Update state
this->state_ = StateDiscoveryWaitForLinkRequest;
}
Result ZehnderRF::startTransmit(const uint8_t *const pData, const int8_t rxRetries,
const std::function<void(void)> callback) {
Result result = ResultOk;
unsigned long startTime;
bool busy = true;
if (this->rfState_ != RfStateIdle) {
ESP_LOGW(TAG, "TX still ongoing");
result = ResultBusy;
} else {
this->onReceiveTimeout_ = callback;
this->retries_ = rxRetries;
// Write data to RF
// if (pData != NULL) { // If frame given, load it in the nRF. Else use previous TX payload
// ESP_LOGD(TAG, "Write payload");
this->rf_->writeTxPayload(pData, FAN_FRAMESIZE); // Use framesize
// }
this->rfState_ = RfStateWaitAirwayFree;
this->airwayFreeWaitTime_ = millis();
}
return result;
}
void ZehnderRF::rfComplete(void) {
this->retries_ = -1; // Disable this->retries_
this->rfState_ = RfStateIdle;
}
void ZehnderRF::rfHandler(void) {
switch (this->rfState_) {
case RfStateIdle:
break;
case RfStateWaitAirwayFree:
if ((millis() - this->airwayFreeWaitTime_) > 5000) {
ESP_LOGW(TAG, "Airway too busy, giving up");
this->rfState_ = RfStateIdle;
if (this->onReceiveTimeout_ != NULL) {
this->onReceiveTimeout_();
}
} else if (this->rf_->airwayBusy() == false) {
ESP_LOGD(TAG, "Start TX");
this->rf_->startTx(FAN_TX_FRAMES, nrf905::Receive); // After transmit, wait for response
this->rfState_ = RfStateTxBusy;
}
break;
case RfStateTxBusy:
break;
case RfStateRxWait:
if ((this->retries_ >= 0) && ((millis() - this->msgSendTime_) > FAN_REPLY_TIMEOUT)) {
ESP_LOGD(TAG, "Receive timeout");
if (this->retries_ > 0) {
--this->retries_;
ESP_LOGD(TAG, "No data received, retry again (left: %u)", this->retries_);
this->rfState_ = RfStateWaitAirwayFree;
this->airwayFreeWaitTime_ = millis();
} else if (this->retries_ == 0) {
// Oh oh, ran out of options
ESP_LOGD(TAG, "No messages received, giving up now...");
if (this->onReceiveTimeout_ != NULL) {
this->onReceiveTimeout_();
}
// Back to idle
this->rfState_ = RfStateIdle;
}
}
break;
default:
break;
}
}
} // namespace zehnder
} // namespace esphome