Use helper function for bitshift

This commit is contained in:
thetestspecimen 2024-10-07 23:43:54 +03:00
parent b7043f3fcd
commit 93c2ec2bbb
No known key found for this signature in database
GPG Key ID: C88001C355A25170

View File

@ -39,14 +39,14 @@ void Sen0501Component::setup() {
} else {
uint8_t buf[2];
this->read_bytes(REG_PID, buf, 2);
uint16_t product_id = buf[0] << 8 | buf[1];
uint16_t product_id = this->encode_uint16(buf[0], buf[1]);
if ((product_id != DEVICE_PID_GRAVITY) && (product_id != DEVICE_PID_BREAKOUT)) {
this->error_code_ = WRONG_DEVICE_ID;
this->mark_failed();
return;
}
this->read_bytes(REG_VID, buf, 2);
uint16_t vendor_id = buf[0] << 8 | buf[1];
uint16_t vendor_id = this->encode_uint16(buf[0], buf[1]);
if (vendor_id != DEVICE_VID) {
this->error_code_ = WRONG_VENDOR_ID;
this->mark_failed();
@ -97,10 +97,9 @@ void Sen0501Component::read_temperature_() {
if (this->temperature_ == nullptr)
return;
uint8_t buffer[2];
uint16_t data;
float temp;
this->read_bytes(REG_TEMP, buffer, 2);
data = buffer[0] << 8 | buffer[1];
uint16_t data = this->encode_uint16(buffer[0], buffer[1]);
temp = 175.0f * float(data) / 65536.0f - 45.0f;
this->temperature_->publish_state(temp);
}
@ -109,10 +108,9 @@ void Sen0501Component::read_humidity_() {
if (this->humidity_ == nullptr)
return;
uint8_t buffer[2];
uint16_t data;
float humidity;
this->read_bytes(REG_HUMIDITY, buffer, 2);
data = buffer[0] << 8 | buffer[1];
uint16_t data = this->encode_uint16(buffer[0], buffer[1]);
humidity = (float) data * 100 / 65536;
this->humidity_->publish_state(humidity);
}
@ -121,18 +119,17 @@ void Sen0501Component::read_uv_intensity_() {
if (this->uv_intensity_ == nullptr)
return;
uint8_t buffer[2];
uint16_t uv_level;
uint16_t version = 0;
float ultra_violet;
this->read_bytes(REG_VERSION, buffer, 2);
version = buffer[0] << 8 | buffer[1];
version = this->encode_uint16(buffer[0], buffer[1]);
if (version == 0x1001) {
this->read_bytes(REG_ULTRAVIOLET_INTENSITY, buffer, 2);
uv_level = buffer[0] << 8 | buffer[1];
uint16_t uv_level = this->encode_uint16(buffer[0], buffer[1]);
ultra_violet = (float) uv_level / 1800.0;
} else {
this->read_bytes(REG_ULTRAVIOLET_INTENSITY, buffer, 2);
uv_level = buffer[0] << 8 | buffer[1];
uint16_t uv_level = this->encode_uint16(buffer[0], buffer[1]);
float output_voltage = 3.0 * uv_level / 1024;
if (output_voltage <= 0.99) {
output_voltage = 0.99;
@ -148,9 +145,8 @@ void Sen0501Component::read_luminous_intensity_() {
if (this->luminous_intensity_ == nullptr)
return;
uint8_t buffer[2];
uint16_t data;
this->read_bytes(REG_LUMINOUS_INTENSITY, buffer, 2);
data = buffer[0] << 8 | buffer[1];
uint16_t data = this->encode_uint16(buffer[0], buffer[1]);
float luminous = data;
luminous = luminous * (1.0023f + luminous * (8.1488e-5f + luminous * (-9.3924e-9f + luminous * 6.0135e-13f)));
this->luminous_intensity_->publish_state(luminous);
@ -160,9 +156,8 @@ void Sen0501Component::read_atmospheric_pressure_() {
if (this->atmospheric_pressure_ == nullptr)
return;
uint8_t buffer[2];
uint16_t atmosphere;
this->read_bytes(REG_ATMOSPHERIC_PRESSURE, buffer, 2);
atmosphere = buffer[0] << 8 | buffer[1];
uint16_t atmosphere = this->encode_uint16(buffer[0], buffer[1]);
this->atmospheric_pressure_->publish_state(atmosphere);
if (this->elevation_ == nullptr)
return;