Skip to content

Commit

Permalink
Modify existing a/c code to use setBits()/GETBITS().
Browse files Browse the repository at this point in the history
* Improve certain setting values in a couple of protocols after using 
narrower bit patterns.
* Lots of code/style formatting issues improved for code readablity.
* Reduce some duplicate code instances.
* Minor tweaks here and there.

* Except for some value changes, all unit tests passed and remain 
(largely) unmodified.
  • Loading branch information
crankyoldgit committed Sep 30, 2019
1 parent 336b427 commit 86867c7
Show file tree
Hide file tree
Showing 53 changed files with 2,025 additions and 2,612 deletions.
2 changes: 1 addition & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ kElectraAcMessageGap LITERAL1
kElectraAcMinRepeat LITERAL1
kElectraAcMinTemp LITERAL1
kElectraAcModeMask LITERAL1
kElectraAcOffsetTemp LITERAL1
kElectraAcTempDelta LITERAL1
kElectraAcOneSpace LITERAL1
kElectraAcPowerMask LITERAL1
kElectraAcStateLength LITERAL1
Expand Down
58 changes: 27 additions & 31 deletions src/ir_Amcor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using irutils::addBoolToString;
using irutils::addModeToString;
using irutils::addFanToString;
using irutils::addTempToString;
using irutils::setBits;

#if SEND_AMCOR
// Send a Amcor HVAC formatted message.
Expand Down Expand Up @@ -144,49 +145,45 @@ void IRAmcorAc::on(void) { setPower(true); }
void IRAmcorAc::off(void) { setPower(false); }

void IRAmcorAc::setPower(const bool on) {
remote_state[kAmcorPowerByte] &= ~kAmcorPowerMask;
remote_state[kAmcorPowerByte] |= (on ? kAmcorPowerOn : kAmcorPowerOff);
setBits(&remote_state[kAmcorPowerByte], kAmcorPowerOffset, kAmcorPowerSize,
on ? kAmcorPowerOn : kAmcorPowerOff);
}

bool IRAmcorAc::getPower(void) {
return ((remote_state[kAmcorPowerByte] & kAmcorPowerMask) == kAmcorPowerOn);
return GETBITS8(remote_state[kAmcorPowerByte], kAmcorPowerOffset,
kAmcorPowerSize) == kAmcorPowerOn;
}

// Set the temp in deg C
void IRAmcorAc::setTemp(const uint8_t degrees) {
uint8_t temp = std::max(kAmcorMinTemp, degrees);
temp = std::min(kAmcorMaxTemp, temp);

temp <<= 1;
remote_state[kAmcorTempByte] &= ~kAmcorTempMask;
remote_state[kAmcorTempByte] |= temp;
setBits(&remote_state[kAmcorTempByte], kAmcorTempOffset, kAmcorTempSize,
temp);
}

uint8_t IRAmcorAc::getTemp(void) {
return (remote_state[kAmcorTempByte] & kAmcorTempMask) >> 1;
return GETBITS8(remote_state[kAmcorTempByte], kAmcorTempOffset,
kAmcorTempSize);
}

// Maximum Cooling or Hearing
void IRAmcorAc::setMax(const bool on) {
if (on) {
switch (getMode()) {
case kAmcorCool:
setTemp(kAmcorMinTemp);
break;
case kAmcorHeat:
setTemp(kAmcorMaxTemp);
break;
default: // Not allowed in all other operating modes.
return;
case kAmcorCool: setTemp(kAmcorMinTemp); break;
case kAmcorHeat: setTemp(kAmcorMaxTemp); break;
// Not allowed in all other operating modes.
default: return;
}
remote_state[kAmcorSpecialByte] |= kAmcorMaxMask;
} else {
remote_state[kAmcorSpecialByte] &= ~kAmcorMaxMask;
}
setBits(&remote_state[kAmcorSpecialByte], kAmcorMaxOffset, kAmcorMaxSize,
on ? kAmcorMax : 0);
}

bool IRAmcorAc::getMax(void) {
return ((remote_state[kAmcorSpecialByte] & kAmcorMaxMask) == kAmcorMaxMask);
return GETBITS8(remote_state[kAmcorSpecialByte], kAmcorMaxOffset,
kAmcorMaxSize) == kAmcorMax;
}

// Set the speed of the fan
Expand All @@ -196,36 +193,35 @@ void IRAmcorAc::setFan(const uint8_t speed) {
case kAmcorFanMin:
case kAmcorFanMed:
case kAmcorFanMax:
remote_state[kAmcorModeFanByte] &= ~kAmcorFanMask;
remote_state[kAmcorModeFanByte] |= speed << 4;
setBits(&remote_state[kAmcorModeFanByte], kAmcorFanOffset, kAmcorFanSize,
speed);
break;
default:
setFan(kAmcorFanAuto);
}
}

uint8_t IRAmcorAc::getFan(void) {
return (remote_state[kAmcorModeFanByte] & kAmcorFanMask) >> 4;
return GETBITS8(remote_state[kAmcorModeFanByte], kAmcorFanOffset,
kAmcorFanSize);
}

uint8_t IRAmcorAc::getMode(void) {
return remote_state[kAmcorModeFanByte] & kAmcorModeMask;
return GETBITS8(remote_state[kAmcorModeFanByte], kAmcorModeOffset,
kAmcorModeSize);
}

void IRAmcorAc::setMode(const uint8_t mode) {
remote_state[kAmcorSpecialByte] &= ~kAmcorVentMask; // Clear the vent setting
switch (mode) {
case kAmcorFan:
remote_state[kAmcorSpecialByte] |= kAmcorVentMask; // Set the vent option
// FALL-THRU
case kAmcorCool:
case kAmcorHeat:
case kAmcorDry:
case kAmcorAuto:
// Mask out bits
remote_state[kAmcorModeFanByte] &= ~kAmcorModeMask;
// Set the mode at bit positions
remote_state[kAmcorModeFanByte] |= mode;
setBits(&remote_state[kAmcorSpecialByte], kAmcorVentOffset,
kAmcorVentSize, (mode == kAmcorFan) ? kAmcorVentOn : 0);
setBits(&remote_state[kAmcorModeFanByte], kAmcorModeOffset,
kAmcorModeSize, mode);
return;
default:
this->setMode(kAmcorAuto);
Expand Down
26 changes: 17 additions & 9 deletions src/ir_Amcor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,44 @@ const uint8_t kAmcorFanMin = 0b001;
const uint8_t kAmcorFanMed = 0b010;
const uint8_t kAmcorFanMax = 0b011;
const uint8_t kAmcorFanAuto = 0b100;
const uint8_t kAmcorFanMask = 0b01110000;
const uint8_t kAmcorFanOffset = 4;
const uint8_t kAmcorFanSize = 3;
// Modes
const uint8_t kAmcorCool = 0b001;
const uint8_t kAmcorHeat = 0b010;
const uint8_t kAmcorFan = 0b011; // Aka "Vent"
const uint8_t kAmcorDry = 0b100;
const uint8_t kAmcorAuto = 0b101;
const uint8_t kAmcorModeMask = 0b00000111;
const uint8_t kAmcorModeOffset = 0;
const uint8_t kAmcorModeSize = 3;

// state[2]
const uint8_t kAmcorTempByte = 2;
// Temperature
const uint8_t kAmcorMinTemp = 12; // Celsius
const uint8_t kAmcorMaxTemp = 32; // Celsius
const uint8_t kAmcorTempMask = 0b01111110;
const uint8_t kAmcorTempOffset = 1;
const uint8_t kAmcorTempSize = 6; // Bits

// state[5]
// Power
const uint8_t kAmcorPowerByte = 5;
const uint8_t kAmcorPowerMask = 0b11110000;
const uint8_t kAmcorPowerOn = 0b00110000; // 0x30
const uint8_t kAmcorPowerOff = 0b11000000; // 0xC0
const uint8_t kAmcorPowerOffset = 4;
const uint8_t kAmcorPowerSize = 4;
const uint8_t kAmcorPowerOn = 0b0011; // 0x3
const uint8_t kAmcorPowerOff = 0b1100; // 0xC

// state[6]
const uint8_t kAmcorSpecialByte = 6;
// Max Mode (aka "Lo" in Cool and "Hi" in Heat)
const uint8_t kAmcorMaxMask = 0b00000011; // 0x03
// "Vent" Mode
const uint8_t kAmcorVentMask = 0b11000000; // 0xC0
const uint8_t kAmcorMax = 0b11;
const uint8_t kAmcorMaxOffset = 0;
const uint8_t kAmcorMaxSize = 2;

// "Vent" Mode
const uint8_t kAmcorVentOn = 0b11;
const uint8_t kAmcorVentOffset = 6;
const uint8_t kAmcorVentSize = 2;
// state[7]
// Checksum byte.
const uint8_t kAmcorChecksumByte = kAmcorStateLength - 1;
Expand Down
57 changes: 24 additions & 33 deletions src/ir_Argo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using irutils::addLabeledString;
using irutils::addModeToString;
using irutils::addTempToString;
using irutils::setBit;
using irutils::setBits;

#if SEND_ARGO
// Send an Argo A/C message.
Expand Down Expand Up @@ -129,36 +130,31 @@ bool IRArgoAC::getMax(void) { return GETBIT8(argo[9], kArgoMaxBitOffset); }
// Sending 0 equals +4
void IRArgoAC::setTemp(const uint8_t degrees) {
uint8_t temp = std::max(kArgoMinTemp, degrees);
temp = std::min(kArgoMaxTemp, temp);

// offset 4 degrees. "If I want 12 degrees, I need to send 8"
temp -= kArgoTempOffset;
// delta 4 degrees. "If I want 12 degrees, I need to send 8"
temp = std::min(kArgoMaxTemp, temp) - kArgoTempDelta;
// Settemp = Bit 6,7 of byte 2, and bit 0-2 of byte 3
// mask out bits
// argo[13] & 0x00000100; // mask out ON/OFF Bit
argo[2] &= ~kArgoTempLowMask;
argo[3] &= ~kArgoTempHighMask;

// append to bit 6,7
argo[2] += temp << 6;
// remove lowest to bits and append in 0-2
argo[3] += ((temp >> 2) & kArgoTempHighMask);
setBits(&argo[2], kArgoTempLowOffset, kArgoTempLowSize, temp);
setBits(&argo[3], kArgoTempHighOffset, kArgoTempHighSize,
temp >> kArgoTempLowSize);
}

uint8_t IRArgoAC::getTemp(void) {
return (((argo[3] & kArgoTempHighMask) << 2 ) | (argo[2] >> 6)) +
kArgoTempOffset;
return ((GETBITS8(argo[3], kArgoTempHighOffset,
kArgoTempHighSize) << kArgoTempLowSize) |
GETBITS8(argo[2], kArgoTempLowOffset, kArgoTempLowSize)) +
kArgoTempDelta;
}

// Set the speed of the fan
void IRArgoAC::setFan(const uint8_t fan) {
// Mask out bits
argo[3] &= ~kArgoFanMask;
// Set fan mode at bit positions
argo[3] |= (std::min(fan, kArgoFan3) << 3);
setBits(&argo[3], kArgoFanOffset, kArgoFanSize, std::min(fan, kArgoFan3));
}

uint8_t IRArgoAC::getFan(void) { return (argo[3] & kArgoFanMask) >> 3; }
uint8_t IRArgoAC::getFan(void) {
return GETBITS8(argo[3], kArgoFanOffset, kArgoFanSize);
}

void IRArgoAC::setFlap(const uint8_t flap) {
flap_mode = flap;
Expand All @@ -168,7 +164,7 @@ void IRArgoAC::setFlap(const uint8_t flap) {
uint8_t IRArgoAC::getFlap(void) { return flap_mode; }

uint8_t IRArgoAC::getMode(void) {
return (argo[2] & kArgoModeMask) >> 3;
return GETBITS8(argo[2], kArgoModeOffset, kArgoModeSize);
}

void IRArgoAC::setMode(const uint8_t mode) {
Expand All @@ -179,10 +175,7 @@ void IRArgoAC::setMode(const uint8_t mode) {
case kArgoOff:
case kArgoHeat:
case kArgoHeatAuto:
// Mask out bits
argo[2] &= ~kArgoModeMask;
// Set the mode at bit positions
argo[2] |= ((mode << 3) & kArgoModeMask);
setBits(&argo[2], kArgoModeOffset, kArgoModeSize, mode);
return;
default:
this->setMode(kArgoAuto);
Expand All @@ -207,19 +200,17 @@ void IRArgoAC::setTime(void) {

void IRArgoAC::setRoomTemp(const uint8_t degrees) {
uint8_t temp = std::min(degrees, kArgoMaxRoomTemp);
temp = std::max(temp, kArgoTempOffset);
temp -= kArgoTempOffset;;
// Mask out bits
argo[3] &= ~kArgoRoomTempLowMask;
argo[4] &= ~kArgoRoomTempHighMask;

argo[3] += temp << 5; // Append to bit 5,6,7
argo[4] += temp >> 3; // Remove lowest 3 bits and append in 0,1
temp = std::max(temp, kArgoTempDelta) - kArgoTempDelta;
setBits(&argo[3], kArgoRoomTempLowOffset, kArgoRoomTempLowSize, temp);
setBits(&argo[4], kArgoRoomTempHighOffset, kArgoRoomTempHighSize,
temp >> kArgoRoomTempLowSize);
}

uint8_t IRArgoAC::getRoomTemp(void) {
return ((argo[4] & kArgoRoomTempHighMask) << 3 | (argo[3] >> 5)) +
kArgoTempOffset;
return ((GETBITS8(argo[4], kArgoRoomTempHighOffset,
kArgoRoomTempHighSize) << kArgoRoomTempLowSize) |
GETBITS8(argo[3], kArgoRoomTempLowOffset, kArgoRoomTempLowSize)) +
kArgoTempDelta;
}

// Convert a standard A/C mode into its native mode.
Expand Down
72 changes: 44 additions & 28 deletions src/ir_Argo.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,64 @@

// byte[2]
const uint8_t kArgoHeatBit = 0b00100000;
const uint8_t kArgoModeMask = 0b00111000;
const uint8_t kArgoTempLowMask = 0b11000000;
const uint8_t kArgoCool = 0b000; // 0b000 (LSB) 0
const uint8_t kArgoDry = 0b001; // 0b100 (LSB) 1
const uint8_t kArgoAuto = 0b010; // 0b010 (LSB) 2
const uint8_t kArgoOff = 0b011; // 0b110 (LSB) 3
const uint8_t kArgoHeat = 0b100; // 0b001 (LSB) 4
const uint8_t kArgoHeatAuto = 0b101; // 0b101 (LSB) 5
// kArgoTempLowMask = 0b11000000;
const uint8_t kArgoTempLowOffset = 5;
const uint8_t kArgoTempLowSize = 2;

// Mode 0b00111000
const uint8_t kArgoModeOffset = 3;
const uint8_t kArgoModeSize = 3;
const uint8_t kArgoCool = 0b000;
const uint8_t kArgoDry = 0b001;
const uint8_t kArgoAuto = 0b010;
const uint8_t kArgoOff = 0b011;
const uint8_t kArgoHeat = 0b100;
const uint8_t kArgoHeatAuto = 0b101;
// ?no idea what mode that is
const uint8_t kArgoHeatBlink = 0b110; // 0b011 (LSB) 6
const uint8_t kArgoHeatBlink = 0b110;

// byte[3]
const uint8_t kArgoTempHighMask = 0b00000111;
const uint8_t kArgoFanMask = 0b00011000;
const uint8_t kArgoRoomTempLowMask = 0b11100000;
const uint8_t kArgoFanAuto = 0; // 0b00
const uint8_t kArgoFan3 = 3; // 0b11
const uint8_t kArgoFan2 = 2; // 0b01
const uint8_t kArgoFan1 = 1; // 0b10
// kArgoTempHighMask = 0b00000111;
const uint8_t kArgoTempHighOffset = 0;
const uint8_t kArgoTempHighSize = 3;
// Fan 0b00011000
const uint8_t kArgoFanOffset = 3;
const uint8_t kArgoFanSize = 2;
const uint8_t kArgoFanAuto = 0; // 0b00
const uint8_t kArgoFan1 = 1; // 0b01
const uint8_t kArgoFan2 = 2; // 0b10
const uint8_t kArgoFan3 = 3; // 0b11
// kArgoRoomTempLowMask = 0b11100000;
const uint8_t kArgoRoomTempLowOffset = 5;
const uint8_t kArgoRoomTempLowSize = 3;

// byte[4]
const uint8_t kArgoRoomTempHighMask = 0b00000011;
const uint8_t kArgoTempOffset = 4;
const uint8_t kArgoMaxRoomTemp = ((1 << 5) - 1) + kArgoTempOffset; // 35C
// kArgoRoomTempHighMask = 0b00000011;
const uint8_t kArgoRoomTempHighOffset = 0;
const uint8_t kArgoRoomTempHighSize = 2;

const uint8_t kArgoTempDelta = 4;
const uint8_t kArgoMaxRoomTemp =
((1 << (kArgoRoomTempHighSize + kArgoRoomTempLowSize)) - 1) +
kArgoTempDelta; // 35C

// byte[9]
const uint8_t kArgoNightBitOffset = 2;
const uint8_t kArgoMaxBitOffset = 3;
const uint8_t kArgoPowerBitOffset = 5;
const uint8_t kArgoIFeelBitOffset = 7;

const uint8_t kArgoMinTemp = 10; // Celsius offset +4
const uint8_t kArgoMinTemp = 10; // Celsius delta +4
const uint8_t kArgoMaxTemp = 32; // Celsius

const uint8_t kArgoFlapAuto = 0; // 0b000
const uint8_t kArgoFlap1 = 1; // 0b100
const uint8_t kArgoFlap2 = 2; // 0b010
const uint8_t kArgoFlap3 = 3; // 0b110
const uint8_t kArgoFlap4 = 4; // 0b001
const uint8_t kArgoFlap5 = 5; // 0b101
const uint8_t kArgoFlap6 = 6; // 0b011
const uint8_t kArgoFlapFull = 7; // 0b111
const uint8_t kArgoFlapAuto = 0;
const uint8_t kArgoFlap1 = 1;
const uint8_t kArgoFlap2 = 2;
const uint8_t kArgoFlap3 = 3;
const uint8_t kArgoFlap4 = 4;
const uint8_t kArgoFlap5 = 5;
const uint8_t kArgoFlap6 = 6;
const uint8_t kArgoFlapFull = 7;

// Legacy defines. (Deperecated)
#define ARGO_COOL_ON kArgoCoolOn
Expand Down
Loading

0 comments on commit 86867c7

Please sign in to comment.