2008-09-23 06:21:28 +00:00
|
|
|
/*
|
2011-12-26 21:35:57 +00:00
|
|
|
htop - BatteryMeter.c
|
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2021-09-22 09:33:00 +00:00
|
|
|
Released under the GNU GPLv2+, see the COPYING file
|
2011-12-26 21:35:57 +00:00
|
|
|
in the source distribution for its full text.
|
2008-09-23 06:21:28 +00:00
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
This meter written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
|
2008-09-23 06:21:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BatteryMeter.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
#include "CRT.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "Object.h"
|
2020-11-17 22:19:42 +00:00
|
|
|
#include "Platform.h"
|
2020-10-14 18:21:09 +00:00
|
|
|
#include "XUtils.h"
|
2008-09-23 06:21:28 +00:00
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
|
2020-09-28 10:23:07 +00:00
|
|
|
static const int BatteryMeter_attributes[] = {
|
2008-09-23 06:21:28 +00:00
|
|
|
BATTERY
|
|
|
|
};
|
|
|
|
|
2020-10-06 11:13:16 +00:00
|
|
|
static void BatteryMeter_updateValues(Meter* this) {
|
2014-11-27 23:02:52 +00:00
|
|
|
ACPresence isOnAC;
|
|
|
|
double percent;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2020-11-17 07:12:38 +00:00
|
|
|
Platform_getBattery(&percent, &isOnAC);
|
2014-11-27 23:02:52 +00:00
|
|
|
|
2020-09-07 09:52:42 +00:00
|
|
|
if (isnan(percent)) {
|
|
|
|
this->values[0] = NAN;
|
2020-10-06 11:13:16 +00:00
|
|
|
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "N/A");
|
2014-11-27 23:02:52 +00:00
|
|
|
return;
|
2009-04-27 21:48:09 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
this->values[0] = percent;
|
|
|
|
|
2020-12-18 14:49:37 +00:00
|
|
|
const char* text;
|
|
|
|
switch (isOnAC) {
|
|
|
|
case AC_PRESENT:
|
|
|
|
text = this->mode == TEXT_METERMODE ? " (Running on A/C)" : "(A/C)";
|
|
|
|
break;
|
|
|
|
case AC_ABSENT:
|
|
|
|
text = this->mode == TEXT_METERMODE ? " (Running on battery)" : "(bat)";
|
|
|
|
break;
|
|
|
|
case AC_ERROR:
|
|
|
|
default:
|
|
|
|
text = "";
|
|
|
|
break;
|
2008-09-23 06:21:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 11:13:16 +00:00
|
|
|
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%.1f%%%s", percent, text);
|
2008-09-23 06:21:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
const MeterClass BatteryMeter_class = {
|
2012-12-05 15:12:20 +00:00
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete
|
|
|
|
},
|
2016-05-04 05:39:26 +00:00
|
|
|
.updateValues = BatteryMeter_updateValues,
|
2012-12-05 15:12:20 +00:00
|
|
|
.defaultMode = TEXT_METERMODE,
|
2016-03-11 02:54:34 +00:00
|
|
|
.maxItems = 1,
|
2008-09-23 06:21:28 +00:00
|
|
|
.total = 100.0,
|
|
|
|
.attributes = BatteryMeter_attributes,
|
|
|
|
.name = "Battery",
|
|
|
|
.uiName = "Battery",
|
|
|
|
.caption = "Battery: "
|
|
|
|
};
|