Skip to content

Commit 187af3d

Browse files
committed
drop all dependencies, slightly rework flow
1 parent d0cfe29 commit 187af3d

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

main.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"bufio"
55
"fmt"
6-
"github.com/codeskyblue/go-sh"
76
"io"
87
"os"
98
"os/exec"
@@ -62,23 +61,27 @@ func main() {
6261
}
6362
}
6463

65-
for counter < 10 && board_found == false {
66-
PrintlnVerbose("Waiting for device...")
67-
out, err := sh.Command(dfu, dfu_flags, "-l").Output()
64+
dfu_search_command := []string{dfu, dfu_flags, "-l"}
65+
66+
for counter < 100 && board_found == false {
67+
if counter%10 == 0 {
68+
PrintlnVerbose("Waiting for device...")
69+
}
70+
err, found := launchCommandAndWaitForOutput(dfu_search_command, "sensor_core", false)
6871
if err != nil {
6972
fmt.Println(err)
7073
os.Exit(1)
7174
}
72-
if counter == 4 {
75+
if counter == 40 {
7376
fmt.Println("Flashing is taking longer than expected")
7477
fmt.Println("Try pressing MASTER_RESET button")
7578
}
76-
if strings.Contains(string(out), "sensor_core") {
79+
if found == true {
7780
board_found = true
7881
PrintlnVerbose("Device found!")
7982
break
8083
}
81-
time.Sleep(1000 * time.Millisecond)
84+
time.Sleep(100 * time.Millisecond)
8285
counter++
8386
}
8487

@@ -88,34 +91,37 @@ func main() {
8891
}
8992

9093
dfu_download := []string{dfu, dfu_flags, "-D", bin_file_name, "-v", "--alt", "7", "-R"}
94+
err, _ := launchCommandAndWaitForOutput(dfu_download, "", true)
9195

92-
oscmd := exec.Command(dfu_download[0], dfu_download[1:]...)
96+
if err == nil {
97+
fmt.Println("SUCCESS: Sketch will execute in about 5 seconds.")
98+
os.Exit(0)
99+
} else {
100+
fmt.Println("ERROR: Upload failed on " + com_port)
101+
os.Exit(1)
102+
}
103+
}
93104

105+
func launchCommandAndWaitForOutput(command []string, stringToSearch string, print_output bool) (error, bool) {
106+
oscmd := exec.Command(command[0], command[1:]...)
94107
tellCommandNotToSpawnShell(oscmd)
95-
96108
stdout, _ := oscmd.StdoutPipe()
97-
98109
stderr, _ := oscmd.StderrPipe()
99-
100110
multi := io.MultiReader(stderr, stdout)
101-
102111
err := oscmd.Start()
103-
104112
in := bufio.NewScanner(multi)
105-
106113
in.Split(bufio.ScanLines)
107-
114+
found := false
108115
for in.Scan() {
109-
PrintlnVerbose(in.Text())
116+
if print_output {
117+
PrintlnVerbose(in.Text())
118+
}
119+
if stringToSearch != "" {
120+
if strings.Contains(in.Text(), stringToSearch) {
121+
found = true
122+
}
123+
}
110124
}
111-
112125
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-
}
126+
return err, found
121127
}

0 commit comments

Comments
 (0)