Micro:bit BLE Keyboard

Posted on
  • Hi again!
    I'm trying to make a BLE keyboard with micro:bit to send a keystroke when it is tilted to the right, left, front, back.
    This is my code (I've not finish to write all)

    var kb = require("ble_hid_keyboard");
    NRF.setServices(undefined, { hid : kb.report });
    
    var min = -400;
    var max = 400;
    
    
      var x = Microbit.accel().x*1000;
      var y = Microbit.accel().y*1000;
      var z = Microbit.accel().z*1000;
    
    if (x > min && x < max && (y > min && y < max && z < (-800))){
      kb.tap(kb.KEY.A);
    } else {
        if (x > min && x < max && (y > min && y < max && z > 800)) {
          kb.tap(kb.KEY.F);
        } else {
            if (x > min && x < max && (z > min && z < max && y > 800)) {
                kb.tap(kb.KEY.D);
            } else {
                if (x > min && x < max && (z > min && z < max && y < (-800))) {
                    kb.tap(kb.KEY.E);
                } else {
                    if (y > min && y < max && (z > min && z < max && x > 800)) {
                        kb.tap(kb.KEY.B);
                    } else {
                        if (y > min && y < max && (z > min && z < max && x < (-800))) 
                            kb.tap(kb.KEY.C);
                    }
                }
            }
        }
    }
    

    I'm using the example of BLE Keyboard here and I receive this error

    Uncaught TypeError: Expecting a number or something iterable, got undefined
     at line 1 col 104
    ...0,0,0],function(){a&&a()})})
                                  ^
    in function "tap" called from line 16 col 18
      kb.tap(kb.KEY.A);
                     ^
    

    or this one

    >New interpreter error: UART_OVERFLOW
    >Uncaught ReferenceError: "requirerage" is not defined
     at line 2 col 1
    requirerage").write(".bootcde","85,\nPAD­_MINUS:86,PAD_PLUS:8...
    ^
    Uncaught SyntaxError: Got '}' expected ','
     at line 11 col 5
        }
        ^
    

    If I make a test with just

    print("A");
    

    instead of

    kb.tap(kb.KEY.A);
    

    I've no error and the code works as I want.

    What it can be?

  • What it can be?

    UART_OVERFLOW

    The code is probably sent too fast to the device. As a workaround you can try to check Settings->Communication->Throttle Send. Then uploading will be slower.

  • Hi fanoush.
    I tried your suggestion but UART_OVERFLOW error is still there :-(

  • Oh, then I don't know. But it is pretty strange it didn't help because when throttle is enabled it is almost unbearably slow to upload anything so not sure how it could still overflow.

    Also when thinking about it the Micro:bit 1 build it is pretty limited due to low RAM and flash memory. Can you just type NRF.sendHIDReport on left side if this method is even present? if not then the keyboard stuff won't work anyway and you would need custom build of Espruino to enable it.

    Having the micro:bit v2 would be much much better.

  • I was not precise fanoush, sorry.
    I am already using Micro:bit v2.
    The code works correctly, but often I need to flash it many times until the error doesn't occur.

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

Micro:bit BLE Keyboard

Posted by Avatar for coat @coat

Actions