|
16 | 16 | package serialutils
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "fmt" |
19 | 20 | "time"
|
20 | 21 |
|
21 | 22 | "github.com/pkg/errors"
|
22 | 23 | "go.bug.st/serial"
|
23 | 24 | )
|
24 | 25 |
|
25 |
| -// Reset a board using the 1200 bps port-touch. If wait is true, it will wait |
26 |
| -// for a new port to appear (which could change sometimes) and returns that. |
27 |
| -// The error is set if the port listing fails. |
28 |
| -func Reset(port string, wait bool) (string, error) { |
29 |
| - // Touch port at 1200bps |
30 |
| - if err := TouchSerialPortAt1200bps(port); err != nil { |
31 |
| - return "", errors.New("1200bps Touch") |
32 |
| - } |
33 |
| - |
34 |
| - if wait { |
35 |
| - // Wait for port to disappear and reappear |
36 |
| - if p, err := WaitForNewSerialPortOrDefaultTo(port); err == nil { |
37 |
| - port = p |
38 |
| - } else { |
39 |
| - return "", errors.WithMessage(err, "detecting upload port") |
40 |
| - } |
41 |
| - } |
42 |
| - |
43 |
| - return port, nil |
44 |
| -} |
45 |
| - |
46 | 26 | // TouchSerialPortAt1200bps open and close the serial port at 1200 bps. This
|
47 | 27 | // is used on many Arduino boards as a signal to put the board in "bootloader"
|
48 | 28 | // mode.
|
@@ -71,59 +51,127 @@ func TouchSerialPortAt1200bps(port string) error {
|
71 | 51 | return nil
|
72 | 52 | }
|
73 | 53 |
|
74 |
| -// WaitForNewSerialPortOrDefaultTo is meant to be called just after a reset. It watches the ports connected |
75 |
| -// to the machine until a port appears. The new appeared port is returned or, if the operation |
76 |
| -// timeouts, the default port provided as parameter is returned. |
77 |
| -func WaitForNewSerialPortOrDefaultTo(defaultPort string) (string, error) { |
78 |
| - if p, err := WaitForNewSerialPort(); err != nil { |
79 |
| - return "", errors.WithMessage(err, "detecting upload port") |
80 |
| - } else if p != "" { |
81 |
| - // on OS X, if the port is opened too quickly after it is detected, |
82 |
| - // a "Resource busy" error occurs, add a delay to workaround. |
83 |
| - // This apply to other platforms as well. |
84 |
| - time.Sleep(500 * time.Millisecond) |
85 |
| - |
86 |
| - return p, nil |
| 54 | +func getPortMap() (map[string]bool, error) { |
| 55 | + ports, err := serial.GetPortsList() |
| 56 | + if err != nil { |
| 57 | + return nil, errors.WithMessage(err, "listing serial ports") |
87 | 58 | }
|
88 |
| - return defaultPort, nil |
| 59 | + res := map[string]bool{} |
| 60 | + for _, port := range ports { |
| 61 | + res[port] = true |
| 62 | + } |
| 63 | + return res, nil |
89 | 64 | }
|
90 | 65 |
|
91 |
| -// WaitForNewSerialPort is meant to be called just after a reset. It watches the ports connected |
92 |
| -// to the machine until a port appears. The new appeared port is returned. |
93 |
| -func WaitForNewSerialPort() (string, error) { |
94 |
| - getPortMap := func() (map[string]bool, error) { |
95 |
| - ports, err := serial.GetPortsList() |
96 |
| - if err != nil { |
97 |
| - return nil, errors.WithMessage(err, "listing serial ports") |
98 |
| - } |
99 |
| - res := map[string]bool{} |
100 |
| - for _, port := range ports { |
101 |
| - res[port] = true |
102 |
| - } |
103 |
| - return res, nil |
104 |
| - } |
| 66 | +// ResetProgressCallbacks is a struct that defines a bunch of function callback |
| 67 | +// to observe the Reset function progress. |
| 68 | +type ResetProgressCallbacks struct { |
| 69 | + // TouchingPort is called to signal the 1200-bps touch of the reported port |
| 70 | + TouchingPort func(port string) |
| 71 | + // WaitingForNewSerial is called to signal that we are waiting for a new port |
| 72 | + WaitingForNewSerial func() |
| 73 | + // BootloaderPortFound is called to signal that the wait is completed and to |
| 74 | + // report the port found, or the empty string if no ports have been found and |
| 75 | + // the wait has timed-out. |
| 76 | + BootloaderPortFound func(port string) |
| 77 | + // Debug reports messages useful for debugging purposes. In normal conditions |
| 78 | + // these messages should not be displayed to the user. |
| 79 | + Debug func(msg string) |
| 80 | +} |
105 | 81 |
|
| 82 | +// Reset a board using the 1200 bps port-touch and wait for new ports. |
| 83 | +// Both reset and wait are optional: |
| 84 | +// - if port is "" touch will be skipped |
| 85 | +// - if wait is false waiting will be skipped |
| 86 | +// If wait is true, this function will wait for a new port to appear and returns that |
| 87 | +// one, otherwise the empty string is returned if the new port can not be detected or |
| 88 | +// if the wait parameter is false. |
| 89 | +// The error is set if the port listing fails. |
| 90 | +func Reset(portToTouch string, wait bool, cb *ResetProgressCallbacks) (string, error) { |
106 | 91 | last, err := getPortMap()
|
| 92 | + if cb != nil && cb.Debug != nil { |
| 93 | + cb.Debug(fmt.Sprintf("LAST: %v", last)) |
| 94 | + } |
107 | 95 | if err != nil {
|
108 | 96 | return "", err
|
109 | 97 | }
|
110 | 98 |
|
| 99 | + if portToTouch != "" && last[portToTouch] { |
| 100 | + if cb != nil && cb.Debug != nil { |
| 101 | + cb.Debug(fmt.Sprintf("TOUCH: %v", portToTouch)) |
| 102 | + } |
| 103 | + if cb != nil && cb.TouchingPort != nil { |
| 104 | + cb.TouchingPort(portToTouch) |
| 105 | + } |
| 106 | + if err := TouchSerialPortAt1200bps(portToTouch); err != nil { |
| 107 | + fmt.Println("TOUCH: error during reset:", err) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if !wait { |
| 112 | + return "", nil |
| 113 | + } |
| 114 | + if cb != nil && cb.WaitingForNewSerial != nil { |
| 115 | + cb.WaitingForNewSerial() |
| 116 | + } |
| 117 | + |
111 | 118 | deadline := time.Now().Add(10 * time.Second)
|
112 | 119 | for time.Now().Before(deadline) {
|
113 | 120 | now, err := getPortMap()
|
114 | 121 | if err != nil {
|
115 | 122 | return "", err
|
116 | 123 | }
|
117 |
| - |
| 124 | + if cb != nil && cb.Debug != nil { |
| 125 | + cb.Debug(fmt.Sprintf("WAIT: %v", now)) |
| 126 | + } |
| 127 | + hasNewPorts := false |
118 | 128 | for p := range now {
|
119 | 129 | if !last[p] {
|
120 |
| - return p, nil // Found it! |
| 130 | + hasNewPorts = true |
| 131 | + break |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if hasNewPorts { |
| 136 | + if cb != nil && cb.Debug != nil { |
| 137 | + cb.Debug("New ports found!") |
| 138 | + } |
| 139 | + |
| 140 | + // on OS X, if the port is opened too quickly after it is detected, |
| 141 | + // a "Resource busy" error occurs, add a delay to workaround. |
| 142 | + // This apply to other platforms as well. |
| 143 | + time.Sleep(time.Second) |
| 144 | + |
| 145 | + // Some boards have a glitch in the bootloader: some user experienced |
| 146 | + // the USB serial port appearing and disappearing rapidly before |
| 147 | + // settling. |
| 148 | + // This check ensure that the port is stable after one second. |
| 149 | + check, err := getPortMap() |
| 150 | + if err != nil { |
| 151 | + return "", err |
| 152 | + } |
| 153 | + if cb != nil && cb.Debug != nil { |
| 154 | + cb.Debug(fmt.Sprintf("CHECK: %v", check)) |
| 155 | + } |
| 156 | + for p := range check { |
| 157 | + if !last[p] { |
| 158 | + if cb != nil && cb.BootloaderPortFound != nil { |
| 159 | + cb.BootloaderPortFound(p) |
| 160 | + } |
| 161 | + return p, nil // Found it! |
| 162 | + } |
| 163 | + } |
| 164 | + if cb != nil && cb.Debug != nil { |
| 165 | + cb.Debug("Port check failed... still waiting") |
121 | 166 | }
|
122 | 167 | }
|
123 | 168 |
|
124 | 169 | last = now
|
125 | 170 | time.Sleep(250 * time.Millisecond)
|
126 | 171 | }
|
127 | 172 |
|
| 173 | + if cb != nil && cb.BootloaderPortFound != nil { |
| 174 | + cb.BootloaderPortFound("") |
| 175 | + } |
128 | 176 | return "", nil
|
129 | 177 | }
|
0 commit comments