Puck.js based counter on Web page #2743
Unanswered
gfwilliams
asked this question in
Puck.js, Pixl.js, Jolt.js and MDBT42
Replies: 1 comment
-
Posted at 2018-10-03 by ElanII Esteemed Mr. Gordon thanks truly a lot from my heart for helping me and using you time for this. I have no word for thank You ... BR Carlo |
Beta Was this translation helpful? Give feedback.
0 replies
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
-
Posted at 2018-10-03 by @gfwilliams
I just had a question about how you'd go about making a Puck that connects to a Web Page, and makes a counter go up by 1 when pressed once, and down by 1 when pressed twice.
The first step is detecting the difference between 1 and 2 presses. You'd have to upload some code to Puck.js with the Web IDE for this but Puck.js has a timer so it is pretty easy.
As it happens there's a tutorial at http://www.espruino.com/Single+Button+Combination+Lock which shows you how to count the number of button presses. Halfway through there's this:
I was also asked if there was a need to pair the Puck with the webpage each time it loads. The normal way of communicating with a webpage would be Web Bluetooth - but this does require pairing each time.
However since we only want to send data from the Puck, we'll just use Bluetooth HID - this makes the Puck appear like a Bluetooth Keyboard so you pair with the operating system once and you're sorted.
We'll just send a character for the number of times the button was pressed - so pressing twice would be the same as hitting the
2
key on your keyboard.Mashing the code above together with http://www.espruino.com/Puck.js+Keyboard you get:
Upload that code, disconnect, reconnect with your device, and you're sorted!
Then in your webpage all you need is something using a
keypress
listener: https://developer.mozilla.org/en-US/docs/Web/Events/keypress#ExampleBeta Was this translation helpful? Give feedback.
All reactions