diff --git a/gateway/include/Protocol_1.h b/gateway/include/Protocol_1.h index ee77167..70d7817 100644 --- a/gateway/include/Protocol_1.h +++ b/gateway/include/Protocol_1.h @@ -1,9 +1,6 @@ #pragma once #include "Protocol.h" - -#define RC_STATE(value) value & 0x1 -#define RC_DEVICE(value) (value >> 1) & 0x1F -#define RC_GROUP(value) (value >> 6) & 0x1F +#include "Decoder.h" class Protocol_1 : public Protocol { @@ -29,41 +26,4 @@ public: rcSwitch["channel"] = decoder.device; rcSwitch["raw_value"] = value; } - -private: - struct Decoder { - bool state; - char group; - byte device; - - void decode(unsigned long value) { - value = value >> 2; - unsigned long res = 0; - for (int i = 0; i < 12; i++) { - res |= ((value & 1) ^ 1) << i; - value = value >> 2; - } - - state = RC_STATE(res); - group = RC_GROUP(res); - switch (RC_DEVICE(res)) { - case 0b10000: - device = 1; - break; - case 0b01000: - device = 2; - break; - case 0b00100: - device = 3; - break; - case 0b00010: - device = 4; - break; - case 0b00001: - device = 5; - break; - } - } - }; }; - diff --git a/gateway/include/decoder.h b/gateway/include/decoder.h new file mode 100644 index 0000000..37e9eac --- /dev/null +++ b/gateway/include/decoder.h @@ -0,0 +1,38 @@ +#define RC_STATE(value) value & 0x1 +#define RC_DEVICE(value) (value >> 1) & 0x1F +#define RC_GROUP(value) (value >> 6) & 0x1F + +struct Decoder { + bool state; + char group; + unsigned char device; + + void decode(unsigned long value) { + value = value >> 2; + unsigned long res = 0; + for (int i = 0; i < 12; i++) { + res |= ((value & 1) ^ 1) << i; + value = value >> 2; + } + + state = RC_STATE(res); + group = RC_GROUP(res); + switch (RC_DEVICE(res)) { + case 0b10000: + device = 1; + break; + case 0b01000: + device = 2; + break; + case 0b00100: + device = 3; + break; + case 0b00010: + device = 4; + break; + case 0b00001: + device = 5; + break; + } + } +}; diff --git a/gateway/platformio.ini b/gateway/platformio.ini index 5d8e537..2afa12c 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -21,3 +21,6 @@ lib_deps = adafruit/DHT sensor library@1.3.10 build_flags = -D DHT_SENSOR=0 upload_port = /dev/ttyUSB0 + +[env:native] +platform = native diff --git a/gateway/test/test_decoder.cpp b/gateway/test/test_decoder.cpp new file mode 100644 index 0000000..32ba2d7 --- /dev/null +++ b/gateway/test/test_decoder.cpp @@ -0,0 +1,36 @@ +#include +#include "Decoder.h" + + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + +void test_1_2_on(void) { + Decoder d; + d.decode(5574993); + TEST_ASSERT_EQUAL(true, d.state); + TEST_ASSERT_EQUAL(1, d.group); + TEST_ASSERT_EQUAL(2, d.device); +} + +void test_1_1_off(void) { + Decoder d; + d.decode(5571924); + TEST_ASSERT_EQUAL(false, d.state); + TEST_ASSERT_EQUAL(1, d.group); + TEST_ASSERT_EQUAL(1, d.device); +} + +int main(int argc, char **argv) { + UNITY_BEGIN(); + RUN_TEST(test_1_2_on); + RUN_TEST(test_1_1_off); + UNITY_END(); + + return 0; +}