You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.4 KiB
Go
115 lines
2.4 KiB
Go
2 years ago
|
// package i2c wraps the [i2c package] to interact with the i2c
|
||
|
// with devices on the bus
|
||
2 years ago
|
//
|
||
2 years ago
|
// [i2c package]: https://pkg.go.dev/periph.io/x/conn/v3/i2c#pkg-overview
|
||
2 years ago
|
package i2c
|
||
3 years ago
|
|
||
|
import (
|
||
2 years ago
|
"FRMS/internal/pkg/logging"
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
_ "log"
|
||
|
"os/exec"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
3 years ago
|
|
||
2 years ago
|
// GetConnected returns a map of each device address and its current
|
||
|
// connection status.
|
||
2 years ago
|
func GetConnected(b int) (map[int]bool, error) {
|
||
2 years ago
|
|
||
2 years ago
|
bus := strconv.Itoa(b)
|
||
2 years ago
|
devices := make(map[int]bool) // only keys
|
||
2 years ago
|
|
||
2 years ago
|
cmd := exec.Command("i2cdetect", "-y", "-r", bus)
|
||
2 years ago
|
|
||
2 years ago
|
var out bytes.Buffer
|
||
|
var errs bytes.Buffer
|
||
|
cmd.Stderr = &errs
|
||
|
cmd.Stdout = &out
|
||
2 years ago
|
|
||
2 years ago
|
if err := cmd.Run(); err != nil {
|
||
2 years ago
|
|
||
|
logging.Debug(
|
||
|
logging.DError,
|
||
|
"I2C scan error %v",
|
||
|
errs.String(),
|
||
|
)
|
||
|
|
||
2 years ago
|
return devices, err
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
// parsing the command output
|
||
2 years ago
|
outString := out.String()
|
||
|
split := strings.SplitAfter(outString, ":")
|
||
2 years ago
|
|
||
|
// 1st entry is reserved and ending is always \n##:
|
||
2 years ago
|
split = split[1:]
|
||
2 years ago
|
|
||
2 years ago
|
// create empty slice for all the devices
|
||
|
for i, v := range split {
|
||
2 years ago
|
lastDevice := strings.Index(v, "\n")
|
||
|
|
||
|
trimmed := v[:lastDevice]
|
||
2 years ago
|
trimmed = strings.Trim(trimmed, " ")
|
||
2 years ago
|
|
||
2 years ago
|
count := strings.Split(trimmed, " ")
|
||
2 years ago
|
|
||
2 years ago
|
for j, d := range count {
|
||
|
// the first row has to be offset by 3 but after its just i*16 + j
|
||
2 years ago
|
offset := j
|
||
2 years ago
|
if i == 0 {
|
||
2 years ago
|
offset += 3
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
addr := i*16 + offset
|
||
|
|
||
2 years ago
|
if !strings.Contains(d, "--") && !strings.Contains(d, "UU") {
|
||
|
devices[addr] = true
|
||
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return devices, nil
|
||
3 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
// SendCmd sends an arbitrary command string to the device at addr on i2c bus b.
|
||
|
// Command will be converted from a string to bytes before
|
||
|
// attempting to be sent.
|
||
2 years ago
|
func SendCmd(b, addr int, command string) (string, error) {
|
||
2 years ago
|
|
||
2 years ago
|
var cmd *exec.Cmd
|
||
2 years ago
|
bus := strconv.Itoa(b)
|
||
2 years ago
|
// default to an empty read
|
||
|
operation := "r20"
|
||
|
frmt_cmd := ""
|
||
2 years ago
|
if command != "" {
|
||
|
// command, do write
|
||
|
operation = fmt.Sprintf("w%d", len(command)) // write
|
||
|
// formatting cmd
|
||
|
for _, char := range command {
|
||
|
// loop over string
|
||
|
frmt_cmd += fmt.Sprintf("0x%x", char)
|
||
2 years ago
|
}
|
||
2 years ago
|
cmd = exec.Command("i2ctransfer", "-y", bus, fmt.Sprintf("%s@0x%x", operation, addr), frmt_cmd)
|
||
|
} else {
|
||
|
// reading
|
||
|
cmd = exec.Command("i2ctransfer", "-y", bus, fmt.Sprintf("%s@0x%x", operation, addr))
|
||
|
}
|
||
2 years ago
|
|
||
|
// execute command
|
||
2 years ago
|
var out bytes.Buffer
|
||
|
var errs bytes.Buffer
|
||
|
cmd.Stderr = &errs
|
||
|
cmd.Stdout = &out
|
||
2 years ago
|
|
||
2 years ago
|
if err := cmd.Run(); err != nil {
|
||
2 years ago
|
|
||
|
logging.Debug(logging.DError, "I2C command error %v", err)
|
||
|
|
||
2 years ago
|
return "", err
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return out.String(), nil
|
||
2 years ago
|
}
|