Log In  
Follow
yukinotenshi

Tiny things enthusiast


Hello~~
I have just found about PICO-8 recently and I must say this is a neat little platform to play around :D

So after scrolling the manual a few times, I realize that PICO-8 got the 128 bytes of GPIO, which begs to be used for all sort of external communication. However, I haven't found someone who has made a wrapper library for this, all of the examples I found are simply examples of how someone use the GPIO for their specific communication requirements.

I tinkered for a while in my free time and thought that I might as well just create my own. The requirements that I had in mind is simply:

  • able use the GPIO without race condition between the Host and PICO-8
  • able to send a message of arbitrary length, even more than 128 bytes
  • is configurable, doesn't take all of the GPIO space
  • enable user to create their own protocol on top for their own usage
  • reliable

A little bit of details:


So after (not so) much consideration, I choose to implement a packet based protocol design. The header is 8 bytes long (actually can shrink to 4 bytes, but eh) with these fields:
0 - packet type
1 - sequence number
2 - context id
3 - packet length
4 - packet crc
5, 6, 7 - empty, reserved
8 - [up to]127 - body

by default the packet length is set to be 96 bytes

In order to send a message, the flow would be like this:

  • sender: send message start packet (includes the total message length in body)
  • client: ack
  • sender: send message part packet
  • client: ack
  • sender: send message end packet
  • client: ack, call the user registered callback for this message type

The IPC class have several states:

  • noop: nothing to do
  • wait_msg_start_ack: sent start message, wait for ack
  • send_msg_in_progress
  • receive_msg_in_progress

To make it race condition free, we simply define which packets sent by which system by a convention:

  • if packet_type % 2 == 0 then pico sent it
  • if packet_type % 2 == 1 then the external host sent it

currently, it has several built in packet_types:

  • pico_noop = 0
  • ext_noop = 1
  • pico_ping = 2
  • ext_ping = 3
  • pico_pong = 4
  • ext_pong = 5
  • pico_ack = 6
  • ext_ack = 7
  • pico_reject = 8
  • ext_reject = 9
  • pico_start_msg = 10
  • ext_start_msg = 11
  • pico_end_msg = 12
  • ext_end_msg = 13

[ Continue Reading.. ]

6
7 comments