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
1 year ago
|
// package i2c wraps the [i2c package] to interact with the i2c
|
||
|
// with devices on the bus
|
||
1 year ago
|
//
|
||
1 year ago
|
// [i2c package]: https://pkg.go.dev/periph.io/x/conn/v3/i2c#pkg-overview
|
||
2 years ago
|
package i2c
|
||
2 years ago
|
|
||
|
import (
|
||
2 years ago
|
"FRMS/internal/pkg/logging"
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
_ "log"
|
||
|
"os/exec"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
2 years ago
|
|
||
1 year 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) {
|
||
1 year ago
|
|
||
2 years ago
|
bus := strconv.Itoa(b)
|
||
2 years ago
|
devices := make(map[int]bool) // only keys
|
||
1 year ago
|
|
||
2 years ago
|
cmd := exec.Command("i2cdetect", "-y", "-r", bus)
|
||
1 year ago
|
|
||
2 years ago
|
var out bytes.Buffer
|
||
|
var errs bytes.Buffer
|
||
|
cmd.Stderr = &errs
|
||
|
cmd.Stdout = &out
|
||
1 year ago
|
|
||
2 years ago
|
if err := cmd.Run(); err != nil {
|
||
1 year ago
|
|
||
|
logging.Debug(
|
||
|
logging.DError,
|
||
|
"I2C scan error %v",
|
||
|
errs.String(),
|
||
|
)
|
||
|
|
||
2 years ago
|
return devices, err
|
||
|
}
|
||
2 years ago
|
|
||
1 year ago
|
// parsing the command output
|
||
2 years ago
|
outString := out.String()
|
||
|
split := strings.SplitAfter(outString, ":")
|
||
1 year ago
|
|
||
|
// 1st entry is reserved and ending is always \n##:
|
||
2 years ago
|
split = split[1:]
|
||
1 year ago
|
|
||
2 years ago
|
// create empty slice for all the devices
|
||
|
for i, v := range split {
|
||
1 year ago
|
lastDevice := strings.Index(v, "\n")
|
||
|
|
||
|
trimmed := v[:lastDevice]
|
||
2 years ago
|
trimmed = strings.Trim(trimmed, " ")
|
||
1 year ago
|
|
||
2 years ago
|
count := strings.Split(trimmed, " ")
|
||
1 year 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
|
||
1 year ago
|
offset := j
|
||
2 years ago
|
if i == 0 {
|
||
1 year ago
|
offset += 3
|
||
2 years ago
|
}
|
||
1 year ago
|
|
||
|
addr := i*16 + offset
|
||
|
|
||
2 years ago
|
if !strings.Contains(d, "--") && !strings.Contains(d, "UU") {
|
||
|
devices[addr] = true
|
||
|
}
|
||
|
}
|
||
|
}
|
||
1 year ago
|
|
||
2 years ago
|
return devices, nil
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
1 year 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) {
|
||
1 year ago
|
|
||
2 years ago
|
var cmd *exec.Cmd
|
||
2 years ago
|
bus := strconv.Itoa(b)
|
||
1 year 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))
|
||
|
}
|
||
1 year ago
|
|
||
|
// execute command
|
||
2 years ago
|
var out bytes.Buffer
|
||
|
var errs bytes.Buffer
|
||
|
cmd.Stderr = &errs
|
||
|
cmd.Stdout = &out
|
||
1 year ago
|
|
||
2 years ago
|
if err := cmd.Run(); err != nil {
|
||
1 year ago
|
|
||
|
logging.Debug(logging.DError, "I2C command error %v", err)
|
||
|
|
||
2 years ago
|
return "", err
|
||
|
}
|
||
1 year ago
|
|
||
2 years ago
|
return out.String(), nil
|
||
2 years ago
|
}
|