@@ -5,15 +5,18 @@ import { get as getConfig } from "./ConfigStorage";
55import { isWeb } from "./utils/isWeb" ;
66import { usbDevices } from "./usb_devices" ;
77import { serialShim } from "./serial_shim.js" ;
8+ import { usbShim } from "./usb_shim.js" ;
89import { EventBus } from "../components/eventBus" ;
910
1011const serial = serialShim ( ) ;
12+ const usb = usbShim ( ) ;
1113
1214const DEFAULT_PORT = 'noselection' ;
1315const DEFAULT_BAUDS = 115200 ;
1416
1517const PortHandler = new function ( ) {
16- this . currentPorts = [ ] ;
18+ this . currentSerialPorts = [ ] ;
19+ this . currentUsbPorts = [ ] ;
1720 this . portPicker = {
1821 selectedPort : DEFAULT_PORT ,
1922 selectedBauds : DEFAULT_BAUDS ,
@@ -38,13 +41,10 @@ PortHandler.initialize = function () {
3841 serial . addEventListener ( "addedDevice" , ( event ) => this . addedSerialDevice ( event . detail ) ) ;
3942 serial . addEventListener ( "removedDevice" , ( event ) => this . removedSerialDevice ( event . detail ) ) ;
4043
41- if ( ! this . portAvailable ) {
42- this . check_usb_devices ( ) ;
43- }
44+ usb . addEventListener ( "addedDevice" , ( event ) => this . addedUsbDevice ( event . detail ) ) ;
4445
45- if ( ! this . dfuAvailable ) {
46- this . addedSerialDevice ( ) ;
47- }
46+ this . addedUsbDevice ( ) ;
47+ this . addedSerialDevice ( ) ;
4848} ;
4949
5050PortHandler . setShowVirtualMode = function ( showVirtualMode ) {
@@ -58,34 +58,55 @@ PortHandler.setShowManualMode = function (showManualMode) {
5858} ;
5959
6060PortHandler . addedSerialDevice = function ( device ) {
61- this . updateCurrentPortsList ( )
61+ this . updateCurrentSerialPortsList ( )
6262 . then ( ( ) => {
6363 const selectedPort = this . selectActivePort ( device ) ;
6464 if ( ! device || selectedPort === device . path ) {
6565 // Send this event when the port handler auto selects a new device
66- EventBus . $emit ( 'port-handler:auto-select-device' , selectedPort ) ;
66+ EventBus . $emit ( 'port-handler:auto-select-serial- device' , selectedPort ) ;
6767 }
6868 } ) ;
6969} ;
7070
7171PortHandler . removedSerialDevice = function ( device ) {
72- this . updateCurrentPortsList ( )
72+ this . updateCurrentSerialPortsList ( )
7373 . then ( ( ) => {
7474 if ( this . portPicker . selectedPort === device . path ) {
7575 this . selectActivePort ( ) ;
7676 }
7777 } ) ;
7878} ;
7979
80+ PortHandler . addedUsbDevice = function ( device ) {
81+ this . updateCurrentUsbPortsList ( )
82+ . then ( ( ) => {
83+ const selectedPort = this . selectActivePort ( device ) ;
84+ if ( ! device || selectedPort === device . path ) {
85+ // Send this event when the port handler auto selects a new device
86+ EventBus . $emit ( 'port-handler:auto-select-usb-device' , selectedPort ) ;
87+ }
88+ } ) ;
89+ } ;
90+
8091PortHandler . onChangeSelectedPort = function ( port ) {
8192 this . portPicker . selectedPort = port ;
8293} ;
8394
84- PortHandler . updateCurrentPortsList = function ( ) {
95+ PortHandler . updateCurrentSerialPortsList = function ( ) {
8596 return serial . getDevices ( )
8697 . then ( ( ports ) => {
87- ports = this . sortPorts ( ports ) ;
88- this . currentPorts = ports ;
98+ const orderedPorts = this . sortPorts ( ports ) ;
99+ this . portAvailable = orderedPorts . length > 0 ;
100+ this . currentSerialPorts = orderedPorts ;
101+ } ) ;
102+ } ;
103+
104+ PortHandler . updateCurrentUsbPortsList = function ( ) {
105+ return usb . getDevices ( )
106+ . then ( ( ports ) => {
107+ const orderedPorts = this . sortPorts ( ports ) ;
108+ this . dfuAvailable = orderedPorts . length > 0 ;
109+ this . currentUsbPorts = orderedPorts ;
89110 } ) ;
90111} ;
91112
@@ -110,43 +131,54 @@ PortHandler.askSerialPermissionPort = function() {
110131
111132PortHandler . selectActivePort = function ( suggestedDevice ) {
112133
113- // Return the same that is connected
134+ const deviceFilter = [ 'AT32' , 'CP210' , 'SPR' , 'STM' ] ;
135+ let selectedPort ;
136+
137+ // Return the same that is connected to serial
114138 if ( serial . connected ) {
115- return serial . getConnectedPort ( ) ;
139+ selectedPort = serial . getConnectedPort ( ) ;
116140 }
117141
118- let selectedPort ;
119- const deviceFilter = [ 'AT32' , 'CP210' , 'SPR' , 'STM' ] ;
142+ // Return the same that is connected to usb (dfu mode)
143+ if ( usb . usbDevice ) {
144+ selectedPort = usb . getConnectedPort ( ) ;
145+ }
120146
121- if ( suggestedDevice ) {
147+ // Return the suggested device (the new device that has been detected)
148+ if ( ! selectedPort && suggestedDevice ) {
122149 selectedPort = suggestedDevice . path ;
123- this . portAvailable = true ;
124- } else {
125- for ( let port of this . currentPorts ) {
126- const portName = port . displayName ;
127- const pathSelect = port . path ;
128- const deviceRecognized = deviceFilter . some ( device => portName . includes ( device ) ) ;
129- const legacyDeviceRecognized = portName . includes ( 'usb' ) ;
130- if ( deviceRecognized || legacyDeviceRecognized ) {
131- selectedPort = pathSelect ;
132- this . portAvailable = true ;
133- console . log ( `Porthandler detected device ${ portName } on port: ${ pathSelect } ` ) ;
134- break ;
135- }
150+ }
151+
152+ // Return some serial port that is recognized by the filter
153+ if ( ! selectedPort ) {
154+ selectedPort = this . currentUsbPorts . find ( device => deviceFilter . some ( filter => device . displayName . includes ( filter ) ) ) ;
155+ if ( selectedPort ) {
156+ selectedPort = selectedPort . path ;
136157 }
158+ }
137159
138- if ( ! selectedPort ) {
139- this . portAvailable = false ;
140- if ( this . showVirtualMode ) {
141- selectedPort = "virtual" ;
142- } else if ( this . showManualMode ) {
143- selectedPort = "manual" ;
144- }
160+ // Return some usb port that is recognized by the filter
161+ if ( ! selectedPort ) {
162+ selectedPort = this . currentSerialPorts . find ( device => deviceFilter . some ( filter => device . displayName . includes ( filter ) ) ) ;
163+ if ( selectedPort ) {
164+ selectedPort = selectedPort . path ;
145165 }
146166 }
147167
168+ // Return the virtual port
169+ if ( ! selectedPort && this . showVirtualMode ) {
170+ selectedPort = "virtual" ;
171+ }
172+
173+ // Return the manual port
174+ if ( ! selectedPort && this . showManualMode ) {
175+ selectedPort = "manual" ;
176+ }
177+
178+ // Return the default port if no other port was selected
148179 this . portPicker . selectedPort = selectedPort || DEFAULT_PORT ;
149- console . log ( `Porthandler default device is '${ this . portPicker . selectedPort } '` ) ;
180+
181+ console . log ( `Porthandler automatically selected device is '${ this . portPicker . selectedPort } '` ) ;
150182 return selectedPort ;
151183} ;
152184
0 commit comments