Skip to content

Commit

Permalink
refactor ir_Tcl (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriuslzx authored Jan 9, 2021
1 parent 63e2538 commit 8cf7004
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 98 deletions.
139 changes: 67 additions & 72 deletions src/ir_Tcl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ using irutils::addIntToString;
using irutils::addLabeledString;
using irutils::addModeToString;
using irutils::addTempToString;
using irutils::setBit;
using irutils::setBits;

#if SEND_TCL112AC
/// Send a TCL 112-bit A/C message.
Expand All @@ -49,13 +47,13 @@ IRTcl112Ac::IRTcl112Ac(const uint16_t pin, const bool inverted,
: _irsend(pin, inverted, use_modulation) { stateReset(); }

/// Set up hardware to be able to send a message.
void IRTcl112Ac::begin(void) { this->_irsend.begin(); }
void IRTcl112Ac::begin(void) { _irsend.begin(); }

#if SEND_TCL112AC
/// Send the current internal state as an IR message.
/// @param[in] repeat Nr. of times the message will be repeated.
void IRTcl112Ac::send(const uint16_t repeat) {
this->_irsend.sendTcl112Ac(getRaw(), kTcl112AcStateLength, repeat);
_irsend.sendTcl112Ac(getRaw(), kTcl112AcStateLength, repeat);
}
#endif // SEND_TCL112AC

Expand All @@ -75,7 +73,7 @@ uint8_t IRTcl112Ac::calcChecksum(uint8_t state[], const uint16_t length) {
void IRTcl112Ac::checksum(const uint16_t length) {
// Stored the checksum value in the last byte.
if (length > 1)
remote_state[length - 1] = calcChecksum(remote_state, length);
_.Sum = calcChecksum(_.raw, length);
}

/// Verify the checksum is valid for a given state.
Expand All @@ -92,45 +90,45 @@ void IRTcl112Ac::stateReset(void) {
static const uint8_t reset[kTcl112AcStateLength] = {
0x23, 0xCB, 0x26, 0x01, 0x00, 0x24, 0x03, 0x07, 0x40, 0x00, 0x00, 0x00,
0x00, 0x03};
memcpy(remote_state, reset, kTcl112AcStateLength);
std::memcpy(_.raw, reset, kTcl112AcStateLength);
}

/// Get a PTR to the internal state/code for this protocol.
/// @return PTR to a code for this protocol based on the current internal state.
uint8_t* IRTcl112Ac::getRaw(void) {
this->checksum();
return remote_state;
checksum();
return _.raw;
}

/// Set the internal state from a valid code for this protocol.
/// @param[in] new_code A valid code for this protocol.
/// @param[in] length The length/size of the new_code array.
void IRTcl112Ac::setRaw(const uint8_t new_code[], const uint16_t length) {
memcpy(remote_state, new_code, std::min(length, kTcl112AcStateLength));
std::memcpy(_.raw, new_code, std::min(length, kTcl112AcStateLength));
}

/// Set the requested power state of the A/C to on.
void IRTcl112Ac::on(void) { this->setPower(true); }
void IRTcl112Ac::on(void) { setPower(true); }

/// Set the requested power state of the A/C to off.
void IRTcl112Ac::off(void) { this->setPower(false); }
void IRTcl112Ac::off(void) { setPower(false); }

/// Change the power setting.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRTcl112Ac::setPower(const bool on) {
setBit(&remote_state[5], kTcl112AcPowerOffset, on);
_.Power = on;
}

/// Get the value of the current power setting.
/// @return true, the setting is on. false, the setting is off.
bool IRTcl112Ac::getPower(void) {
return GETBIT8(remote_state[5], kTcl112AcPowerOffset);
bool IRTcl112Ac::getPower(void) const {
return _.Power;
}

/// Get the operating mode setting of the A/C.
/// @return The current operating mode setting.
uint8_t IRTcl112Ac::getMode(void) {
return remote_state[6] & 0xF;
uint8_t IRTcl112Ac::getMode(void) const {
return _.Mode;
}

/// Set the operating mode of the A/C.
Expand All @@ -141,16 +139,16 @@ void IRTcl112Ac::setMode(const uint8_t mode) {
// If we get an unexpected mode, default to AUTO.
switch (mode) {
case kTcl112AcFan:
this->setFan(kTcl112AcFanHigh);
setFan(kTcl112AcFanHigh);
// FALLTHRU
case kTcl112AcAuto:
case kTcl112AcCool:
case kTcl112AcHeat:
case kTcl112AcDry:
setBits(&remote_state[6], kLowNibble, kTcl112AcModeSize, mode);
_.Mode = mode;
break;
default:
setMode(kTcl112AcAuto);
_.Mode = kTcl112AcAuto;
}
}

Expand All @@ -164,18 +162,16 @@ void IRTcl112Ac::setTemp(const float celsius) {
// Convert to integer nr. of half degrees.
uint8_t nrHalfDegrees = safecelsius * 2;
// Do we have a half degree celsius?
setBit(&remote_state[12], kTcl112AcHalfDegreeOffset, nrHalfDegrees & 1);
setBits(&remote_state[7], kLowNibble, kNibbleSize,
(uint8_t)kTcl112AcTempMax - nrHalfDegrees / 2);
_.HalfDegree = nrHalfDegrees & 1;
_.Temp = static_cast<uint8_t>(kTcl112AcTempMax - nrHalfDegrees / 2);
}

/// Get the current temperature setting.
/// @return The current setting for temp. in degrees celsius.
/// @note The temperature resolution is 0.5 of a degree.
float IRTcl112Ac::getTemp(void) {
float result = kTcl112AcTempMax - GETBITS8(remote_state[7], kLowNibble,
kNibbleSize);
if (GETBIT8(remote_state[12], kTcl112AcHalfDegreeOffset)) result += 0.5;
float IRTcl112Ac::getTemp(void) const {
float result = kTcl112AcTempMax - _.Temp;
if (_.HalfDegree) result += 0.5;
return result;
}

Expand All @@ -188,94 +184,93 @@ void IRTcl112Ac::setFan(const uint8_t speed) {
case kTcl112AcFanLow:
case kTcl112AcFanMed:
case kTcl112AcFanHigh:
setBits(&remote_state[8], kLowNibble, kTcl112AcFanSize, speed);
_.Fan = speed;
break;
default:
this->setFan(kTcl112AcFanAuto);
_.Fan = kTcl112AcFanAuto;
}
}

/// Get the current fan speed setting.
/// @return The current fan speed/mode.
uint8_t IRTcl112Ac::getFan(void) {
return GETBITS8(remote_state[8], kLowNibble, kTcl112AcFanSize);
uint8_t IRTcl112Ac::getFan(void) const {
return _.Fan;
}

/// Set the economy setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRTcl112Ac::setEcono(const bool on) {
setBit(&remote_state[5], kTcl112AcBitEconoOffset, on);
_.Econo = on;
}

/// Get the economy setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRTcl112Ac::getEcono(void) {
return GETBIT8(remote_state[5], kTcl112AcBitEconoOffset);
bool IRTcl112Ac::getEcono(void) const {
return _.Econo;
}

/// Set the Health (Filter) setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRTcl112Ac::setHealth(const bool on) {
setBit(&remote_state[6], kTcl112AcBitHealthOffset, on);
_.Health = on;
}

/// Get the Health (Filter) setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRTcl112Ac::getHealth(void) {
return GETBIT8(remote_state[6], kTcl112AcBitHealthOffset);
bool IRTcl112Ac::getHealth(void) const {
return _.Health;
}

/// Set the Light (LED/Display) setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRTcl112Ac::setLight(const bool on) {
setBit(&remote_state[5], kTcl112AcBitLightOffset, !on); // Cleared when on.
_.Light = !on; // Cleared when on.
}

/// Get the Light (LED/Display) setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRTcl112Ac::getLight(void) {
return !GETBIT8(remote_state[5], kTcl112AcBitLightOffset);
bool IRTcl112Ac::getLight(void) const {
return !_.Light;
}

/// Set the horizontal swing setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRTcl112Ac::setSwingHorizontal(const bool on) {
setBit(&remote_state[12], kTcl112AcBitSwingHOffset, on);
_.SwingH = on;
}

/// Get the horizontal swing setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRTcl112Ac::getSwingHorizontal(void) {
return GETBIT8(remote_state[12], kTcl112AcBitSwingHOffset);
bool IRTcl112Ac::getSwingHorizontal(void) const {
return _.SwingH;
}

/// Set the vertical swing setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRTcl112Ac::setSwingVertical(const bool on) {
setBits(&remote_state[8], kTcl112AcSwingVOffset, kTcl112AcSwingVSize,
on ? kTcl112AcSwingVOn : kTcl112AcSwingVOff);
_.SwingV = (on ? kTcl112AcSwingVOn : kTcl112AcSwingVOff);
}

/// Get the vertical swing setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRTcl112Ac::getSwingVertical(void) {
return GETBITS8(remote_state[8], kTcl112AcSwingVOffset, kTcl112AcSwingVSize);
bool IRTcl112Ac::getSwingVertical(void) const {
return _.SwingV;
}

/// Set the Turbo setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRTcl112Ac::setTurbo(const bool on) {
setBit(&remote_state[6], kTcl112AcBitTurboOffset, on);
_.Turbo = on;
if (on) {
this->setFan(kTcl112AcFanHigh);
this->setSwingVertical(true);
_.Fan = kTcl112AcFanHigh;
_.SwingV = kTcl112AcSwingVOn;
}
}

/// Get the Turbo setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRTcl112Ac::getTurbo(void) {
return GETBIT8(remote_state[6], kTcl112AcBitTurboOffset);
bool IRTcl112Ac::getTurbo(void) const {
return _.Turbo;
}

/// Convert a stdAc::opmode_t enum into its native mode.
Expand Down Expand Up @@ -332,23 +327,23 @@ stdAc::fanspeed_t IRTcl112Ac::toCommonFanSpeed(const uint8_t spd) {

/// Convert the current internal state into its stdAc::state_t equivalent.
/// @return The stdAc equivalent of the native settings.
stdAc::state_t IRTcl112Ac::toCommon(void) {
stdAc::state_t IRTcl112Ac::toCommon(void) const {
stdAc::state_t result;
result.protocol = decode_type_t::TCL112AC;
result.model = -1; // Not supported.
result.power = this->getPower();
result.mode = this->toCommonMode(this->getMode());
result.power = _.Power;
result.mode = toCommonMode(_.Mode);
result.celsius = true;
result.degrees = this->getTemp();
result.fanspeed = this->toCommonFanSpeed(this->getFan());
result.swingv = this->getSwingVertical() ? stdAc::swingv_t::kAuto :
result.degrees = getTemp();
result.fanspeed = toCommonFanSpeed(_.Fan);
result.swingv = _.SwingV ? stdAc::swingv_t::kAuto :
stdAc::swingv_t::kOff;
result.swingh = this->getSwingHorizontal() ? stdAc::swingh_t::kAuto :
result.swingh = _.SwingH ? stdAc::swingh_t::kAuto :
stdAc::swingh_t::kOff;
result.turbo = this->getTurbo();
result.light = this->getLight();
result.filter = this->getHealth();
result.econo = this->getEcono();
result.turbo = _.Turbo;
result.light = getLight();
result.filter = _.Health;
result.econo = _.Econo;
// Not supported.
result.quiet = false;
result.clean = false;
Expand All @@ -360,24 +355,24 @@ stdAc::state_t IRTcl112Ac::toCommon(void) {

/// Convert the current internal state into a human readable string.
/// @return A human readable string.
String IRTcl112Ac::toString(void) {
String IRTcl112Ac::toString(void) const {
String result = "";
result.reserve(140); // Reserve some heap for the string to reduce fragging.
result += addBoolToString(getPower(), kPowerStr, false);
result += addModeToString(getMode(), kTcl112AcAuto, kTcl112AcCool,
result += addBoolToString(_.Power, kPowerStr, false);
result += addModeToString(_.Mode, kTcl112AcAuto, kTcl112AcCool,
kTcl112AcHeat, kTcl112AcDry, kTcl112AcFan);
uint16_t nrHalfDegrees = this->getTemp() * 2;
uint16_t nrHalfDegrees = getTemp() * 2;
result += addIntToString(nrHalfDegrees / 2, kTempStr);
if (nrHalfDegrees & 1) result += F(".5");
result += 'C';
result += addFanToString(getFan(), kTcl112AcFanHigh, kTcl112AcFanLow,
result += addFanToString(_.Fan, kTcl112AcFanHigh, kTcl112AcFanLow,
kTcl112AcFanAuto, kTcl112AcFanAuto, kTcl112AcFanMed);
result += addBoolToString(getEcono(), kEconoStr);
result += addBoolToString(getHealth(), kHealthStr);
result += addBoolToString(_.Econo, kEconoStr);
result += addBoolToString(_.Health, kHealthStr);
result += addBoolToString(getLight(), kLightStr);
result += addBoolToString(getTurbo(), kTurboStr);
result += addBoolToString(getSwingHorizontal(), kSwingHStr);
result += addBoolToString(getSwingVertical(), kSwingVStr);
result += addBoolToString(_.Turbo, kTurboStr);
result += addBoolToString(_.SwingH, kSwingHStr);
result += addBoolToString(_.SwingV, kSwingVStr);
return result;
}

Expand Down
Loading

0 comments on commit 8cf7004

Please sign in to comment.