From 0270cc35572a505e05032e0383e535001bd5702b Mon Sep 17 00:00:00 2001 From: Paolo Asperti Date: Tue, 19 Jan 2021 10:39:35 +0100 Subject: [PATCH] i2c display outlet status --- src/board/board_i2c.go | 16 ---------------- src/display.go | 33 +++++++++++++++++++++++---------- src/main.go | 1 + 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/board/board_i2c.go b/src/board/board_i2c.go index bfca622..5c43fe2 100644 --- a/src/board/board_i2c.go +++ b/src/board/board_i2c.go @@ -118,9 +118,7 @@ func (c *I2CChannel) setToggle() { data = &c.parent.i2cDataB } mask = 1 << (c.Num % 8) - log.Printf("toggle1: %v \n", c.parent.i2cDataA[0]) (*data)[0] ^= mask - log.Printf("toggle2: %v \n", c.parent.i2cDataA[0]) } func (c *I2CChannel) On() error { @@ -195,7 +193,6 @@ func (c *I2CChannel) Status() bool { data = &c.parent.i2cDataB } value := ((*data)[0] >> (c.Num % 8) & 1) == 1 - log.Printf("status ch %v: %v", c.Num, value) return value } @@ -228,12 +225,10 @@ func (b *I2CBoard) ReadStatus() { b.i2cDevice.Tx([]byte{0x12}, replyA[:]) b.i2cDevice.Tx([]byte{0x13}, replyB[:]) - log.Printf("read1: %v \n", replyA) if b.inverted { replyA[0] ^= 0xFF replyB[0] ^= 0xFF } - log.Printf("read2: %v \n", replyA) b.i2cDataA[0] = replyA[0] b.i2cDataB[0] = replyB[0] @@ -249,19 +244,11 @@ func (b *I2CBoard) WriteStatus() { dA[1] = b.i2cDataA[0] - log.Printf("write1: %v \n", dA) - dB[1] = b.i2cDataB[0] if b.inverted { dA[1] ^= 0xff dB[1] ^= 0xff } - // dA = append([]byte{0x12}, dA...) - log.Printf("write2: %v \n", dA) - // dB = append([]byte{0x13}, dB...) - - // b.i2cDevice.Tx(dA, oA) - // b.i2cDevice.Tx(dB, oB) if _, err := b.i2cDevice.Write(dA); err != nil { syslog.Err(err.Error()) @@ -270,9 +257,6 @@ func (b *I2CBoard) WriteStatus() { syslog.Err(err.Error()) } - log.Printf("write3: %v \n", dA) - // log.Printf("write3: %v - %v\n", dA, oA) - b.lastUpdate = time.Now() } diff --git a/src/display.go b/src/display.go index ce7d78f..c9408fd 100644 --- a/src/display.go +++ b/src/display.go @@ -1,12 +1,14 @@ package main import ( + "fmt" "image" "image/color" "net" "time" "git.openpdu.org/OpenPDU/openpdu/i2c" + "git.openpdu.org/OpenPDU/openpdu/outlet" "git.openpdu.org/OpenPDU/openpdu/syslog" "github.com/spf13/viper" "golang.org/x/image/font" @@ -22,8 +24,7 @@ func init() { viper.SetDefault("Display.H", 32) viper.SetDefault("Display.Rotated", false) viper.SetDefault("Display.SwapTopBottom", false) - go displayLoop() - syslog.Info("display setup completed") + viper.SetDefault("Display.networkinterface", "eth0") } func displayLoop() { @@ -113,19 +114,31 @@ func getIPs() map[string]string { return out } -func disp(dev *ssd1306.Dev) error { +func dispOutletStatusString() string { + out := "" + for o := range outlet.Outlets { + if outlet.Outlets[o].Channel.Status() { + out += fmt.Sprint(outlet.Outlets[o].Num) + } else { + out += "." + } + } + return out +} +func disp(dev *ssd1306.Dev) error { img := image.NewRGBA(image.Rect(0, 0, 128, 32)) + iface := viper.GetString("Display.NetworkInterface") ips := getIPs() + ip, found := ips[iface] + if !found { + ip = "unknown" + } lineHeight := 12 posX := 12 posY := lineHeight - // for name, ip := range ips { - for _, ip := range ips { - // addLabel(img, posX, posY, name) - // posY += lineHeight - addLabel(img, posX, posY, ip) - posY += lineHeight - } + addLabel(img, posX, posY, ip) + posY += lineHeight + addLabel(img, posX, posY, dispOutletStatusString()) return dev.Draw(img.Bounds(), img, image.Point{}) } diff --git a/src/main.go b/src/main.go index 4b02eb0..f7c985d 100644 --- a/src/main.go +++ b/src/main.go @@ -18,6 +18,7 @@ func main() { MQTTSetup() go MQTTRefreshLoop() go ups.UpsConnect() + go displayLoop() syslog.Info("hostname: " + viper.GetString("system.hostname")) webui.StartServer() }