{"id":31,"date":"2018-05-13T22:29:26","date_gmt":"2018-05-13T22:29:26","guid":{"rendered":"http:\/\/www.astill.org\/wp\/?p=31"},"modified":"2018-05-13T22:32:59","modified_gmt":"2018-05-13T22:32:59","slug":"npm-raspberry-pi-zero-onoff-and-closures","status":"publish","type":"post","link":"http:\/\/www.astill.org\/wp\/?p=31","title":{"rendered":"NPM Raspberry Pi Zero onoff and Closures"},"content":{"rendered":"<p>Using a Pi Zero to monitor some input pins, a change on the input will trigger an event.<br \/>\nThis is using NPM onff (https:\/\/www.npmjs.com\/package\/onoff).<\/p>\n<p>The example code with a button worked well for a single input, but to have some cleaner code that had an array of inputs an ability to identify the pin that triggered the event was required. The watch callback only takes two parameters, so a closure (<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Closures\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Closures<\/a> ) is needed (hints here <a href=\"https:\/\/www.raspberrypi.org\/forums\/viewtopic.php?t=152105\">https:\/\/www.raspberrypi.org\/forums\/viewtopic.php?t=152105<\/a> ).<\/p>\n<p>The code below is a simple example of this. An array of sensorPin values that are acting as the input triggers. The Gpio inputs are<\/p>\n<pre>\/**\r\n * Simple code to have notifications (interrupts) when the signal to the input pins change.\r\n *\/\r\n const Gpio = require('onoff').Gpio;\r\n\r\nvar sensorPins = [27,22,5,6,13,26,23,18];\r\n var sensors = [];\r\n\r\n\/\/ Loop through each configured sensor and create the onoff\r\n for (var i=0; i&lt;sensorPins.length; i++) {\r\n \/\/ Use a closure to identify the pin in the callback\r\n function createSensor(pin) {\r\n var sensor = new Gpio(pin,'in','rising', {debounceTimeout:10})\r\n\r\nsensor.watch(function (err, value) {\r\n if (err) {\r\n throw err;\r\n }\r\n\r\nconsole.log(\"Pin \"+pin+\":\"+value);\r\n });\r\n };\r\n var newSensor = createSensor(sensorPins[i]);\r\n\r\nsensors.push(newSensor);\r\n }\r\n\r\n\/**\r\n * Ctrl-c handler\r\n *\/\r\n process.on('SIGINT', function () {\r\n for (var i=0; i&lt;sensorPins.length; i++) {\r\n sensors[i].unexport();\r\n }\r\n });<\/pre>\n<p>gist: <a href=\"https:\/\/gist.github.com\/jastill\/929a09f80c03a44b21b5710cf16c1b81\">https:\/\/gist.github.com\/jastill\/929a09f80c03a44b21b5710cf16c1b81<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using a Pi Zero to monitor some input pins, a change on the input will trigger an event. This is&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.astill.org\/wp\/index.php?rest_route=\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.astill.org\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.astill.org\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.astill.org\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.astill.org\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=31"}],"version-history":[{"count":4,"href":"http:\/\/www.astill.org\/wp\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":35,"href":"http:\/\/www.astill.org\/wp\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions\/35"}],"wp:attachment":[{"href":"http:\/\/www.astill.org\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.astill.org\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.astill.org\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}