Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the command from the queue as soon as we send #36

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions sensor/src/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,14 @@ void IRAM_ATTR panelSelected() {
clearRXbuffer();
}

void sendCommand(String command, int count) {
void sendCommand(String command, int count = 1) {
Serial.printf("Sending %s - %u times\n", command.c_str(), count);
for (int i = 0; i < count; i++) {
sendBuffer.enqueue(command.c_str());
}
for (int i = 0; i < 3; i++) {
sendBuffer.enqueue(COMMAND_EMPTY);
}
}

void setOption(int currentIndex, int targetIndex, int options, String command = COMMAND_DOWN) {
Expand All @@ -184,7 +187,7 @@ void onSwitchStateChanged(bool state, HASwitch* sender) {
Serial.printf("Switch %s changed - ", sender->getName());
if (state != lightState) {
Serial.println("Toggle");
sendBuffer.enqueue(COMMAND_LIGHT);
sendCommand(COMMAND_LIGHT);
} else {
Serial.println("No change needed");
}
Expand All @@ -206,22 +209,22 @@ void onModeSwitchStateChanged(int8_t index, HASelect* sender) {
Serial.printf("Mode Switch changed - %u\n", index);
int currentIndex = sender->getCurrentState();
int options = 3;
sendBuffer.enqueue(COMMAND_CHANGE_MODE);
sendCommand(COMMAND_CHANGE_MODE);
setOption(currentIndex, index, options);
sendBuffer.enqueue(COMMAND_CHANGE_MODE);
sendCommand(COMMAND_CHANGE_MODE);
}

void onButtonPress(HAButton* sender) {
String name = sender->getName();
Serial.printf("Button press - %s\n", name);
if (name == "Up") {
sendBuffer.enqueue(COMMAND_UP);
sendCommand(COMMAND_UP);
} else if (name == "Down") {
sendBuffer.enqueue(COMMAND_DOWN);
sendCommand(COMMAND_DOWN);
} else if (name == "Mode") {
sendBuffer.enqueue(COMMAND_CHANGE_MODE);
sendCommand(COMMAND_CHANGE_MODE);
} else if (name == "Time") {
sendBuffer.enqueue(COMMAND_TIME);
sendCommand(COMMAND_TIME);
} else {
Serial.printf("Unknown button %s\n", name);
}
Expand All @@ -242,21 +245,16 @@ void onTargetTemperatureCommand(HANumeric temperature, HAHVAC* sender) {

int target = temperatureFloat * 2; // 0.5 inc so double
int current = tubTargetTemp * 2;
sendBuffer.enqueue(COMMAND_UP); // Enter set temp mode
sendBuffer.enqueue(COMMAND_EMPTY);
sendCommand(COMMAND_UP); // Enter set temp mode

if (temperatureFloat > tubTargetTemp) {
for (int i = 0; i < (target - current); i++) {
Serial.println("Raise the temp");
sendBuffer.enqueue(COMMAND_UP);
// sendBuffer.enqueue(COMMAND_EMPTY);
}
int count = (target - current);
Serial.printf("Raise the temp x %u\n", count);
sendCommand(COMMAND_UP, count);
} else {
for (int i = 0; i < (current - target); i++) {
Serial.println("Lower the temp");
sendBuffer.enqueue(COMMAND_DOWN);
// sendBuffer.enqueue(COMMAND_EMPTY);
}
int count = (current - target);
Serial.printf("Lower the temp x %u\n", count);
sendCommand(COMMAND_DOWN, count);
}

// sender->setTargetTemperature(temperature); // report target temperature back to the HA panel - better to see what
Expand Down Expand Up @@ -745,11 +743,6 @@ void handleMessage(size_t len, uint8_t buf[]) {
} else {
telnetSend("CMD: " + cmd);
}
if (!lastRaw3.equals(cmd)) {
// Controller responded to command
sendBuffer.dequeue();
Serial.printf("YAY: command response : %u\n", delayTime);
}

if (!lastRaw3.equals(cmd) && cmd != "0000000000") { // ignore idle command
lastRaw3 = cmd;
Expand Down Expand Up @@ -863,7 +856,7 @@ void sendCommand() {
// wait for tx to finish and flush the rx buffer
tub.flush(false);
if (digitalRead(PIN_5_PIN) == LOW) {
// sendBuffer.dequeue(); // TODO: trying to resend now till we see response
sendBuffer.dequeue();
Serial.printf("message sent : %u\n", delayTime);
// delayTime += 10;
}
Expand Down