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

rewrite Airwell by using bit field #1254

Merged
merged 2 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
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
57 changes: 26 additions & 31 deletions src/ir_Airwell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ using irutils::addBoolToString;
using irutils::addModeToString;
using irutils::addFanToString;
using irutils::addTempToString;
using irutils::setBit;
using irutils::setBits;

#if SEND_AIRWELL
/// Send an Airwell Manchester Code formatted message.
Expand Down Expand Up @@ -94,14 +92,14 @@ void IRAirwellAc::begin(void) { _irsend.begin(); }
/// Get the raw state of the object, suitable to be sent with the appropriate
/// IRsend object method.
/// @return A copy of the internal state.
uint64_t IRAirwellAc::getRaw(void) {
return remote_state;
uint64_t IRAirwellAc::getRaw(void) const {
return _.raw;
}

/// Set the raw state of the object.
/// @param[in] state The raw state from the native IR message.
void IRAirwellAc::setRaw(const uint64_t state) {
remote_state = state;
_.raw = state;
}

#if SEND_AIRWELL
Expand All @@ -114,25 +112,25 @@ void IRAirwellAc::send(const uint16_t repeat) {

/// Reset the internals of the object to a known good state.
void IRAirwellAc::stateReset(void) {
remote_state = kAirwellKnownGoodState;
_.raw = kAirwellKnownGoodState;
}

/// Turn on/off the Power Airwell setting.
/// @param[in] on The desired setting state.
void IRAirwellAc::setPowerToggle(const bool on) {
setBit(&remote_state, kAirwellPowerToggleBit, on);
_.PowerToggle = on;
}

/// Get the power toggle setting from the internal state.
/// @return A boolean indicating the setting.
bool IRAirwellAc::getPowerToggle(void) {
return GETBIT64(remote_state, kAirwellPowerToggleBit);
bool IRAirwellAc::getPowerToggle(void) const {
return _.PowerToggle;
}

/// Get the current operation mode setting.
/// @return The current operation mode.
uint8_t IRAirwellAc::getMode(void) {
return GETBITS64(remote_state, kAirwellModeOffset, kAirwellModeSize);
uint8_t IRAirwellAc::getMode(void) const {
return _.Mode;
}

/// Set the desired operation mode.
Expand All @@ -144,10 +142,10 @@ void IRAirwellAc::setMode(const uint8_t mode) {
case kAirwellHeat:
case kAirwellDry:
case kAirwellAuto:
setBits(&remote_state, kAirwellModeOffset, kAirwellModeSize, mode);
_.Mode = mode;
break;
default:
setMode(kAirwellAuto);
_.Mode = kAirwellAuto;
}
setFan(getFan()); // Ensure the fan is at the correct speed for the new mode.
}
Expand Down Expand Up @@ -182,15 +180,14 @@ stdAc::opmode_t IRAirwellAc::toCommonMode(const uint8_t mode) {
/// @param[in] speed The desired setting.
/// @note The speed is locked to Low when in Dry mode.
void IRAirwellAc::setFan(const uint8_t speed) {
setBits(&remote_state, kAirwellFanOffset, kAirwellFanSize,
(getMode() == kAirwellDry) ? kAirwellFanLow
: std::min(speed, kAirwellFanAuto));
_.Fan = (_.Mode == kAirwellDry) ? kAirwellFanLow
: std::min(speed, kAirwellFanAuto);
}

/// Get the current fan speed setting.
/// @return The current fan speed.
uint8_t IRAirwellAc::getFan(void) {
return GETBITS64(remote_state, kAirwellFanOffset, kAirwellFanSize);
uint8_t IRAirwellAc::getFan(void) const {
return _.Fan;
}

/// Convert a stdAc::fanspeed_t enum into it's native speed.
Expand Down Expand Up @@ -228,27 +225,25 @@ stdAc::fanspeed_t IRAirwellAc::toCommonFanSpeed(const uint8_t speed) {
void IRAirwellAc::setTemp(const uint8_t degrees) {
uint8_t temp = std::max(kAirwellMinTemp, degrees);
temp = std::min(kAirwellMaxTemp, temp);
setBits(&remote_state, kAirwellTempOffset, kAirwellTempSize,
temp - kAirwellMinTemp + 1);
_.Temp = (temp - kAirwellMinTemp + 1);
}

/// Get the current temperature setting.
/// @return Get current setting for temp. in degrees celsius.
uint8_t IRAirwellAc::getTemp(void) {
return GETBITS64(remote_state, kAirwellTempOffset,
kAirwellTempSize) + kAirwellMinTemp - 1;
uint8_t IRAirwellAc::getTemp(void) const {
return _.Temp + kAirwellMinTemp - 1;
}

/// Convert the current internal state into its stdAc::state_t equivilant.
/// @return The stdAc equivilant of the native settings.
stdAc::state_t IRAirwellAc::toCommon(void) {
stdAc::state_t IRAirwellAc::toCommon(void) const {
stdAc::state_t result;
result.protocol = decode_type_t::AIRWELL;
result.power = getPowerToggle();
result.mode = toCommonMode(getMode());
result.power = _.PowerToggle;
result.mode = toCommonMode(_.Mode);
result.celsius = true;
result.degrees = getTemp();
result.fanspeed = toCommonFanSpeed(getFan());
result.fanspeed = toCommonFanSpeed(_.Fan);
// Not supported.
result.model = -1;
result.turbo = false;
Expand All @@ -267,13 +262,13 @@ stdAc::state_t IRAirwellAc::toCommon(void) {

/// Convert the current internal state into a human readable string.
/// @return A human readable string.
String IRAirwellAc::toString(void) {
String IRAirwellAc::toString(void) const {
String result = "";
result.reserve(70); // Reserve some heap for the string to reduce fragging.
result += addBoolToString(getPowerToggle(), kPowerToggleStr, false);
result += addModeToString(getMode(), kAirwellAuto, kAirwellCool,
result += addBoolToString(_.PowerToggle, kPowerToggleStr, false);
result += addModeToString(_.Mode, kAirwellAuto, kAirwellCool,
kAirwellHeat, kAirwellDry, kAirwellFan);
result += addFanToString(getFan(), kAirwellFanHigh, kAirwellFanLow,
result += addFanToString(_.Fan, kAirwellFanHigh, kAirwellFanLow,
kAirwellFanAuto, kAirwellFanAuto,
kAirwellFanMedium);
result += addTempToString(getTemp());
Expand Down
42 changes: 22 additions & 20 deletions src/ir_Airwell.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,34 @@
#include "IRsend_test.h"
#endif


union AirwellProtocol{
uint64_t raw; // The state of the IR remote in native IR code form.
struct {
uint64_t :19;
uint64_t Temp :4;
uint64_t :5;
uint64_t Fan :2;
uint64_t Mode :3;
uint64_t PowerToggle:1;
uint64_t :0;
};
};
// Constants
const uint64_t kAirwellKnownGoodState = 0x140500002; // Mode Fan, Speed 1, 25C
// Temperature
const uint8_t kAirwellMinTemp = 16; // Celsius
const uint8_t kAirwellMaxTemp = 30; // Celsius
const uint8_t kAirwellTempSize = 4; // Bits
const uint8_t kAirwellTempOffset = 19; // 0b1111 << 19
// Fan
const uint8_t kAirwellFanSize = 2; // Bits
const uint8_t kAirwellFanOffset = 28; // 0b11 << 28
const uint8_t kAirwellFanLow = 0; // 0b00
const uint8_t kAirwellFanMedium = 1; // 0b01
const uint8_t kAirwellFanHigh = 2; // 0b10
const uint8_t kAirwellFanAuto = 3; // 0b11
// Modes
const uint8_t kAirwellModeSize = 3; // Bits
const uint8_t kAirwellModeOffset = 30; // 0b111 << 30
const uint8_t kAirwellCool = 1; // 0b001
const uint8_t kAirwellHeat = 2; // 0b010
const uint8_t kAirwellAuto = 3; // 0b011
const uint8_t kAirwellDry = 4; // 0b100
const uint8_t kAirwellFan = 5; // 0b101
// Power
const uint8_t kAirwellPowerToggleBit = 33; // 0b1 << 33


// Classes
Expand All @@ -67,21 +70,21 @@ class IRAirwellAc {
#endif // SEND_AIRWELL
void begin();
void setPowerToggle(const bool on);
bool getPowerToggle();
bool getPowerToggle() const;
void setTemp(const uint8_t temp);
uint8_t getTemp();
uint8_t getTemp() const;
void setFan(const uint8_t speed);
uint8_t getFan();
uint8_t getFan() const;
void setMode(const uint8_t mode);
uint8_t getMode();
uint64_t getRaw();
uint8_t getMode() const;
uint64_t getRaw() const;
void setRaw(const uint64_t state);
uint8_t convertMode(const stdAc::opmode_t mode);
uint8_t convertFan(const stdAc::fanspeed_t speed);
static uint8_t convertMode(const stdAc::opmode_t mode);
static uint8_t convertFan(const stdAc::fanspeed_t speed);
static stdAc::opmode_t toCommonMode(const uint8_t mode);
static stdAc::fanspeed_t toCommonFanSpeed(const uint8_t speed);
stdAc::state_t toCommon(void);
String toString();
stdAc::state_t toCommon(void) const;
String toString() const;
#ifndef UNIT_TEST

private:
Expand All @@ -91,7 +94,6 @@ class IRAirwellAc {
IRsendTest _irsend; ///< Instance of the testing IR send class
/// @endcond
#endif
uint64_t remote_state; // The state of the IR remote in native IR code form.
void checksum(void);
AirwellProtocol _;
};
#endif // IR_AIRWELL_H_