diff --git a/.changeset/charging-from-current.md b/.changeset/charging-from-current.md new file mode 100644 index 0000000..cd747c6 --- /dev/null +++ b/.changeset/charging-from-current.md @@ -0,0 +1,5 @@ +--- +"ewiz": patch +--- + +**Plugging in shows "Charging" at once, and the MagSafe light stops flickering.** macOS's charging flag can lag a plug-in by over 20 seconds while current is already flowing into the battery. eWiz waited on that flag, so the panel said "Plugged in, not charging", the icon had no bolt, and the MagSafe light went amber, then green, then amber again. eWiz now treats current flowing into the battery as charging. Also adds `scripts/plug-trace.py`, which logs the battery, macOS, the helper and the menu bar side by side across a plug or unplug. diff --git a/Sources/EWizKit/BatteryMonitor.swift b/Sources/EWizKit/BatteryMonitor.swift index d3ebaf0..6af4c0c 100644 --- a/Sources/EWizKit/BatteryMonitor.swift +++ b/Sources/EWizKit/BatteryMonitor.swift @@ -88,6 +88,22 @@ public enum BatteryMonitor { snap.isExternalConnected = snap.isPluggedIn } + /// Current into the battery that counts as charging whatever the flag says. Well above + /// the trickle a full or held battery shows on the adapter, well below a real charge. + public static let chargingCurrentFloor = 300 + + /// Whether the battery is charging: macOS's flag, or current actually flowing in. + /// + /// `IsCharging` can trail the current by a long way. Traced on a plug-in: +1063 mA into + /// the battery one second after the cable, and the flag still "No" 23 seconds later. Every + /// surface waited on the flag, so the panel said "Plugged in, not charging", the icon had + /// no bolt, and the helper set the MagSafe light green over a battery taking 4 A (macOS + /// had lit it amber, so it went amber, green, amber). Current is the fact; the flag is a + /// report of it. Never the other way: a flag saying yes is kept. + public static func isCharging(flag: Bool, amperage: Int, cableIn: Bool, full: Bool) -> Bool { + flag || (cableIn && !full && amperage >= chargingCurrentFloor) + } + // MARK: - AppleSmartBattery (hardware detail) private static func readSmartBattery(into snap: inout BatterySnapshot) { @@ -108,6 +124,14 @@ public enum BatteryMonitor { snap.isExternalConnected = ext } + // Signed mA, positive into the battery. CFNumber keeps the sign; the ioreg text + // prints negatives as huge unsigned values, the number itself doesn't. + if let amps = (props["Amperage"] as? NSNumber)?.intValue { + snap.isCharging = Self.isCharging(flag: snap.isCharging, amperage: amps, + cableIn: snap.isExternalConnected, + full: snap.isFullyCharged) + } + if let cycles = props["CycleCount"] as? Int { snap.cycleCount = cycles } diff --git a/Sources/EWizKit/Control.swift b/Sources/EWizKit/Control.swift index 73b7db2..e11b353 100644 --- a/Sources/EWizKit/Control.swift +++ b/Sources/EWizKit/Control.swift @@ -396,7 +396,9 @@ public enum HelperBuild { /// v30: charging starting or stopping wakes the loop too. The source doesn't switch /// when current starts after a resume or a plug-in, so the helper went on /// reporting "held" until its next tick. - public static let version = 30 + /// v31: charging is read from current flowing in, not only macOS's `IsCharging`, which + /// trailed a plug-in by 23 s and had the MagSafe light go amber, green, amber. + public static let version = 31 } public enum ControlError: Error, CustomStringConvertible { diff --git a/Tests/EWizKitTests/PowerFlowTests.swift b/Tests/EWizKitTests/PowerFlowTests.swift index db24d18..62bb3bf 100644 --- a/Tests/EWizKitTests/PowerFlowTests.swift +++ b/Tests/EWizKitTests/PowerFlowTests.swift @@ -54,3 +54,27 @@ struct PowerFlowTests { #expect(f.systemWatts == 18) } } + +@Suite("Charging from current, not only the flag") +struct ChargingFromCurrentTests { + @Test("current flowing in counts before macOS's flag turns over") + func currentBeatsALaggingFlag() { + #expect(BatteryMonitor.isCharging(flag: false, amperage: 1063, cableIn: true, full: false)) + } + + @Test("a trickle on the adapter is not charging") + func trickleIsNot() { + #expect(!BatteryMonitor.isCharging(flag: false, amperage: 120, cableIn: true, full: false)) + } + + @Test("no cable, or a full battery, is never charging from current alone") + func needsCableAndRoom() { + #expect(!BatteryMonitor.isCharging(flag: false, amperage: 2000, cableIn: false, full: false)) + #expect(!BatteryMonitor.isCharging(flag: false, amperage: 2000, cableIn: true, full: true)) + } + + @Test("the flag saying yes is kept") + func flagWins() { + #expect(BatteryMonitor.isCharging(flag: true, amperage: -500, cableIn: true, full: false)) + } +} diff --git a/scripts/plug-trace.py b/scripts/plug-trace.py new file mode 100644 index 0000000..7293ad8 --- /dev/null +++ b/scripts/plug-trace.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python3 +"""Trace a plug / unplug through every layer, to see which one lags. + +Every ~250 ms it reads: + hw the battery itself (IORegistry AppleSmartBattery): cable, charging, %, current + macos the power source macOS reports (pmset -g batt) + helper what ewiz-helper's status says (control socket) + app what the eWiz menu-bar item says (its accessibility label) +and prints a line only when something changed, with the time since the last cable +change. On each change it also grabs the menu-bar item, so the icon's colour is on +record. Ctrl-C to stop. Output: stdout and plug-trace-