added support for div_ratio 1000

This commit is contained in:
j0ta29 2023-08-26 18:48:37 +00:00
parent f0ba8873ca
commit 357eb19945
3 changed files with 37 additions and 1 deletions

View file

@ -44,7 +44,9 @@ SENSOR_BASE_SCHEMA = cv.Schema(
),
cv.Required(CONF_ADDRESS): cv.hex_uint32_t,
# cv.Required(CONF_BYTES): cv.one_of(1, 2, 4, int=True),
cv.Optional(CONF_DIV_RATIO, default=1): cv.one_of(1, 10, 100, 3600, int=True),
cv.Optional(CONF_DIV_RATIO, default=1): cv.one_of(
1, 10, 100, 1000, 3600, int=True
),
}
)

View file

@ -84,6 +84,17 @@ void OptolinkSensorBase::setup_datapoint() {
unfitting_value_type();
}
break;
case 1000:
switch (bytes_) {
case 4:
datapoint_ = new Datapoint<conv4_1000_F>(get_component_name().c_str(), "optolink", address_, writeable_);
datapoint_->setCallback([this](const IDatapoint &dp, DPValue dp_value) {
ESP_LOGI(TAG, "recieved data for datapoint %s: %f", dp.getName(), dp_value.getFloat());
value_changed(dp_value.getFloat());
});
break;
}
break;
case 3600:
switch (bytes_) {
case 4:
@ -94,6 +105,7 @@ void OptolinkSensorBase::setup_datapoint() {
});
break;
}
break;
default:
unfitting_value_type();
}
@ -209,6 +221,20 @@ DPValue conv2_100_F::decode(const uint8_t *in) {
return out;
}
void conv4_1000_F::encode(uint8_t *out, DPValue in) {
int32_t tmp = floor((in.getFloat() * 1000) + 0.5);
out[3] = tmp >> 24;
out[2] = tmp >> 16;
out[1] = tmp >> 8;
out[0] = tmp & 0xFF;
}
DPValue conv4_1000_F::decode(const uint8_t *in) {
int32_t tmp = in[3] << 24 | in[2] << 16 | in[1] << 8 | in[0];
DPValue out(tmp / 1000.0f);
return out;
}
} // namespace optolink
} // namespace esphome

View file

@ -58,6 +58,14 @@ class conv2_100_F : public DPType {
size_t get_length() const { return 2; }
};
// NOLINTNEXTLINE
class conv4_1000_F : public DPType {
public:
void encode(uint8_t *out, DPValue in);
DPValue decode(const uint8_t *in);
const size_t getLength() const { return 4; }
};
} // namespace optolink
} // namespace esphome