• Hi!

    How could I stop Espruino to move console or force it to move to Serial6 (which does not exist on Espruino board?) when USB cable is disconnected?

    I saw the case to do it after boot by defining onInit function and call setConsole() there.
    http://forum.espruino.com/conversations/­863/#comment11883

    Is there event to watch for USB cable disconnected so that I can call Serial6.setConsole()?

    I embed Espruino into a keyboard together with Adafruit EZ-Key module. Espruino scan the keyboard and send data which key is pressed to EZ-Key via Serial1(B6, B7). EZ-Key act as a BT Keyboard for iPhone. All USART1-5 enabled ports are used for keyscan excpet B6 and B7. I do want to have console on USB for debug but I also like to use it without USB connected for real use.

  • You should be able to do LoopbackA.setConsole () on onInit - LoopbackA goes to LoopbackB and vice versa. If you've explicitly set the console then it shouldn't move when USB is plugged in, so I guess you could make a handler for USB.onData that would swap the console back to USB when you pressed a button.

    That way you could get USB comms when you plug in, and after power cycling/saving it would go back to being on Loopback...

  • Thank you very much for you advice, Gordon!

    Doing LoopbackA.setConsole() on onInit works great! Serial1 is quiet now ;-)

    you could make a handler for USB.onData that would swap the console
    back to USB when you pressed a button.

    I couldn't fully understand your suggestion.
    Do you mean

    USB.onData(function (e) { USB.setConsole(); });
    

    ?
    It seems overkill to on every character received on USB.
    Or perhaps you meant assigning USB.setConsole for a press of button?

    function onInit(){
      LoopbackA.setConsole();
      setWatch(function(){ USB.setConsole();}, BTN, true);
    }
    

    Anyway, above onInit function seem to behave as I wanted.
    (just have to remember to call onInit or LoopbackA.setConsole(); on WebIDE before unplugging USB.)

  • Glad it's sorted!

    You could maybe do something like:

    USB.onData(function (e) { 
      USB.setConsole(); 
      USB.onData(undefined);
    });
    

    So that only the first character received will call the callback... But if you're not using the button for anything, it's a nice easy way of working. I guess you could make it toggle back and forth? You can check what the current console device is using process.env.CONSOLE

  • Thanks again, Gordon!

    setWatch(function(){ 
    			if (process.env.CONSOLE != "USB"){
    				USB.setConsole();
    			}
    			else{
    				LoopbackA.setConsole();
    			}
    		}, BTN, {repeat:true, edge:"rising", debounce:10 });
    

    Taking your advice, I made it to toggle.
    But your USB.onData(undefined); solution is a lot nicer !

  • Hi Gordon

    USB.on('data',function (e) { 
            USB.setConsole(); 
            USB.on('data',undefined);
    });
    

    Is working as expected, but a warning is signaled:

    WARNING: Second argument to EventEmitter.on(..) must be a function or a String (containing code)

  • ...what about passing an 'empty' function instead of undefined?

    USB.on('data',function () {
        USB.on('data',function(){});
        USB.setConsole(); 
    });
    

    ...with setting the on-handler first and then the redirect...

  • XXX.on('yyy', ...) will add an event listener to the list of event listeners, rather than replacing the existing one like I think you intend...

    If you want to remove an event listener, just use USB.removeAllListeners('data')

  • Also... When the console is on USB (or any device for that matter), existing listeners for the data handler won't get called, so there may not be any point removing them.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Disable or move console to fake terminal when USB is disconnected

Posted by Avatar for aerialist_user6911 @aerialist_user6911

Actions