Merge pull request #61 from nephros/ac-support

Add Support for AC Charger in addition to USB
This commit is contained in:
Matti Viljanen 2023-03-04 13:18:50 +02:00 committed by GitHub
commit 943d1b2879
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 73 additions and 3 deletions

View file

@ -45,6 +45,7 @@ Item {
property int counter: 0 property int counter: 0
running: (enableLowBatteryAnimation running: (enableLowBatteryAnimation
&& !battery.chargerConnected && !battery.chargerConnected
&& !battery.acConnected
&& _charge <= settings.lowAlert) && _charge <= settings.lowAlert)
repeat: true repeat: true
interval: 400 interval: 400

View file

@ -132,8 +132,15 @@ Page {
} }
MyDetailItem { MyDetailItem {
property bool connected: (battery.chargerConnected || battery.acConnected)
property string chargerType: {
if ( battery.chargerConnected && !battery.acConnected) { return qsTr("USB") }
else if (!battery.chargerConnected && battery.acConnected) { return qsTr("AC") }
else if ( battery.chargerConnected && battery.acConnected) { return qsTr("USB") + "/" + qsTr("AC") }
return "unknown power source"
}
label: qsTr("Charger connected:") label: qsTr("Charger connected:")
value: battery.chargerConnected ? qsTr("yes") : qsTr("no") value: connected ? (qsTr("yes") + " (" + chargerType + ")") : qsTr("no")
} }
MyDetailItem { MyDetailItem {
label: qsTr("State:") label: qsTr("State:")

View file

@ -79,6 +79,19 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
logL("Charger status file: " + (chargerConnectedFile ? chargerConnectedFile->fileName() : notFound)); logL("Charger status file: " + (chargerConnectedFile ? chargerConnectedFile->fileName() : notFound));
// Number: 0 or 1
filenames.clear();
filenames << "/sys/class/power_supply/ac/present";
foreach(const QString& file, filenames) {
if(!acConnectedFile && QFile::exists(file)) {
acConnectedFile = new QFile(file, this);
break;
}
}
logL("AC status file: " + (acConnectedFile ? acConnectedFile->fileName() : notFound));
// Number: temperature // Number: temperature
filenames.clear(); filenames.clear();
filenames << "/sys/class/power_supply/battery/temp" filenames << "/sys/class/power_supply/battery/temp"
@ -170,6 +183,16 @@ void Battery::updateData()
chargerConnectedFile->close(); chargerConnectedFile->close();
} }
if(acConnectedFile && acConnectedFile->open(QIODevice::ReadOnly)) {
nextAcConnected = acConnectedFile->readLine().trimmed().toInt();
if(nextAcConnected != acConnected) {
acConnected = nextAcConnected;
emit acConnectedChanged(acConnected);
logM(QString("AC: %1").arg(acConnected ? "connected" : "disconnected"));
}
acConnectedFile->close();
}
if(stateFile && stateFile->open(QIODevice::ReadOnly)) { if(stateFile && stateFile->open(QIODevice::ReadOnly)) {
nextState = (QString(stateFile->readLine().trimmed().toLower())); nextState = (QString(stateFile->readLine().trimmed().toLower()));
if(nextState != state) { if(nextState != state) {
@ -183,7 +206,7 @@ void Battery::updateData()
if(currentFile && currentFile->open(QIODevice::ReadOnly)) { if(currentFile && currentFile->open(QIODevice::ReadOnly)) {
current = currentFile->readLine().trimmed().toInt(); current = currentFile->readLine().trimmed().toInt();
if(!invertDecided) { if(!invertDecided) {
invertCurrent = (!chargerConnected && current > 10); invertCurrent = (!chargerConnected && !acConnected && current > 10);
if(invertCurrent) logL("Battery current inverted"); if(invertCurrent) logL("Battery current inverted");
else logL("Battery current not inverted"); else logL("Battery current not inverted");
invertDecided = true; invertDecided = true;
@ -230,3 +253,5 @@ int Battery::getTemperature(){ return temperature; }
bool Battery::getChargingEnabled() { return chargingEnabled; } bool Battery::getChargingEnabled() { return chargingEnabled; }
bool Battery::getChargerConnected() { return chargerConnected; } bool Battery::getChargerConnected() { return chargerConnected; }
bool Battery::getAcConnected() { return acConnected; }

View file

@ -33,6 +33,7 @@ class Battery : public QObject
Q_PROPERTY(int current READ getCurrent NOTIFY currentChanged) Q_PROPERTY(int current READ getCurrent NOTIFY currentChanged)
Q_PROPERTY(int maxChargeCurrent READ getMaxChargeCurrent) Q_PROPERTY(int maxChargeCurrent READ getMaxChargeCurrent)
Q_PROPERTY(bool chargerConnected READ getChargerConnected NOTIFY chargerConnectedChanged) Q_PROPERTY(bool chargerConnected READ getChargerConnected NOTIFY chargerConnectedChanged)
Q_PROPERTY(bool acConnected READ getAcConnected NOTIFY acConnectedChanged)
Q_PROPERTY(QString state READ getState NOTIFY stateChanged) Q_PROPERTY(QString state READ getState NOTIFY stateChanged)
Q_PROPERTY(bool chargingEnabled READ getChargingEnabled NOTIFY chargingEnabledChanged) Q_PROPERTY(bool chargingEnabled READ getChargingEnabled NOTIFY chargingEnabledChanged)
@ -48,6 +49,7 @@ public:
int getMaxChargeCurrent(); int getMaxChargeCurrent();
bool getCharging(); bool getCharging();
bool getChargerConnected(); bool getChargerConnected();
bool getAcConnected();
QString getState(); QString getState();
QString getHealth(); QString getHealth();
@ -62,6 +64,7 @@ private:
QFile* chargeFile = nullptr; QFile* chargeFile = nullptr;
QFile* currentFile = nullptr; QFile* currentFile = nullptr;
QFile* chargerConnectedFile = nullptr; QFile* chargerConnectedFile = nullptr;
QFile* acConnectedFile = nullptr;
QFile* stateFile = nullptr; QFile* stateFile = nullptr;
QFile* chargingEnabledFile = nullptr; QFile* chargingEnabledFile = nullptr;
QFile* maxChargeCurrentFile = nullptr; QFile* maxChargeCurrentFile = nullptr;
@ -75,6 +78,7 @@ private:
int charge = 100; // 100% full int charge = 100; // 100% full
int current = 0; // Not charging/discharging int current = 0; // Not charging/discharging
bool chargerConnected = false; // Charger plugged in bool chargerConnected = false; // Charger plugged in
bool acConnected = false; // AC plugged in
QString state = "idle"; // dis/charging, idle, unknown QString state = "idle"; // dis/charging, idle, unknown
bool chargingEnabled = true; // Only ever disabled manually bool chargingEnabled = true; // Only ever disabled manually
int maxChargeCurrent = 0; // Charge current limit in micro amps int maxChargeCurrent = 0; // Charge current limit in micro amps
@ -91,9 +95,11 @@ private:
bool invertDecided = false; bool invertDecided = false;
bool nextChargerConnected = chargerConnected; bool nextChargerConnected = chargerConnected;
bool nextAcConnected = acConnected;
QString nextState = state; QString nextState = state;
bool nextChargingEnabled = chargingEnabled; bool nextChargingEnabled = chargingEnabled;
QString nextHealth = health; QString nextHealth = health;
int nextTemperature = temperature; int nextTemperature = temperature;
@ -103,6 +109,7 @@ signals:
void stateChanged(QString); void stateChanged(QString);
void chargingEnabledChanged(bool); void chargingEnabledChanged(bool);
void chargerConnectedChanged(bool); void chargerConnectedChanged(bool);
void acConnectedChanged(bool);
void healthChanged(QString); void healthChanged(QString);
void temperatureChanged(int); void temperatureChanged(int);
}; };

View file

@ -130,6 +130,19 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
logL("Charger status file: " + (chargerConnectedFile ? chargerConnectedFile->fileName() : notFound)); logL("Charger status file: " + (chargerConnectedFile ? chargerConnectedFile->fileName() : notFound));
// Charger connected, bool (number): 0 or 1
filenames.clear();
filenames << "/sys/class/power_supply/ac/present";
foreach(const QString& file, filenames) {
if(!acConnectedFile && QFile::exists(file)) {
acConnectedFile = new QFile(file, this);
break;
}
}
logL("AC status file: " + (acConnectedFile ? acConnectedFile->fileName() : notFound));
// Number: temperature // Number: temperature
filenames.clear(); filenames.clear();
filenames << "/sys/class/power_supply/battery/temp" filenames << "/sys/class/power_supply/battery/temp"
@ -260,10 +273,19 @@ void Battery::updateData()
chargerConnectedFile->close(); chargerConnectedFile->close();
} }
if(acConnectedFile && acConnectedFile->open(QIODevice::ReadOnly)) {
nextAcConnected = acConnectedFile->readLine().trimmed().toInt();
if(nextAcConnected != acConnected) {
acConnected = nextAcConnected;
logM(QString("AC: %1").arg(acConnected ? "connected" : "disconnected"));
}
acConnectedFile->close();
}
if(currentFile && currentFile->open(QIODevice::ReadOnly)) { if(currentFile && currentFile->open(QIODevice::ReadOnly)) {
current = currentFile->readLine().trimmed().toInt(); current = currentFile->readLine().trimmed().toInt();
if(!invertDecided) { if(!invertDecided) {
invertCurrent = (!chargerConnected && current > 10); invertCurrent = (!chargerConnected && !acConnected && current > 10);
if(invertCurrent) logL("Battery current inverted"); if(invertCurrent) logL("Battery current inverted");
else logL("Battery current not inverted"); else logL("Battery current not inverted");
invertDecided = true; invertDecided = true;
@ -507,6 +529,10 @@ bool Battery::getChargerConnected() {
return chargerConnected; return chargerConnected;
} }
bool Battery::getAcConnected() {
return acConnected;
}
void Battery::shutdown() { void Battery::shutdown() {
logM("Shutting down..."); logM("Shutting down...");
chargeNotification->close(); chargeNotification->close();

View file

@ -42,6 +42,7 @@ public:
int getCharge(); int getCharge();
bool getCharging(); bool getCharging();
bool getChargerConnected(); bool getChargerConnected();
bool getAcConnected();
QString getState(); QString getState();
bool getChargingEnabled(); bool getChargingEnabled();
@ -78,6 +79,7 @@ private:
Logger *logger; Logger *logger;
QFile *chargeFile = nullptr; QFile *chargeFile = nullptr;
QFile *chargerConnectedFile = nullptr; QFile *chargerConnectedFile = nullptr;
QFile *acConnectedFile = nullptr;
QFile *currentFile = nullptr; QFile *currentFile = nullptr;
QFile *stateFile = nullptr; QFile *stateFile = nullptr;
QFile *chargingEnabledFile = nullptr; QFile *chargingEnabledFile = nullptr;
@ -97,6 +99,7 @@ private:
int charge = 100; // 100% full int charge = 100; // 100% full
int current = 0; // Charging/discharging current in microamps int current = 0; // Charging/discharging current in microamps
bool chargerConnected = false; // Charger plugged in bool chargerConnected = false; // Charger plugged in
bool acConnected = false; // AC plugged in
QString state = "idle"; // dis/charging, idle, unknown QString state = "idle"; // dis/charging, idle, unknown
bool chargingEnabled = true; // Only ever disabled manually bool chargingEnabled = true; // Only ever disabled manually
int maxChargeCurrent = 0; int maxChargeCurrent = 0;
@ -114,6 +117,7 @@ private:
bool invertDecided = false; bool invertDecided = false;
bool nextChargerConnected = chargerConnected; bool nextChargerConnected = chargerConnected;
bool nextAcConnected = acConnected;
QString nextState = state; QString nextState = state;
bool nextChargingEnabled = chargingEnabled; bool nextChargingEnabled = chargingEnabled;
int nextTemperature = temperature; int nextTemperature = temperature;