[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));
// 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
filenames.clear();
filenames << "/sys/class/power_supply/battery/temp"
@ -260,10 +274,19 @@ void Battery::updateData()
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)) {
current = currentFile->readLine().trimmed().toInt();
if(!invertDecided) {
invertCurrent = (!chargerConnected && current > 10);
invertCurrent = (!chargerConnected && !acConnected && current > 10);
if(invertCurrent) logL("Battery current inverted");
else logL("Battery current not inverted");
invertDecided = true;
@ -507,6 +530,10 @@ bool Battery::getChargerConnected() {
return chargerConnected;
}
bool Battery::getAcConnected() {
return acConnected;
}
void Battery::shutdown() {
logM("Shutting down...");
chargeNotification->close();

View file

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