Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
fix multible remote roomcontrollers
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Jan 24, 2021
1 parent 37148f1 commit 28e13e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/roomcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,43 @@

namespace emsesp {

static uint32_t rc_time_ = 0;
static int16_t remotetemp[4] = {EMS_VALUE_SHORT_NOTSET, EMS_VALUE_SHORT_NOTSET, EMS_VALUE_SHORT_NOTSET, EMS_VALUE_SHORT_NOTSET};
// init statics
bool Roomctrl::switch_off_[HCS] = {false, false, false, false};
uint32_t Roomctrl::rc_time_[HCS] = {0, 0, 0, 0};
int16_t Roomctrl::remotetemp_[HCS] = {EMS_VALUE_SHORT_NOTSET, EMS_VALUE_SHORT_NOTSET, EMS_VALUE_SHORT_NOTSET, EMS_VALUE_SHORT_NOTSET};

/**
* set the temperature,
*/
void Roomctrl::set_remotetemp(const uint8_t hc, const int16_t temp) {
if (hc > 3) {
if (hc >= HCS) {
return;
}
remotetemp[hc] = temp;
if (remotetemp_[hc] != EMS_VALUE_SHORT_NOTSET && temp == EMS_VALUE_SHORT_NOTSET) {
switch_off_[hc] = true;
}
remotetemp_[hc] = temp;
}

/**
* if remote control is active send the temperature every minute
*/
void Roomctrl::send(const uint8_t addr) {
uint8_t hc_ = addr - ADDR;
uint8_t hc = addr - ADDR;
// check address, reply only on addresses 0x18..0x1B
if (hc_ > 3) {
if (hc >= HCS) {
return;
}
// no reply if the temperature is not set
if (remotetemp[hc_] == EMS_VALUE_SHORT_NOTSET) {
if (remotetemp_[hc] == EMS_VALUE_SHORT_NOTSET && !switch_off_[hc]) {
return;
}
if (uuid::get_uptime() - rc_time_ > SEND_INTERVAL) { // send every minute
rc_time_ = uuid::get_uptime(); // use EMS-ESP's millis() to prevent overhead
temperature(addr, 0x00); // send to all

// send roomtemp to all every minute
if (uuid::get_uptime() - rc_time_[hc] > SEND_INTERVAL || switch_off_[hc]) {
rc_time_[hc] = uuid::get_uptime();
temperature(addr, 0x00);
switch_off_[hc] = false;
} else {
// acknowledge every poll, otherwise the master shows error A22-816
EMSuart::send_poll(addr);
Expand All @@ -59,14 +67,14 @@ void Roomctrl::send(const uint8_t addr) {
* check if there is a message for the remote room controller
*/
void Roomctrl::check(const uint8_t addr, const uint8_t * data) {
uint8_t hc_ = (addr & 0x7F) - ADDR;
uint8_t hc = (addr & 0x7F) - ADDR;

// check address, reply only on addresses 0x18..0x1B
if (hc_ > 3) {
if (hc >= HCS) {
return;
}
// no reply if the temperature is not set
if (remotetemp[hc_] == EMS_VALUE_SHORT_NOTSET) {
if (remotetemp_[hc] == EMS_VALUE_SHORT_NOTSET) {
return;
}
// reply to writes with write nack byte
Expand All @@ -78,7 +86,7 @@ void Roomctrl::check(const uint8_t addr, const uint8_t * data) {
// empty message back if temperature not set or unknown message type
if (data[2] == 0x02) {
version(addr, data[0]);
} else if (remotetemp[hc_] == EMS_VALUE_SHORT_NOTSET) {
} else if (remotetemp_[hc] == EMS_VALUE_SHORT_NOTSET) {
unknown(addr, data[0], data[2], data[3]);
} else if (data[2] == 0xAF && data[3] == 0) {
temperature(addr, data[0]);
Expand Down Expand Up @@ -121,15 +129,15 @@ void Roomctrl::unknown(uint8_t addr, uint8_t dst, uint8_t type, uint8_t offset)
*/
void Roomctrl::temperature(uint8_t addr, uint8_t dst) {
uint8_t data[10];
uint8_t hc_ = addr - ADDR;
data[0] = addr;
data[1] = dst;
data[2] = 0xAF;
data[3] = 0;
data[4] = (uint8_t)(remotetemp[hc_] >> 8);
data[5] = (uint8_t)(remotetemp[hc_] & 0xFF);
data[6] = 0;
data[7] = EMSbus::calculate_crc(data, 7); // apppend CRC
uint8_t hc = addr - ADDR;
data[0] = addr;
data[1] = dst;
data[2] = 0xAF;
data[3] = 0;
data[4] = (uint8_t)(remotetemp_[hc] >> 8);
data[5] = (uint8_t)(remotetemp_[hc] & 0xFF);
data[6] = 0;
data[7] = EMSbus::calculate_crc(data, 7); // apppend CRC
EMSuart::transmit(data, 8);
}
/**
Expand Down
6 changes: 6 additions & 0 deletions src/roomcontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ class Roomctrl {
private:
static constexpr uint8_t ADDR = 0x18;
static constexpr uint32_t SEND_INTERVAL = 60000; // 1 minute
static constexpr uint8_t HCS = 4; // max 4 heating circuits

static void version(uint8_t addr, uint8_t dst);
static void unknown(uint8_t addr, uint8_t dst, uint8_t type, uint8_t offset);
static void temperature(uint8_t addr, uint8_t dst);
static void nack_write();

static bool switch_off_[HCS];
static uint32_t rc_time_[HCS];
static int16_t remotetemp_[HCS];

};

} // namespace emsesp
Expand Down

0 comments on commit 28e13e0

Please sign in to comment.