hp <URL>
to 744625
(or international 1.586.474.4625
) to move the HackPack to a page of your creation.This guide will show you (most of) the low level API we’ve exposed to you. The UI is HTML, CSS, and Javascript inside a Python webview—and there are a number of goodies in Python we’d love you do grab.
Here’s how to grab them.
While designing for the webview, you’ll need to be aware of its specifications (and the limits of the hardware).
To change from MOUSE (default) mode to ARROW mode, there is a utility function in the Python API inputArrow
.
After 21 seconds, the Hackpack will reset to MOUSE mode; we suggest calling ARROW mode every 5-7 seconds to keep the Hackpack input in that mode.
Every function is called in the same way, with a method name and a JSON object. For many functions the JSON can be empty, but take heed of the ones which expect some kind of input. For example, you can call a light pattern with:
pywebview.api.showLightsParams({'demo': 3, 'repeat': 2});
We’ve created three classes of functions for you to call with your web app:
Function | Sample Call | Description |
checkWifiConnection | pywebview.api.checkWifiConnection({}); | Check if WiFi is up |
get | pywebview.api.get({'key': 'test'}); | Get a key from Hackpack local storage |
getIpAddress | pywebview.api.getIpAddress({}); | Get the device IP address |
getRandomNumber | pywebview.api.getRandomNumber({}); | Get a random number |
inputArrow | pywebview.api.inputArrow({}); | Set input type to ARROW mode (for 21 seconds) |
inputCursor | pywebview.api.inputCursor({}); | Set the input type to MOUSE mode (default) |
lightsOff | pywebview.api.lightsOff({}); | Send a 'Lights Off' sequence and clear any stored sequence |
longTime | pywebview.api.longTime({}); | DANGER. Pause webview for 15 seconds |
set | pywebview.api.set({'data': '12345', 'key': 'test'}); | Set a key in Hackpack local storage |
setLightIntensity | pywebview.api.setLightIntensity({'intensity': '30'}); | Set light intensity [0-100] in Percentage. ‘15’ is default. |
wifiPowerManagementOff | pywebview.api.wifiPowerManagementOff({}); | Turn off WiFi power management |
wifiPowerManagementOn | pywebview.api.wifiPowerManagementOn({}); | Turn on WiFi power management |
Function | Sample Call | Description |
changeWebView | pywebview.api.changeWebView({'url': 'https://www.twilio.com'}); | Move the URL on the HackPack. Wherever you point it, tap the escape/power button 3 times to go back. |
launchDoom | pywebview.api.launchDoom({}); | Launch Doom |
launchMicropolis | pywebview.api.launchMicropolis({}); | Launch Micropolis |
launchOpenTTD | pywebview.api.launchOpenTTD({}); | Launch OpenTTD |
showLights | pywebview.api.showLights({}); | Show the scanner pattern (default) |
showLightsParams | pywebview.api.showLightsParams({'demo': 3, 'repeat': 2}); | Show a demo [0-39] and repeat X times. You can find the demos here. (SPOILER ALERT) |
textLights | pywebview.api.textLights({'message': '128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,4000'}); | Set the lights to a color (R,G,Bx5 then delay in ms). The command to the left is full white for 4 seconds. |
Function | Sample Call | Description |
addLightCommand | {'message': '128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,4000'} | Send CUSTOM light command directly to lightsocket. For examples, see here. |
clearLightSequence | {} | Clear the currently stored light commands. |
lightLights | {} | Send a 'Show Lights' Command |
There are some other methods exposed through the Python API. However, we don’t suggest calling them because they are destructive in various ways, or lower level than you really need for functionality.
The Python-Javascript bridge doesn’t immediately load with your page. Additionally, you’ll probably be testing your webapp on the desktop; you’ll want to use defensive coding to ensure it is loaded.
This example defensively calls a light sequence every 5 seconds with jQuery:
$(document).ready(function() {
var maxRetries = 5; // maximum number of times to look for the PyAPI
var delay = 5000; // in milliseconds
var have_python_api = false; // useful in other parts of the code
function rejectDelay(reason) {
return new Promise(function(resolve, reject) {
setTimeout(reject.bind(null, reason), delay);
});
}
function lightDemo(retry) {
try {
pywebview.api.showLightsParams({'demo': 3, 'repeat': 2});
} catch(ex){
if (retry < maxRetries) {
setTimeout(function() {lightDemo(retry + 1);}, delay);
return;
} else {
console.log("> Too many retries. Are you not in the webview?");
return;
}
}
// Successfully called lights, call them again soon
setTimeout(function(){lightDemo(-1); have_python_api = true;}, delay);
}
setTimeout(function() {lightDemo(0);}, delay);
});
Hackpack v4 was designed to hand over the reigns to your ingenuity and creativity as quickly as possible. We fully encourage you to poke around with these commands and the firmware.
If you have any questions, don’t hesitate to visit us at the Hackpack booth… we’d be thrilled to help.
And, as always, we can’t wait to see what you build. (And watch it flash.)