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});
    
  • Hi Gordon,

    I've read this post and make it work, thank you!... I would like to know something... We can read the caracter with espruino Web app but it is not usefull.... My project is to send a keyboard key (spacebar) to my Winodws software or send sapcebar command via a Android wifi remote app that control this same software by acquiring windows keyboard shortcuts. How can we achieve it ? Any help will be appreciates, if working we probably need some more Puck!

  • The code posted above should do what you want (making Puck.js appear as a keyboard to the connected computer).

    There's a tutorial on it here, which might be best to start off with? espruino.com/Puck.js+Keyboard­

  • Hi Gordon, thanks for answer I make it work, I didn't know Espruino Web app should be open and connected to work... So It's working with my Mac, but unfortunately not working on windows 10. I mapped a S to test because I don't find HID code for spacebar key. So two questions : What is the HID code for Spacebar ? And there's a way to make it work on windows ? Espruino app working on Windows I can upload code on it, the app see Puck.js device but for Mapping HID keyboard keys, it don't work.

  • Are you using the latest (1v92) Espruino firmware? Earlier firmwares didn't support bonding, which Windows needed to work. With it, you shouldn't need the Web IDE open and connected once the device has been programmed.

    And try kb.KEY[" "] for the space key...

  • Hi Gordon, Thank again... could you be more specific please : Here's my lines : it don't take it : var kb = require("ble_hid_keyboard");
    NRF.setServices(undefined, { hid : kb.report });

    function btnPressed() {
    // Send 'spacebar'
    kb.tap(kb.KEY[" "]{

    });
    }

    // trigger btnPressed whenever the button is pressed
    setWatch(btnPressed, BTN, {edge:"rising",repeat:true,debounce:50})­;

  • Hi Gordon ok I find the good code :

    var kb = require("ble_hid_keyboard");
    NRF.setServices(undefined, { hid : kb.report });
    
    function btnPressed() {
      // Send 'spacebar'
      kb.tap(kb.KEY[" "], 0, function() {
       
      });
    }
    
    // trigger btnPressed whenever the button is pressed
    setWatch(btnPressed, BTN, {edge:"rising",repeat:true,debounce:50})­;
    

    Working on my MAC '' SPACEBAR '' then connect to windows but it don't work on Win10 any tips or comments. Can I program it on Mac and make it work on win10 ?

  • Yes, that looks good. Try:

    • Make sure Puck.js is on the 1v92 firmware
    • Connect with the Web IDE on your Mac
    • Program the code above
    • Disconnect from the IDE on the Mac
    • Make sure you follow these instructions: espruino.com/Puck.js#i-can-t-­reconnect-to-my-puck-js-on-mac-os
      (If you'd used the Puck as a HID device n your Mac it's very likely that your Mac is repeatedly connecting to Puck before Windows can do it)
    • Connect via the Bluetooth Pairing menu on your Windows PC. The keyboard icon should appear to the left of it and it'll start working
  • ....see updated next post

  • I'm dropped... I've downloaded the NRF connect app on my android, I see the DFUTARG now, all is good but connect to it and try to upload firmware with 1.92.zip file, it say DFU file error BONDED... I follow instructions and do exactly like in the video tutorial!!!! ? Please Help :)

  • That's an odd one. Try restarting Puck.js once (by lifting the battery) without going into the bootloader mode, and without connecting with a PC, and try again.

    However if it's saying bonded then you may have version 1v92 installed already. You could check if you connect and type 'process.version' on the left hand side of the IDE.

  • Hi Gordon, thanks for tips, I've tried all of them, but nothing to do.... The actual Firmware is 1.88. I've also tried with replacing by a new battery... And Sorry I wrote Bonded but the DFU is Not bonded!

  • Have you tried doing this: espruino.com/Puck.js#i-can-t-­reconnect-to-my-puck-js-on-mac-os

    I guess it might be that the Mac is trying to reconnect to the device even when it's in DFU mode, and that is messing things up. You see the red LED on the Puck when it's in bootloader mode?
    Do you ever see it change to the Blue LED light?

  • Hi Gordon, Thanks again! I've removed Puck.js and I've also put Bluetooth off on my MAC to be sure. Yes I see the Red light when hold button and replace battery. When I connected it via NRF connect it become blue, no problem! Once I try to flash the firmware with the 1.92 zip file, it don't works....Upload Failed!

  • With the same error message of 'not bonded'? Does it get very far with the upload or does it fail immediately?

    I can't remember seeing the message before - do you have another phone or tablet you could try the upload with? and are you sure it's espruino_1v92_puckjs.zip you're using and not espruino_1v92.zip?

  • Afffff!You're Right Grodon, I apologize....Didn't see the entire file title in the Android file download listing, I really tought I had downloaded the good file... All is good now, Firmware suppose to be flash... Will now retry to make work the project with windows... Thank you so much for your support and your patience...details! All are in details! :)

  • Great - thanks for letting us know!

  • 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