mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-09 01:09:51 +03:00
analogsensor float->double #789
This commit is contained in:
@@ -296,7 +296,7 @@ void AnalogSensor::loop() {
|
||||
}
|
||||
|
||||
// update analog information name and offset
|
||||
bool AnalogSensor::update(uint8_t gpio, const std::string & name, float offset, float factor, uint8_t uom, int8_t type) {
|
||||
bool AnalogSensor::update(uint8_t gpio, const std::string & name, double offset, double factor, uint8_t uom, int8_t type) {
|
||||
boolean found_sensor = false; // see if we can find the sensor in our customization list
|
||||
|
||||
EMSESP::webCustomizationService.update(
|
||||
@@ -375,7 +375,7 @@ void AnalogSensor::publish_sensor(const Sensor & sensor) const {
|
||||
snprintf(topic, sizeof(topic), "%s%s/%s", F_(analogsensor), "_data", sensor.name().c_str());
|
||||
}
|
||||
char payload[10];
|
||||
Mqtt::publish(topic, Helpers::render_value(payload, sensor.value(), 2)); // always publish as floats
|
||||
Mqtt::publish(topic, Helpers::render_value(payload, sensor.value(), 2)); // always publish as doubles
|
||||
}
|
||||
}
|
||||
|
||||
@@ -422,7 +422,7 @@ void AnalogSensor::publish_values(const bool force) {
|
||||
case AnalogType::PWM_0:
|
||||
case AnalogType::PWM_1:
|
||||
case AnalogType::PWM_2:
|
||||
dataSensor["value"] = serialized(Helpers::render_value(s, sensor.value(), 2)); // float
|
||||
dataSensor["value"] = serialized(Helpers::render_value(s, sensor.value(), 2)); // double
|
||||
break;
|
||||
default:
|
||||
dataSensor["value"] = (uint8_t)sensor.value(); // convert to char for 1 or 0
|
||||
@@ -581,7 +581,7 @@ bool AnalogSensor::command_info(const char * value, const int8_t id, JsonObject
|
||||
}
|
||||
|
||||
// this creates the sensor, initializing everything
|
||||
AnalogSensor::Sensor::Sensor(const uint8_t gpio, const std::string & name, const float offset, const float factor, const uint8_t uom, const int8_t type)
|
||||
AnalogSensor::Sensor::Sensor(const uint8_t gpio, const std::string & name, const double offset, const double factor, const uint8_t uom, const int8_t type)
|
||||
: gpio_(gpio)
|
||||
, name_(name)
|
||||
, offset_(offset)
|
||||
|
||||
@@ -35,10 +35,10 @@ class AnalogSensor {
|
||||
public:
|
||||
class Sensor {
|
||||
public:
|
||||
Sensor(const uint8_t gpio, const std::string & name, const float offset, const float factor, const uint8_t uom, const int8_t type);
|
||||
Sensor(const uint8_t gpio, const std::string & name, const double offset, const double factor, const uint8_t uom, const int8_t type);
|
||||
~Sensor() = default;
|
||||
|
||||
void set_offset(const float offset) {
|
||||
void set_offset(const double offset) {
|
||||
offset_ = offset;
|
||||
}
|
||||
|
||||
@@ -52,23 +52,23 @@ class AnalogSensor {
|
||||
return gpio_;
|
||||
}
|
||||
|
||||
float value() const {
|
||||
double value() const {
|
||||
return value_;
|
||||
}
|
||||
|
||||
void set_value(float value) {
|
||||
void set_value(const double value) {
|
||||
value_ = value;
|
||||
}
|
||||
|
||||
float factor() const {
|
||||
double factor() const {
|
||||
return factor_;
|
||||
}
|
||||
|
||||
void set_factor(float factor) {
|
||||
void set_factor(const double factor) {
|
||||
factor_ = factor;
|
||||
}
|
||||
|
||||
float offset() const {
|
||||
double offset() const {
|
||||
return offset_;
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ class AnalogSensor {
|
||||
return type_;
|
||||
}
|
||||
|
||||
void set_type(int8_t type) {
|
||||
void set_type(const int8_t type) {
|
||||
type_ = type;
|
||||
}
|
||||
|
||||
@@ -101,10 +101,10 @@ class AnalogSensor {
|
||||
private:
|
||||
uint8_t gpio_;
|
||||
std::string name_;
|
||||
float offset_;
|
||||
float factor_;
|
||||
double offset_;
|
||||
double factor_;
|
||||
uint8_t uom_;
|
||||
float value_; // float because of the factor is a float
|
||||
double value_; // double because of the factor is a double
|
||||
int8_t type_;
|
||||
};
|
||||
|
||||
@@ -157,7 +157,7 @@ class AnalogSensor {
|
||||
return sensors_.size();
|
||||
}
|
||||
|
||||
bool update(uint8_t gpio, const std::string & name, float offset, float factor, uint8_t uom, int8_t type);
|
||||
bool update(uint8_t gpio, const std::string & name, double offset, double factor, uint8_t uom, int8_t type);
|
||||
bool get_value_info(JsonObject & output, const char * cmd, const int8_t id) const;
|
||||
|
||||
#ifdef EMSESP_DEBUG
|
||||
|
||||
@@ -244,7 +244,7 @@ char * Helpers::render_value(char * result, uint8_t value, int8_t format, const
|
||||
|
||||
// float: convert float to char
|
||||
// format is the precision, 0 to 8
|
||||
char * Helpers::render_value(char * result, const float value, const int8_t format) {
|
||||
char * Helpers::render_value(char * result, const double value, const int8_t format) {
|
||||
if (format > 8) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace emsesp {
|
||||
|
||||
class Helpers {
|
||||
public:
|
||||
static char * render_value(char * result, const float value, const int8_t format); // format is the precision
|
||||
static char * render_value(char * result, const double value, const int8_t format); // format is the precision
|
||||
static char * render_value(char * result, const uint8_t value, const int8_t format, const uint8_t fahrenheit = 0);
|
||||
static char * render_value(char * result, const int8_t value, const int8_t format, const uint8_t fahrenheit = 0);
|
||||
static char * render_value(char * result, const uint16_t value, const int8_t format, const uint8_t fahrenheit = 0);
|
||||
|
||||
@@ -44,8 +44,8 @@ class AnalogCustomization {
|
||||
public:
|
||||
uint8_t gpio;
|
||||
std::string name;
|
||||
float offset;
|
||||
float factor;
|
||||
double offset;
|
||||
double factor;
|
||||
uint8_t uom; // 0 is none
|
||||
int8_t type; // -1 is for deletion
|
||||
|
||||
|
||||
@@ -284,8 +284,8 @@ void WebDataService::write_analog(AsyncWebServerRequest * request, JsonVariant &
|
||||
|
||||
uint8_t gpio = analog["gpio"]; // this is the unique key, the GPIO
|
||||
std::string name = analog["name"];
|
||||
float factor = analog["factor"];
|
||||
float offset = analog["offset"];
|
||||
double factor = analog["factor"];
|
||||
double offset = analog["offset"];
|
||||
uint8_t uom = analog["uom"];
|
||||
int8_t type = analog["type"];
|
||||
ok = EMSESP::analogsensor_.update(gpio, name, offset, factor, uom, type);
|
||||
|
||||
Reference in New Issue
Block a user