yukinotenshi [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=56920 [WIP] PicoIPC - GPIO wrapper for seamless networking <p>Hello~~<br /> I have just found about PICO-8 recently and I must say this is a neat little platform to play around :D</p> <p>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.</p> <p>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:</p> <ul> <li>able use the GPIO without race condition between the Host and PICO-8</li> <li>able to send a message of arbitrary length, even more than 128 bytes</li> <li>is configurable, doesn't take all of the GPIO space</li> <li>enable user to create their own protocol on top for their own usage</li> <li>reliable</li> </ul> <p>A little bit of details:<br /> <div><div><input type="button" value=" Show " onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = ' Hide '; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = ' Show '; }"></div><div><div style="display: none;"><br /> 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:<br /> 0 - packet type<br /> 1 - sequence number<br /> 2 - context id<br /> 3 - packet length<br /> 4 - packet crc<br /> 5, 6, 7 - empty, reserved<br /> 8 - [up to]127 - body</p> <p>by default the packet length is set to be 96 bytes</p> <p>In order to send a message, the flow would be like this:</p> <ul> <li>sender: send message start packet (includes the total message length in body)</li> <li>client: ack</li> <li>sender: send message part packet</li> <li>client: ack</li> <li>sender: send message end packet</li> <li>client: ack, call the user registered callback for this message type</li> </ul> <p>The IPC class have several states:</p> <ul> <li>noop: nothing to do</li> <li>wait_msg_start_ack: sent start message, wait for ack</li> <li>send_msg_in_progress</li> <li>receive_msg_in_progress</li> </ul> <p>To make it race condition free, we simply define which packets sent by which system by a convention:</p> <ul> <li>if packet_type % 2 == 0 then pico sent it</li> <li>if packet_type % 2 == 1 then the external host sent it</li> </ul> <p>currently, it has several built in packet_types:</p> <ul> <li>pico_noop = 0</li> <li>ext_noop = 1</li> <li>pico_ping = 2</li> <li>ext_ping = 3</li> <li>pico_pong = 4</li> <li>ext_pong = 5</li> <li>pico_ack = 6</li> <li>ext_ack = 7</li> <li>pico_reject = 8</li> <li>ext_reject = 9</li> <li>pico_start_msg = 10</li> <li>ext_start_msg = 11</li> <li>pico_end_msg = 12</li> <li>ext_end_msg = 13</li> </ul> <p></div></div></div></p> <p>If anyone is interested, you can check the code <a href="https://github.com/yukinotenshi/picoweb.git">here</a><br /> I have added a few simple examples on how you can use it. Simply copy paste the picoipc.p8 and the example script to your cart directory, create the input/output file, and run </p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>pico8 -i input -o output -p picoweb</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>(the flag is necessary to indicate that you're using the library)</p> <p>Please note that this code is still POC quality, the naming is not consistent yet, and a lot of features are missing such as:</p> <ul> <li>sequence number checks and wrap around</li> <li>randomized sequence number start</li> <li>packet buffer update (for progress bar purposes)</li> <li>context switching</li> <li>javascript library</li> <li>nice GUI for the adapter</li> <li>reduce token usage</li> </ul> <p>Later on, as the repository name suggest, I will build a protocol on top of this IPC to support:</p> <ul> <li>HTTP calls</li> <li>Websocket connections</li> </ul> <p>Let me know what you think, I'll continue working on this on the next weekend. Happy tinkering!</p> https://www.lexaloffle.com/bbs/?tid=44054 https://www.lexaloffle.com/bbs/?tid=44054 Sun, 01 Aug 2021 15:39:35 UTC