i2c display outlet status

This commit is contained in:
Paolo Asperti 2021-01-19 10:39:35 +01:00
parent f9813a0c3f
commit 0270cc3557
3 changed files with 24 additions and 26 deletions

View File

@ -118,9 +118,7 @@ func (c *I2CChannel) setToggle() {
data = &c.parent.i2cDataB data = &c.parent.i2cDataB
} }
mask = 1 << (c.Num % 8) mask = 1 << (c.Num % 8)
log.Printf("toggle1: %v \n", c.parent.i2cDataA[0])
(*data)[0] ^= mask (*data)[0] ^= mask
log.Printf("toggle2: %v \n", c.parent.i2cDataA[0])
} }
func (c *I2CChannel) On() error { func (c *I2CChannel) On() error {
@ -195,7 +193,6 @@ func (c *I2CChannel) Status() bool {
data = &c.parent.i2cDataB data = &c.parent.i2cDataB
} }
value := ((*data)[0] >> (c.Num % 8) & 1) == 1 value := ((*data)[0] >> (c.Num % 8) & 1) == 1
log.Printf("status ch %v: %v", c.Num, value)
return value return value
} }
@ -228,12 +225,10 @@ func (b *I2CBoard) ReadStatus() {
b.i2cDevice.Tx([]byte{0x12}, replyA[:]) b.i2cDevice.Tx([]byte{0x12}, replyA[:])
b.i2cDevice.Tx([]byte{0x13}, replyB[:]) b.i2cDevice.Tx([]byte{0x13}, replyB[:])
log.Printf("read1: %v \n", replyA)
if b.inverted { if b.inverted {
replyA[0] ^= 0xFF replyA[0] ^= 0xFF
replyB[0] ^= 0xFF replyB[0] ^= 0xFF
} }
log.Printf("read2: %v \n", replyA)
b.i2cDataA[0] = replyA[0] b.i2cDataA[0] = replyA[0]
b.i2cDataB[0] = replyB[0] b.i2cDataB[0] = replyB[0]
@ -249,19 +244,11 @@ func (b *I2CBoard) WriteStatus() {
dA[1] = b.i2cDataA[0] dA[1] = b.i2cDataA[0]
log.Printf("write1: %v \n", dA)
dB[1] = b.i2cDataB[0] dB[1] = b.i2cDataB[0]
if b.inverted { if b.inverted {
dA[1] ^= 0xff dA[1] ^= 0xff
dB[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 { if _, err := b.i2cDevice.Write(dA); err != nil {
syslog.Err(err.Error()) syslog.Err(err.Error())
@ -270,9 +257,6 @@ func (b *I2CBoard) WriteStatus() {
syslog.Err(err.Error()) syslog.Err(err.Error())
} }
log.Printf("write3: %v \n", dA)
// log.Printf("write3: %v - %v\n", dA, oA)
b.lastUpdate = time.Now() b.lastUpdate = time.Now()
} }

View File

@ -1,12 +1,14 @@
package main package main
import ( import (
"fmt"
"image" "image"
"image/color" "image/color"
"net" "net"
"time" "time"
"git.openpdu.org/OpenPDU/openpdu/i2c" "git.openpdu.org/OpenPDU/openpdu/i2c"
"git.openpdu.org/OpenPDU/openpdu/outlet"
"git.openpdu.org/OpenPDU/openpdu/syslog" "git.openpdu.org/OpenPDU/openpdu/syslog"
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.org/x/image/font" "golang.org/x/image/font"
@ -22,8 +24,7 @@ func init() {
viper.SetDefault("Display.H", 32) viper.SetDefault("Display.H", 32)
viper.SetDefault("Display.Rotated", false) viper.SetDefault("Display.Rotated", false)
viper.SetDefault("Display.SwapTopBottom", false) viper.SetDefault("Display.SwapTopBottom", false)
go displayLoop() viper.SetDefault("Display.networkinterface", "eth0")
syslog.Info("display setup completed")
} }
func displayLoop() { func displayLoop() {
@ -113,19 +114,31 @@ func getIPs() map[string]string {
return out 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)) img := image.NewRGBA(image.Rect(0, 0, 128, 32))
iface := viper.GetString("Display.NetworkInterface")
ips := getIPs() ips := getIPs()
ip, found := ips[iface]
if !found {
ip = "unknown"
}
lineHeight := 12 lineHeight := 12
posX := 12 posX := 12
posY := lineHeight posY := lineHeight
// for name, ip := range ips { addLabel(img, posX, posY, ip)
for _, ip := range ips { posY += lineHeight
// addLabel(img, posX, posY, name) addLabel(img, posX, posY, dispOutletStatusString())
// posY += lineHeight
addLabel(img, posX, posY, ip)
posY += lineHeight
}
return dev.Draw(img.Bounds(), img, image.Point{}) return dev.Draw(img.Bounds(), img, image.Point{})
} }

View File

@ -18,6 +18,7 @@ func main() {
MQTTSetup() MQTTSetup()
go MQTTRefreshLoop() go MQTTRefreshLoop()
go ups.UpsConnect() go ups.UpsConnect()
go displayLoop()
syslog.Info("hostname: " + viper.GetString("system.hostname")) syslog.Info("hostname: " + viper.GetString("system.hostname"))
webui.StartServer() webui.StartServer()
} }