use standard fixed length types

This commit is contained in:
Nicu Hodos 2025-10-08 17:14:02 +02:00
parent 4f9c91c108
commit 55213b9e87
14 changed files with 59 additions and 59 deletions

View File

@ -24,7 +24,7 @@ TYPE| VALUE | VCC | ID
``` ```
##### Sensors types ##### Sensors types
```C++ ```C++
enum SensorType : unsigned short { enum SensorType : uint8_t {
GENERIC = 4, GENERIC = 4,
HUMIDITY = 5, HUMIDITY = 5,
TEMPERATURE = 6, TEMPERATURE = 6,
@ -33,7 +33,7 @@ enum SensorType : unsigned short {
``` ```
##### Sensors IDs ##### Sensors IDs
```C++ ```C++
enum SensorId : unsigned short { enum SensorId : uint8_t {
WINDOW1 = 1, WINDOW1 = 1,
WINDOW2 = 2, WINDOW2 = 2,
WATER_SENSOR = 3, WATER_SENSOR = 3,

View File

@ -8,7 +8,7 @@
#define READ_INTERVAL(c) (c*60*1000UL) // read interval in minutes #define READ_INTERVAL(c) (c*60*1000UL) // read interval in minutes
DHT dht = DHT(DHT11_PIN, DHT11); DHT dht = DHT(DHT11_PIN, DHT11);
unsigned long currentTime = 0; uint32_t currentTime = 0;
namespace Dht { namespace Dht {
void setup() { void setup() {
@ -17,7 +17,7 @@ namespace Dht {
void read() { void read() {
currentTime = millis(); currentTime = millis();
static unsigned long lastReadTime = 0; static uint32_t lastReadTime = 0;
if (currentTime > lastReadTime) { if (currentTime > lastReadTime) {
lastReadTime = currentTime + READ_INTERVAL(5); lastReadTime = currentTime + READ_INTERVAL(5);
StaticJsonDocument<200> jsonDoc; StaticJsonDocument<200> jsonDoc;

View File

@ -2,7 +2,7 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <RCSwitch.h> #include <RCSwitch.h>
enum ProtocolNo : unsigned int { enum ProtocolNo : uint8_t {
NO_PROTOCOL = 0, NO_PROTOCOL = 0,
PROTOCOL_1 = 1, PROTOCOL_1 = 1,
PROTOCOL_2 = 2, PROTOCOL_2 = 2,
@ -16,7 +16,7 @@ protected:
public: public:
explicit Protocol(ProtocolNo protocol) : no(protocol) {} explicit Protocol(ProtocolNo protocol) : no(protocol) {}
Protocol& setProtocol(unsigned int p) { Protocol& setProtocol(uint8_t p) {
no = static_cast<ProtocolNo>(p); no = static_cast<ProtocolNo>(p);
return *this; return *this;
} }
@ -27,7 +27,7 @@ public:
rcDevice.send(rcSwitch["value"]); 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"); JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
rcSwitch["protocol"] = no; rcSwitch["protocol"] = no;
rcSwitch["value"] = value; rcSwitch["value"] = value;

View File

@ -15,7 +15,7 @@ public:
rcSwitch["state"] ? rcDevice.switchOn(group, channel) : rcDevice.switchOff(group, channel); 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"); JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
rcSwitch["protocol"] = no; rcSwitch["protocol"] = no;
RcDecoder decoder; RcDecoder decoder;
@ -27,9 +27,9 @@ public:
} }
#if defined(ESP8266) #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]; char uId[30];
sprintf(uId, "%s_%d", group, channel); sprintf(uId, "%s_%u", group, channel);
return std::string{ uId }; return std::string{ uId };
} }
#endif #endif

View File

@ -7,7 +7,7 @@ class Protocol_2 : public Protocol {
public: public:
Protocol_2() : Protocol(PROTOCOL_2) {} Protocol_2() : Protocol(PROTOCOL_2) {}
void toJson(unsigned long value, JsonDocument& jsonDoc) override { void toJson(uint32_t value, JsonDocument& jsonDoc) override {
switch (value) { switch (value) {
case 637541753L: case 637541753L:
case 771759481L: { case 771759481L: {

View File

@ -5,18 +5,18 @@
struct RcDecoder { struct RcDecoder {
bool state; bool state;
char group[6]; char group[6];
unsigned char device; uint8_t device;
void decode(unsigned long value) { void decode(uint32_t value) {
value = value >> 2; value = value >> 2;
unsigned long res = 0; uint32_t res = 0;
for (int i = 0; i < 12; i++) { for (int i = 0; i < 12; i++) {
res |= ((value & 1) ^ 1) << i; res |= ((value & 1) ^ 1) << i;
value = value >> 2; value = value >> 2;
} }
state = RC_STATE(res); state = RC_STATE(res);
sprintf(group, "%05lu", RC_GROUP(res)); sprintf(group, "%05u", RC_GROUP(res));
switch (RC_DEVICE(res)) { switch (RC_DEVICE(res)) {
case 0b10000: case 0b10000:
device = 1; device = 1;

View File

@ -1,7 +1,7 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "Tiny.h" #include "Tiny.h"
bool buildSensorJson(unsigned long value, JsonDocument& jsonDoc) { bool buildSensorJson(uint32_t value, JsonDocument& jsonDoc) {
JsonObject sensor = jsonDoc.createNestedObject("sensor"); JsonObject sensor = jsonDoc.createNestedObject("sensor");
sensor["id"] = ID(value); sensor["id"] = ID(value);

View File

@ -71,8 +71,8 @@ Command* commands[] = {
) )
.build(), .build(),
#endif #endif
new EasyHomeSwitch{'A', (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, "KabelBox", "Basement"}, new EasyHomeSwitch{'A', (uint32_t[4]) { 4483136, 4626800, 4661552, 4819632 }, (uint32_t[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{'B', (uint32_t[4]) { 4483140, 4626804, 4661556, 4819636 }, (uint32_t[4]) { 4326548, 4537108, 4767524, 4972708 }},
new PollinSwitch{"00001", 1}, new PollinSwitch{"00001", 1},
new PollinSwitch{"00001", 2, "Fire Tv", "Living room"}, new PollinSwitch{"00001", 2, "Fire Tv", "Living room"},
new PollinSwitch{"00001", 3, "Diningroom player", "Dining room"}, new PollinSwitch{"00001", 3, "Diningroom player", "Dining room"},

View File

@ -75,7 +75,7 @@ namespace Board {
void parseSwitches(JsonDocument& jsonDoc) { void parseSwitches(JsonDocument& jsonDoc) {
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"]; JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
switch (static_cast<unsigned int>(rcSwitch["protocol"])) { switch (static_cast<uint16_t>(rcSwitch["protocol"])) {
case 1: { case 1: {
string id = Protocol_1::buildId(static_cast<const char*>(rcSwitch["group"]), static_cast<int>(rcSwitch["channel"])); string id = Protocol_1::buildId(static_cast<const char*>(rcSwitch["group"]), static_cast<int>(rcSwitch["channel"]));
Ha::Switch* el = p1Switches[id]; Ha::Switch* el = p1Switches[id];
@ -85,7 +85,7 @@ namespace Board {
case 2: case 2:
break; break;
default: { default: {
unsigned long value = rcSwitch["value"]; uint32_t value = rcSwitch["value"];
auto range = onSwitches.equal_range(value); auto range = onSwitches.equal_range(value);
for_each(range.first, range.second, [](mapswitches::value_type& x){ for_each(range.first, range.second, [](mapswitches::value_type& x){
x.second->updateState(true); x.second->updateState(true);
@ -100,7 +100,7 @@ namespace Board {
void parseSensors(JsonDocument& jsonDoc, char* message) { void parseSensors(JsonDocument& jsonDoc, char* message) {
JsonObjectConst json = jsonDoc["sensor"]; 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]; auto sensor = GenericSensor::mapSensors[id];
if (sensor) sensor->updateState(message); if (sensor) sensor->updateState(message);
} }

View File

@ -8,7 +8,7 @@
using namespace Ha; using namespace Ha;
typedef unordered_multimap<unsigned long, Ha::Switch*> mapswitches; typedef unordered_multimap<uint32_t, Ha::Switch*> mapswitches;
mapswitches onSwitches; mapswitches onSwitches;
mapswitches offSwitches; mapswitches offSwitches;
@ -19,9 +19,9 @@ auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway"
struct PollinSwitch : Switch { struct PollinSwitch : Switch {
constexpr static const char* man = "Pollin"; constexpr static const char* man = "Pollin";
const char* group; 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]{ : Switch(nullptr, [group, channel]{
// copy id from string into a new pointer, to avoid memory leaks // copy id from string into a new pointer, to avoid memory leaks
return (new string{Protocol_1::buildId(group, channel)})->c_str(); return (new string{Protocol_1::buildId(group, channel)})->c_str();
@ -41,17 +41,17 @@ struct PollinSwitch : Switch {
}; };
struct EasyHomeSwitch : Switch { struct EasyHomeSwitch : Switch {
unsigned long on[8] = { 4326554, 4537114, 4767530, 4972714 }; uint32_t on[8] = { 4326554, 4537114, 4767530, 4972714 };
unsigned long off[8] = { 4483146, 4626810, 4661562, 4819642 }; 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] { : Switch(nullptr, [remotePosition] {
auto uId = new string("easy_home_"); auto uId = new string("easy_home_");
(*uId) += tolower(remotePosition); (*uId) += tolower(remotePosition);
return uId->c_str(); return uId->c_str();
}()) { }()) {
memcpy(&this->on[4], on, 4 * sizeof(unsigned long)); memcpy(&this->on[4], on, 4 * sizeof(uint32_t));
memcpy(&this->off[4], off, 4 * sizeof(unsigned long)); memcpy(&this->off[4], off, 4 * sizeof(uint32_t));
if (!name) { if (!name) {
auto n = new string("Easy Home "); auto n = new string("Easy Home ");
(*n) += remotePosition; (*n) += remotePosition;

View File

@ -6,22 +6,22 @@
#define MASK_STATE 0x1 #define MASK_STATE 0x1
#define MASK_TYPE 0xF #define MASK_TYPE 0xF
#define ID(value) ((unsigned long)value & MASK_ID) #define ID(value) ((uint32_t)value & MASK_ID)
#define VCC(value) (((unsigned long)value & MASK_VCC) << 5) #define VCC(value) (((uint32_t)value & MASK_VCC) << 5)
#define TEMP(value) (((unsigned long)value & MASK_VALUE) << 18) #define TEMP(value) (((uint32_t)value & MASK_VALUE) << 18)
#define HUMIDITY(value) (((unsigned long)value & MASK_VALUE) << 18) #define HUMIDITY(value) (((uint32_t)value & MASK_VALUE) << 18)
#define VALUE(value) (((unsigned long)value & MASK_VALUE) << 18) #define VALUE(value) (((uint32_t)value & MASK_VALUE) << 18)
#define STATE(value) (((unsigned long)value & MASK_STATE) << 27) #define STATE(value) (((uint32_t)value & MASK_STATE) << 27)
#define TYPE(value) (((unsigned long)value & MASK_TYPE) << 28) #define TYPE(value) (((uint32_t)value & MASK_TYPE) << 28)
#define GET_TYPE(value) (((unsigned long)value >> 28) & MASK_TYPE) #define GET_TYPE(value) (((uint32_t)value >> 28) & MASK_TYPE)
#define GET_STATE(value) (((unsigned long)value >> 27) & MASK_STATE) #define GET_STATE(value) (((uint32_t)value >> 27) & MASK_STATE)
#define GET_TEMP(value) (((unsigned long)value >> 18) & MASK_VALUE) #define GET_TEMP(value) (((uint32_t)value >> 18) & MASK_VALUE)
#define GET_HUMIDITY(value) (((unsigned long)value >> 18) & MASK_VALUE) #define GET_HUMIDITY(value) (((uint32_t)value >> 18) & MASK_VALUE)
#define GET_VALUE(value) (((unsigned long)value >> 18) & MASK_VALUE) #define GET_VALUE(value) (((uint32_t)value >> 18) & MASK_VALUE)
#define GET_VCC(value) (((unsigned long)value >> 5) & MASK_VCC) #define GET_VCC(value) (((uint32_t)value >> 5) & MASK_VCC)
enum SensorType : unsigned short { enum SensorType : uint8_t {
GENERIC = 4, GENERIC = 4,
HUMIDITY = 5, HUMIDITY = 5,
TEMPERATURE = 6, TEMPERATURE = 6,
@ -35,7 +35,7 @@ class SensorId {
public: public:
explicit SensorId(uint8_t id) { explicit SensorId(uint8_t id) {
value = id; value = id;
snprintf(strValue, 4, "%d", value); snprintf(strValue, 4, "%u", value);
} }
operator uint8_t() { operator uint8_t() {

View File

@ -5,7 +5,7 @@
namespace TinySwitch { namespace TinySwitch {
RCSwitch mySwitch = RCSwitch(); RCSwitch mySwitch = RCSwitch();
void sendInfo(unsigned long value) { void sendInfo(uint32_t value) {
mySwitch.send(value, 32); mySwitch.send(value, 32);
} }

View File

@ -31,7 +31,7 @@ void setup() {
delay(1000); delay(1000);
} }
Protocol* findProtocol(unsigned int protocol) { Protocol* findProtocol(uint16_t protocol) {
switch (protocol) { switch (protocol) {
case PROTOCOL_1: case PROTOCOL_1:
return &protocol1; return &protocol1;
@ -54,7 +54,7 @@ void readRcSwitch() {
mySwitch.resetAvailable(); mySwitch.resetAvailable();
} }
#else #else
unsigned long value = mySwitch.getReceivedValue(); uint32_t value = mySwitch.getReceivedValue();
mySwitch.resetAvailable(); mySwitch.resetAvailable();
StaticJsonDocument<128> jsonDoc; StaticJsonDocument<128> jsonDoc;

View File

@ -11,13 +11,13 @@ void tearDown(void) {
void test_unknown_sensor_type(void) { void test_unknown_sensor_type(void) {
StaticJsonDocument<200> jsonDoc; StaticJsonDocument<200> jsonDoc;
unsigned long value = TYPE(0); uint32_t value = TYPE(0);
TEST_ASSERT_FALSE(buildSensorJson(value, jsonDoc)); TEST_ASSERT_FALSE(buildSensorJson(value, jsonDoc));
} }
void test_max_temp(void) { void test_max_temp(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
JsonObject sensor = jsonDoc["sensor"]; JsonObject sensor = jsonDoc["sensor"];
@ -26,7 +26,7 @@ void test_max_temp(void) {
void test_max_value(void) { void test_max_value(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
JsonObject sensor = jsonDoc["sensor"]; JsonObject sensor = jsonDoc["sensor"];
@ -35,7 +35,7 @@ void test_max_value(void) {
void test_overflow_value(void) { void test_overflow_value(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
JsonObject sensor = jsonDoc["sensor"]; JsonObject sensor = jsonDoc["sensor"];
@ -44,7 +44,7 @@ void test_overflow_value(void) {
void test_max_voltage(void) { void test_max_voltage(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
JsonObject diagnostic = jsonDoc["sensor"]["diagnostic"]; JsonObject diagnostic = jsonDoc["sensor"]["diagnostic"];
@ -53,7 +53,7 @@ void test_max_voltage(void) {
void test_overflow_voltage(void) { void test_overflow_voltage(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
JsonObject diagnostic = jsonDoc["sensor"]["diagnostic"]; JsonObject diagnostic = jsonDoc["sensor"]["diagnostic"];
@ -62,7 +62,7 @@ void test_overflow_voltage(void) {
void test_temp_sensor(void) { void test_temp_sensor(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
JsonObject sensor = jsonDoc["sensor"]; JsonObject sensor = jsonDoc["sensor"];
@ -72,7 +72,7 @@ void test_temp_sensor(void) {
void test_temp_sensor_with_voltage(void) { void test_temp_sensor_with_voltage(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
JsonObject sensor = jsonDoc["sensor"]; JsonObject sensor = jsonDoc["sensor"];
@ -85,7 +85,7 @@ void test_temp_sensor_with_voltage(void) {
void test_oil_sensor(void) { void test_oil_sensor(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
JsonObject sensor = jsonDoc["sensor"]; JsonObject sensor = jsonDoc["sensor"];
@ -95,7 +95,7 @@ void test_oil_sensor(void) {
void test_oil_sensor_with_voltage(void) { void test_oil_sensor_with_voltage(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
JsonObject sensor = jsonDoc["sensor"]; JsonObject sensor = jsonDoc["sensor"];
@ -108,7 +108,7 @@ void test_oil_sensor_with_voltage(void) {
void test_presence_sensor(void) { void test_presence_sensor(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
JsonObject sensor = jsonDoc["sensor"]; JsonObject sensor = jsonDoc["sensor"];
@ -118,7 +118,7 @@ void test_presence_sensor(void) {
void test_presence_sensor_with_voltage(void) { void test_presence_sensor_with_voltage(void) {
StaticJsonDocument<200> jsonDoc; 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)); TEST_ASSERT_TRUE(buildSensorJson(value, jsonDoc));
JsonObject sensor = jsonDoc["sensor"]; JsonObject sensor = jsonDoc["sensor"];