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
}
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()
}

View File

@ -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, dispOutletStatusString())
return dev.Draw(img.Bounds(), img, image.Point{})
}

View File

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