-
• #2
good question, some time ago I did not find a way, so I added it myself to E object in
jswrap_espruino.c
/*JSON{ "type" : "staticmethod", "ifdef" : "USB", "class" : "E", "name" : "isUSBConnected", "generate" : "jswrap_espruino_isUSBConnected", "return" : ["bool","device is plugged into usb port"] } Determine if device is plugged into computer USB port */ bool jswrap_espruino_isUSBConnected() { return jshIsUSBSERIALConnected(); }
it worked for me at least in STM32 build, I used it like this to start console on serial when USB was not connected 3 seconds after poweron.
if (E.isUSBConnected && (!E.isUSBConnected())){ analogWrite(LED1,0.5,{freq:5}); if (global.Serial1) setTimeout(function(){ // wait 3s and enable uart console if USB is not connected analogWrite(LED1,0.5,{freq:20}); if (!E.isUSBConnected()){ Serial1.setup(115200,{rx:B7,tx:B6}); Serial1.setConsole(); } LED1.reset(); },3000); else LED1.reset(); }
I think
E.getConsole()
can return USB also if it is not connected when it is the default in board file.So maybe something like E.isUSBConnected() could be added if it is not already there? I did not find better place than E, it could be property of USB object itself but that is instance of Serial and I don't know how to do it just for USB and not other Serial instances.
-
• #3
I think E.getConsole() can return USB also if it is not connected when it is the default in board file.
Yes, that's true, or if you force it with
USB.setConsole(1)
.So right now I don't think we have a way, except to alter the code as @fanoush has done. IMO a Serial.isConnected might be quite good. It would be handy for Bluetooth, but also for Serial1/etc it could still return true/false depending on whether they've been set up.
What board are you using @MarkBloom? Is it your own custom one?
Is there a way to check if Espruino is connected to a computer via USB besides something like that?
I'd like my program to have a different behaviour depending on that factor.