Update led handling to a map

This commit is contained in:
Nicholas Thompson
2019-02-26 23:24:41 +02:00
committed by ncthompson
parent eb4398d5b4
commit f189ff0442
2 changed files with 41 additions and 27 deletions

View File

@@ -293,9 +293,8 @@ func (mk *mk2Ser) stateDecode(frame []byte) {
// Decode the LED state frame.
func (mk *mk2Ser) ledDecode(frame []byte) {
mk.info.LedListOn = getLeds(frame[0])
mk.info.LedListBlink = getLeds(frame[1])
mk.info.LEDs = getLEDs(frame[0], frame[1])
// Send charge state request
cmd := make([]byte, 4)
cmd[0] = 0x57 //W
@@ -305,12 +304,18 @@ func (mk *mk2Ser) ledDecode(frame []byte) {
}
// Adds active LEDs to list.
func getLeds(led byte) []int {
leds := make([]int, 0)
func getLEDs(ledsOn, ledsBlink byte) map[Led]LEDstate {
leds := map[Led]LEDstate{}
for i := 0; i < 8; i++ {
t := (led >> uint(i)) & 1
if t == 1 {
leds = append(leds, int(1<<uint(i)))
on := (ledsOn >> uint(i)) & 1
blink := (ledsBlink >> uint(i)) & 1
if on == 1 {
leds[Led(i)] = LedOn
} else if blink == 1 {
leds[Led(i)] = LedBlink
} else {
leds[Led(i)] = LedOff
}
}
return leds

View File

@@ -2,28 +2,38 @@ package mk2if
import "time"
type Led int
const (
LED_TEMPERATURE = 128
LED_LOW_BATTERY = 64
LED_OVERLOAD = 32
LED_INVERTER = 16
LED_FLOAT = 8
LED_BULK = 4
LED_ABSORPTION = 2
LED_MAIN = 1
LedMain Led = iota
LedAbsorption
LedBulk
LedFloat
LedInverter
LedOverload
LedLowBattery
LedTemperature
)
var LedNames = map[int]string{
LED_TEMPERATURE: "Temperature",
LED_LOW_BATTERY: "Low Battery",
LED_OVERLOAD: "Overload",
LED_INVERTER: "Inverter",
LED_FLOAT: "Float",
LED_BULK: "Bulk",
LED_ABSORPTION: "Absorbtion",
LED_MAIN: "Mains",
var LedNames = map[Led]string{
LedTemperature: "led_over_temp",
LedLowBattery: "led_bat_low",
LedOverload: "led_overload",
LedInverter: "led_inverter",
LedFloat: "led_float",
LedBulk: "led_bulk",
LedAbsorption: "led_absorb",
LedMain: "led_mains",
}
type LEDstate int
const (
LedOff LEDstate = iota
LedOn
LedBlink
)
type Mk2Info struct {
// Will be marked as false if an error is detected.
Valid bool
@@ -48,9 +58,8 @@ type Mk2Info struct {
// Charge state 0.0 to 1.0
ChargeState float64
// List only active LEDs
LedListOn []int
LedListBlink []int
// List LEDs
LEDs map[Led]LEDstate
Errors []error