-
Notifications
You must be signed in to change notification settings - Fork 12
/
monitor.go
executable file
·171 lines (141 loc) · 4.28 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Whitecat Blocky Environment, serial port monitor
*
* Copyright (C) 2015 - 2016
* IBEROXARXA SERVICIOS INTEGRALES, S.L.
*
* Author: Jaume Olivé ([email protected] / [email protected])
*
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that the copyright notice and this
* permission notice and warranty disclaimer appear in supporting
* documentation, and that the name of the author not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* The author disclaim all warranties with regard to this
* software, including all implied warranties of merchantability
* and fitness. In no event shall the author be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether
* in an action of contract, negligence or other tortious action,
* arising out of or in connection with the use or performance of
* this software.
*/
package main
import "C"
import (
"github.com/mikepb/go-serial"
"log"
"strconv"
"time"
)
// Connected board
var connectedBoard *Board = nil
// This variable computes the elapsed time monitoring serial ports without success
var elapsed int = 0
func tryLater() {
time.Sleep(time.Millisecond * 10)
if connectedBoard == nil {
elapsed = elapsed + 10
if elapsed > 5000 {
// No board found in the last 5 seconds
notify("boardUpdate", "No board attached")
elapsed = 0
}
}
}
// Monitor serial ports and search for a Lua RTOS device.
// If a Lua RTOS device is found monitor the serial port.
func monitor() {
defer func() {
log.Println("stop monitor ...")
if err := recover(); err != nil {
time.Sleep(time.Millisecond * 1000)
go monitor()
}
}()
log.Println("start monitor ...")
// Notify IDE that monitor is searching for a board
notify("boardUpdate", "Scanning boards")
for {
select {
case <-IdeDetach:
return
default:
if Upgrading {
time.Sleep(time.Millisecond * 100)
continue
}
// If a board is connected thest that is still connected
if connectedBoard != nil {
_, err := connectedBoard.port.InputWaiting()
if err != nil {
// Board is not connected, inform the IDE
connectedBoard.detach()
notify("boardDetached", "")
panic(err)
} else {
// Board is connected, check again later
tryLater()
continue
}
}
// In this point any board is connected, search for a board ...
// Enumerate all serial ports
ports, err := serial.ListPorts()
if err != nil {
log.Println("can't get serial ports")
tryLater()
continue
}
// Search a serial port that matches with one of the supported adapters
skipFirst := false
for _, info := range ports {
// Read VID/PID
vendorId, productId, err := info.USBVIDPID()
if err != nil {
continue
}
// We need a VID / PID
if vendorId != 0 && productId != 0 {
vendorId := "0x" + strconv.FormatInt(int64(vendorId), 16)
productId := "0x" + strconv.FormatInt(int64(productId), 16)
log.Printf("found adapter, VID %s:%s (%s)", vendorId, productId, info.Name())
// Search a VID/PIN into requested devices
if (vendorId == "0x403") && (productId == "0x6010") {
log.Println("aaaaa")
if !skipFirst {
skipFirst = true
continue
}
}
for _, device := range devices {
if device.VendorId == vendorId && device.ProductId == productId {
// This adapter matches
log.Printf("check adapter, VID %s:%s", device.VendorId, device.ProductId)
// Create a candidate board
var candidate Board
// Attach candidate
candidate.maxBauds, _ = strconv.Atoi(device.MaxBauds)
candidate.attach(info)
if connectedBoard != nil {
break
}
}
}
if connectedBoard != nil {
break
}
}
}
if connectedBoard == nil {
tryLater()
}
}
}
}