{"id":15172896,"url":"https://github.com/fivdi/gpio-button","last_synced_at":"2025-09-21T23:31:28.187Z","repository":{"id":57151646,"uuid":"37825631","full_name":"fivdi/gpio-button","owner":"fivdi","description":"Hardware momentary push-buttons the Linux way","archived":false,"fork":false,"pushed_at":"2018-04-09T21:03:03.000Z","size":281,"stargazers_count":20,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-30T03:31:18.418Z","etag":null,"topics":["gpio-keys","iot","javascript","nodejs","raspberry-pi"],"latest_commit_sha":null,"homepage":null,"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/fivdi.png","metadata":{"files":{"readme":"README.md","changelog":"History.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":"2015-06-21T20:55:45.000Z","updated_at":"2023-09-08T16:58:55.000Z","dependencies_parsed_at":"2022-09-06T16:40:48.990Z","dependency_job_id":null,"html_url":"https://github.com/fivdi/gpio-button","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fivdi%2Fgpio-button","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fivdi%2Fgpio-button/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fivdi%2Fgpio-button/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fivdi%2Fgpio-button/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fivdi","download_url":"https://codeload.github.com/fivdi/gpio-button/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233806300,"owners_count":18733188,"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":["gpio-keys","iot","javascript","nodejs","raspberry-pi"],"created_at":"2024-09-27T10:22:52.617Z","updated_at":"2025-09-21T23:31:22.792Z","avatar_url":"https://github.com/fivdi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gpio-button\n\nA JavaScript package for interfacing with hardware momentary push-buttons\nconnected to GPIO pins on Linux systems. Many Linux systems like the Raspberry\nPi have a driver called gpio-keys which can be used to emit events when\nmomentary push-buttons are pressed and released. This package conforms to the\nconventions of the gpio-keys driver and enables very efficient interfacing\nwith momentary push-buttons. The technique may appear a little complex at\nfirst, but when used, it results in an efficient pure JavaScript solution for\nmomentary push-buttons.\n\n## Installation\n\n```\nnpm install gpio-button\n```\n\n## Usage\n\nAssume that there's a momentary push button connected to GPIO4 on a Raspberry\nPi:\n\n\u003cimg src=\"https://raw.githubusercontent.com/fivdi/gpio-button/master/example/button4.png\"\u003e\n\nLet's start with the complex bit, device tree overlays, after that\neverything is easy. A device tree overlay can be used to tell the Linux kernel\nabout hardware that is connected to the system, in this case, a momentary\npush-button. Once the Linux kernel knows about the device tree overlay, it\narranges everything so that the momentary push-button can be used with ease.\n\nThe device tree overlay for a hardware device is described in source code and\ncompiled into a binary format understood by the Linux kernel using the device\ntree compiler.\n\nThe source code for the momentary push-button overlay connected to GPIO4 in\nthe circuit diagram above is:\n\n```\n/dts-v1/;\n/plugin/;\n\n/ {\n    compatible = \"brcm,bcm2835\", \"brcm,bcm2708\", \"brcm,bcm2709\";\n\n    fragment@0 {\n        target = \u003c\u0026gpio\u003e;\n        __overlay__ {\n            button4_pin: button4_pin {\n                brcm,pins = \u003c4\u003e;     /* gpio4 */\n                brcm,function = \u003c0\u003e; /* input */\n                brcm,pull = \u003c1\u003e;     /* pull-down */\n            };\n        };\n    };\n\n    fragment@1 {\n    target-path = \"/soc\";\n        __overlay__ {\n            button4: button4 {\n                compatible = \"gpio-keys\";\n                #address-cells = \u003c1\u003e;\n                #size-cells = \u003c0\u003e;\n                pinctrl-names = \"default\";\n                pinctrl-0 = \u003c\u0026button4_pin\u003e;\n                status = \"okay\";\n\n                button@4 {\n                    label = \"button gpio4\";\n                    linux,code = \u003c4\u003e;\n                    gpios = \u003c\u0026gpio 4 0\u003e;\n                };\n            };\n        };\n    };\n};\n```\n\nBeautiful, isn't it :). The source code can also be found in\n`button4-overlay.dts` in the `example` directory.\n\nOn Raspbian, if the device tree compiler is not already installed it can be\nisntalled with the following command:\n\n```\nsudo apt-get install device-tree-compiler\n```\n\nAnd the overlay is compiled with the following command:\n\n```\ndtc -@ -I dts -O dtb -o button4-overlay.dtb button4-overlay.dts\n```\n\nThe device tree compiler emits three warnings that can be safely ignored. For\nfurther information related to the warnings see\n[here](https://lb.raspberrypi.org/forums/viewtopic.php?f=107\u0026t=161771\u0026p1051588#p1051588).\n\nThe device tree blob `button4-overlay.dtb` produced by the compiler is the\nbinary format understood by the Linux kernel and should be copied to\ndirectory `/boot/overlays`:\n\n```\nsudo cp button4-overlay.dtb /boot/overlays\n```\n\nThe last piece of the puzzle is adding the following line at the end of\n`/boot/config.txt` so that the overlay gets loaded at boot time:\n\n```\ndevice_tree_overlay=overlays/button4-overlay.dtb\n```\n\nAfter the Pi has been rebooted, the following JavaScript program can be used\nto print information when the momentary push-button is pressed, held, or\nreleased:\n\n```js\nvar Button = require('gpio-button'),\n  button4 = new Button('button4');\n\nbutton4.on('press', function () {\n  console.log('press');\n});\n\nbutton4.on('hold', function () {\n  console.log('hold');\n});\n\nbutton4.on('release', function () {\n  console.log('release');\n});\n```\n\n## API\n\n**Button(name)**\n\nReturns a new Button object which inherits from EventEmitter. A 'ready' event\nwill be emitted when the hardware button itself is ready for user interaction.\nThe specified name is a string and and corresponds to the name of the node for\nthe button in the device tree overlay.\n\n**pressed()**\n\nReturns true if the button is pressed, else false.\n\n**held()**\n\nReturns true if the button is held, else false.\n\n**released()**\n\nReturns true if the button is released, else false.\n\n**Event: press**\n\nEmitted when the button is pressed.\n\n**Event: hold**\n\nEmitted continuously when the button is held.\n\n**Event: release**\n\nEmitted when the button is released.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffivdi%2Fgpio-button","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffivdi%2Fgpio-button","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffivdi%2Fgpio-button/lists"}