[AC] Support USB+AC Charging: service

This commit is contained in:
nephros 2023-02-23 16:07:23 +01:00
parent e1cc4cd0f7
commit cc8d1082a4
2 changed files with 32 additions and 1 deletions

View file

@ -130,6 +130,20 @@ 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"
<< "/sys/class/power_supply/axp813-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 +274,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 +530,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;