Skip to content
Browse files

neosuspend - compute current consuption from charge_now and suspend time

This will give us average current in suspend:

http://lists.goldelico.com/pipermail/gta04-owner/2012-August/002853.html

Thanks to Neil Brown for the idea and formula.
  • Loading branch information...
1 parent fbca96a commit 724203505d9b9cd4a0118f079f7bbbf1332be4d3 @radekp committed Aug 26, 2012
Showing with 43 additions and 34 deletions.
  1. +20 −24 devices/gta04/server/neosuspend.cpp
  2. +22 −9 devices/neo/server/neosuspend.cpp
  3. +1 −1 etc/themes/finxi/home.xml
View
44 devices/gta04/server/neosuspend.cpp
@@ -50,6 +50,7 @@ class NeoSuspend : public SystemSuspendHandler
QProcess resumeScript;
QValueSpaceObject batteryVso;
QDateTime suspendTime;
+ int chargeNowBeforeSuspend;
};
QTOPIA_DEMAND_TASK(NeoSuspend, NeoSuspend);
@@ -78,46 +79,41 @@ bool NeoSuspend::canSuspend() const
return ok;
}
+static int readChargeNow()
+{
+ QString chargeNowStr =
+ qReadFile("/sys/class/power_supply/bq27000-battery/charge_now");
+ return chargeNowStr.toInt();
+}
+
bool NeoSuspend::suspend()
{
qLog(PowerManagement) << __PRETTY_FUNCTION__;
QProcess::execute("before-suspend.sh");
-
+ chargeNowBeforeSuspend = readChargeNow();
suspendTime = QDateTime::currentDateTime();
-
- QFile powerStateFile("/sys/power/state");
- if (!powerStateFile.
- open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) {
- qWarning() << "File not opened";
- } else {
- QTextStream out(&powerStateFile);
- out << "mem";
- powerStateFile.close();
- }
+ Qtopia::writeFile("/sys/power/state", "mem", 3, false);
return true;
}
bool NeoSuspend::wake()
{
+ // Average current in suspend computed from charge_now and suspend time
+ QDateTime now = QDateTime::currentDateTime();
+ int chargeNow = readChargeNow();
+ int secs = suspendTime.secsTo(now);
+ if(secs == 0)
+ secs++;
+ int avgCurrent = ((chargeNowBeforeSuspend - chargeNow) * 36) / (10 * secs); // (first_number - last_number) * 3600 / (last_timestamp - first_timestamp), but we want it in mA so 1000x less
+ batteryVso.setAttribute("avg_current_in_suspend", QString::number(avgCurrent));
+
// Read and update current_now. It should contain the current in suspend
QString currentNowStr =
qReadFile("/sys/class/power_supply/bq27000-battery/current_now");
int currentNow = currentNowStr.toInt() / 1000;
batteryVso.setAttribute("current_now_in_suspend", QString::number(currentNow));
-
- // Check if resume was too fast. If yes, it might be GPS which wakes the
- // device up and prevents suspend. As a workaround we try to turn the gps
- // off. For more info see:
- // http://lists.goldelico.com/pipermail/gta04-owner/2012-April/002184.html
- QDateTime now = QDateTime::currentDateTime();
- int secs = suspendTime.secsTo(now);
- if(secs < 10) {
- qLog(PowerManagement) << "Resume was too fast, trying to turn off gps";
- writeFile("/sys/devices/virtual/gpio/gpio145/value", "0");
- writeFile("/sys/devices/virtual/gpio/gpio145/value", "1");
- }
-
+
#ifdef Q_WS_QWS
QWSServer::instance()->refresh();
#endif
View
31 devices/neo/server/neosuspend.cpp
@@ -47,6 +47,8 @@ class NeoSuspend : public SystemSuspendHandler
private:
QProcess resumeScript;
QValueSpaceObject batteryVso;
+ QDateTime suspendTime;
+ int chargeNowBeforeSuspend;
};
QTOPIA_DEMAND_TASK(NeoSuspend, NeoSuspend);
@@ -73,25 +75,36 @@ bool NeoSuspend::canSuspend() const
return ok;
}
+static int readChargeNow()
+{
+ QString chargeNowStr =
+ qReadFile("/sys/class/power_supply/battery/charge_now");
+ return chargeNowStr.toInt();
+}
+
bool NeoSuspend::suspend()
{
qLog(PowerManagement)<< __PRETTY_FUNCTION__;
QProcess::execute("before-suspend.sh");
-
- QFile powerStateFile("/sys/power/state");
- if( !powerStateFile.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) {
- qWarning()<<"File not opened";
- } else {
- QTextStream out(&powerStateFile);
- out << "mem";
- powerStateFile.close();
- }
+ chargeNowBeforeSuspend = readChargeNow();
+ suspendTime = QDateTime::currentDateTime();
+ Qtopia::writeFile("/sys/power/state", "mem", 3, false);
return true;
}
bool NeoSuspend::wake()
{
+ // Average current in suspend computed from charge_now and suspend time
+ QDateTime now = QDateTime::currentDateTime();
+ int chargeNow = readChargeNow();
+ int secs = suspendTime.secsTo(now);
+ if(secs == 0)
+ secs++;
+ int avgCurrent = ((chargeNowBeforeSuspend - chargeNow) * 36) / (10 * secs); // (first_number - last_number) * 3600 / (last_timestamp - first_timestamp), but we want it in mA so 1000x less
+ batteryVso.setAttribute("avg_current_in_suspend", QString::number(avgCurrent));
+
+ // Actual current_now
QString currentNowStr =
qReadFile("/sys/class/power_supply/battery/current_now");
int currentNow = currentNowStr.toInt() / 1000;
View
2 etc/themes/finxi/home.xml
@@ -15,7 +15,7 @@
expr:@/UI/Pressure/mb . "mb "
</text>
<text name="current_now" size="5" bold="yes" rect="2%,0,48%x12pt" align="left,vcenter" color="#ffffff" outline="#000000" transient="yes" >
- expr:@/UI/Battery/current_now . "/". @/UI/Battery/current_now_in_suspend . "mA"
+ expr:@/UI/Battery/current_now . "/" . @/UI/Battery/avg_current_in_suspend . "/" . @/UI/Battery/current_now_in_suspend . "mA"
</text>
</rect>
<layout rect="0,20%,0x45pt" align="hcenter">

0 comments on commit 7242035

Please sign in to comment.
Something went wrong with that request. Please try again.