Remove global variable and use get/set pattern, fix const arg.

This commit is contained in:
Lorenzo Prosseda 2024-11-12 11:08:14 +01:00
parent 2450a8d12c
commit 3d01315c84
No known key found for this signature in database
GPG key ID: 316B7756E0101C16
2 changed files with 14 additions and 7 deletions

View file

@ -5,7 +5,10 @@ namespace esphome {
namespace airton {
static const char *const TAG = "airton.climate";
uint8_t previous_mode_ = 0;
uint8_t AirtonClimate::get_previous_mode_() { return previous_mode_; }
void AirtonClimate::set_previous_mode_(uint8_t mode) { previous_mode_ = mode; }
void AirtonClimate::transmit_state() {
// Sampled valid state
@ -81,9 +84,9 @@ uint8_t AirtonClimate::operation_mode_() {
break;
case climate::CLIMATE_MODE_OFF:
default:
operating_mode = 0b0111 & this->previous_mode_; // Set previous mode with power state bit off
operating_mode = 0b0111 & this->get_previous_mode_(); // Set previous mode with power state bit off
}
this->previous_mode_ = operating_mode;
this->set_previous_mode_(operating_mode);
return operating_mode;
}
@ -161,7 +164,7 @@ uint8_t AirtonClimate::checksum_(const uint8_t *r_state) {
return checksum;
}
bool AirtonClimate::parse_state_frame_(uint8_t frame[]) {
bool AirtonClimate::parse_state_frame_(uint8_t const frame[]) {
uint8_t mode = frame[2];
if (mode & 0b00001000) { // Check if power state bit is set
switch (mode & 0b00000111) { // Mask anything but the least significant 3 bits

View file

@ -45,10 +45,14 @@ class AirtonClimate : public climate_ir::ClimateIR {
climate::CLIMATE_FAN_HIGH},
{climate::CLIMATE_SWING_OFF, climate::CLIMATE_SWING_VERTICAL}) {}
protected:
// Save the previous operation mode globally
private:
// Save the previous operation mode inside instance
uint8_t previous_mode_;
protected:
uint8_t get_previous_mode_();
void set_previous_mode_(uint8_t mode);
// IR transmission payload builder
void transmit_state() override;
@ -64,7 +68,7 @@ class AirtonClimate : public climate_ir::ClimateIR {
// IR receiver buffer
bool on_receive(remote_base::RemoteReceiveData data) override;
bool parse_state_frame_(uint8_t frame[]);
bool parse_state_frame_(uint8_t const frame[]);
};
} // namespace airton