Skip to content

Instantly share code, notes, and snippets.

@larafale
Created March 13, 2017 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larafale/b9d9637eccc2006984cb7df4d41a3b32 to your computer and use it in GitHub Desktop.
Save larafale/b9d9637eccc2006984cb7df4d41a3b32 to your computer and use it in GitHub Desktop.
var Gpio = require('onoff').Gpio
var signal = new Gpio(23, 'in', 'both')
var _ = require('lodash')
var captureTimeOut
var startTime
var data = []
signal.watch((err, val) => {
if(!captureTimeOut) {
startTime = microsecond()
captureTimeOut = setTimeout(capture, 1000)
}
var now = microsecond()
var pulse = now - startTime
startTime = now
data.push([val, pulse])
})
function microsecond(){
return Math.round(process.hrtime()[1] / 1000)
}
function capture(){
var onlyOnes = _.filter(data, x => x[0]==1)
var bin = _.map(onlyOnes, x => x[1] > 1000 ? '1':'0').join('')
var hex = parseInt(bin, 2).toString(16).toUpperCase()
console.log('-------------')
console.log('binary', bin)
console.log('hex', hex)
// reset logic
data = []
captureTimeOut = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment