@@ -11,6 +11,7 @@ import (
11
11
//"syscall"
12
12
//"fmt"
13
13
//"bufio"
14
+ "fmt"
14
15
"io/ioutil"
15
16
"os/exec"
16
17
)
@@ -23,52 +24,50 @@ import (
23
24
// search all board.txt files for map[Product ID]
24
25
// assign it to name
25
26
26
- func removeNonArduinoBoards (ports []OsSerialPort ) []OsSerialPort {
27
- usbcmd := exec .Command ("system_profiler" , "SPUSBDataType" )
28
- grepcmd := exec .Command ("grep" , "0x2341" , "-A5" , "-B1" )
27
+ func associateVidPidWithPort (ports []OsSerialPort ) []OsSerialPort {
29
28
30
- cmdOutput , _ := pipe_commands (usbcmd , grepcmd )
29
+ // prefilter ports
30
+ ports = Filter (ports , func (port OsSerialPort ) bool {
31
+ return ! strings .Contains (port .Name , "Blue" ) && ! strings .Contains (port .Name , "/cu" )
32
+ })
31
33
32
- //log.Println(string(cmdOutput))
33
- cmdOutSlice := strings .Split ( string ( cmdOutput ) , "\n " )
34
+ for index , _ := range ports {
35
+ port_hash := strings .Trim ( ports [ index ]. Name , "/dev/tty.usbmodem " )
34
36
35
- // how many lines is the output? boards attached = lines/8
36
- for i := 0 ; i < len (cmdOutSlice )/ 8 ; i ++ {
37
+ usbcmd := exec .Command ("system_profiler" , "SPUSBDataType" )
38
+ grepcmd := exec .Command ("grep" , "Location ID: 0x" + port_hash [:len (port_hash )- 1 ], "-B6" )
39
+ cmdOutput , _ := pipe_commands (usbcmd , grepcmd )
37
40
38
- cmdOutSliceN := cmdOutSlice [i * 8 : (i + 1 )* 8 ]
41
+ if len (cmdOutput ) == 0 {
42
+ usbcmd = exec .Command ("system_profiler" , "SPUSBDataType" )
43
+ grepcmd = exec .Command ("grep" /*"Serial Number: "+*/ , strings .Trim (port_hash , "0" ), "-B3" , "-A3" )
44
+ cmdOutput , _ = pipe_commands (usbcmd , grepcmd )
39
45
40
- cmdOutMap := make (map [string ]string )
46
+ fmt .Println (string (cmdOutput ))
47
+ }
41
48
42
- for _ , element := range cmdOutSliceN {
43
- if strings .Contains (element , "ID" ) {
44
- element = strings .TrimSpace (element )
45
- arr := strings .Split (element , ": " )
46
- cmdOutMap [arr [0 ]] = arr [1 ]
47
- }
49
+ if len (cmdOutput ) == 0 {
50
+ //give up
51
+ continue
48
52
}
49
53
50
- archBoardName , boardName , _ := getBoardName ( cmdOutMap [ "Product ID" ] )
54
+ cmdOutSlice := strings . Split ( string ( cmdOutput ), " \n " )
51
55
52
- // remove initial 0x and final zeros
53
- ttyHeader := strings .Trim ((cmdOutMap ["Location ID" ]), "0x" )
54
- ttyHeader = strings .Split (ttyHeader , " " )[0 ]
55
- ttyHeader = strings .Trim (ttyHeader , "0" )
56
+ fmt .Println (cmdOutSlice )
56
57
57
- for _ , port := range ports {
58
- if strings .Contains (port .Name , ttyHeader ) {
59
- if ! strings .Contains (port .Name , "/cu" ) {
60
- port .RelatedNames = append (port .RelatedNames , archBoardName )
61
- port .FriendlyName = strings .Trim (boardName , "\n " )
62
- }
58
+ cmdOutMap := make (map [string ]string )
59
+
60
+ for _ , element := range cmdOutSlice {
61
+ if strings .Contains (element , "ID" ) || strings .Contains (element , "Manufacturer" ) {
62
+ element = strings .TrimSpace (element )
63
+ arr := strings .Split (element , ": " )
64
+ cmdOutMap [arr [0 ]] = arr [1 ]
63
65
}
64
66
}
67
+ ports [index ].IdProduct = strings .Split (cmdOutMap ["Product ID" ], " " )[0 ]
68
+ ports [index ].IdVendor = strings .Split (cmdOutMap ["Vendor ID" ], " " )[0 ]
69
+ ports [index ].Manufacturer = cmdOutMap ["Manufacturer" ]
65
70
}
66
-
67
- // additional remove phase
68
- ports = Filter (ports , func (port OsSerialPort ) bool {
69
- return ! strings .Contains (port .Name , "Blue" ) && ! strings .Contains (port .Name , "/cu" )
70
- })
71
-
72
71
return ports
73
72
}
74
73
0 commit comments