Merge pull request #61 from nephros/ac-support
Add Support for AC Charger in addition to USB
This commit is contained in:
commit
943d1b2879
6 changed files with 73 additions and 3 deletions
|
@ -45,6 +45,7 @@ Item {
|
|||
property int counter: 0
|
||||
running: (enableLowBatteryAnimation
|
||||
&& !battery.chargerConnected
|
||||
&& !battery.acConnected
|
||||
&& _charge <= settings.lowAlert)
|
||||
repeat: true
|
||||
interval: 400
|
||||
|
|
|
@ -132,8 +132,15 @@ Page {
|
|||
}
|
||||
|
||||
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:")
|
||||
value: battery.chargerConnected ? qsTr("yes") : qsTr("no")
|
||||
value: connected ? (qsTr("yes") + " (" + chargerType + ")") : qsTr("no")
|
||||
}
|
||||
MyDetailItem {
|
||||
label: qsTr("State:")
|
||||
|
|
|
@ -79,6 +79,19 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
|
|||
|
||||
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
|
||||
filenames.clear();
|
||||
filenames << "/sys/class/power_supply/battery/temp"
|
||||
|
@ -170,6 +183,16 @@ void Battery::updateData()
|
|||
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)) {
|
||||
nextState = (QString(stateFile->readLine().trimmed().toLower()));
|
||||
if(nextState != state) {
|
||||
|
@ -183,7 +206,7 @@ void Battery::updateData()
|
|||
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;
|
||||
|
@ -230,3 +253,5 @@ int Battery::getTemperature(){ return temperature; }
|
|||
bool Battery::getChargingEnabled() { return chargingEnabled; }
|
||||
|
||||
bool Battery::getChargerConnected() { return chargerConnected; }
|
||||
|
||||
bool Battery::getAcConnected() { return acConnected; }
|
||||
|
|
|
@ -33,6 +33,7 @@ class Battery : public QObject
|
|||
Q_PROPERTY(int current READ getCurrent NOTIFY currentChanged)
|
||||
Q_PROPERTY(int maxChargeCurrent READ getMaxChargeCurrent)
|
||||
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(bool chargingEnabled READ getChargingEnabled NOTIFY chargingEnabledChanged)
|
||||
|
||||
|
@ -48,6 +49,7 @@ public:
|
|||
int getMaxChargeCurrent();
|
||||
bool getCharging();
|
||||
bool getChargerConnected();
|
||||
bool getAcConnected();
|
||||
QString getState();
|
||||
|
||||
QString getHealth();
|
||||
|
@ -62,6 +64,7 @@ private:
|
|||
QFile* chargeFile = nullptr;
|
||||
QFile* currentFile = nullptr;
|
||||
QFile* chargerConnectedFile = nullptr;
|
||||
QFile* acConnectedFile = nullptr;
|
||||
QFile* stateFile = nullptr;
|
||||
QFile* chargingEnabledFile = nullptr;
|
||||
QFile* maxChargeCurrentFile = nullptr;
|
||||
|
@ -75,6 +78,7 @@ private:
|
|||
int charge = 100; // 100% full
|
||||
int current = 0; // Not charging/discharging
|
||||
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; // Charge current limit in micro amps
|
||||
|
@ -91,9 +95,11 @@ private:
|
|||
bool invertDecided = false;
|
||||
|
||||
bool nextChargerConnected = chargerConnected;
|
||||
bool nextAcConnected = acConnected;
|
||||
QString nextState = state;
|
||||
bool nextChargingEnabled = chargingEnabled;
|
||||
|
||||
|
||||
QString nextHealth = health;
|
||||
int nextTemperature = temperature;
|
||||
|
||||
|
@ -103,6 +109,7 @@ signals:
|
|||
void stateChanged(QString);
|
||||
void chargingEnabledChanged(bool);
|
||||
void chargerConnectedChanged(bool);
|
||||
void acConnectedChanged(bool);
|
||||
void healthChanged(QString);
|
||||
void temperatureChanged(int);
|
||||
};
|
||||
|
|
|
@ -130,6 +130,19 @@ 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";
|
||||
|
||||
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 +273,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 +529,10 @@ bool Battery::getChargerConnected() {
|
|||
return chargerConnected;
|
||||
}
|
||||
|
||||
bool Battery::getAcConnected() {
|
||||
return acConnected;
|
||||
}
|
||||
|
||||
void Battery::shutdown() {
|
||||
logM("Shutting down...");
|
||||
chargeNotification->close();
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue