|
|
|
@ -3,8 +3,9 @@ package I2C
|
|
|
|
|
// file has general wrappers to interact with i2c-tools
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
_ "fmt"
|
|
|
|
|
"fmt"
|
|
|
|
|
_ "log"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"bytes"
|
|
|
|
|
"strings"
|
|
|
|
@ -37,7 +38,7 @@ func (b *I2CBus) Scan() map[int]bool {
|
|
|
|
|
cmd.Stderr = &errs
|
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
|
logging.Debug(logging.DError, "I2C ERROR: %v", errs.String())
|
|
|
|
|
logging.Debug(logging.DError, "I2C error performing scan. %v", errs.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outString := out.String()
|
|
|
|
@ -82,9 +83,11 @@ func (b *I2CBus) GetStatus(addr int) bool {
|
|
|
|
|
a := strconv.Itoa(addr)
|
|
|
|
|
cmd := exec.Command("i2cdetect","-y","-r",bus,a,a)
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
var errs bytes.Buffer
|
|
|
|
|
cmd.Stderr = &errs
|
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
|
logging.Debug(logging.DError,"I2C ERROR: %v", err)
|
|
|
|
|
logging.Debug(logging.DError,"I2C error getting status! %v", errs.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outString := out.String()
|
|
|
|
@ -101,3 +104,35 @@ func (b *I2CBus) GetStatus(addr int) bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *I2CBus) GetData(addr int) string {
|
|
|
|
|
b.Lock()
|
|
|
|
|
defer b.Unlock()
|
|
|
|
|
|
|
|
|
|
bus := strconv.Itoa(b.int)
|
|
|
|
|
a := strconv.FormatInt(int64(addr),16)
|
|
|
|
|
cmd := exec.Command("i2ctransfer","-y",bus,fmt.Sprintf("r40@0x%s",a))
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
var errs bytes.Buffer
|
|
|
|
|
cmd.Stderr = &errs
|
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
|
logging.Debug(logging.DError,"I2C error getting data! %v", errs.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outString := out.String()
|
|
|
|
|
split := strings.SplitAfter(outString," ") //getting chars 0x12 0x2f etc
|
|
|
|
|
var final string
|
|
|
|
|
for _,v := range split {
|
|
|
|
|
trimmed := strings.TrimLeft(v, "0x ") // trimming extra bs in front of num
|
|
|
|
|
trimmed = strings.TrimRight(trimmed," \n") // trimming back
|
|
|
|
|
if trimmed != "ff" {
|
|
|
|
|
final += trimmed
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ret, err := hex.DecodeString(final)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return string(ret)
|
|
|
|
|
}
|
|
|
|
|