{"id":22020586,"url":"https://github.com/netbymatt/unicorn-hat-mini","last_synced_at":"2026-04-20T13:06:15.238Z","repository":{"id":38220890,"uuid":"506474452","full_name":"netbymatt/unicorn-hat-mini","owner":"netbymatt","description":"A NodeJS driver for the Unicorn HAT Mini from Pimoroni. This software is based on Pimoroni's official driver and examples written in Python.","archived":false,"fork":false,"pushed_at":"2023-02-04T13:34:49.000Z","size":189,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-13T17:59:53.799Z","etag":null,"topics":["hat","pimoroni","raspberry-pi","unicorn"],"latest_commit_sha":null,"homepage":"","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/netbymatt.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2022-06-23T02:42:15.000Z","updated_at":"2022-06-23T03:01:48.000Z","dependencies_parsed_at":"2023-02-18T16:46:04.047Z","dependency_job_id":null,"html_url":"https://github.com/netbymatt/unicorn-hat-mini","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/netbymatt/unicorn-hat-mini","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netbymatt%2Funicorn-hat-mini","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netbymatt%2Funicorn-hat-mini/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netbymatt%2Funicorn-hat-mini/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netbymatt%2Funicorn-hat-mini/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/netbymatt","download_url":"https://codeload.github.com/netbymatt/unicorn-hat-mini/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netbymatt%2Funicorn-hat-mini/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32048455,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T11:35:06.609Z","status":"ssl_error","status_checked_at":"2026-04-20T11:34:48.899Z","response_time":94,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["hat","pimoroni","raspberry-pi","unicorn"],"created_at":"2024-11-30T06:07:05.926Z","updated_at":"2026-04-20T13:06:15.221Z","avatar_url":"https://github.com/netbymatt.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Unicorn HAT Mini\nA NodeJS driver for the [Unicorn HAT Mini](https://shop.pimoroni.com/products/unicorn-hat-mini) from [Pimoroni](https://www.pimoroni.com). This software is based on Pimoroni's [official driver and examples](https://github.com/pimoroni/unicornhatmini-python) written in Python.\n\nThe device is a 17x7 RGB LED matrix with 4 buttons.\n\n# Installing \n``` bash\nnpm i unicorn-hat-mini\n```\n\n# Demos\nCloning this repository will bring with demos that are not part of the npm package. Sudo must be used to access the GPIO when calling the demo. Ctrl-C can be used to exit the demos.\n```bash\n# random colors on each pixel every 0.5 seconds\nsudo node ./demo/index.js\n# moving rainbow across the display, direct port from Python example\nsudo node ./demo/rainbow.js\n```\n\n## Operating notes\nThis driver makes use of [node-rpio](https://github.com/jperkin/node-rpio) in the ```gpiomem: false``` mode which requires you to run this as root (```sudo```). Additional troubleshooting and rationale can be found in the node-rpio [README](https://github.com/jperkin/node-rpio#important-system-requirements).\n\n# Usage\n``` javascript\nconst UHM = require('unicorn-hat-mini');\nconst uhm = new UHM();\n\nuhm.brightness = 0.1;\nuhm.setAll(255, 255, 255);\nuhm.show();\n```\n## Combining with other IO functions\nTo allow control of the IO not used by the HAT, ```UHM.rpio``` is available to set up additional IO and other functionality that the node-rpio library provides.\n\n\n# API\n## UHM(options)\nUHM() is a class and must be called with ```new```. It returns an instance of the driver ```uhm```.\n``` javascript\nconst uhm = UHM({brightness: 0.5});\n```\n\n|Option|Default|Description|\n|---|---|---|\n|options.brightness|0.2|Global Brightness of display 0.0-1.0|\n|options.enableButtons|true|Use the internal button handler and event emitter. Set to false if you prefer to set up your own access to the HAT's buttons|\n|options.exitProcess|true|The driver cleans up the open IO ports when ```SIGINT```, ```SIGTERM```, ```SIGUSR2``` (for compatability with [nodemon](https://github.com/remy/nodemon))\n\n## UHM.ROWS = 7\nConstant number of rows on HAT device.\n\n## UHM.COLS = 17\nConstant number of columns on HAT device.\n\n## UHM.rpio\nInstance of node-rpio provided for allowing configuration of additional IO not used by the HAT.\n\n## uhm.setPixel(row, col, r, g, b)\nSet the color of the pixel at (row, col). Must call ```uhm.show()``` for changes to be displayed on the HAT.\n``` javascript\n// set pixel at row 3 column 5 to purple\nuhm.setPixel(3,5,255,0,255);\n// Optional variant:\nuhm.setPixel(3,5,[255,0,255]);\n```\n\n|Parameter|Range|Description|\n|---|---|---|\n|row|0-3|Vertical position of selected pixel|\n|col|0-16|Horizontal position of selected pixel|\n|r|0-255|Red value of pixel. Automatically converted to the HAT limits of 0-63. Optional variant: r can be an array of color values ```[r, g, b]```.|\n|g|0-255|Green value of pixel. Automatically converted to the HAT limits of 0-63.|\n|b|0-255|Blue value of pixel. Automatically converted to the HAT limits of 0-63.|\n\n## uhm.getPixel(row, col)\nGet the color of the pixel at (row, col).\n``` javascript\n// set pixel at row 3 column 5 to purple\nuhm.setPixel(3,5,255,0,255);\nconst pixel = uhm.getPixel(3,5,255,0,255);\n// pixel = [255, 0, 255]\n```\n\n|Parameter|Range|Description|\n|---|---|---|\n|row|0-3|Vertical position of selected pixel|\n|col|0-16|Horizontal position of selected pixel|\n\n## uhm.setAll(r, g, b)\nSet the color of all pixels. Must call ```uhm.show()``` for changes to be displayed on the HAT.\n``` javascript\n// set all pixels to yellow\nuhm.setAll(255,255,0);\n// Optional variant:\nuhm.setPixel([255,255,0]);\n```\n|Parameter|Range|Description|\n|---|---|---|\n|r|0-255|Red value of pixel. Automatically converted to the HAT limits of 0-63. Optional variant: r can be an array of color values ```[r, g, b]```.|\n|g|0-255|Green value of pixel. Automatically converted to the HAT limits of 0-63.|\n|b|0-255|Blue value of pixel. Automatically converted to the HAT limits of 0-63.|\n\n## uhm.getAll()\nReturn all pixels as an array in the format: [row][col] = [r, g, b] format\n``` javascript\nconst all = uhm.getAll();\n// all = [[[r, g, b], [r, g, b], ...], [[r, g, b], ...], ...]\n```\n\n## uhm.show()\nUpdate the display with the latest display buffer as modified by ```uhm.setPixel()``` and ```uhm.setAll()```.\n``` javascript\nuhm.show();\n```\n\n## uhm.clear()\nSet all pixels to black/off.\n``` javascript\nuhm.clear();\n```\n\n## uhm.brightness\nSet or read the current global brightness of the display 0.0 - 1.0.\n``` javascript\n// set brightness to 50%\nuhm.brightness = 0.5;\n// read current brightness\nconsole.log(uhm.brightness)\n// displays 0.5\n```\n\n## uhm.buttonA, uhm.buttonB, uhm.buttonX, uhm.buttonY\n``` javascript\n// read state of button A\nconst buttonState = uhm.buttonA;\n```\nReturns the state of any of the 4 buttons. The value is not read by accessing this property, instead the driver polls the 4 buttons and produces events (see ```uhm.on```) for change of button states. The most recent state of each button is recorded internally during each event and this most-recent value is returned by these properties. If ```options.enableButtons``` is ```false``` returns undefined.\n\n## uhm.on(event, handler)\nA set of events triggered by pressing and releasing buttons. Events can be triggered in the pressed state:\n```javascript\nevent = \n'button-a-pressed'\n'button-b-pressed'\n'button-x-pressed'\n'button-y-pressed'\n```\nIn the released state:\n```javascript\nevent =\n'button-a-released'\n'button-b-released'\n'button-x-released'\n'button-y-released'\n```\nOn both state changes:\n```javascript\nevent =\n'button-a'\n'button-b'\n'button-x'\n'button-y'\n```\nOr for any button state change:\n```javascript\nevent =\n'button'\n```\n\nThe handler takes the same callback signature for all button states\n```javascript\nhandler = (button, state) =\u003e {\n\t// button = 'a', 'b', 'x', 'y'\n\t// state = true for pressed, false for released\n}\n```\nA simple example\n```javascript\n// displays \"button b is true\" or \"button b is false\"\n// as the button is pressed and released\nuhm.on('button-b', (button, state) =\u003e {\n\tconsole.log(`Button ${button} is ${state}`);\n});\n```\n\n# Debugging\nDebugging presents some challenges because the node application must be run as sudo. The solution that I have come up with uses [VSCode](https://code.visualstudio.com) and a separately launched [nodemon](https://nodemon.io/) process. Saving a file will trigger nodemon to restart the process and the debugger will need to be connected again.\n```json\nlaunch.json\n{\n\t\"version\": \"0.2.0\",\n\t\"configurations\": [\n\t\t{\n\t\t\t\"name\": \"Attach\",\n\t\t\t\"port\": 9229,\n\t\t\t\"request\": \"attach\",\n\t\t\t\"skipFiles\": [\n\t\t\t\t\"\u003cnode_internals\u003e/**\",\n\t\t\t\t\"**/node_modules/**\"\n\t\t\t],\n\t\t\t\"type\": \"node\",\n\t\t\t\"address\": \"127.0.0.1\",\n\t\t\t\"continueOnAttach\": true,\t// This causes the paused process to continue upon attaching the debugger. It saves a step of having to press F5 (continue) when starting the debugger. It creates the \"feel\" of launching the process directly inside VSCode\n\t\t\t\"localRoot\": \"${workspaceFolder}\",\n\t\t\t\"remoteRoot\": \"/home/ubuntu/projects/unicorn-hat-mini\"\n\t\t}\n\t]\n}\n```\n\n```bash\n# run nodemon\n# inspect-brk causes the debugger to pause immediately upon entering the program\nsudo ./node_modules/nodemon/bin/nodemon.js --inspect-brk demo/rainbow.js\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetbymatt%2Funicorn-hat-mini","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnetbymatt%2Funicorn-hat-mini","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetbymatt%2Funicorn-hat-mini/lists"}