Thank you @allObjects. I have this working now. Initially it had the same error, but after increasing the setTimeout from 1 to 50, it worked in the console. However, it would fail if anything was added to FIFO string whilst a send was in progress, but increasing the setTimeout to 100 has cured this.
To get it to type the string over bluetooth I had to get rid of all the console.logs. Here is the code I have working now:
function onInit() {
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);
setTimeout(function(_){
kb.tap(kb.KEY[c],0,_._sent.bind(_)); },100,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("ABCCANNONBALL");}
// hidKBD.type("XYZ");
setWatch(startSend, BTN, {edge:"rising",repeat:true,debounce:50});
}
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Thank you @allObjects. I have this working now. Initially it had the same error, but after increasing the setTimeout from 1 to 50, it worked in the console. However, it would fail if anything was added to FIFO string whilst a send was in progress, but increasing the setTimeout to 100 has cured this.
To get it to type the string over bluetooth I had to get rid of all the console.logs. Here is the code I have working now: