2026-04-03 13:25:31 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/theater/picomap/lib/picoserial"
|
|
|
|
|
"github.com/theater/picomap/lib/picotool"
|
2026-04-03 16:59:11 +09:00
|
|
|
"github.com/theater/picomap/lib/wire"
|
2026-04-03 13:25:31 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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 != "" {
|
2026-04-03 17:32:14 +09:00
|
|
|
fmt.Printf("Sending bootsel request to %s...\n", dev)
|
|
|
|
|
req, err := wire.EncodeMessage(&wire.RequestBOOTSEL{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("encoding request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
resp, err := picoserial.SendAndReceive(dev, req, 2*time.Second)
|
2026-04-03 16:59:11 +09:00
|
|
|
if err != nil {
|
2026-04-03 13:25:31 +09:00
|
|
|
return err
|
|
|
|
|
}
|
2026-04-03 16:59:11 +09:00
|
|
|
if len(resp) > 0 {
|
|
|
|
|
msg, err := wire.DecodeMessage(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "warning: failed to decode response: %v\n", err)
|
|
|
|
|
} else {
|
|
|
|
|
switch msg.(type) {
|
2026-04-03 17:32:14 +09:00
|
|
|
case *wire.ResponseBOOTSEL:
|
2026-04-03 16:59:11 +09:00
|
|
|
fmt.Println("Device confirmed reboot into BOOTSEL mode.")
|
|
|
|
|
default:
|
|
|
|
|
fmt.Printf("Unexpected response type: %T\n", msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-03 13:25:31 +09:00
|
|
|
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
|
|
|
|
|
}
|