Created
December 2, 2017 16:25
-
-
Save Jan-Bart/c288743e46f2ef6b8175e2762bf10d16 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
const async = require('async'); | |
const gpio = require('rpi-gpio'); | |
const BUZZER_PIN = 5; | |
const TIME_ON = 150; | |
const TIME_OFF = 900; | |
const BUZZ_TIME = 20; | |
gpio.setMode(gpio.MODE_BCM); | |
gpio.setup(BUZZER_PIN, gpio.DIR_LOW, write); | |
function makeSound(cb) { | |
setTimeout(() => { | |
gpio.write(BUZZER_PIN, true, cb); | |
}, TIME_OFF); | |
} | |
function muteSound(cb) { | |
setTimeout(() => { | |
gpio.write(BUZZER_PIN, false, cb); | |
}, TIME_ON); | |
} | |
function highAndLow(callback) { | |
makeSound((err) => { | |
muteSound((err) => { | |
callback(err); | |
}); | |
}); | |
} | |
function write() { | |
let count = 0; | |
async.whilst( | |
() => { return count < BUZZ_TIME; }, | |
(cb) => { count++; highAndLow(cb); }, | |
function (err, n) { | |
console.log(err, n); | |
} | |
); | |
} | |
process.on('SIGINT', function () { | |
console.log("Caught interrupt signal"); | |
muteSound(() => { | |
process.exit(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment