-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
figured out how to eval a compiled lua function!
- Loading branch information
Showing
5 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Playdate USB demo</title> | ||
<link rel="stylesheet" href="./assets/styles.css"> | ||
<script src="./playdate-usb.js"></script> | ||
</head> | ||
<body> | ||
<div class="wrapper"> | ||
<header> | ||
<h1>Playdate USB demo - eval</h1> | ||
</header> | ||
<main> | ||
<div class="panel"> | ||
<div class="row"> | ||
<div class="pull-left"> | ||
<button onclick="connect()" id="connectButton"> connect to playdate </button> | ||
<span id="serial"></span> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="panel" id="data"> | ||
<p> | ||
This example uses evalLuaPayload() to send a compiled Lua payload over USB and execute it on your Playdate - as long as the current application is not a system app. This payload is designed to print all fo the global variables from the current app's lua runtime, the code is: | ||
<pre>for n in pairs(_G) do print(n) end</pre> | ||
</p> | ||
</div> | ||
</main> | ||
<footer class="row"> | ||
<div class="pull-left"> | ||
<a href="//github.com/jaames/playdate-usb">playdate-usb</a> | <a href="//github.com/jaames/playdate-usb/blob/main/examples/example-basic.html">page source</a> | ||
</div> | ||
<div class="pull-right"> | ||
built by <a href="//github.com/jaames">james daniel</a> | ||
</div> | ||
</footer> | ||
</div> | ||
|
||
<script> | ||
var device; | ||
|
||
const data = document.getElementById('data'); | ||
|
||
// Check WebUSB support, display error if not supported | ||
try { | ||
playdateUsb.assertUsbSupported(); | ||
} | ||
catch (e) { | ||
document.getElementById('serial').innerHTML = e.message; | ||
document.getElementById('connectButton').disabled = true; | ||
} | ||
|
||
async function runPayload(device) { | ||
// fetch the payload - this is a PDC-compiled lua payload extracted from a .PDZ file | ||
const resp = await fetch('./eval_payload.luac'); | ||
// get its contents as an ArrayBuffer | ||
const payload = await resp.arrayBuffer(); | ||
// send it to the Playdate | ||
return await device.evalLuaPayload(payload); | ||
} | ||
|
||
async function connect() { | ||
try { | ||
device = await playdateUsb.requestConnectPlaydate(); | ||
// device setup | ||
await device.open(); | ||
const serial = await device.getSerial(); | ||
document.getElementById('serial').innerHTML = `Connected to Playdate ${ serial }`; | ||
device.on('disconnect', () => { | ||
document.getElementById('serial').innerHTML = 'Playdate disconnected'; | ||
}); | ||
|
||
const consoleData = await runPayload(device); | ||
document.getElementById('data'); | ||
data.innerHTML += "<b>results:</b>"; | ||
data.innerHTML += consoleData.split('\n').join('</br>'); | ||
console.log(consoleData); | ||
} | ||
catch(e) { | ||
console.warn(e.message); | ||
document.getElementById('serial').innerHTML = 'Error connecting to Playdate, try again'; | ||
} | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters