You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Ok - so you have a connection to the Puck by Web Bluetooth, and you want to do something whenever the button on the Puck is pressed?

    For that, you want to use the slightly more advanced part of the Puck.js library, since the simple bit deals with initiating everything from the webpage, where you want to initiate it from the Puck: http://www.espruino.com/Puck.js+Web+Blue­tooth#two-way-communications

    On the Website, you could do something like this:

    <html>
     <head>
     </head>
     <body>
      <script src="https://www.puck-js.com/puck.js"></­script>
      <div id="connect_button">Connect!</div>
      <script type="text/javascript">
        // Called when we get a line of data 
        function onLine(v) {
          if (v.indexOf("Pressed")>=0)
           console.log("Button pressed!");
        }
    
        // When clicked, connect or disconnect
        var connection;    
        document.getElementById("connect_button"­).addEventListener("click", function() {
          if (connection) {
            connection.close();
            connection = undefined;
          }
          Puck.connect(function(c) {
            if (!c) {
              alert("Couldn't connect!");
              return;
            }
            connection = c;
            // Handle the data we get back, and call 'onLine'
            // whenever we get a complete line
            var buf = "";
            connection.on("data", function(d) {
              buf += d;
              var i = buf.indexOf("\n");
              while (i>=0) {
                onLine(buf.substr(0,i));
                buf = buf.substr(i+1);
                i = buf.indexOf("\n");
              }
            });
            // First, reset Puck.js
            connection.write("reset();\n", function() {
              // Wait for it to reset itself
              setTimeout(function() {
                // Now tell it to print text whenever the button is pressed
                connection.write('\x10setWatch(function(­) {console.log("Pressed");}, BTN, { edge: "rising", debounce: 50, repeat: true });\n',
                  function() { console.log("Ready..."); });
              }, 1500);
            });
          });
        });
      </script>
     </body>
    </html>
    

    You'll need the console window open... When you click the 'Connect' text it connects, uploads your 'setWatch' code, and then listens for a line containing the text 'Pressed' and writes something to the console

About

Avatar for Gordon @Gordon started