use standard fixed length types
This commit is contained in:
parent
4f9c91c108
commit
55213b9e87
@ -24,7 +24,7 @@ TYPE| VALUE | VCC | ID
|
||||
```
|
||||
##### Sensors types
|
||||
```C++
|
||||
enum SensorType : unsigned short {
|
||||
enum SensorType : uint8_t {
|
||||
GENERIC = 4,
|
||||
HUMIDITY = 5,
|
||||
TEMPERATURE = 6,
|
||||
@ -33,7 +33,7 @@ enum SensorType : unsigned short {
|
||||
```
|
||||
##### Sensors IDs
|
||||
```C++
|
||||
enum SensorId : unsigned short {
|
||||
enum SensorId : uint8_t {
|
||||
WINDOW1 = 1,
|
||||
WINDOW2 = 2,
|
||||
WATER_SENSOR = 3,
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#define READ_INTERVAL(c) (c*60*1000UL) // read interval in minutes
|
||||
|
||||
DHT dht = DHT(DHT11_PIN, DHT11);
|
||||
unsigned long currentTime = 0;
|
||||
uint32_t currentTime = 0;
|
||||
|
||||
namespace Dht {
|
||||
void setup() {
|
||||
@ -17,7 +17,7 @@ namespace Dht {
|
||||
|
||||
void read() {
|
||||
currentTime = millis();
|
||||
static unsigned long lastReadTime = 0;
|
||||
static uint32_t lastReadTime = 0;
|
||||
if (currentTime > lastReadTime) {
|
||||
lastReadTime = currentTime + READ_INTERVAL(5);
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <RCSwitch.h>
|
||||
|
||||
enum ProtocolNo : unsigned int {
|
||||
enum ProtocolNo : uint8_t {
|
||||
NO_PROTOCOL = 0,
|
||||
PROTOCOL_1 = 1,
|
||||
PROTOCOL_2 = 2,
|
||||
@ -16,7 +16,7 @@ protected:
|
||||
public:
|
||||
explicit Protocol(ProtocolNo protocol) : no(protocol) {}
|
||||
|
||||
Protocol& setProtocol(unsigned int p) {
|
||||
Protocol& setProtocol(uint8_t p) {
|
||||
no = static_cast<ProtocolNo>(p);
|
||||
return *this;
|
||||
}
|
||||
@ -27,7 +27,7 @@ public:
|
||||
rcDevice.send(rcSwitch["value"]);
|
||||
}
|
||||
|
||||
virtual void toJson(unsigned long value, JsonDocument& jsonDoc) {
|
||||
virtual void toJson(uint32_t value, JsonDocument& jsonDoc) {
|
||||
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
||||
rcSwitch["protocol"] = no;
|
||||
rcSwitch["value"] = value;
|
||||
|
||||
@ -15,7 +15,7 @@ public:
|
||||
rcSwitch["state"] ? rcDevice.switchOn(group, channel) : rcDevice.switchOff(group, channel);
|
||||
}
|
||||
|
||||
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
|
||||
void toJson(uint32_t value, JsonDocument& jsonDoc) override {
|
||||
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
||||
rcSwitch["protocol"] = no;
|
||||
RcDecoder decoder;
|
||||
@ -27,9 +27,9 @@ public:
|
||||
}
|
||||
|
||||
#if defined(ESP8266)
|
||||
static std::string buildId(const char* group, const unsigned char channel) {
|
||||
static std::string buildId(const char* group, const uint8_t channel) {
|
||||
char uId[30];
|
||||
sprintf(uId, "%s_%d", group, channel);
|
||||
sprintf(uId, "%s_%u", group, channel);
|
||||
return std::string{ uId };
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -7,7 +7,7 @@ class Protocol_2 : public Protocol {
|
||||
public:
|
||||
Protocol_2() : Protocol(PROTOCOL_2) {}
|
||||
|
||||
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
|
||||
void toJson(uint32_t value, JsonDocument& jsonDoc) override {
|
||||
switch (value) {
|
||||
case 637541753L:
|
||||
case 771759481L: {
|
||||
|
||||
@ -5,18 +5,18 @@
|
||||
struct RcDecoder {
|
||||
bool state;
|
||||
char group[6];
|
||||
unsigned char device;
|
||||
uint8_t device;
|
||||
|
||||
void decode(unsigned long value) {
|
||||
void decode(uint32_t value) {
|
||||
value = value >> 2;
|
||||
unsigned long res = 0;
|
||||
uint32_t res = 0;
|
||||
for (int i = 0; i < 12; i++) {
|
||||
res |= ((value & 1) ^ 1) << i;
|
||||
value = value >> 2;
|
||||
}
|
||||
|
||||
state = RC_STATE(res);
|
||||
sprintf(group, "%05lu", RC_GROUP(res));
|
||||
sprintf(group, "%05u", RC_GROUP(res));
|
||||
switch (RC_DEVICE(res)) {
|
||||
case 0b10000:
|
||||
device = 1;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include "Tiny.h"
|
||||
|
||||
bool buildSensorJson(unsigned long value, JsonDocument& jsonDoc) {
|
||||
bool buildSensorJson(uint32_t value, JsonDocument& jsonDoc) {
|
||||
JsonObject sensor = jsonDoc.createNestedObject("sensor");
|
||||
sensor["id"] = ID(value);
|
||||
|
||||
|
||||
@ -71,8 +71,8 @@ Command* commands[] = {
|
||||
)
|
||||
.build(),
|
||||
#endif
|
||||
new EasyHomeSwitch{'A', (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, "KabelBox", "Basement"},
|
||||
new EasyHomeSwitch{'B', (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }},
|
||||
new EasyHomeSwitch{'A', (uint32_t[4]) { 4483136, 4626800, 4661552, 4819632 }, (uint32_t[4]) { 4326544, 4537104, 4767520, 4972704 }, "KabelBox", "Basement"},
|
||||
new EasyHomeSwitch{'B', (uint32_t[4]) { 4483140, 4626804, 4661556, 4819636 }, (uint32_t[4]) { 4326548, 4537108, 4767524, 4972708 }},
|
||||
new PollinSwitch{"00001", 1},
|
||||
new PollinSwitch{"00001", 2, "Fire Tv", "Living room"},
|
||||
new PollinSwitch{"00001", 3, "Diningroom player", "Dining room"},
|
||||
|
||||
@ -75,7 +75,7 @@ namespace Board {
|
||||
|
||||
void parseSwitches(JsonDocument& jsonDoc) {
|
||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||
switch (static_cast<unsigned int>(rcSwitch["protocol"])) {
|
||||
switch (static_cast<uint16_t>(rcSwitch["protocol"])) {
|
||||
case 1: {
|
||||
string id = Protocol_1::buildId(static_cast<const char*>(rcSwitch["group"]), static_cast<int>(rcSwitch["channel"]));
|
||||
Ha::Switch* el = p1Switches[id];
|
||||
@ -85,7 +85,7 @@ namespace Board {
|
||||
case 2:
|
||||
break;
|
||||
default: {
|
||||
unsigned long value = rcSwitch["value"];
|
||||
uint32_t value = rcSwitch["value"];
|
||||
auto range = onSwitches.equal_range(value);
|
||||
for_each(range.first, range.second, [](mapswitches::value_type& x){
|
||||
x.second->updateState(true);
|
||||
@ -100,7 +100,7 @@ namespace Board {
|
||||
|
||||
void parseSensors(JsonDocument& jsonDoc, char* message) {
|
||||
JsonObjectConst json = jsonDoc["sensor"];
|
||||
string id = to_string((unsigned int)json["id"]);
|
||||
string id = to_string((uint16_t)json["id"]);
|
||||
auto sensor = GenericSensor::mapSensors[id];
|
||||
if (sensor) sensor->updateState(message);
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
|
||||
using namespace Ha;
|
||||
|
||||
typedef unordered_multimap<unsigned long, Ha::Switch*> mapswitches;
|
||||
typedef unordered_multimap<uint32_t, Ha::Switch*> mapswitches;
|
||||
|
||||
mapswitches onSwitches;
|
||||
mapswitches offSwitches;
|
||||
@ -19,9 +19,9 @@ auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway"
|
||||
struct PollinSwitch : Switch {
|
||||
constexpr static const char* man = "Pollin";
|
||||
const char* group;
|
||||
unsigned char channel;
|
||||
uint8_t channel;
|
||||
|
||||
PollinSwitch(const char* group, const unsigned char channel, const char* name = nullptr, const char* area = nullptr)
|
||||
PollinSwitch(const char* group, const uint8_t channel, const char* name = nullptr, const char* area = nullptr)
|
||||
: Switch(nullptr, [group, channel]{
|
||||
// copy id from string into a new pointer, to avoid memory leaks
|
||||
return (new string{Protocol_1::buildId(group, channel)})->c_str();
|
||||
@ -41,17 +41,17 @@ struct PollinSwitch : Switch {
|
||||
};
|
||||
|
||||
struct EasyHomeSwitch : Switch {
|
||||
unsigned long on[8] = { 4326554, 4537114, 4767530, 4972714 };
|
||||
unsigned long off[8] = { 4483146, 4626810, 4661562, 4819642 };
|
||||
uint32_t on[8] = { 4326554, 4537114, 4767530, 4972714 };
|
||||
uint32_t off[8] = { 4483146, 4626810, 4661562, 4819642 };
|
||||
|
||||
EasyHomeSwitch(const char remotePosition, unsigned long on[4], unsigned long off[4], const char* name = nullptr, const char* area = nullptr)
|
||||
EasyHomeSwitch(const char remotePosition, uint32_t on[4], uint32_t off[4], const char* name = nullptr, const char* area = nullptr)
|
||||
: Switch(nullptr, [remotePosition] {
|
||||
auto uId = new string("easy_home_");
|
||||
(*uId) += tolower(remotePosition);
|
||||
return uId->c_str();
|
||||
}()) {
|
||||
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
|
||||
memcpy(&this->off[4], off, 4 * sizeof(unsigned long));
|
||||
memcpy(&this->on[4], on, 4 * sizeof(uint32_t));
|
||||
memcpy(&this->off[4], off, 4 * sizeof(uint32_t));
|
||||
if (!name) {
|
||||
auto n = new string("Easy Home ");
|
||||
(*n) += remotePosition;
|
||||
|
||||
@ -6,22 +6,22 @@
|
||||
#define MASK_STATE 0x1
|
||||
#define MASK_TYPE 0xF
|
||||
|
||||
#define ID(value) ((unsigned long)value & MASK_ID)
|
||||
#define VCC(value) (((unsigned long)value & MASK_VCC) << 5)
|
||||
#define TEMP(value) (((unsigned long)value & MASK_VALUE) << 18)
|
||||
#define HUMIDITY(value) (((unsigned long)value & MASK_VALUE) << 18)
|
||||
#define VALUE(value) (((unsigned long)value & MASK_VALUE) << 18)
|
||||
#define STATE(value) (((unsigned long)value & MASK_STATE) << 27)
|
||||
#define TYPE(value) (((unsigned long)value & MASK_TYPE) << 28)
|
||||
#define ID(value) ((uint32_t)value & MASK_ID)
|
||||
#define VCC(value) (((uint32_t)value & MASK_VCC) << 5)
|
||||
#define TEMP(value) (((uint32_t)value & MASK_VALUE) << 18)
|
||||
#define HUMIDITY(value) (((uint32_t)value & MASK_VALUE) << 18)
|
||||
#define VALUE(value) (((uint32_t)value & MASK_VALUE) << 18)
|
||||
#define STATE(value) (((uint32_t)value & MASK_STATE) << 27)
|
||||
#define TYPE(value) (((uint32_t)value & MASK_TYPE) << 28)
|
||||
|
||||
#define GET_TYPE(value) (((unsigned long)value >> 28) & MASK_TYPE)
|
||||
#define GET_STATE(value) (((unsigned long)value >> 27) & MASK_STATE)
|
||||
#define GET_TEMP(value) (((unsigned long)value >> 18) & MASK_VALUE)
|
||||
#define GET_HUMIDITY(value) (((unsigned long)value >> 18) & MASK_VALUE)
|
||||
#define GET_VALUE(value) (((unsigned long)value >> 18) & MASK_VALUE)
|
||||
#define GET_VCC(value) (((unsigned long)value >> 5) & MASK_VCC)
|
||||
#define GET_TYPE(value) (((uint32_t)value >> 28) & MASK_TYPE)
|
||||
#define GET_STATE(value) (((uint32_t)value >> 27) & MASK_STATE)
|
||||
#define GET_TEMP(value) (((uint32_t)value >> 18) & MASK_VALUE)
|
||||
#define GET_HUMIDITY(value) (((uint32_t)value >> 18) & MASK_VALUE)
|
||||
#define GET_VALUE(value) (((uint32_t)value >> 18) & MASK_VALUE)
|
||||
#define GET_VCC(value) (((uint32_t)value >> 5) & MASK_VCC)
|
||||
|
||||
enum SensorType : unsigned short {
|
||||
enum SensorType : uint8_t {
|
||||
GENERIC = 4,
|
||||
HUMIDITY = 5,
|
||||
TEMPERATURE = 6,
|
||||
@ -35,7 +35,7 @@ class SensorId {
|
||||
public:
|
||||
explicit SensorId(uint8_t id) {
|
||||
value = id;
|
||||
snprintf(strValue, 4, "%d", value);
|
||||
snprintf(strValue, 4, "%u", value);
|
||||
}
|
||||
|
||||
operator uint8_t() {
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
namespace TinySwitch {
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
void sendInfo(unsigned long value) {
|
||||
void sendInfo(uint32_t value) {
|
||||
mySwitch.send(value, 32);
|
||||
}
|
||||
|
||||
|
||||
@ -31,7 +31,7 @@ void setup() {
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
Protocol* findProtocol(unsigned int protocol) {
|
||||
Protocol* findProtocol(uint16_t protocol) {
|
||||
switch (protocol) {
|
||||
case PROTOCOL_1:
|
||||
return &protocol1;
|
||||
@ -54,7 +54,7 @@ void readRcSwitch() {
|
||||
mySwitch.resetAvailable();
|
||||
}
|
||||
#else
|
||||
unsigned long value = mySwitch.getReceivedValue();
|
||||
uint32_t value = mySwitch.getReceivedValue();
|
||||
mySwitch.resetAvailable();
|
||||
|
||||
StaticJsonDocument<128> jsonDoc;
|
||||
|
||||
@ -11,13 +11,13 @@ void tearDown(void) {
|
||||
|
||||
void test_unknown_sensor_type(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = TYPE(0);
|
||||
uint32_t value = TYPE(0);
|
||||
TEST_ASSERT_FALSE(buildSensorJson(value, jsonDoc));
|
||||
}
|
||||
|
||||
void test_max_temp(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = TEMP(1023) | TYPE(SensorType::TEMPERATURE);
|
||||
uint32_t value = TEMP(1023) | TYPE(SensorType::TEMPERATURE);
|
||||
TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject sensor = jsonDoc["sensor"];
|
||||
@ -26,7 +26,7 @@ void test_max_temp(void) {
|
||||
|
||||
void test_max_value(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = VALUE(1023) | TYPE(SensorType::GENERIC);
|
||||
uint32_t value = VALUE(1023) | TYPE(SensorType::GENERIC);
|
||||
TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject sensor = jsonDoc["sensor"];
|
||||
@ -35,7 +35,7 @@ void test_max_value(void) {
|
||||
|
||||
void test_overflow_value(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = VALUE(1024) | TYPE(SensorType::GENERIC);
|
||||
uint32_t value = VALUE(1024) | TYPE(SensorType::GENERIC);
|
||||
TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject sensor = jsonDoc["sensor"];
|
||||
@ -44,7 +44,7 @@ void test_overflow_value(void) {
|
||||
|
||||
void test_max_voltage(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = VCC(8191) | TYPE(SensorType::GENERIC);
|
||||
uint32_t value = VCC(8191) | TYPE(SensorType::GENERIC);
|
||||
TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject diagnostic = jsonDoc["sensor"]["diagnostic"];
|
||||
@ -53,7 +53,7 @@ void test_max_voltage(void) {
|
||||
|
||||
void test_overflow_voltage(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = VCC(8192) | TYPE(SensorType::GENERIC);
|
||||
uint32_t value = VCC(8192) | TYPE(SensorType::GENERIC);
|
||||
TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject diagnostic = jsonDoc["sensor"]["diagnostic"];
|
||||
@ -62,7 +62,7 @@ void test_overflow_voltage(void) {
|
||||
|
||||
void test_temp_sensor(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = ID(TEMP_SENSOR) | TEMP(210) | TYPE(SensorType::TEMPERATURE);
|
||||
uint32_t value = ID(TEMP_SENSOR) | TEMP(210) | TYPE(SensorType::TEMPERATURE);
|
||||
TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject sensor = jsonDoc["sensor"];
|
||||
@ -72,7 +72,7 @@ void test_temp_sensor(void) {
|
||||
|
||||
void test_temp_sensor_with_voltage(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = ID(TEMP_SENSOR) | TEMP(320) | TYPE(SensorType::TEMPERATURE) | VCC(2847L);
|
||||
uint32_t value = ID(TEMP_SENSOR) | TEMP(320) | TYPE(SensorType::TEMPERATURE) | VCC(2847L);
|
||||
TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject sensor = jsonDoc["sensor"];
|
||||
@ -85,7 +85,7 @@ void test_temp_sensor_with_voltage(void) {
|
||||
|
||||
void test_oil_sensor(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = ID(OIL_SENSOR) | VALUE(150) | TYPE(SensorType::GENERIC);
|
||||
uint32_t value = ID(OIL_SENSOR) | VALUE(150) | TYPE(SensorType::GENERIC);
|
||||
TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject sensor = jsonDoc["sensor"];
|
||||
@ -95,7 +95,7 @@ void test_oil_sensor(void) {
|
||||
|
||||
void test_oil_sensor_with_voltage(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = ID(OIL_SENSOR) | TEMP(200) | TYPE(SensorType::GENERIC) | VCC(2847L);
|
||||
uint32_t value = ID(OIL_SENSOR) | TEMP(200) | TYPE(SensorType::GENERIC) | VCC(2847L);
|
||||
TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject sensor = jsonDoc["sensor"];
|
||||
@ -108,7 +108,7 @@ void test_oil_sensor_with_voltage(void) {
|
||||
|
||||
void test_presence_sensor(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = ID(PRESENCE_SENSOR) | STATE(1) | TYPE(SensorType::CONTACT);
|
||||
uint32_t value = ID(PRESENCE_SENSOR) | STATE(1) | TYPE(SensorType::CONTACT);
|
||||
TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject sensor = jsonDoc["sensor"];
|
||||
@ -118,7 +118,7 @@ void test_presence_sensor(void) {
|
||||
|
||||
void test_presence_sensor_with_voltage(void) {
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
unsigned long value = ID(PRESENCE_SENSOR) | STATE(0) | TYPE(SensorType::CONTACT) | VCC(3847L);
|
||||
uint32_t value = ID(PRESENCE_SENSOR) | STATE(0) | TYPE(SensorType::CONTACT) | VCC(3847L);
|
||||
TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
|
||||
|
||||
JsonObject sensor = jsonDoc["sensor"];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user