@@ -3,7 +3,6 @@ package main
3
3
import (
4
4
"bufio"
5
5
"fmt"
6
- "github.com/codeskyblue/go-sh"
7
6
"io"
8
7
"os"
9
8
"os/exec"
@@ -62,23 +61,27 @@ func main() {
62
61
}
63
62
}
64
63
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 )
68
71
if err != nil {
69
72
fmt .Println (err )
70
73
os .Exit (1 )
71
74
}
72
- if counter == 4 {
75
+ if counter == 40 {
73
76
fmt .Println ("Flashing is taking longer than expected" )
74
77
fmt .Println ("Try pressing MASTER_RESET button" )
75
78
}
76
- if strings . Contains ( string ( out ), "sensor_core" ) {
79
+ if found == true {
77
80
board_found = true
78
81
PrintlnVerbose ("Device found!" )
79
82
break
80
83
}
81
- time .Sleep (1000 * time .Millisecond )
84
+ time .Sleep (100 * time .Millisecond )
82
85
counter ++
83
86
}
84
87
@@ -88,34 +91,37 @@ func main() {
88
91
}
89
92
90
93
dfu_download := []string {dfu , dfu_flags , "-D" , bin_file_name , "-v" , "--alt" , "7" , "-R" }
94
+ err , _ := launchCommandAndWaitForOutput (dfu_download , "" , true )
91
95
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
+ }
93
104
105
+ func launchCommandAndWaitForOutput (command []string , stringToSearch string , print_output bool ) (error , bool ) {
106
+ oscmd := exec .Command (command [0 ], command [1 :]... )
94
107
tellCommandNotToSpawnShell (oscmd )
95
-
96
108
stdout , _ := oscmd .StdoutPipe ()
97
-
98
109
stderr , _ := oscmd .StderrPipe ()
99
-
100
110
multi := io .MultiReader (stderr , stdout )
101
-
102
111
err := oscmd .Start ()
103
-
104
112
in := bufio .NewScanner (multi )
105
-
106
113
in .Split (bufio .ScanLines )
107
-
114
+ found := false
108
115
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
+ }
110
124
}
111
-
112
125
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
121
127
}
0 commit comments