Replace load.sh with Go program
This commit is contained in:
35
lib/picoserial/picoserial.go
Normal file
35
lib/picoserial/picoserial.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package picoserial
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.bug.st/serial"
|
||||
"go.bug.st/serial/enumerator"
|
||||
)
|
||||
|
||||
func FindDevice() (string, error) {
|
||||
ports, err := enumerator.GetDetailedPortsList()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("enumerating ports: %w", err)
|
||||
}
|
||||
for _, p := range ports {
|
||||
if p.IsUSB {
|
||||
return p.Name, nil
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func SendByte(portName string, b byte) error {
|
||||
port, err := serial.Open(portName, &serial.Mode{BaudRate: 115200})
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening %s: %w", portName, err)
|
||||
}
|
||||
defer port.Close()
|
||||
|
||||
_, err = port.Write([]byte{b})
|
||||
if err != nil {
|
||||
return fmt.Errorf("writing to %s: %w", portName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
24
lib/picotool/picotool.go
Normal file
24
lib/picotool/picotool.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package picotool
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func Load(uf2Path string) error {
|
||||
cmd := exec.Command("picotool", "load", "-f", uf2Path)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("picotool load: %w\n%s", err, out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Reboot() error {
|
||||
cmd := exec.Command("picotool", "reboot")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("picotool reboot: %w\n%s", err, out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user