Skip to content

Commit

Permalink
add battery "Charge-Discharge Cycle Count" sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiye committed Nov 28, 2024
1 parent ffc0272 commit 599d20f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions OpenHardwareMonitor/UI/SensorGadget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,9 @@ protected override void OnPaint(PaintEventArgs e)
case SensorType.Factor:
format = "{0:F3}";
break;
case SensorType.IntFactor:
format = "{0:F0}";
break;
case SensorType.TimeSpan:
format = "{0:g}";
break;
Expand Down
3 changes: 3 additions & 0 deletions OpenHardwareMonitor/UI/SensorNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public SensorNode(ISensor sensor, PersistentSettings settings, UnitManager unitM
case SensorType.Factor:
Format = "{0:F3}";
break;
case SensorType.IntFactor:
Format = "{0:F0}";
break;
case SensorType.Frequency:
Format = "{0:F1} Hz";
break;
Expand Down
5 changes: 4 additions & 1 deletion OpenHardwareMonitor/UI/SensorNotifyIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ private string GetString()
case SensorType.Factor:
case SensorType.Conductivity:
return $"{Sensor.Value:F1}";
case SensorType.IntFactor:
return $"{Sensor.Value:F0}";
case SensorType.Throughput:
return GetThroughputValue(Sensor.Value ?? 0);
case SensorType.Control:
Expand Down Expand Up @@ -371,7 +373,8 @@ public void Update()
case SensorType.Level: format = "\n{0}: {1:F1} %"; break;
case SensorType.Power: format = "\n{0}: {1:F0} W"; break;
case SensorType.Data: format = "\n{0}: {1:F0} GB"; break;
case SensorType.Factor: format = "\n{0}: {1:F3} GB"; break;
case SensorType.Factor: format = "\n{0}: {1:F3}"; break;
case SensorType.IntFactor: format = "\n{0}: {1:F0}"; break;
case SensorType.Energy: format = "\n{0}: {0:F0} mWh"; break;
case SensorType.Noise: format = "\n{0}: {0:F0} dBA"; break;
case SensorType.Conductivity: format = "\n{0}: {0:F1} µS/cm"; break;
Expand Down
1 change: 1 addition & 0 deletions OpenHardwareMonitor/UI/TypeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public TypeNode(SensorType sensorType, Identifier parentId, PersistentSettings s
Text = "Data";
break;
case SensorType.Factor:
case SensorType.IntFactor:
Image = EmbeddedResources.GetImage("factor.png");
Text = "Factors";
break;
Expand Down
27 changes: 27 additions & 0 deletions OpenHardwareMonitorLib/Hardware/Battery/Battery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal sealed class Battery : Hardware
private readonly uint _batteryTag;
private readonly Sensor _chargeDischargeCurrent;
private readonly Sensor _chargeDischargeRate;
private readonly Sensor _cycleCount;
private readonly Sensor _chargeLevel;
private readonly Sensor _degradationLevel;
private readonly Sensor _designedCapacity;
Expand Down Expand Up @@ -103,6 +104,13 @@ public Battery
ActivateSensor(_fullChargedCapacity);
ActivateSensor(_degradationLevel);
}

_cycleCount = new Sensor("Charge-Discharge Cycle Count", 0, SensorType.IntFactor, this, settings);
if (batteryInfo.CycleCount > 0)
{
_cycleCount.Value = batteryInfo.CycleCount;
ActivateSensor(_cycleCount);
}
}

public float? ChargeDischargeCurrent { get; private set; }
Expand Down Expand Up @@ -245,13 +253,32 @@ public override void Update()
_temperature.Value = null;
}

bqi.InformationLevel = Kernel32.BATTERY_QUERY_INFORMATION_LEVEL.BatteryInformation;
Kernel32.BATTERY_INFORMATION bi = default;
if (Kernel32.DeviceIoControl(_batteryHandle,
Kernel32.IOCTL.IOCTL_BATTERY_QUERY_INFORMATION,
ref bqi,
Marshal.SizeOf(bqi),
ref bi,
Marshal.SizeOf(bi),
out _,
IntPtr.Zero))
{
_cycleCount.Value = bi.CycleCount;
}
else
{
_cycleCount.Value = null;
}

ActivateSensorIfValueNotNull(_remainingCapacity);
ActivateSensorIfValueNotNull(_chargeLevel);
ActivateSensorIfValueNotNull(_voltage);
ActivateSensorIfValueNotNull(_chargeDischargeCurrent);
ActivateSensorIfValueNotNull(_chargeDischargeRate);
ActivateSensorIfValueNotNull(_remainingTime);
ActivateSensorIfValueNotNull(_temperature);
ActivateSensorIfValueNotNull(_cycleCount);
}

public override void Close()
Expand Down
1 change: 1 addition & 0 deletions OpenHardwareMonitorLib/Hardware/ISensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum SensorType
Control, // %
Level, // %
Factor, // 1
IntFactor, // 1 (int)
Data, // GB = 2^30 Bytes
SmallData, // MB = 2^20 Bytes
Throughput, // B/s
Expand Down

0 comments on commit 599d20f

Please sign in to comment.