Skip to content

Commit 22dd30a

Browse files
committed
Initial commit
0 parents  commit 22dd30a

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

main.go

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

util_unix.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import (
4+
"os/exec"
5+
)
6+
7+
func tellCommandNotToSpawnShell(_ *exec.Cmd) {
8+
}

util_windows.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import (
4+
"os/exec"
5+
"syscall"
6+
)
7+
8+
func tellCommandNotToSpawnShell(oscmd *exec.Cmd) {
9+
oscmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
10+
}

0 commit comments

Comments
 (0)