Skip to content
This repository was archived by the owner on Jan 28, 2022. It is now read-only.

Commit 3d30a04

Browse files
authored
feat: Configurable battery capacity (#502)
This closes #498
1 parent c553b9d commit 3d30a04

9 files changed

+34
-21
lines changed

hardware-schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@
124124
"title": "Battery fuel gauge",
125125
"properties": {
126126
"enabled": { "$ref": "#/definitions/enabled" },
127+
"capacity": {
128+
"type": "integer",
129+
"minimum": 1000,
130+
"default": 2500
131+
},
127132
"i2c": { "$ref": "#/definitions/i2c" }
128133
}
129134
},

hardware.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
},
3636
"batteryFuelGauge": {
3737
"enabled": true,
38+
"capacity": 2500,
3839
"i2c": {
3940
"device": "/dev/i2c-3",
4041
"id": 85

sources/hardware/batteryfuelgauge.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ class BatteryFuelGauge : public Device {
7171
void chargingDone();
7272

7373
protected:
74-
explicit BatteryFuelGauge(QString name, QObject *parent = nullptr) : Device(name, parent) {}
74+
explicit BatteryFuelGauge(QString name, int capacity, QObject *parent = nullptr)
75+
: Device(name, parent), m_capacity(capacity) {
76+
Q_ASSERT(capacity);
77+
}
7578

76-
int m_capacity = 2500;
79+
int m_capacity;
7780
};

sources/hardware/hw_config.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define HW_CFG_BTN_INTR_HANDLER "buttonInterruptHandler"
3838
#define HW_CFG_BATTERY_CHARGER "batteryCharger"
3939
#define HW_CFG_BATTERY_FUEL_GAUGE "batteryFuelGauge"
40+
#define HW_CFG_BATTERY_CAPACITY "capacity"
4041
#define HW_CFG_HAPTIC_MOTOR "hapticMotor"
4142
#define HW_CFG_GESTURE "gesture"
4243
#define HW_CFG_LIGHT "light"

sources/hardware/linux/arm/bq27441.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@
3434

3535
static Q_LOGGING_CATEGORY(CLASS_LC, "hw.dev.BQ27441");
3636

37-
BQ27441::BQ27441(InterruptHandler *interruptHandler, const QString &i2cDevice, int i2cDeviceId, QObject *parent)
38-
: BatteryFuelGauge("BQ27441 battery fuel gauge", parent),
37+
BQ27441::BQ27441(InterruptHandler *interruptHandler, int capacity, const QString &i2cDevice, int i2cDeviceId,
38+
QObject *parent)
39+
: BatteryFuelGauge("BQ27441 battery fuel gauge", capacity, parent),
3940
m_i2cDevice(i2cDevice),
4041
m_i2cDeviceId(i2cDeviceId),
4142
m_i2cFd(0) {
4243
Q_ASSERT(interruptHandler);
4344
Q_ASSERT(!i2cDevice.isEmpty());
44-
qCDebug(CLASS_LC()) << name() << i2cDevice << "with id:" << i2cDeviceId;
45+
qCDebug(CLASS_LC()) << name() << i2cDevice << "with id:" << i2cDeviceId << ", capacity:" << capacity;
4546

4647
connect(interruptHandler, &InterruptHandler::interruptEvent, this, [&](int event) {
4748
if (event == InterruptHandler::BATTERY) {
@@ -63,7 +64,7 @@ bool BQ27441::open() {
6364

6465
/* Initialize I2C */
6566
bool initialized = false;
66-
m_i2cFd = wiringPiI2CSetupInterface(qPrintable(m_i2cDevice), m_i2cDeviceId);
67+
m_i2cFd = wiringPiI2CSetupInterface(qPrintable(m_i2cDevice), m_i2cDeviceId);
6768
if (m_i2cFd == -1) {
6869
qCCritical(CLASS_LC) << "Unable to open or select I2C device" << m_i2cDeviceId << "on" << m_i2cDevice;
6970
} else {
@@ -96,23 +97,23 @@ const QLoggingCategory &BQ27441::logCategory() const { return CLASS_LC(); }
9697

9798
void BQ27441::updateBatteryValues() {
9899
if (getDesignCapacity() != m_capacity) {
99-
qCDebug(CLASS_LC) << "Design capacity does not match.";
100-
101100
// calibrate the gauge
102-
qCDebug(CLASS_LC) << "Fuel gauge calibration. Setting charge capacity to:" << m_capacity;
101+
qCDebug(CLASS_LC) << "Design capacity does not match. Fuel gauge calibration. Setting charge capacity to:"
102+
<< m_capacity;
103103
changeCapacity(m_capacity);
104104
}
105105

106106
m_level = getStateOfCharge();
107107
emit levelChanged();
108-
qCDebug(CLASS_LC()) << "Battery level:" << m_level;
109108

110109
m_voltage = wiringPiI2CReadReg16(m_i2cFd, BQ27441_COMMAND_VOLTAGE); // getVoltage();
111-
qCDebug(CLASS_LC()) << "Battery voltage:" << m_voltage;
112110

113111
m_health = getStateOfHealth();
112+
if (CLASS_LC().isDebugEnabled()) {
113+
qCDebug(CLASS_LC()) << "Battery level:" << m_level << "%, battery voltage:" << m_voltage
114+
<< "mV, battery health:" << m_health;
115+
}
114116
emit healthChanged();
115-
qCDebug(CLASS_LC()) << "Battery health:" << m_health;
116117

117118
m_averagePower = static_cast<int>(
118119
static_cast<int16_t>(wiringPiI2CReadReg16(m_i2cFd, BQ27441_COMMAND_AVG_POWER))); // getAveragePower();
@@ -154,12 +155,13 @@ void BQ27441::updateBatteryValues() {
154155

155156
// calculate remaining battery life
156157
float remainingLife = static_cast<float>(getRemainingCapacity()) / static_cast<float>(abs(getAverageCurrent()));
157-
m_remainingLife = (m_remainingLife + remainingLife) / 2;
158+
m_remainingLife = (m_remainingLife + remainingLife) / 2;
158159
emit remainingLifeChanged();
159160

160-
qCDebug(CLASS_LC()) << "Average power" << m_averagePower << "mW";
161-
qCDebug(CLASS_LC()) << "Average current" << getAverageCurrent() << "mA";
162-
qCDebug(CLASS_LC()) << "Remaining battery life" << m_remainingLife << "h";
161+
if (CLASS_LC().isDebugEnabled()) {
162+
qCDebug(CLASS_LC()) << "Average power" << m_averagePower << "mW, average current" << getAverageCurrent()
163+
<< "mA, remaining battery life" << m_remainingLife << "h";
164+
}
163165
}
164166

165167
void BQ27441::begin() {

sources/hardware/linux/arm/bq27441.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class BQ27441 : public BatteryFuelGauge {
8181
Q_OBJECT
8282

8383
public:
84-
explicit BQ27441(InterruptHandler *interruptHandler, const QString &i2cDevice,
84+
explicit BQ27441(InterruptHandler *interruptHandler, int capacity, const QString &i2cDevice,
8585
int i2cDeviceId = BQ27441_I2C_ADDRESS, QObject *parent = nullptr);
8686
~BQ27441() override;
8787

sources/hardware/linux/arm/hw_factory_yio.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "mcp23017_interrupt.h"
4343

4444
const QString HardwareFactoryYio::DEF_I2C_DEVICE = "/dev/i2c-3";
45+
const int HardwareFactoryYio::DEF_BATTERY_CAPACITY = 2500;
4546

4647
static Q_LOGGING_CATEGORY(CLASS_LC, "hw.factory.yio");
4748

@@ -187,10 +188,11 @@ BatteryCharger *HardwareFactoryYio::buildBatteryCharger(const QVariantMap &confi
187188
}
188189

189190
BatteryFuelGauge *HardwareFactoryYio::buildBatteryFuelGauge(const QVariantMap &config) {
191+
auto capacity = ConfigUtil::getValue(config, HW_CFG_BATTERY_CAPACITY, DEF_BATTERY_CAPACITY).toInt();
190192
auto dev = ConfigUtil::getValue(config, HW_CFG_PATH_I2C_DEV, DEF_I2C_DEVICE).toString();
191193
auto id = ConfigUtil::getValue(config, HW_CFG_PATH_I2C_ID, BQ27441_I2C_ADDRESS).toInt();
192194

193-
BatteryFuelGauge *device = new BQ27441(getInterruptHandler(), dev, id, this);
195+
BatteryFuelGauge *device = new BQ27441(getInterruptHandler(), capacity, dev, id, this);
194196
connect(device, &Device::error, this, &HardwareFactoryYio::onError);
195197

196198
return device;

sources/hardware/linux/arm/hw_factory_yio.h

+1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,5 @@ class HardwareFactoryYio : public HardwareFactoryLinux {
6060

6161
// default values for YIO
6262
static const QString DEF_I2C_DEVICE;
63+
static const int DEF_BATTERY_CAPACITY;
6364
};

sources/hardware/mock/batteryfuelgauge_mock.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ class BatteryFuelGaugeMock : public BatteryFuelGauge {
2828
Q_OBJECT
2929

3030
public:
31-
explicit BatteryFuelGaugeMock(QObject* parent = nullptr) : BatteryFuelGauge("BatteryFuelGaugeMock", parent) {
32-
setCapacity(2500);
33-
}
31+
explicit BatteryFuelGaugeMock(QObject* parent = nullptr) : BatteryFuelGauge("BatteryFuelGaugeMock", 2500, parent) {}
3432

3533
~BatteryFuelGaugeMock() override {}
3634

0 commit comments

Comments
 (0)