You are reading a single comment by @fanoush and its replies. Click here to read the full conversation.
  • 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.

About

Avatar for fanoush @fanoush started