Created
May 14, 2020 12:47
-
-
Save aqmattil/38a22cba76b4439690745ab742afd115 to your computer and use it in GitHub Desktop.
Simple example to show how rpio.poll freezes on Balena
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rpio = require('rpio'); | |
const ws281x = require ('rpi-ws281x-native'); | |
var rpi_options = { | |
gpiomem: false, // /dev/gpiomem for non-root but only GPIO access ( /dev/mem provides full access but requires root) | |
mapping: 'physical' // use physical pins (stay the same on all boards) | |
} | |
var active = false | |
var timeLastPressedStopButton = Date.now(); | |
const STOP_BUTTON_PIN = 18 // input (physical pin 18 = bcm (gpio) 24) | |
const DEBOUNCE_INTERVAL = 2000 | |
rpio.init(rpi_options); | |
setupPins(); | |
function setupPins() { | |
rpio.open(STOP_BUTTON_PIN, rpio.INPUT, rpio.PULL_UP); | |
rpio.poll(STOP_BUTTON_PIN, stopButtonPress, rpio.POLL_LOW) | |
} | |
// end button press - simple switch to turn leds green/red | |
function stopButtonPress(pin) { | |
var state = rpio.read(pin); | |
// avoid sending multiple queries, i.e. button press sends 1, button release sends 0, but we are only interested in 1 | |
// debouncing is done in code, i.e. we consider only those signals that have enough time interval between them | |
var newStopButtonPress = (Date.now() - timeLastPressedStopButton) > DEBOUNCE_INTERVAL ? true : false | |
if(state === 1 && newStopButtonPress) { | |
console.log("[" + Date.now() + "]: end button pressed, state: " + state); | |
timeLastPressedStopButton = Date.now(); | |
newStopButtonPress = false; | |
if(active) { | |
ledStripSuccess() | |
} else { | |
ledStripFail() | |
} | |
active = !active | |
} | |
} | |
// set up led strip | |
const NUM_LEDS = 210; | |
const STRIP_LENGTH = 3; | |
var Lights = new Uint32Array(NUM_LEDS); | |
ws281x.init(NUM_LEDS); | |
// light leds to show that the code is running... | |
for(var i=0; i<NUM_LEDS; i++) { Lights[i] = 0x0000ff } | |
Lights[NUM_LEDS - 1] = 0xff0000; // mark the end of the strip for debugging | |
ws281x.render(Lights); | |
function ledStripFail() { | |
for(var i=0; i<NUM_LEDS; i++) { | |
Lights[i] = 0xff0000 | |
ws281x.render(Lights) | |
} | |
} | |
function ledStripSuccess() { | |
for(var i=0; i<NUM_LEDS; i++) { | |
Lights[i] = 0x00ff00 | |
} | |
ws281x.render(Lights) | |
} | |
function clearStrip() { | |
for(var i=0; i<NUM_LEDS; i++) { | |
Lights[i] = 0x000000 | |
} | |
ws281x.render(Lights) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment