Skip to content

Commit

Permalink
fix: magsafe led may keep in the off-state
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie0129 committed Nov 12, 2024
1 parent ee66300 commit 18ce7df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
20 changes: 10 additions & 10 deletions loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions sleepcallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
29 changes: 16 additions & 13 deletions smc/magsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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
}

0 comments on commit 18ce7df

Please sign in to comment.