GPIO: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
imported>ZOSK1712
No edit summary
imported>ZOSK1712
No edit summary
Line 6: Line 6:


In the web exports, you may use arbitrary bytes in these memory locations to be read from and written to by JavaScript code. To access it, you have to declare a new array, as following:
In the web exports, you may use arbitrary bytes in these memory locations to be read from and written to by JavaScript code. To access it, you have to declare a new array, as following:
<pre>
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
var pico8_gpio = new Array(128);
var pico8_gpio = new Array(128);
</syntaxhighlight>
</syntaxhighlight>
</pre>
You can then write to the bytes like so :
You can then write to the bytes like so :
<pre>
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
pico8_gpio[0] = 10;
pico8_gpio[0] = 10;
</syntaxhighlight>
</syntaxhighlight>
</pre>
This allows JavaScript to communicate with Pico 8.
This allows JavaScript to communicate with Pico 8.



Revision as of 16:19, 2 March 2019

The PICO-8 builds for Raspberry Pi and PocketChip include support for reading and writing to the GPIO pins on those devices (added in v0.1.9). You can also use the GPIO interface from read/write to JavaScript in web exports of PICO-8 carts (added in v0.1.7).

API

To access the GPIO pins, peek or poke to bytes

0x5f80..0x5fff

in memory. PICO-8 will automatically set a pin to read mode when you peek the memory address or write mode when you poke to the address.

In the web exports, you may use arbitrary bytes in these memory locations to be read from and written to by JavaScript code. To access it, you have to declare a new array, as following:

<syntaxhighlight lang="javascript">
var pico8_gpio = new Array(128);
</syntaxhighlight>

You can then write to the bytes like so :

<syntaxhighlight lang="javascript">
pico8_gpio[0] = 10;
</syntaxhighlight>

This allows JavaScript to communicate with Pico 8.

In the hardware implementations, the GPIO pins act digitally, with 0 representing 0 and 255 representing 1. Also, hardware implementations may not support all 128 pins. Raspberry Pi, for instance, reportedly only supports the first 8 pins.

Raspberry Pi Setup

Raspberry Pi B+

On a Raspberry Pi B+ model, no additional setup was necessary (using v0.1.10). Run the pico8 binary via

sudo ./pico8

to enable access to the GPIO pins. If you do not use sudo to run PICO-8, the program will crash with a WiringPi error when your cart tries to access the GPIO memory space.

Raspberry Pi 2 and newer models

One newer Raspberry Pi models, the version of WiringPi included in the statically-linked pico8 binary. Instead, you will have to build and install SDL 2.0.5 from source as well as a more recent version of WiringPi. Then, use the pico8_dyn binary to run PICO-8.

Older models

On older models of the Raspberry Pi than the B+ model, GPIO may not work at all with PICO-8.

PocketChip Setup

TODO: document experience using PocketChip with PICO-8. Example: I think we had to install a newer (unofficial?) version of PICO-8 using the Debian package manager.