Puck Lite GetBattery error

Posted on
  • Hi there, this simple code gives me a

    Puck.getBatteryPercentage is not a function

    at SVGPathElement.<anonymous> (pucktest.html:47:22)
    

    should it not just work in the web as in the console?

    <html>
     <head>
       <style>
         body { margin:0;  }
         svg {
           display:block; position:absolute;
           top:0%; left:0%; width:100%; height:100%;
         }
       </style>    
     </head>
     <body>
      <div>
      <svg viewBox="0 0 24 24">
        <path fill="#000000" d="M12,2A7,7 0 0,0 5,9C5,11.38 6.19,13.47 8,14.74V17A1,1 0 0,0 9,18H15A1,1 0 0,0 16,17V14.74C17.81,13.47 19,11.38 19,9A7,7 0 0,0 12,2M9,21A1,1 0 0,0 10,22H14A1,1 0 0,0 15,21V20H9V21Z" />
      </svg>
      </div>
      <div>
      <button id="btn-stat">Get Status</button>
      </div>
      <script src="https://www.puck-js.com/puck.js"></script>
      <script type="text/javascript">
        // Get the actual curve inside the SVG. You could make differemt
        // parts of a more complex SVG do different things...
        var path = document.getElementsByTagName('path')[0];
        var sync = document.getElementById('btn-stat');
     
        // Make sure your mouse cursor turns into a hand when over it, and gray it out
        path.style="cursor:pointer;fill:#BBB";
        // the possible states we could be in
        var state = 0;
        var states = [
          { color : "#444", command : "digitalWrite([LED1],0);\n" },
          { color : "red", command : "digitalWrite([LED1],1);\n" },
          { color : "green", command : "digitalWrite([LED1],2);\n" },
          { color : "blue", command : "digitalWrite([LED1],4);\n" },
        ];
    
    
        // Now send commands to turn the LED on or off
        path.addEventListener("click", function() {
          state++;
          if (state>=states.length)
            state=0;
          path.style.fill=states[state].color;
          Puck.write(states[state].command);
    
          var bat = Puck.getBatteryPercentage();
    
        });
      </script>
    </body>
    </html>
    
  • Hi,

    Sorry for the delay. The issue is you're running that command in the web browser, and not on the Puck itself.

    For instance to set the LED you do Puck.write("digitalWrite(LED1,1);\n"); not just digitalWrite(LED1,1);

    So what you need is:

    Puck.eval("Puck.getBatteryPercentage()", function(value) {
      console.log(value);
    });
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Puck Lite GetBattery error

Posted by Avatar for user153457 @user153457

Actions