Update display.go, i2c.go, and i2c.go

This commit is contained in:
Paolo Asperti 2021-01-10 19:46:47 +01:00
parent be8d733232
commit 8e21944d8e
3 changed files with 33 additions and 127 deletions

View File

@ -6,6 +6,7 @@ import (
"net" "net"
"time" "time"
"git.openpdu.org/OpenPDU/openpdu/i2c"
"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"
@ -35,7 +36,7 @@ func displayLoop() {
continue continue
} }
if i2cbus == nil { if i2c.I2Cbus == nil {
syslog.Warning("ssd1306 i2cbus not found") syslog.Warning("ssd1306 i2cbus not found")
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
continue continue
@ -50,7 +51,7 @@ func displayLoop() {
} }
// Open a handle to a ssd1306 connected on the I²C bus: // Open a handle to a ssd1306 connected on the I²C bus:
dev, err := ssd1306.NewI2C(i2cbus, &opts) dev, err := ssd1306.NewI2C(i2c.I2Cbus, &opts)
if err != nil { if err != nil {
syslog.Err(err.Error()) syslog.Err(err.Error())
} }

View File

@ -1,125 +0,0 @@
package main
import (
"errors"
"fmt"
"git.openpdu.org/OpenPDU/openpdu/syslog"
"periph.io/x/periph/conn/i2c"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/host"
)
// I2CBoard bla
type I2CBoard struct {
i2cdev i2c.Dev
channels uint
data []byte
inverted bool
}
// MyBoard bla
var MyBoard = I2CBoard{
channels: 8,
inverted: true,
}
var i2cbus i2c.Bus
func (b I2CBoard) channelStatus(ch uint) (bool, error) {
return false, nil
if b.channels <= 0 {
return false, errors.New("Board without channels")
}
if ch >= b.channels {
return false, errors.New("Invalid channel")
}
write := []byte{0x0A}
err := b.i2cdev.Tx(write, b.data)
if err != nil {
return false, err
}
byteToConsider := ch / b.channels
value := (b.data[byteToConsider] >> (ch % 8) & 1) == 1
if b.inverted {
value = !value
}
return value, nil
}
func (b I2CBoard) channelToggle(ch uint) error {
var mask byte
if b.channels <= 0 {
return errors.New("Board without channels")
}
if ch >= b.channels {
return errors.New("Invalid channel")
}
// if b.data == nil {
_, _ = b.channelStatus(ch)
// }
byteToConsider := ch / b.channels
mask = 1 << (ch % 8)
v := b.data[byteToConsider]
v ^= mask
b.data[byteToConsider] = v
write := append([]byte{0x09}, b.data...)
_, err := b.i2cdev.Write(write)
if err != nil {
return err
}
return nil
}
func init() {
var err error
syslog.Info("i2c setup")
// Make sure periph is initialized.
if _, err = host.Init(); err != nil {
syslog.Err(err.Error())
}
// Use i2creg I²C bus registry to find the first available I²C bus.
// i2cbus, err = i2creg.Open("/dev/i2c-1")
i2cbus, err = i2creg.Open("")
if err != nil {
syslog.Err(err.Error())
}
syslog.Info("i2c setup completed")
}
func initI2C() {
mydevice := &i2c.Dev{Addr: 0x27, Bus: i2cbus}
// Send a command 0x10 and expect a 5 bytes reply.
// write := []byte{0x10}
write := []byte{0x0, 0x0}
// read := make([]byte, 5)
// if err := d.Tx(write, read); err != nil {
if _, err := mydevice.Write(write); err != nil {
syslog.Err(err.Error())
}
MyBoard.i2cdev = *mydevice
MyBoard.data = make([]byte, (MyBoard.channels-1)/8+1)
go func() {
var i uint
for i = 0; i < 8; i++ {
v, err := MyBoard.channelStatus(i)
if err != nil {
syslog.Err(err.Error())
}
syslog.Info(fmt.Sprintf("Channel %d status: %v", i, v))
}
}()
}

30
src/i2c/i2c.go Normal file
View File

@ -0,0 +1,30 @@
package i2c
import (
"git.openpdu.org/OpenPDU/openpdu/syslog"
I2C "periph.io/x/periph/conn/i2c"
I2CREG "periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/host"
)
var I2Cbus I2C.Bus
func init() {
var err error
syslog.Info("i2c setup")
// Make sure periph is initialized.
if _, err = host.Init(); err != nil {
syslog.Err(err.Error())
}
// Use i2creg I²C bus registry to find the first available I²C bus.
// i2cbus, err = i2creg.Open("/dev/i2c-1")
I2Cbus, err = I2CREG.Open("")
if err != nil {
syslog.Err(err.Error())
}
syslog.Info("i2c setup completed")
}