Checkpoint
This commit is contained in:
54 files changed
+6855
-526
No files matched your search
+30
-12
@@ -2,12 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v3"
|
||||
|
||||
"go.maze.io/harmon"
|
||||
"go.maze.io/harmon/config"
|
||||
@@ -17,14 +17,29 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
configFlag := flag.String("config", "", "configuration file path (default: auto detect)")
|
||||
debugFlag := flag.Bool("debug", false, "enable debug log messages")
|
||||
traceFlag := flag.Bool("trace", false, "enable trace log messages (very noisy!)")
|
||||
flag.Parse()
|
||||
cmd := &cli.Command{
|
||||
Name: "harmon",
|
||||
Usage: "Harmon is a playful music player frontend",
|
||||
Action: runFunc,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{Name: "config", Usage: "configuration file path"},
|
||||
&cli.BoolFlag{Name: "debug", Usage: "enable debug log messages"},
|
||||
&cli.BoolFlag{Name: "trace", Usage: "enable trace log messages (noisy!)"},
|
||||
},
|
||||
Commands: []*cli.Command{
|
||||
scanCommand,
|
||||
},
|
||||
}
|
||||
|
||||
if *traceFlag {
|
||||
if err := cmd.Run(context.Background(), os.Args); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func runFunc(ctx context.Context, cmd *cli.Command) error {
|
||||
if cmd.Bool("trace") {
|
||||
logrus.SetLevel(logrus.TraceLevel)
|
||||
} else if *debugFlag {
|
||||
} else if cmd.Bool("debug") {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
}
|
||||
|
||||
@@ -34,13 +49,14 @@ func main() {
|
||||
"host_arch": runtime.GOARCH,
|
||||
}).Info("Harmon starting")
|
||||
|
||||
if *configFlag == "" {
|
||||
if *configFlag = findConfigFile(); *configFlag == "" {
|
||||
configFile := cmd.String("config")
|
||||
if configFile == "" {
|
||||
if configFile = findConfigFile(); configFile == "" {
|
||||
logrus.Fatal("could not auto detect configuration file; supply one with -config")
|
||||
}
|
||||
}
|
||||
|
||||
config, err := config.Load(*configFlag)
|
||||
config, err := config.Load(configFile)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatal("configuration file contains errors")
|
||||
}
|
||||
@@ -52,7 +68,7 @@ func main() {
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
signals := make(chan os.Signal)
|
||||
signals := make(chan os.Signal, 1)
|
||||
signal.Notify(signals, os.Interrupt)
|
||||
signal.Notify(signals, os.Kill)
|
||||
go func(signals <-chan os.Signal, cancel func()) {
|
||||
@@ -62,6 +78,8 @@ func main() {
|
||||
}(signals, cancel)
|
||||
|
||||
if err = controller.Run(ctx); err != nil && err != context.Canceled {
|
||||
logrus.WithError(err).Fatal("terminating by fatal error")
|
||||
//logrus.WithError(err).Fatal("terminating by fatal error")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/go-daq/smbus"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
var scanCommand = &cli.Command{
|
||||
Name: "scan",
|
||||
Category: "i2c",
|
||||
Usage: "Scan the I2C bus for active devices",
|
||||
Action: scanFunc,
|
||||
Flags: []cli.Flag{
|
||||
&cli.IntFlag{Name: "bus", Usage: "I²C bus to use", Value: 1},
|
||||
&cli.IntFlag{Name: "first", Usage: "First address", Value: 0x03},
|
||||
&cli.IntFlag{Name: "last", Usage: "Last address", Value: 0x77},
|
||||
&cli.BoolFlag{Name: "error", Usage: "Print errors"},
|
||||
},
|
||||
}
|
||||
|
||||
func scanFunc(ctx context.Context, cmd *cli.Command) error {
|
||||
bus, err := smbus.OpenFile(int(cmd.Int("bus")))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer bus.Close()
|
||||
|
||||
var (
|
||||
first = int(cmd.Int("first"))
|
||||
last = int(cmd.Int("last"))
|
||||
errs []error
|
||||
)
|
||||
|
||||
fmt.Println(" 0 1 2 3 4 5 6 7 8 9 a b c d e f")
|
||||
for i := 0; i < 128; i += 16 {
|
||||
fmt.Printf("%02x: ", i)
|
||||
for j := 0; j < 16; j++ {
|
||||
if i+j < first || i+j > last {
|
||||
fmt.Print(" ")
|
||||
continue
|
||||
}
|
||||
|
||||
if err := bus.SetAddr(uint8(i + j)); err != nil {
|
||||
errs = append(errs, err)
|
||||
fmt.Print("UU ")
|
||||
continue
|
||||
}
|
||||
|
||||
useRead := ((i+j >= 0x30 && i+j <= 0x37) || (i+j >= 0x50 && i+j <= 0x5F))
|
||||
if useRead || true {
|
||||
_, err = bus.ReadReg(uint8(i+j), 0)
|
||||
}
|
||||
if err == nil {
|
||||
fmt.Printf("%02x ", i+j)
|
||||
errs = append(errs, nil)
|
||||
} else {
|
||||
fmt.Printf("-- ")
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
if cmd.Bool("error") {
|
||||
for i := first; i < last; i++ {
|
||||
if err := errs[i-first]; err != nil {
|
||||
fmt.Printf("%02x: %v\n", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bus.Close()
|
||||
}
|
||||
|
||||
func ioctl(fd, cmd, arg uintptr) (err error) {
|
||||
_, _, e1 := syscall.Syscall6(syscall.SYS_IOCTL, fd, cmd, arg, 0, 0, 0)
|
||||
if e1 != 0 {
|
||||
err = e1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type i2cCmd struct {
|
||||
rw uint8
|
||||
cmd uint8
|
||||
len uint32
|
||||
ptr unsafe.Pointer
|
||||
}
|
||||
Reference in new issue
Block a user