migrate callbacks from std::vector to std::map
This commit is contained in:
parent
c635427d64
commit
5f6e586945
11 changed files with 143 additions and 178 deletions
|
@ -22,8 +22,6 @@ ClimateCard::ClimateCard(uint8_t ir_pin, AirConditioner ac, uint8_t sensor_type,
|
|||
this->state.ac_temperature = 25;
|
||||
this->state.ac_mode = 0;
|
||||
this->state.ac_fan_speed = 0;
|
||||
// Initialize callbacks
|
||||
this->callbacks = std::vector<std::function<void(uint8_t, uint8_t, uint8_t)>>();
|
||||
}
|
||||
|
||||
ClimateCard::ClimateCard(uint8_t ir_pin, AirConditioner ac) : ClimateCard(ir_pin, ac, AC_SENSOR_TYPE_NONE, 0)
|
||||
|
@ -89,46 +87,24 @@ void ClimateCard::saveStateToFRAM()
|
|||
fram->write8(fram_address, state.ac_temperature);
|
||||
fram->write8(fram_address + 1, state.ac_mode);
|
||||
fram->write8(fram_address + 2, state.ac_fan_speed);
|
||||
Serial.println("Saved state to FRAM");
|
||||
Serial.write(0xFF);
|
||||
Serial.write(0xFF);
|
||||
Serial.write(0xFF);
|
||||
}
|
||||
|
||||
void ClimateCard::loadStateFromFRAM()
|
||||
{
|
||||
Serial.print("Loading temperature from FRAM at address ");
|
||||
Serial.print(fram_address);
|
||||
Serial.print(": ");
|
||||
Serial.println(fram->read8(fram_address));
|
||||
state.ac_temperature = fram->read8(fram_address);
|
||||
Serial.print("Loading mode from FRAM at address ");
|
||||
Serial.print(fram_address + 1);
|
||||
Serial.print(": ");
|
||||
Serial.println(fram->read8(fram_address + 1));
|
||||
state.ac_mode = fram->read8(fram_address + 1);
|
||||
Serial.print("Loading fan speed from FRAM at address ");
|
||||
Serial.print(fram_address + 2);
|
||||
Serial.print(": ");
|
||||
Serial.println(fram->read8(fram_address + 2));
|
||||
state.ac_fan_speed = fram->read8(fram_address + 2);
|
||||
Serial.write(0xFF);
|
||||
Serial.write(0xFF);
|
||||
Serial.write(0xFF);
|
||||
// if (state.ac_temperature > ac.max_temperature)
|
||||
// state.ac_temperature = ac.max_temperature;
|
||||
// else if (state.ac_temperature < ac.min_temperature)
|
||||
// state.ac_temperature = ac.min_temperature;
|
||||
// // If mode is out of range, set to 0
|
||||
// if (state.ac_mode > ac.modes)
|
||||
// state.ac_mode = 0;
|
||||
// // If fan speed is out of range, set to 0
|
||||
// if (state.ac_fan_speed > ac.fan_speeds)
|
||||
// state.ac_fan_speed = 0;
|
||||
if (state.ac_temperature > ac.max_temperature)
|
||||
state.ac_temperature = ac.max_temperature;
|
||||
else if (state.ac_temperature < ac.min_temperature)
|
||||
state.ac_temperature = ac.min_temperature;
|
||||
// If mode is out of range, set to 0
|
||||
if (state.ac_mode > ac.modes)
|
||||
state.ac_mode = 0;
|
||||
// If fan speed is out of range, set to 0
|
||||
if (state.ac_fan_speed > ac.fan_speeds)
|
||||
state.ac_fan_speed = 0;
|
||||
updateAirConditioner();
|
||||
for (uint8_t i = 0; i < callbacks.size(); i++)
|
||||
for (const auto& callback : callbacks)
|
||||
{
|
||||
callbacks[i](this->state.ac_mode, this->state.ac_fan_speed, this->state.ac_temperature);
|
||||
callback.second(this->state.ac_mode, this->state.ac_fan_speed, this->state.ac_temperature);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -163,11 +139,7 @@ void ClimateCard::setFanSpeed(uint8_t fan_speed)
|
|||
|
||||
void ClimateCard::registerChangeCallback(std::function<void(uint8_t, uint8_t, uint8_t)> callback)
|
||||
{
|
||||
Serial.print("Registering callback");
|
||||
Serial.write(0xFF);
|
||||
Serial.write(0xFF);
|
||||
Serial.write(0xFF);
|
||||
callbacks.push_back(callback);
|
||||
callbacks[callbacks_handler_count++] = callback;
|
||||
}
|
||||
|
||||
uint8_t ClimateCard::getType()
|
||||
|
@ -202,9 +174,9 @@ void ClimateCard::updateSensor()
|
|||
room_temperature = ds18b20->getTempC();
|
||||
break;
|
||||
}
|
||||
for (uint8_t i = 0; i < sensor_callbacks.size(); i++)
|
||||
for (const auto& callback : sensor_callbacks)
|
||||
{
|
||||
sensor_callbacks[i](room_temperature, humidity);
|
||||
callback.second(room_temperature, humidity);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -229,14 +201,9 @@ void ClimateCard::updateAirConditioner()
|
|||
// rmt_write_items(RMT_TX_CHANNEL, items, itemCount, true);
|
||||
// rmt_wait_tx_done(RMT_TX_CHANNEL, portMAX_DELAY);
|
||||
// // Publish state
|
||||
Serial.print("Callbacks: ");
|
||||
Serial.println(callbacks.size());
|
||||
Serial.write(0xFF);
|
||||
Serial.write(0xFF);
|
||||
Serial.write(0xFF);
|
||||
for (uint8_t i = 0; i < callbacks.size(); i++)
|
||||
for (const auto& callback : callbacks)
|
||||
{
|
||||
callbacks[i](this->state.ac_mode, this->state.ac_fan_speed, this->state.ac_temperature);
|
||||
callback.second(this->state.ac_mode, this->state.ac_fan_speed, this->state.ac_temperature);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -272,6 +239,15 @@ uint8_t ClimateCard::getFanSpeed()
|
|||
|
||||
void ClimateCard::registerSensorCallback(std::function<void(float, float)> callback)
|
||||
{
|
||||
sensor_callbacks.push_back(callback);
|
||||
sensor_callbacks[sensor_callbacks_handler_count++] = callback;
|
||||
}
|
||||
|
||||
void ClimateCard::unregisterChangeCallback(uint8_t handler)
|
||||
{
|
||||
callbacks.erase(handler);
|
||||
}
|
||||
|
||||
void ClimateCard::unregisterSensorCallback(uint8_t handler)
|
||||
{
|
||||
sensor_callbacks.erase(handler);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue