ESPHome
2026.3.0-dev
Loading...
Searching...
No Matches
esphome
components
sy6970
sensor
sy6970_sensor.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
../sy6970.h
"
4
#include "
esphome/components/sensor/sensor.h
"
5
6
namespace
esphome::sy6970
{
7
8
// Template for voltage sensors (converts mV to V)
9
template
<u
int
8_t REG, u
int
8_t MASK, u
int
16_t BASE, u
int
16_t STEP>
10
class
VoltageSensor
:
public
SY6970Listener
,
public
sensor::Sensor
{
11
public
:
12
void
on_data
(
const
SY6970Data
&data)
override
{
13
uint8_t
val
= data.
registers
[REG] & MASK;
14
uint16_t voltage_mv = BASE + (
val
* STEP);
15
this->
publish_state
(voltage_mv * 0.001f);
// Convert mV to V
16
}
17
};
18
19
// Template for current sensors (returns mA)
20
template
<u
int
8_t REG, u
int
8_t MASK, u
int
16_t BASE, u
int
16_t STEP>
21
class
CurrentSensor
:
public
SY6970Listener
,
public
sensor::Sensor
{
22
public
:
23
void
on_data
(
const
SY6970Data
&data)
override
{
24
uint8_t
val
= data.
registers
[REG] & MASK;
25
uint16_t current_ma = BASE + (
val
* STEP);
26
this->
publish_state
(current_ma);
27
}
28
};
29
30
// Specialized sensor types using templates
31
using
SY6970VbusVoltageSensor
=
VoltageSensor<SY6970_REG_VBUS_VOLTAGE, 0x7F, VBUS_BASE_MV, VBUS_STEP_MV>
;
32
using
SY6970BatteryVoltageSensor
=
VoltageSensor<SY6970_REG_BATV, 0x7F, VBAT_BASE_MV, VBAT_STEP_MV>
;
33
using
SY6970SystemVoltageSensor
=
VoltageSensor<SY6970_REG_VINDPM_STATUS, 0x7F, VSYS_BASE_MV, VSYS_STEP_MV>
;
34
using
SY6970ChargeCurrentSensor
=
CurrentSensor<SY6970_REG_CHARGE_CURRENT_MONITOR, 0x7F, 0, CHG_CURRENT_STEP_MA>
;
35
36
// Precharge current sensor needs special handling (bit shift)
37
class
SY6970PrechargeCurrentSensor
:
public
SY6970Listener
,
public
sensor::Sensor
{
38
public
:
39
void
on_data
(
const
SY6970Data
&data)
override
{
40
uint8_t iprechg = (data.
registers
[SY6970_REG_PRECHARGE_CURRENT] >> 4) & 0x0F;
41
uint16_t iprechg_ma = PRE_CHG_BASE_MA + (iprechg * PRE_CHG_STEP_MA);
42
this->
publish_state
(iprechg_ma);
43
}
44
};
45
46
}
// namespace esphome::sy6970
esphome::sensor::Sensor
Base-class for all sensors.
Definition
sensor.h:43
esphome::sensor::Sensor::publish_state
void publish_state(float state)
Publish a new state to the front-end.
Definition
sensor.cpp:65
esphome::sy6970::CurrentSensor
Definition
sy6970_sensor.h:21
esphome::sy6970::CurrentSensor::on_data
void on_data(const SY6970Data &data) override
Definition
sy6970_sensor.h:23
esphome::sy6970::SY6970Listener
Definition
sy6970.h:71
esphome::sy6970::SY6970PrechargeCurrentSensor
Definition
sy6970_sensor.h:37
esphome::sy6970::SY6970PrechargeCurrentSensor::on_data
void on_data(const SY6970Data &data) override
Definition
sy6970_sensor.h:39
esphome::sy6970::VoltageSensor
Definition
sy6970_sensor.h:10
esphome::sy6970::VoltageSensor::on_data
void on_data(const SY6970Data &data) override
Definition
sy6970_sensor.h:12
val
mopeka_std_values val[4]
Definition
mopeka_std_check.h:8
esphome::sy6970
Definition
sy6970_binary_sensor.h:6
sensor.h
esphome::sy6970::SY6970Data
Definition
sy6970.h:66
esphome::sy6970::SY6970Data::registers
uint8_t registers[21]
Definition
sy6970.h:67
sy6970.h
Generated by
1.12.0