Skip to content

Commit

Permalink
Unit tests for Coolix protocol. (#224)
Browse files Browse the repository at this point in the history
- Unit tests
- Change the way decoding is done so that it supports 64 bit sizes.
- Update status of the decoder from ALPHA to BETA.
  • Loading branch information
crankyoldgit authored May 28, 2017
1 parent c85c2b4 commit 57e2ef2
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 20 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ script:
- test/ir_Panasonic_test
- test/ir_Dish_test
- test/ir_Whynter_test
- test/ir_Coolix_test
- test/ir_Aiwa_test
- test/ir_Denon_test
- test/ir_Sanyo_test
Expand Down
42 changes: 23 additions & 19 deletions src/ir_Coolix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void IRsend::sendCOOLIX(uint64_t data, uint16_t nbits, uint16_t repeat) {
// Returns:
// boolean: True if it can decode it, false if it can't.
//
// Status: ALPHA / untested.
// Status: BETA / Probably working.
bool IRrecv::decodeCOOLIX(decode_results *results, uint16_t nbits,
bool strict) {
// The protocol sends the data normal + inverted, alternating on
Expand All @@ -95,9 +95,10 @@ bool IRrecv::decodeCOOLIX(decode_results *results, uint16_t nbits,
return false;

uint64_t data = 0;
uint64_t inverted = 0;
uint16_t offset = OFFSET_START;

if (nbits * 2 > sizeof(data) * 8)
if (nbits > sizeof(data) * 8)
return false; // We can't possibly capture a Coolix packet that big.

// Header
Expand All @@ -109,37 +110,40 @@ bool IRrecv::decodeCOOLIX(decode_results *results, uint16_t nbits,
// Data
// Twice as many bits as there are normal plus inverted bits.
for (uint16_t i = 0; i < nbits * 2; i++, offset++) {
bool flip = (i / 8) % 2;
if (!matchMark(results->rawbuf[offset++], COOLIX_BIT_MARK))
return false;
if (matchSpace(results->rawbuf[offset], COOLIX_ONE_SPACE))
data = (data << 1) | 1; // 1
else if (matchSpace(results->rawbuf[offset], COOLIX_ZERO_SPACE))
data <<= 1; // 0
else
if (matchSpace(results->rawbuf[offset], COOLIX_ONE_SPACE)) { // 1
if (flip)
inverted = (inverted << 1) | 1;
else
data = (data << 1) | 1;
} else if (matchSpace(results->rawbuf[offset], COOLIX_ZERO_SPACE)) { // 0
if (flip)
inverted <<= 1;
else
data <<= 1;
} else {
return false;
}
}

// Footer
if (!matchMark(results->rawbuf[offset], COOLIX_BIT_MARK))
return false;

// Data should now be (MSB to LSB) byte1,!byte1,byte2,!byte2,byte3,!byte3
// Decode, and verify if needed.
uint64_t result = 0; // Build a new result for we destroy the existing data.
for (uint16_t i = 0; i < nbits; i += 8) {
uint8_t inverted = (data & 0xFF) ^ 0xFF; // Un-invert the byte.
data >>= 8;
uint8_t normal = data & 0xFF;
data >>= 8;
if (strict && data != inverted) // Compliance
return false; // The message doesn't verify.
result |= (normal << i); // Add the byte in front of the previous result.
// Compliance
uint64_t orig = data; // Save a copy of the data.
if (strict) {
for (uint16_t i = 0; i < nbits; i += 8, data >>= 8, inverted >>= 8)
if ((data & 0xFF) != ((inverted & 0xFF) ^ 0xFF))
return false;
}

// Success
results->decode_type = COOLIX;
results->bits = nbits;
results->value = result;
results->value = orig;
results->address = 0;
results->command = 0;
return true;
Expand Down
11 changes: 10 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TESTS = IRutils_test IRsend_test ir_NEC_test ir_GlobalCache_test \
ir_Sherwood_test ir_Sony_test ir_Samsung_test ir_Kelvinator_test \
ir_JVC_test ir_RCMM_test ir_LG_test ir_Mitsubishi_test ir_Sharp_test \
ir_RC5_RC6_test ir_Panasonic_test ir_Dish_test ir_Whynter_test \
ir_Aiwa_test ir_Denon_test ir_Sanyo_test ir_Daikin_test
ir_Aiwa_test ir_Denon_test ir_Sanyo_test ir_Daikin_test ir_Coolix_test

# All Google Test headers. Usually you shouldn't change this
# definition.
Expand Down Expand Up @@ -230,6 +230,15 @@ ir_Whynter_test.o : ir_Whynter_test.cpp $(USER_DIR)/IRremoteESP8266.h $(USER_DIR
ir_Whynter_test : IRrecv.o IRsend.o IRtimer.o IRutils.o ir_GlobalCache.o ir_Whynter_test.o ir_Whynter.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

ir_Coolix.o : $(USER_DIR)/IRremoteESP8266.h $(USER_DIR)/IRsend.h $(USER_DIR)/IRrecv.h $(USER_DIR)/ir_Coolix.cpp $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/ir_Coolix.cpp

ir_Coolix_test.o : ir_Coolix_test.cpp $(USER_DIR)/IRremoteESP8266.h $(USER_DIR)/IRsend.h $(USER_DIR)/IRrecv.h IRsend_test.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -I$(USER_DIR) -c ir_Coolix_test.cpp

ir_Coolix_test : IRrecv.o IRsend.o IRtimer.o IRutils.o ir_GlobalCache.o ir_Coolix_test.o ir_Coolix.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

ir_Aiwa.o : $(USER_DIR)/IRremoteESP8266.h $(USER_DIR)/IRsend.h $(USER_DIR)/IRrecv.h $(USER_DIR)/ir_Aiwa.cpp $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/ir_Aiwa.cpp

Expand Down
Loading

0 comments on commit 57e2ef2

Please sign in to comment.