{"id":19353623,"url":"https://github.com/benwiley4000/pico8-gpio-listener","last_synced_at":"2025-04-23T07:31:30.939Z","repository":{"id":57323938,"uuid":"128178821","full_name":"benwiley4000/pico8-gpio-listener","owner":"benwiley4000","description":"🎙️🕹️ Listen to changes to your PICO-8 game's GPIO pins","archived":false,"fork":false,"pushed_at":"2018-04-08T09:23:01.000Z","size":364,"stargazers_count":48,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-20T21:02:30.488Z","etag":null,"topics":["game-development","gpio","gpio-pins","pico-8","web"],"latest_commit_sha":null,"homepage":"https://benwiley4000.github.io/pico8-gpio-listener/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benwiley4000.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-05T08:28:30.000Z","updated_at":"2025-04-10T20:20:41.000Z","dependencies_parsed_at":"2022-09-09T09:21:46.567Z","dependency_job_id":null,"html_url":"https://github.com/benwiley4000/pico8-gpio-listener","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benwiley4000%2Fpico8-gpio-listener","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benwiley4000%2Fpico8-gpio-listener/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benwiley4000%2Fpico8-gpio-listener/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benwiley4000%2Fpico8-gpio-listener/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benwiley4000","download_url":"https://codeload.github.com/benwiley4000/pico8-gpio-listener/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250391282,"owners_count":21422871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["game-development","gpio","gpio-pins","pico-8","web"],"created_at":"2024-11-10T04:43:31.761Z","updated_at":"2025-04-23T07:31:30.595Z","avatar_url":"https://github.com/benwiley4000.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pico8-gpio-listener\n\nA small library for listening to changes to your [PICO-8](https://www.lexaloffle.com/pico-8.php) web game's GPIO pins.\n\n### [Try it here!](https://benwiley4000.github.io/pico8-gpio-listener/)\n\nThe fact you can read and write PICO-8 GPIO values from the web wrapper is awesome - it means you can do things like emit vibrations according to game state, change the site theme to match different in-game levels, or maybe even develop a brand new input scheme.\n\nHowever the API for reading these values is a bit basic... PICO-8 writes GPIO values to an array, and it's up to you, the developer to check those values periodically to see if they're different. What if you could just tell JavaScript to let you know whenever the GPIO pins get updated with new values?\n\nVoilà:\n\n```js\nvar gpio = getP8Gpio();\nvar unsubscribe = gpio.subscribe(function(indices) {\n  console.log(\n    'New values at indices ' + indices.join(', ') + ': ' +\n    indices.map(function(i) { return gpio[i]; }).join(', ')\n  );\n});\n// unsubscribe later if you want...\nunsubscribe();\n```\n\nThis uses JavaScript setters under the hood to watch each index in the array, which means the watching part is shoved into a native background thread (== faster!). No need to write once-a-frame JS iterator loops or anything like that. Whenever PICO-8 writes to the GPIO array, you get notified immediately.\n\nOf course you can also write back to the GPIO array:\n\n```js\ngpio[3] = 255;\ngpio[4] = 0;\n```\n\n```console\nNew values at indices 3, 4: 255, 0\n```\n\nYour listener will be notified about your own changes as well; it's up to you if you want to do anything with that.\n\nBy default, listeners only get called if at least one value has changed in the last call stack. If you want to be notified about every update, new value or not, you can pass a second argument, `verbose`, which is a boolean:\n\n```js\ngpio.subscribe(function(indices) {\n  console.log(\n    'The values ' +\n    indices.map(function(i) { return gpio[i]; }).join(', ') +\n    ' at indices ' + indices.join(', ') +\n    ' probably didn\\'t change, but I am logging them anyway..'\n  );\n}, true);\n```\n\n## including via script tag\n\nYou can download pico8-gpio-listener.js and include it in your page with a script tag:\n\n```html\n\u003cscript src=\"pico8-gpio-listener.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  // your code here\n\u003c/script\u003e\n```\n\nOr if you prefer to fetch it from a CDN:\n\n```html\n\u003cscript src=\"https://unpkg.com/pico8-gpio-listener\"\u003e\u003c/script\u003e\n```\n\n(It's better to specify a specific version string rather than letting unpkg serve you the latest version each time the page is fetched; try opening https://unpkg.com/pico8-gpio-listener in a web browser first so it resolves to a more specific URL, then include that as your script `src`.)\n\n## installing as a module\n\nYou can also install from npm:\n\n```console\nnpm install --save pico8-gpio-listener\n```\n\nAnd use like this:\n\n```js\nvar getP8Gpio = require('pico8-gpio-listener');\n\nvar gpio = getP8Gpio();\ngpio.subscribe(function(indices) {\n  console.log(indices.map(function(i) {\n    return {\n      index: i,\n      value: gpio[i]\n    };\n  }));\n});\n```\n\n## building example site\n\nTo build a new copy of the example javascript export, open PICO-8 and run:\n\n```console\nload example.p8\nexport index.js\n```\n\nThen open index.html in a web browser.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenwiley4000%2Fpico8-gpio-listener","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenwiley4000%2Fpico8-gpio-listener","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenwiley4000%2Fpico8-gpio-listener/lists"}