@@ -165,6 +165,7 @@ static time_t Platform_Battery_cacheTime;
165165static BatteryInfo Platform_Battery_cache = {
166166 .ac = AC_ERROR ,
167167 .percent = NAN ,
168+ .powerCurr = NAN ,
168169 .energyCurr = NAN ,
169170 .energyFull = NAN ,
170171};
@@ -849,6 +850,7 @@ static void Platform_Battery_getProcData(BatteryInfo* info) {
849850static void Platform_Battery_getSysData (BatteryInfo * info ) {
850851 info -> percent = NAN ;
851852 info -> ac = AC_ERROR ;
853+ info -> powerCurr = NAN ;
852854 info -> energyCurr = NAN ;
853855 info -> energyFull = NAN ;
854856
@@ -858,6 +860,9 @@ static void Platform_Battery_getSysData(BatteryInfo* info) {
858860
859861 uint64_t totalFull = 0 ;
860862 uint64_t totalRemain = 0 ;
863+ int64_t totalPower = 0 ;
864+
865+ bool havePower = false;
861866
862867 const struct dirent * dirEntry ;
863868 while ((dirEntry = readdir (dir ))) {
@@ -910,6 +915,9 @@ static void Platform_Battery_getSysData(BatteryInfo* info) {
910915 uint8_t haveBatteryVoltage = 0 ; // 0 = no, 1 = min_voltage, 2 = curr_voltage
911916 bool haveBatteryLevel = false;
912917
918+ bool haveBatteryCurrent = false;
919+ bool haveBatteryPower = false;
920+
913921 uint64_t batteryEnergyFull = 0 ;
914922 uint64_t batteryEnergyCurr = 0 ;
915923
@@ -919,6 +927,11 @@ static void Platform_Battery_getSysData(BatteryInfo* info) {
919927 uint64_t batteryVoltage = 0 ;
920928 uint64_t batteryLevel = 0 ;
921929
930+ int64_t batteryCurrent = 0 ;
931+ int64_t batteryPower = 0 ;
932+
933+ bool batteryIsDischarging = false;
934+
922935 const char * line ;
923936
924937 char * buf = buffer ;
@@ -975,6 +988,18 @@ static void Platform_Battery_getSysData(BatteryInfo* info) {
975988 haveBatteryVoltage = 2 ;
976989 continue ;
977990 }
991+
992+ if (String_eq (field , "CURRENT_NOW" )) {
993+ batteryCurrent = val ;
994+ haveBatteryCurrent = true;
995+ continue ;
996+ }
997+
998+ if (String_eq (field , "POWER_NOW" )) {
999+ batteryPower += val ;
1000+ haveBatteryPower = true;
1001+ continue ;
1002+ }
9781003 }
9791004
9801005 if (haveBatteryLevel ) {
@@ -1004,6 +1029,19 @@ static void Platform_Battery_getSysData(BatteryInfo* info) {
10041029 totalFull += batteryEnergyFull ;
10051030 totalRemain += batteryEnergyCurr > batteryEnergyFull ? batteryEnergyFull : batteryEnergyCurr ;
10061031 }
1032+
1033+ if (!haveBatteryPower && haveBatteryCurrent && haveBatteryVoltage ) {
1034+ batteryPower = (batteryCurrent * batteryVoltage ) / 1000000 ;
1035+ haveBatteryPower = true;
1036+ }
1037+
1038+ if (haveBatteryPower ) {
1039+ if (batteryIsDischarging )
1040+ batteryPower = - batteryPower ;
1041+
1042+ totalPower += batteryPower ;
1043+ havePower = true;
1044+ }
10071045 } else if (type == AC ) {
10081046 if (info -> ac != AC_ERROR )
10091047 goto next ;
@@ -1032,6 +1070,10 @@ static void Platform_Battery_getSysData(BatteryInfo* info) {
10321070 info -> energyCurr = (double ) totalRemain / 1000000.0 ;
10331071 info -> energyFull = (double ) totalFull / 1000000.0 ;
10341072 }
1073+
1074+ if (havePower ) {
1075+ info -> powerCurr = (double ) totalPower / 1000000.0 ;
1076+ }
10351077}
10361078
10371079void Platform_getBattery (BatteryInfo * info ) {
@@ -1045,6 +1087,7 @@ void Platform_getBattery(BatteryInfo* info) {
10451087 Platform_Battery_cache = (BatteryInfo ) {
10461088 .ac = AC_ERROR ,
10471089 .percent = NAN ,
1090+ .powerCurr = NAN ,
10481091 .energyCurr = NAN ,
10491092 .energyFull = NAN ,
10501093 };
0 commit comments