|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "github.com/codeskyblue/go-sh" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "runtime" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +var verbose bool |
| 17 | + |
| 18 | +func PrintlnVerbose(a ...interface{}) { |
| 19 | + if verbose { |
| 20 | + fmt.Println(a...) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func main() { |
| 25 | + fmt.Println("Starting download script...") |
| 26 | + |
| 27 | + // ARG 1: Path to binaries |
| 28 | + // ARG 2: BIN File to download |
| 29 | + // ARG 3: TTY port to use. |
| 30 | + // ARG 4: quiet/verbose |
| 31 | + // path may contain \ need to change all to / |
| 32 | + |
| 33 | + args := os.Args[1:] |
| 34 | + |
| 35 | + bin_path := args[0] |
| 36 | + dfu := bin_path + "/dfu-util" |
| 37 | + dfu = filepath.ToSlash(dfu) |
| 38 | + dfu_flags := "-d,8087:0ABA" |
| 39 | + |
| 40 | + bin_file_name := args[1] |
| 41 | + |
| 42 | + com_port := args[2] |
| 43 | + verbosity := args[3] |
| 44 | + |
| 45 | + if verbosity == "quiet" { |
| 46 | + verbose = false |
| 47 | + } else { |
| 48 | + verbose = true |
| 49 | + } |
| 50 | + |
| 51 | + PrintlnVerbose("Args to shell:", args) |
| 52 | + PrintlnVerbose("Serial Port: " + com_port) |
| 53 | + PrintlnVerbose("BIN FILE " + bin_file_name) |
| 54 | + |
| 55 | + counter := 0 |
| 56 | + board_found := false |
| 57 | + |
| 58 | + if runtime.GOOS == "darwin" { |
| 59 | + library_path := os.Getenv("DYLD_LIBRARY_PATH") |
| 60 | + if !strings.Contains(library_path, bin_path) { |
| 61 | + os.Setenv("DYLD_LIBRARY_PATH", bin_path+":"+library_path) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + for counter < 10 && board_found == false { |
| 66 | + PrintlnVerbose("Waiting for device...") |
| 67 | + out, err := sh.Command(dfu, dfu_flags, "-l").Output() |
| 68 | + if err != nil { |
| 69 | + fmt.Println(err) |
| 70 | + os.Exit(1) |
| 71 | + } |
| 72 | + if counter == 4 { |
| 73 | + fmt.Println("Flashing is taking longer than expected") |
| 74 | + fmt.Println("Try pressing MASTER_RESET button") |
| 75 | + } |
| 76 | + if strings.Contains(string(out), "sensor_core") { |
| 77 | + board_found = true |
| 78 | + PrintlnVerbose("Device found!") |
| 79 | + break |
| 80 | + } |
| 81 | + time.Sleep(1000 * time.Millisecond) |
| 82 | + counter++ |
| 83 | + } |
| 84 | + |
| 85 | + if board_found == false { |
| 86 | + fmt.Println("ERROR: Timed out waiting for Arduino 101 on " + com_port) |
| 87 | + os.Exit(1) |
| 88 | + } |
| 89 | + |
| 90 | + dfu_download := []string{dfu, dfu_flags, "-D", bin_file_name, "-v", "--alt", "7", "-R"} |
| 91 | + |
| 92 | + oscmd := exec.Command(dfu_download[0], dfu_download[1:]...) |
| 93 | + |
| 94 | + tellCommandNotToSpawnShell(oscmd) |
| 95 | + |
| 96 | + stdout, _ := oscmd.StdoutPipe() |
| 97 | + |
| 98 | + stderr, _ := oscmd.StderrPipe() |
| 99 | + |
| 100 | + multi := io.MultiReader(stderr, stdout) |
| 101 | + |
| 102 | + err := oscmd.Start() |
| 103 | + |
| 104 | + in := bufio.NewScanner(multi) |
| 105 | + |
| 106 | + in.Split(bufio.ScanLines) |
| 107 | + |
| 108 | + for in.Scan() { |
| 109 | + PrintlnVerbose(in.Text()) |
| 110 | + } |
| 111 | + |
| 112 | + err = oscmd.Wait() |
| 113 | + |
| 114 | + if err == nil { |
| 115 | + fmt.Println("SUCCESS: Sketch will execute in about 5 seconds.") |
| 116 | + os.Exit(0) |
| 117 | + } else { |
| 118 | + fmt.Println("ERROR: Upload failed on " + com_port) |
| 119 | + os.Exit(1) |
| 120 | + } |
| 121 | +} |
0 commit comments