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

View File

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