extract decoder for easy testing

This commit is contained in:
Nicu Hodos 2022-10-27 17:22:35 +02:00
parent 32d9183b5e
commit f9a34ff31a
4 changed files with 78 additions and 41 deletions

View File

@ -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;
}
}
};
};

38
gateway/include/decoder.h Normal file
View File

@ -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;
}
}
};

View File

@ -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

View File

@ -0,0 +1,36 @@
#include <unity.h>
#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;
}