Replace load.sh with Go program

This commit is contained in:
Ian Gulliver
2026-04-03 13:25:31 +09:00
parent 3a9dc78a5b
commit b076cce34a
6 changed files with 143 additions and 22 deletions

60
cmd/load/main.go Normal file
View File

@@ -0,0 +1,60 @@
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/theater/picomap/lib/picoserial"
"github.com/theater/picomap/lib/picotool"
)
func main() {
wd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
buildDir := filepath.Join(wd, "build")
if err := run(buildDir); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run(buildDir string) error {
fmt.Println("Building...")
cmd := exec.Command("make", "-C", buildDir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("build failed: %w", err)
}
dev, err := picoserial.FindDevice()
if err != nil {
return err
}
if dev != "" {
fmt.Printf("Sending 'b' to %s to enter BOOTSEL mode...\n", dev)
if err := picoserial.SendByte(dev, 'b'); err != nil {
return err
}
time.Sleep(2 * time.Second)
}
uf2 := filepath.Join(buildDir, "picomap.uf2")
fmt.Println("Loading firmware...")
if err := picotool.Load(uf2); err != nil {
return err
}
fmt.Println("Rebooting...")
_ = picotool.Reboot()
fmt.Println("Done.")
return nil
}