Skip to content

Commit

Permalink
Alphatec: fix enable (#10142)
Browse files Browse the repository at this point in the history
  • Loading branch information
premultiply authored Oct 26, 2023
1 parent 2f44969 commit c3ba0d8
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions charger/alphatec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package charger

// LICENSE

// Copyright (c) 2019-2022 andig
// Copyright (c) 2019-2023 andig, premultiply

// This module is NOT covered by the MIT license. All rights reserved.

Expand Down Expand Up @@ -32,13 +32,12 @@ import (

// Alphatec charger implementation
type Alphatec struct {
conn *modbus.Connection
status api.ChargeStatus
conn *modbus.Connection
curr uint16
}

const (
alphatecRegStatus = 0
alphatecRegEnable = 4
alphatecRegAmpsConfig = 5
)

Expand Down Expand Up @@ -83,70 +82,86 @@ func NewAlphatec(uri, device, comset string, baudrate int, proto modbus.Protocol
conn.Logger(log.TRACE)

wb := &Alphatec{
conn: conn,
status: api.StatusB,
conn: conn,
curr: 6,
}

// get initial state from charger
curr, err := wb.getCurrent()
if err != nil {
return nil, fmt.Errorf("current limit: %w", err)
}
if curr > 0 {
wb.curr = curr
}

return wb, err
}

func (wb *Alphatec) setCurrent(current uint16) error {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(current))

_, err := wb.conn.WriteMultipleRegisters(alphatecRegAmpsConfig, 1, b)

return err
}

func (wb *Alphatec) getCurrent() (uint16, error) {
b, err := wb.conn.ReadHoldingRegisters(alphatecRegAmpsConfig, 1)
if err != nil {
return 0, err
}

return binary.BigEndian.Uint16(b), nil
}

// Status implements the api.Charger interface
func (wb *Alphatec) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadHoldingRegisters(alphatecRegStatus, 1)
if err != nil {
return api.StatusNone, err
}

var res api.ChargeStatus
switch u := binary.BigEndian.Uint16(b); u {
case 1:
res = api.StatusA
return api.StatusA, nil
case 2:
res = api.StatusB
return api.StatusB, nil
case 3:
res = api.StatusC
case 8:
res = wb.status
if wb.status == api.StatusC {
res = api.StatusB
}
return api.StatusC, nil
default:
return api.StatusNone, fmt.Errorf("invalid status: %d", u)
}

wb.status = res

return res, nil
}

// Enabled implements the api.Charger interface
func (wb *Alphatec) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(alphatecRegEnable, 1)
if err != nil {
return false, err
}
curr, err := wb.getCurrent()

return binary.BigEndian.Uint16(b) == 0, nil
return curr >= 6, err
}

// Enable implements the api.Charger interface
func (wb *Alphatec) Enable(enable bool) error {
b := make([]byte, 2)
if !enable {
binary.BigEndian.PutUint16(b, 1)
var curr uint16
if enable {
curr = wb.curr
}

_, err := wb.conn.WriteMultipleRegisters(alphatecRegEnable, 1, b)

return err
return wb.setCurrent(curr)
}

// MaxCurrent implements the api.Charger interface
func (wb *Alphatec) MaxCurrent(current int64) error {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(current))
if current < 6 {
return fmt.Errorf("invalid current %d", current)
}

_, err := wb.conn.WriteMultipleRegisters(alphatecRegAmpsConfig, 1, b)
err := wb.setCurrent(uint16(current))
if err == nil {
wb.curr = uint16(current)
}

return err
}

0 comments on commit c3ba0d8

Please sign in to comment.