From 18ce7dfbb6579b98c64d76c8be146f227c7d4be9 Mon Sep 17 00:00:00 2001 From: Charlie Chiang Date: Tue, 12 Nov 2024 14:45:41 +0800 Subject: [PATCH] fix: magsafe led may keep in the off-state --- loop.go | 20 ++++++++++---------- sleepcallback.go | 4 ++-- smc/magsafe.go | 29 ++++++++++++++++------------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/loop.go b/loop.go index 47b2cac..33e5bc6 100644 --- a/loop.go +++ b/loop.go @@ -143,20 +143,20 @@ func maintainLoopInner() bool { } func updateMagSafeLed(isChargingEnabled bool) { - ledCharging, err := smcConn.IsMagSafeCharging() + err := smcConn.SetMagSafeCharging(isChargingEnabled) if err != nil { - logrus.Errorf("IsMagSafeCharging failed: %v", err) - } - - if isChargingEnabled != ledCharging { - err := smcConn.SetMagSafeCharging(isChargingEnabled) - if err != nil { - logrus.Errorf("SetMagSafeCharging failed: %v", err) - } + logrus.Errorf("SetMagSafeCharging failed: %v", err) } } -func printStatus(batteryCharge int, lower int, upper int, isChargingEnabled bool, isPluggedIn bool, maintainedChargingInProgress bool) { +func printStatus( + batteryCharge int, + lower int, + upper int, + isChargingEnabled bool, + isPluggedIn bool, + maintainedChargingInProgress bool, +) { logrus.Debugf("batteryCharge=%d, lower=%d, upper=%d, chargingEnabled=%t, isPluggedIn=%t, maintainedChargingInProgress=%t", batteryCharge, lower, diff --git a/sleepcallback.go b/sleepcallback.go index 780b210..fada0cb 100644 --- a/sleepcallback.go +++ b/sleepcallback.go @@ -111,7 +111,7 @@ func systemWillSleepCallback() { logrus.Errorf("DisableCharging failed: %v", err) return } - err = smcConn.SetMagSafeLedState(smc.LedOff) + err = smcConn.SetMagSafeLedState(smc.LEDOff) if err != nil { logrus.Errorf("SetMagSafeLedState failed: %v", err) } @@ -142,7 +142,7 @@ func systemHasPoweredOnCallback() { sleep(postSleepLoopDelaySeconds) if config.DisableChargingPreSleep && config.ControlMagSafeLED { - err := smcConn.SetMagSafeLedState(smc.LedOff) + err := smcConn.SetMagSafeLedState(smc.LEDOff) if err != nil { logrus.Errorf("SetMagSafeLedState failed: %v", err) } diff --git a/smc/magsafe.go b/smc/magsafe.go index 40b2aa8..b30a772 100644 --- a/smc/magsafe.go +++ b/smc/magsafe.go @@ -3,15 +3,18 @@ package smc import "github.com/sirupsen/logrus" // MagSafeLedState is the state of the MagSafe LED. -type MagSafeLedState int +type MagSafeLedState uint8 // Representation of MagSafeLedState. const ( - LedOff MagSafeLedState = 1 - LedGreen MagSafeLedState = 3 - LedOrange MagSafeLedState = 4 - LedErrorOnce MagSafeLedState = 5 - LedErrorPerm MagSafeLedState = 6 + LEDSystem MagSafeLedState = 0x00 + LEDOff MagSafeLedState = 0x01 + LEDGreen MagSafeLedState = 0x03 + LEDOrange MagSafeLedState = 0x04 + LEDErrorOnce MagSafeLedState = 0x05 + LEDErrorPermSlow MagSafeLedState = 0x06 + LEDErrorPermFast MagSafeLedState = 0x07 + LEDErrorPermOff MagSafeLedState = 0x19 ) // SetMagSafeLedState . @@ -27,16 +30,16 @@ func (c *Connection) GetMagSafeLedState() (MagSafeLedState, error) { v, err := c.Read(MagSafeLedKey) if err != nil || len(v.Bytes) != 1 { - return LedOrange, err + return LEDOrange, err } rawState := MagSafeLedState(v.Bytes[0]) - ret := LedOrange + ret := LEDOrange switch rawState { - case LedOff, LedGreen, LedOrange, LedErrorOnce, LedErrorPerm: + case LEDOff, LEDGreen, LEDOrange, LEDErrorOnce, LEDErrorPermSlow: ret = rawState case 2: - ret = LedGreen + ret = LEDGreen } logrus.Tracef("GetMagSafeLedState returned %v", ret) return ret, nil @@ -50,9 +53,9 @@ func (c *Connection) CheckMagSafeExistence() bool { // SetMagSafeCharging . func (c *Connection) SetMagSafeCharging(charging bool) error { - state := LedGreen + state := LEDGreen if charging { - state = LedOrange + state = LEDOrange } return c.SetMagSafeLedState(state) } @@ -61,5 +64,5 @@ func (c *Connection) SetMagSafeCharging(charging bool) error { func (c *Connection) IsMagSafeCharging() (bool, error) { state, err := c.GetMagSafeLedState() - return state != LedGreen, err + return state == LEDOrange, err }