OpenSourceSimWheelESP32
Open-source wireless steering wheel/button box for ESP32 boards
Loading...
Searching...
No Matches
BatteryMonitorHardware.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include "InternalTypes.hpp"
15#include "SimWheelTypes.hpp"
16
17//-------------------------------------------------------------------
18// Abstract hardware interface
19//-------------------------------------------------------------------
20
26{
27public:
35 virtual void getStatus(BatteryStatus &currentStatus);
36
41 virtual void onStart() {};
42
43protected:
52 virtual bool read_soc(uint8_t &soc) = 0;
53};
54
55//-------------------------------------------------------------------
56// Optional hardware witnesses
57//-------------------------------------------------------------------
58
64{
65public:
79 InputGPIO sensePin,
80 bool negativeLogic,
81 bool enableInternalPullResistor);
82
95 static void setChargingWitness(
96 InputGPIO sensePin,
97 bool negativeLogic,
98 bool enableInternalPullResistor);
99
105 static void update(BatteryStatus &status);
106
107private:
108 inline static InputGPIO _powerSensePin{};
109 inline static bool _powerSenseNegativeLogic = false;
110 inline static InputGPIO _chargingSensePin{};
111 inline static bool _chargingSenseNegativeLogic = false;
112};
113
114//-------------------------------------------------------------------
115// Testing
116//-------------------------------------------------------------------
117
123{
124public:
127
128public:
135 {
136 this->status = fakeStatus;
137 }
138
139 virtual void getStatus(BatteryStatus &currentStatus) override
140 {
141 if (status != nullptr)
142 currentStatus = *status;
143 }
144
145 virtual bool read_soc(uint8_t &soc) override { return false; }
146};
147
148//-------------------------------------------------------------------
149// MAX1704x hardware
150//-------------------------------------------------------------------
151
157{
158protected:
160 void *device = nullptr;
161
170 bool read(uint8_t regAddress, uint16_t &value);
171
180 bool write(uint8_t regAddress, uint16_t value);
181
189
190 virtual bool read_soc(uint8_t &currentSoC) override;
191
192public:
200 MAX1704x(I2CBus bus = I2CBus::PRIMARY, uint8_t i2c_address = 0xFF);
201
203 virtual ~MAX1704x();
204
205 virtual void onStart() override
206 {
207 quickStart();
208 }
209
210}; // class MAX1704x
211
212//-------------------------------------------------------------------
213// Voltage divider hardware
214//-------------------------------------------------------------------
215
221{
222protected:
229
230 virtual bool read_soc(uint8_t &currentSoC) override;
231
232public:
233 // NOTE: these public members are for testing. Do not tamper with them.
234
237
239 int read();
240
241public:
260 ADC_GPIO battREADPin,
261 OutputGPIO battENPin,
262 uint32_t resistorToGND = 200,
263 uint32_t resistorToBattery = 110);
264
271 static uint8_t readingToSoC(int reading);
272
273}; // class VoltageDividerMonitor
274
275//-------------------------------------------------------------------
Types and constants used everywhere for firmware implementation.
Types and constants required for custom firmware setup.
I2CBus
I2C bus controller.
Hardware witnesses regarding battery chargers.
static void setExternalPowerWitness(InputGPIO sensePin, bool negativeLogic, bool enableInternalPullResistor)
Set a GPIO pin to sense the power wire.
static void setChargingWitness(InputGPIO sensePin, bool negativeLogic, bool enableInternalPullResistor)
Set a GPIO pin to sense if the battery is being charged.
static void update(BatteryStatus &status)
Update the battery status according to the witnesses.
Interface to hardware implementing a battery monitor.
virtual void getStatus(BatteryStatus &currentStatus)
Get the battery status.
virtual void onStart()
Called once when the battery monitor daemon is started.
virtual bool read_soc(uint8_t &soc)=0
Retrieve the current state of charge.
Fake battery monitor for testing.
virtual bool read_soc(uint8_t &soc) override
Retrieve the current state of charge.
FakeBatteryMonitor(BatteryStatus *fakeStatus)
Construct a new Fake Battery Monitor object.
virtual void getStatus(BatteryStatus &currentStatus) override
Get the battery status.
BatteryStatus * status
Pointer to variable that holds fake battery status.
MAX1704x chips for battery monitoring.
bool read(uint8_t regAddress, uint16_t &value)
Read from a register.
MAX1704x(I2CBus bus=I2CBus::PRIMARY, uint8_t i2c_address=0xFF)
Construct a new MAX1704x object.
bool quickStart()
Send a quick start command.
virtual ~MAX1704x()
Destroy the MAX1704x object.
void * device
I2C slave device (must be type-casted)
virtual void onStart() override
Called once when the battery monitor daemon is started.
bool write(uint8_t regAddress, uint16_t value)
Write to a register.
virtual bool read_soc(uint8_t &currentSoC) override
Retrieve the current state of charge.
Battery monitor implemented as a voltage divider.
virtual bool read_soc(uint8_t &currentSoC) override
Retrieve the current state of charge.
int lastBatteryReading
Last ADC reading.
OutputGPIO _batteryENPin
ADC-capable GPIO for reading.
int CHARGING_ADC_READING
Minimum expected ADC reading when the battery is charging.
ADC_GPIO _batteryREADPin
output GPIO to enable/disable the circuitry
VoltageDividerMonitor(ADC_GPIO battREADPin, OutputGPIO battENPin, uint32_t resistorToGND=200, uint32_t resistorToBattery=110)
Construct a new Voltage Divider Monitor object.
static uint8_t readingToSoC(int reading)
Translate a battery reading to a state of charge.
int read()
Get ADC reading.
ADC-capable GPIO pin number.
Battery status.
Input-capable GPIO pin number.
Output-capable GPIO pin number.