From 61122532adbf86d5faaa57192f3015b548cbeace Mon Sep 17 00:00:00 2001 From: Henrik Ekblad Date: Wed, 13 Aug 2014 09:39:45 +0200 Subject: [PATCH] Float values sent in binary format over the air Move base radio id to MyConfig.h --- libraries/MySensors/MyConfig.h | 2 ++ libraries/MySensors/MyMessage.cpp | 23 +++++++++++++++++------ libraries/MySensors/MyMessage.h | 10 +++++++--- libraries/MySensors/MySensor.h | 2 -- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/libraries/MySensors/MyConfig.h b/libraries/MySensors/MyConfig.h index f959650..9cb8cb6 100644 --- a/libraries/MySensors/MyConfig.h +++ b/libraries/MySensors/MyConfig.h @@ -8,6 +8,8 @@ #define RF24_DATARATE RF24_250KBPS //RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps #define RF24_PA_LEVEL RF24_PA_MAX //Sensor PA Level == RF24_PA_MIN=-18dBm, RF24_PA_LOW=-12dBm, RF24_PA_HIGH=-6dBM, and RF24_PA_MAX=0dBm #define RF24_PA_LEVEL_GW RF24_PA_LEVEL //Gateway PA Level, defaults to Sensor net PA Level. Tune here if using an amplified nRF2401+ in your gateway. +#define BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL) // This is also act as base value for sensor nodeId addresses. Change this (or channel) if you have more than one sensor network. + /*** * Enable/Disable debug logging diff --git a/libraries/MySensors/MyMessage.cpp b/libraries/MySensors/MyMessage.cpp index 3e4d17c..8c5492a 100644 --- a/libraries/MySensors/MyMessage.cpp +++ b/libraries/MySensors/MyMessage.cpp @@ -80,6 +80,9 @@ char* MyMessage::getString(char *buffer) const { case P_ULONG32: ultoa(ulValue, buffer, 10); break; + case P_FLOAT32: + dtostrf(fValue,2,fPrecision,buffer); + break; case P_CUSTOM: return getStream(buffer); break; @@ -105,8 +108,15 @@ bool MyMessage::getBool() const { return getInt(); } -double MyMessage::getDouble() const { - return strtod(data, NULL); +float MyMessage::getFloat() const { + switch(miGetPayloadType()) { + case P_FLOAT32: + return fValue; + case P_STRING: + return atof(data); + default: + return 0; + } } long MyMessage::getLong() const { @@ -195,10 +205,11 @@ MyMessage& MyMessage::set(uint8_t value) { } -MyMessage& MyMessage::set(double value, uint8_t decimals) { - dtostrf(value,2,decimals,data); - miSetLength(strlen(data)); - miSetPayloadType(P_STRING); +MyMessage& MyMessage::set(float value, uint8_t decimals) { + miSetLength(5); // 32 bit float + persi + miSetPayloadType(P_FLOAT32); + fValue=value; + fPrecision = decimals; return *this; } diff --git a/libraries/MySensors/MyMessage.h b/libraries/MySensors/MyMessage.h index 67ff9b5..b1d8c04 100644 --- a/libraries/MySensors/MyMessage.h +++ b/libraries/MySensors/MyMessage.h @@ -63,7 +63,7 @@ typedef enum { } stream; typedef enum { - P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM + P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM, P_FLOAT32 } payload; @@ -139,7 +139,7 @@ public: void* getCustom() const; uint8_t getByte() const; bool getBool() const; - double getDouble() const; + float getFloat() const; long getLong() const; unsigned long getULong() const; int getInt() const; @@ -157,7 +157,7 @@ public: MyMessage& set(void* payload, uint8_t length); MyMessage& set(const char* value); MyMessage& set(uint8_t value); - MyMessage& set(double value, uint8_t decimals); + MyMessage& set(float value, uint8_t decimals); MyMessage& set(unsigned long value); MyMessage& set(long value); MyMessage& set(unsigned int value); @@ -192,6 +192,10 @@ struct long lValue; unsigned int uiValue; int iValue; + struct { + float fValue; + uint8_t fPrecision; // Number of decimals when serializing + }; char data[MAX_PAYLOAD + 1]; } __attribute__((packed)); #ifdef __cplusplus diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index 7db57b6..45544ba 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -57,8 +57,6 @@ #define EEPROM_LOCAL_CONFIG_ADDRESS (EEPROM_FIRMWARE_CRC_ADDRESS+2) // First free address for sketch static configuration // This is the nodeId for sensor net gateway receiver sketch (where all sensors should send their data). -// This is also act as base value for sensor nodeId -#define BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL) #define GATEWAY_ADDRESS ((uint8_t)0) #define BROADCAST_ADDRESS ((uint8_t)0xFF) #define TO_ADDR(x) (BASE_RADIO_ID + x)