Skip to content

Commit e92a750

Browse files
authored
refactor ir_Trotec (#1412)
1 parent 4e6784f commit e92a750

File tree

2 files changed

+90
-78
lines changed

2 files changed

+90
-78
lines changed

src/ir_Trotec.cpp

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ using irutils::addIntToString;
3131
using irutils::addLabeledString;
3232
using irutils::addModeToString;
3333
using irutils::addTempToString;
34-
using irutils::setBit;
35-
using irutils::setBits;
3634

3735
#if SEND_TROTEC
3836
/// Send a Trotec message.
@@ -64,7 +62,7 @@ void IRsend::sendTrotec(const unsigned char data[], const uint16_t nbytes,
6462
/// @param[in] use_modulation Is frequency modulation to be used?
6563
IRTrotecESP::IRTrotecESP(const uint16_t pin, const bool inverted,
6664
const bool use_modulation)
67-
: _irsend(pin, inverted, use_modulation) { this->stateReset(); }
65+
: _irsend(pin, inverted, use_modulation) { stateReset(); }
6866

6967
/// Set up hardware to be able to send a message.
7068
void IRTrotecESP::begin(void) { _irsend.begin(); }
@@ -96,118 +94,114 @@ bool IRTrotecESP::validChecksum(const uint8_t state[], const uint16_t length) {
9694

9795
/// Calculate & set the checksum for the current internal state of the remote.
9896
void IRTrotecESP::checksum(void) {
99-
remote_state[kTrotecStateLength - 1] = sumBytes(remote_state + 2,
100-
kTrotecStateLength - 3);
97+
_.Sum = sumBytes(_.raw + 2, kTrotecStateLength - 3);
10198
}
10299

103100
/// Reset the state of the remote to a known good state/sequence.
104101
void IRTrotecESP::stateReset(void) {
105-
for (uint8_t i = 2; i < kTrotecStateLength; i++) remote_state[i] = 0x0;
102+
for (uint8_t i = 2; i < kTrotecStateLength; i++) _.raw[i] = 0x0;
106103

107-
remote_state[0] = kTrotecIntro1;
108-
remote_state[1] = kTrotecIntro2;
104+
_.Intro1 = kTrotecIntro1;
105+
_.Intro2 = kTrotecIntro2;
109106

110-
this->setPower(false);
111-
this->setTemp(kTrotecDefTemp);
112-
this->setSpeed(kTrotecFanMed);
113-
this->setMode(kTrotecAuto);
107+
_.Power = false;
108+
setTemp(kTrotecDefTemp);
109+
_.Fan = kTrotecFanMed;
110+
_.Mode = kTrotecAuto;
114111
}
115112

116113
/// Get a PTR to the internal state/code for this protocol.
117114
/// @return PTR to a code for this protocol based on the current internal state.
118115
uint8_t* IRTrotecESP::getRaw(void) {
119-
this->checksum();
120-
return remote_state;
116+
checksum();
117+
return _.raw;
121118
}
122119

123120
/// Set the internal state from a valid code for this protocol.
124121
/// @param[in] state A valid code for this protocol.
125122
void IRTrotecESP::setRaw(const uint8_t state[]) {
126-
memcpy(remote_state, state, kTrotecStateLength);
123+
memcpy(_.raw, state, kTrotecStateLength);
127124
}
128125

129126
/// Set the requested power state of the A/C to on.
130-
void IRTrotecESP::on(void) { this->setPower(true); }
127+
void IRTrotecESP::on(void) { setPower(true); }
131128

132129
/// Set the requested power state of the A/C to off.
133-
void IRTrotecESP::off(void) { this->setPower(false); }
130+
void IRTrotecESP::off(void) { setPower(false); }
134131

135132
/// Change the power setting.
136133
/// @param[in] on true, the setting is on. false, the setting is off.
137134
void IRTrotecESP::setPower(const bool on) {
138-
setBit(&remote_state[2], kTrotecPowerBitOffset, on);
135+
_.Power = on;
139136
}
140137

141138
/// Get the value of the current power setting.
142139
/// @return true, the setting is on. false, the setting is off.
143-
bool IRTrotecESP::getPower(void) {
144-
return GETBIT8(remote_state[2], kTrotecPowerBitOffset);
140+
bool IRTrotecESP::getPower(void) const {
141+
return _.Power;
145142
}
146143

147144
/// Set the speed of the fan.
148145
/// @param[in] fan The desired setting.
149146
void IRTrotecESP::setSpeed(const uint8_t fan) {
150147
uint8_t speed = std::min(fan, kTrotecFanHigh);
151-
setBits(&remote_state[2], kTrotecFanOffset, kTrotecFanSize, speed);
148+
_.Fan = speed;
152149
}
153150

154151
/// Get the current fan speed setting.
155152
/// @return The current fan speed/mode.
156-
uint8_t IRTrotecESP::getSpeed(void) {
157-
return GETBITS8(remote_state[2], kTrotecFanOffset, kTrotecFanSize);
153+
uint8_t IRTrotecESP::getSpeed(void) const {
154+
return _.Fan;
158155
}
159156

160157
/// Set the operating mode of the A/C.
161158
/// @param[in] mode The desired operating mode.
162159
void IRTrotecESP::setMode(const uint8_t mode) {
163-
setBits(&remote_state[2], kTrotecModeOffset, kTrotecModeSize,
164-
(mode > kTrotecFan) ? kTrotecAuto : mode);
160+
_.Mode = (mode > kTrotecFan) ? kTrotecAuto : mode;
165161
}
166162

167163
/// Get the operating mode setting of the A/C.
168164
/// @return The current operating mode setting.
169-
uint8_t IRTrotecESP::getMode(void) {
170-
return GETBITS8(remote_state[2], kTrotecModeOffset, kTrotecModeSize);
165+
uint8_t IRTrotecESP::getMode(void) const {
166+
return _.Mode;
171167
}
172168

173169
/// Set the temperature.
174170
/// @param[in] celsius The temperature in degrees celsius.
175171
void IRTrotecESP::setTemp(const uint8_t celsius) {
176172
uint8_t temp = std::max(celsius, kTrotecMinTemp);
177173
temp = std::min(temp, kTrotecMaxTemp);
178-
setBits(&remote_state[3], kTrotecTempOffset, kTrotecTempSize,
179-
temp - kTrotecMinTemp);
174+
_.Temp = temp - kTrotecMinTemp;
180175
}
181176

182177
/// Get the current temperature setting.
183178
/// @return The current setting for temp. in degrees celsius.
184-
uint8_t IRTrotecESP::getTemp(void) {
185-
return GETBITS8(remote_state[3], kTrotecTempOffset, kTrotecTempSize) +
186-
kTrotecMinTemp;
179+
uint8_t IRTrotecESP::getTemp(void) const {
180+
return _.Temp + kTrotecMinTemp;
187181
}
188182

189183
/// Set the Sleep setting of the A/C.
190184
/// @param[in] on true, the setting is on. false, the setting is off.
191185
void IRTrotecESP::setSleep(const bool on) {
192-
setBit(&remote_state[3], kTrotecSleepBitOffset, on);
186+
_.Sleep = on;
193187
}
194188

195189
/// Get the Sleep setting of the A/C.
196190
/// @return true, the setting is on. false, the setting is off.
197-
bool IRTrotecESP::getSleep(void) {
198-
return GETBIT8(remote_state[3], kTrotecSleepBitOffset);
191+
bool IRTrotecESP::getSleep(void) const {
192+
return _.Sleep;
199193
}
200194

201195
/// Set the timer time in nr. of Hours.
202196
/// @param[in] timer Nr. of Hours. Max is `kTrotecMaxTimer`
203197
void IRTrotecESP::setTimer(const uint8_t timer) {
204-
setBit(&remote_state[5], kTrotecTimerBitOffset, timer);
205-
remote_state[6] = (timer > kTrotecMaxTimer) ? kTrotecMaxTimer : timer;
198+
_.Timer = timer;
199+
_.Hours = (timer > kTrotecMaxTimer) ? kTrotecMaxTimer : timer;
206200
}
207201

208202
/// Get the timer time in nr. of Hours.
209203
/// @return Nr. of Hours.
210-
uint8_t IRTrotecESP::getTimer(void) { return remote_state[6]; }
204+
uint8_t IRTrotecESP::getTimer(void) const { return _.Hours; }
211205

212206
/// Convert a stdAc::opmode_t enum into its native mode.
213207
/// @param[in] mode The enum to be converted.
@@ -262,15 +256,15 @@ stdAc::fanspeed_t IRTrotecESP::toCommonFanSpeed(const uint8_t spd) {
262256

263257
/// Convert the current internal state into its stdAc::state_t equivalent.
264258
/// @return The stdAc equivalent of the native settings.
265-
stdAc::state_t IRTrotecESP::toCommon(void) {
259+
stdAc::state_t IRTrotecESP::toCommon(void) const {
266260
stdAc::state_t result;
267261
result.protocol = decode_type_t::TROTEC;
268-
result.power = this->getPower();
269-
result.mode = this->toCommonMode(this->getMode());
262+
result.power = _.Power;
263+
result.mode = toCommonMode(_.Mode);
270264
result.celsius = true;
271-
result.degrees = this->getTemp();
272-
result.fanspeed = this->toCommonFanSpeed(this->getSpeed());
273-
result.sleep = this->getSleep() ? 0 : -1;
265+
result.degrees = getTemp();
266+
result.fanspeed = toCommonFanSpeed(_.Fan);
267+
result.sleep = _.Sleep ? 0 : -1;
274268
// Not supported.
275269
result.model = -1; // Not supported.
276270
result.swingv = stdAc::swingv_t::kOff;
@@ -288,16 +282,16 @@ stdAc::state_t IRTrotecESP::toCommon(void) {
288282

289283
/// Convert the current internal state into a human readable string.
290284
/// @return A human readable string.
291-
String IRTrotecESP::toString(void) {
285+
String IRTrotecESP::toString(void) const {
292286
String result = "";
293287
result.reserve(100); // Reserve some heap for the string to reduce fragging.
294-
result += addBoolToString(getPower(), kPowerStr, false);
295-
result += addModeToString(getMode(), kTrotecAuto, kTrotecCool, kTrotecAuto,
288+
result += addBoolToString(_.Power, kPowerStr, false);
289+
result += addModeToString(_.Mode, kTrotecAuto, kTrotecCool, kTrotecAuto,
296290
kTrotecDry, kTrotecFan);
297291
result += addTempToString(getTemp());
298-
result += addFanToString(getSpeed(), kTrotecFanHigh, kTrotecFanLow,
292+
result += addFanToString(_.Fan, kTrotecFanHigh, kTrotecFanLow,
299293
kTrotecFanHigh, kTrotecFanHigh, kTrotecFanMed);
300-
result += addBoolToString(getSleep(), kSleepStr);
294+
result += addBoolToString(_.Sleep, kSleepStr);
301295
return result;
302296
}
303297

src/ir_Trotec.h

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,56 @@
2222
#include "IRsend_test.h"
2323
#endif
2424

25+
/// Native representation of a Trotec A/C message.
26+
union TrotecProtocol{
27+
uint8_t raw[kTrotecStateLength]; ///< Remote state in IR code form.
28+
struct {
29+
// Byte 0
30+
uint8_t Intro1:8; // fixed value
31+
// Byte 1
32+
uint8_t Intro2:8; // fixed value
33+
// Byte 2
34+
uint8_t Mode :2;
35+
uint8_t :1;
36+
uint8_t Power :1;
37+
uint8_t Fan :2;
38+
uint8_t :2;
39+
// Byte 3
40+
uint8_t Temp :4;
41+
uint8_t :3;
42+
uint8_t Sleep :1;
43+
// Byte 4
44+
uint8_t :8;
45+
// Byte 5
46+
uint8_t :6;
47+
uint8_t Timer :1;
48+
uint8_t :1;
49+
// Byte 6
50+
uint8_t Hours :8;
51+
// Byte 7
52+
uint8_t :8;
53+
// Byte 8
54+
uint8_t Sum :8;
55+
};
56+
};
57+
2558
// Constants
26-
// Byte 0
2759
const uint8_t kTrotecIntro1 = 0x12;
28-
29-
// Byte 1
3060
const uint8_t kTrotecIntro2 = 0x34;
3161

32-
// Byte 2
33-
const uint8_t kTrotecModeOffset = 0;
34-
const uint8_t kTrotecModeSize = 2; // Nr. of bits
3562
const uint8_t kTrotecAuto = 0;
3663
const uint8_t kTrotecCool = 1;
3764
const uint8_t kTrotecDry = 2;
3865
const uint8_t kTrotecFan = 3;
3966

40-
const uint8_t kTrotecPowerBitOffset = 3;
41-
42-
const uint8_t kTrotecFanOffset = 4;
43-
const uint8_t kTrotecFanSize = 2; // Nr. of bits
4467
const uint8_t kTrotecFanLow = 1;
4568
const uint8_t kTrotecFanMed = 2;
4669
const uint8_t kTrotecFanHigh = 3;
4770

48-
// Byte 3
49-
const uint8_t kTrotecTempOffset = 0;
50-
const uint8_t kTrotecTempSize = 4; // Nr. of bits
5171
const uint8_t kTrotecMinTemp = 18;
5272
const uint8_t kTrotecDefTemp = 25;
5373
const uint8_t kTrotecMaxTemp = 32;
54-
const uint8_t kTrotecSleepBitOffset = 7;
55-
56-
// Byte 5
57-
const uint8_t kTrotecTimerBitOffset = 6;
5874

59-
// Byte 6
6075
const uint8_t kTrotecMaxTimer = 23;
6176

6277
// Legacy defines. (Deprecated)
@@ -91,33 +106,36 @@ class IRTrotecESP {
91106
void on(void);
92107
void off(void);
93108
void setPower(const bool state);
94-
bool getPower(void);
109+
bool getPower(void) const;
95110

96111
void setTemp(const uint8_t celsius);
97-
uint8_t getTemp(void);
112+
uint8_t getTemp(void) const;
98113

99114
void setSpeed(const uint8_t fan);
100-
uint8_t getSpeed(void);
115+
uint8_t getSpeed(void) const;
116+
117+
void setFan(const uint8_t fan) { setSpeed(fan); }
118+
uint8_t getFan(void) const { return getSpeed(); }
101119

102-
uint8_t getMode(void);
120+
uint8_t getMode(void) const;
103121
void setMode(const uint8_t mode);
104122

105-
bool getSleep(void);
123+
bool getSleep(void) const;
106124
void setSleep(const bool on);
107125

108-
uint8_t getTimer(void);
126+
uint8_t getTimer(void) const;
109127
void setTimer(const uint8_t timer);
110128

111129
uint8_t* getRaw(void);
112130
void setRaw(const uint8_t state[]);
113131
static bool validChecksum(const uint8_t state[],
114132
const uint16_t length = kTrotecStateLength);
115-
uint8_t convertMode(const stdAc::opmode_t mode);
116-
uint8_t convertFan(const stdAc::fanspeed_t speed);
133+
static uint8_t convertMode(const stdAc::opmode_t mode);
134+
static uint8_t convertFan(const stdAc::fanspeed_t speed);
117135
static stdAc::opmode_t toCommonMode(const uint8_t mode);
118136
static stdAc::fanspeed_t toCommonFanSpeed(const uint8_t speed);
119-
stdAc::state_t toCommon(void);
120-
String toString(void);
137+
stdAc::state_t toCommon(void) const;
138+
String toString(void) const;
121139
#ifndef UNIT_TEST
122140

123141
private:
@@ -127,7 +145,7 @@ class IRTrotecESP {
127145
IRsendTest _irsend; ///< Instance of the testing IR send class
128146
/// @endcond
129147
#endif // UNIT_TEST
130-
uint8_t remote_state[kTrotecStateLength]; ///< Remote state in IR code form.
148+
TrotecProtocol _;
131149
static uint8_t calcChecksum(const uint8_t state[],
132150
const uint16_t length = kTrotecStateLength);
133151
void checksum(void);

0 commit comments

Comments
 (0)