Using Puck as a single button keyboard

Posted on
  • I'm trying to setup my puck as a single button keyboard to send a character (or two) when the button is pressed, I'm not having much luck and struggling with how to debug it, as I understand it I have to disconnect the web bluetooth IDE before the puck can connect as a keyboard?
    I've flashed the code below to the puck then disconnect and press the button, the BLUE LED comes on for 10sec then the red one flashes as I would expect indicating its failed, I've tried scanning for new bluetooth devices (keyboards) from my Mac and iPhone after I disconnect but nothing is found.

    var kb = require("ble_hid_keyboard");
    NRF.setServices(undefined, { hid : kb.report });
    var reset_timer;
    
    function sendKB(){
      LED3.set();
      kb.tap(kb.KEY.H, kb.MODIFY.SHIFT, function() {
        kb.tap(kb.KEY.i, 0);
        });
      clearTimeout(reset_timer);
      LED2.set();
      setTimeout("LED2.reset()",1000);
      LED3.reset();
    }
    
    function onTimeout() {
      setTimeout("LED1.reset()",1000);
      LED1.set();
      LED3.reset();
    }
    
    setWatch(function() {
      reset_timer = setTimeout("onTimeout()",10000);
      sendKB();
      }, BTN, {edge:"rising", debounce:50, repeat:true});
    

    I'm running 1.9.1 on my puck, updated to that this morning

  • I just uploaded that code, and it does work for me. Well, it prints out the H - I guess it's possible there's a bug and you need a delay between the first and second character.

    Are you sure you've properly disconnected? If you can't see Puck.js advertised it's usually because a device is still connected to it.

    Someone did report this bug: github.com/espruino/Espruino/iss­ues/1101

    So on Android and Chromebook there are problems if you disconnect and reconnect twice (obviously the first is needed to enable HID) - but it worked fine for me on iOS.

  • Just to add, you should be able to use the IDE and HID at the same time, you just need to do it all on the same computer.

    Either that or you can connect up a wired serial port and do it that way.

  • hmm ok I'll have another go, I've taken out the 2nd character for now just to keep it simple.
    I've been having lots of problems with re-connecting tothe puck usually involving removing the battery and starting over

  • Hmm, and this is connecting from a Mac?

    There's a thing you can do where you remove a paired bluetooth device. I think if you do Shift+Option and then click the bluetooth icon, and then your Puck.js device, you get to click remove/reset next to it.

    That'll stop the Mac auto-connecting, which could be causing your problems?

    edit: I just discovered the multiple keypress problem. Next time I update the website it should fix itself.

  • ah yeah that worked (shift+alt) click on the bluetooth icon in the menu bar then select remove for the Puck and then reconnect via the WebIDE, now its sending the character :)

    Next challenge is to get this connected to my RasPi as a keyboard, any tips?

    :)

  • Great! Actually looks like this is already in troubleshooting: espruino.com/Puck.js#i-can-t-­reconnect-to-my-puck-js-on-mac-os

    get this connected to my RasPi as a keyboard, any tips?

    I'm not sure. This looks promising: raspberrypi.org/forums/viewt­opic.php?f=28&t=176808

    However since Puck.js doesn't do Bonding right now it may fail in the same way. However, it does work on Chromebook, which I know uses Bluez, so it's possible it'll be fine.

  • Just thought I'd share the finished code for sending a word with a click of the button as a BLE HID Keyboard, not yet tried linking to the RasPi but works great with my iMac

    var kb = require("ble_hid_keyboard");
    NRF.setServices(undefined, { hid : kb.report });
    var reset_timer;
    var char_timer;
    var arr;
    
    var word = "Hello";
    
    
    function send(){
      LED3.set();
      arr = word.split("");
      char_timer = setInterval("sendChar()", 100);
      LED2.set();
      setTimeout("LED2.reset()",1000);
      LED3.reset();
    }
    
    function onTimeout() {
      setTimeout("LED1.reset()",5000);
      LED1.set();
      LED3.reset();
    }
    
    function sendChar(){
        if (arr.length !== 0){
            var char = arr.shift();
            if (char == char.toLowerCase())
            {
                sk = 0;
            }
            else
            {
                sk = 0x02;
            }
            kb.tap(kb.KEY[char.toUpperCase()], sk);
        }
        else{
            clearInterval(char_timer);
        }
    }
    
    setWatch(function() {
      send();
      }, BTN, {edge:"rising", debounce:50, repeat:true});
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Using Puck as a single button keyboard

Posted by Avatar for sammachin @sammachin

Actions