You are reading a single comment by @tronic98776 and its replies. Click here to read the full conversation.
  • By "playing around", I was using an editor and the console in Chrome to try and get my head around callbacks. In the end I went through a number of the tutorials on w3schools and think I have a better idea of it now. I wanted to add console.logs so I could see exactly when/where the code was failing because I would need to be disconnected from the IDE to test the BLE HID sending.

    I am conscious that I am taking up your time in what may be considered my JS education rather than anything Espruino specific. I am grateful of everybody who has contributed to this thread.

    @allObjects, I tried implementing your updated code with the console.logs. (here is the entire script)

    
    var kb = require("ble_hid_keyboard");
    NRF.setServices(undefined, { hid : kb.report });
    
    // setup of you convenience hidKeyBoardObject... (here or later)
    var hidKBD = ( { string: ""
                    
    , type: function(s) { 
        this.string += s; 
        if (this.string.length == s.length) { 
                          
                          
          this._send(this.string.charAt(0)); // get first char out
      } }
    ,   _send: function(c) {
            console.log("About to send char",c);
            kb.tap(kb.KEY[c],0,this._sent.bind(this)­);
        }
    ,   _sent: function() {
            console.log("Char",this.string.charAt(0)­,"has been sent / tapped");
            this.string = this.string.substring(1);
            console.log("Left to send / tap is",this.string);
            if (this.string.length > 0) {
                console.log("Since length of this.string is",this.string.length
                           ,"and is > 0, next char 'in line' -"
                           ,this.string.charAt(0)," - has to be sent / tapped.");
                this._send(this.string.charAt(0));
            } else {
                console.log("Since nothing is left to tap / send - all is tapped");
                console.log("/ sent, (FIFO  is empty), nothing has to be done");
                console.log(" anymore and  'logical recursion' stops/ends  here.");
            }
        }
    });
    
    // setup and other code functions of yours (here or before hidKBD)
       // invoke your set up code as needed (init and getting connections)
       function startSend(){
         hidKBD.type("ABC");}
      // hidKBD.type("XYZ");
    
    	
    setWatch(startSend, BTN, {edge:"rising",repeat:true,debounce:50})­;
    
    
    
    

    I can now see that I am getting an error after the second character has been sent, here is the full console output from espruino IDE:

    BLE Connected, queueing BLE restart for later
    About to send char A
    Char A has been sent / tapped
    Left to send / tap is BC
    Since length of this.string is 2 and is > 0, next char 'in line' - B - has to be sent / tapped.
    About to send char B
    Char B has been sent / tapped
    Left to send / tap is C
    Since length of this.string is 1 and is > 0, next char 'in line' - C - has to be sent / tapped.
    About to send char C
    Uncaught Error: Got BLE error 0x3004 (NO_TX_PACKETS)
    at line 1 col 104
    ...0,0,0],function(){a&&a()})})

                              ^
    

    in function "tap" called from line 16 col 49

        kb.tap(kb.KEY[c],0,this._sent.bind(this)­);
                                                ^
    

    in function "_send" called from line 26 col 45

            this._send(this.string.charAt(0));
                                            ^
    

    in function "a" called from line 1 col 6
    a&&a()

     ^
    

    in function called from system
    ab

About

Avatar for tronic98776 @tronic98776 started