Skip to content

Commit 599d20f

Browse files
committed
add battery "Charge-Discharge Cycle Count" sensor
1 parent ffc0272 commit 599d20f

6 files changed

Lines changed: 39 additions & 1 deletion

File tree

OpenHardwareMonitor/UI/SensorGadget.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,9 @@ protected override void OnPaint(PaintEventArgs e)
637637
case SensorType.Factor:
638638
format = "{0:F3}";
639639
break;
640+
case SensorType.IntFactor:
641+
format = "{0:F0}";
642+
break;
640643
case SensorType.TimeSpan:
641644
format = "{0:g}";
642645
break;

OpenHardwareMonitor/UI/SensorNode.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public SensorNode(ISensor sensor, PersistentSettings settings, UnitManager unitM
6060
case SensorType.Factor:
6161
Format = "{0:F3}";
6262
break;
63+
case SensorType.IntFactor:
64+
Format = "{0:F0}";
65+
break;
6366
case SensorType.Frequency:
6467
Format = "{0:F1} Hz";
6568
break;

OpenHardwareMonitor/UI/SensorNotifyIcon.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ private string GetString()
187187
case SensorType.Factor:
188188
case SensorType.Conductivity:
189189
return $"{Sensor.Value:F1}";
190+
case SensorType.IntFactor:
191+
return $"{Sensor.Value:F0}";
190192
case SensorType.Throughput:
191193
return GetThroughputValue(Sensor.Value ?? 0);
192194
case SensorType.Control:
@@ -371,7 +373,8 @@ public void Update()
371373
case SensorType.Level: format = "\n{0}: {1:F1} %"; break;
372374
case SensorType.Power: format = "\n{0}: {1:F0} W"; break;
373375
case SensorType.Data: format = "\n{0}: {1:F0} GB"; break;
374-
case SensorType.Factor: format = "\n{0}: {1:F3} GB"; break;
376+
case SensorType.Factor: format = "\n{0}: {1:F3}"; break;
377+
case SensorType.IntFactor: format = "\n{0}: {1:F0}"; break;
375378
case SensorType.Energy: format = "\n{0}: {0:F0} mWh"; break;
376379
case SensorType.Noise: format = "\n{0}: {0:F0} dBA"; break;
377380
case SensorType.Conductivity: format = "\n{0}: {0:F1} µS/cm"; break;

OpenHardwareMonitor/UI/TypeNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public TypeNode(SensorType sensorType, Identifier parentId, PersistentSettings s
7070
Text = "Data";
7171
break;
7272
case SensorType.Factor:
73+
case SensorType.IntFactor:
7374
Image = EmbeddedResources.GetImage("factor.png");
7475
Text = "Factors";
7576
break;

OpenHardwareMonitorLib/Hardware/Battery/Battery.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal sealed class Battery : Hardware
1313
private readonly uint _batteryTag;
1414
private readonly Sensor _chargeDischargeCurrent;
1515
private readonly Sensor _chargeDischargeRate;
16+
private readonly Sensor _cycleCount;
1617
private readonly Sensor _chargeLevel;
1718
private readonly Sensor _degradationLevel;
1819
private readonly Sensor _designedCapacity;
@@ -103,6 +104,13 @@ public Battery
103104
ActivateSensor(_fullChargedCapacity);
104105
ActivateSensor(_degradationLevel);
105106
}
107+
108+
_cycleCount = new Sensor("Charge-Discharge Cycle Count", 0, SensorType.IntFactor, this, settings);
109+
if (batteryInfo.CycleCount > 0)
110+
{
111+
_cycleCount.Value = batteryInfo.CycleCount;
112+
ActivateSensor(_cycleCount);
113+
}
106114
}
107115

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

256+
bqi.InformationLevel = Kernel32.BATTERY_QUERY_INFORMATION_LEVEL.BatteryInformation;
257+
Kernel32.BATTERY_INFORMATION bi = default;
258+
if (Kernel32.DeviceIoControl(_batteryHandle,
259+
Kernel32.IOCTL.IOCTL_BATTERY_QUERY_INFORMATION,
260+
ref bqi,
261+
Marshal.SizeOf(bqi),
262+
ref bi,
263+
Marshal.SizeOf(bi),
264+
out _,
265+
IntPtr.Zero))
266+
{
267+
_cycleCount.Value = bi.CycleCount;
268+
}
269+
else
270+
{
271+
_cycleCount.Value = null;
272+
}
273+
248274
ActivateSensorIfValueNotNull(_remainingCapacity);
249275
ActivateSensorIfValueNotNull(_chargeLevel);
250276
ActivateSensorIfValueNotNull(_voltage);
251277
ActivateSensorIfValueNotNull(_chargeDischargeCurrent);
252278
ActivateSensorIfValueNotNull(_chargeDischargeRate);
253279
ActivateSensorIfValueNotNull(_remainingTime);
254280
ActivateSensorIfValueNotNull(_temperature);
281+
ActivateSensorIfValueNotNull(_cycleCount);
255282
}
256283

257284
public override void Close()

OpenHardwareMonitorLib/Hardware/ISensor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public enum SensorType
2020
Control, // %
2121
Level, // %
2222
Factor, // 1
23+
IntFactor, // 1 (int)
2324
Data, // GB = 2^30 Bytes
2425
SmallData, // MB = 2^20 Bytes
2526
Throughput, // B/s

0 commit comments

Comments
 (0)