intval3/tests/gpio.js

49 lines
1.0 KiB
JavaScript
Raw Normal View History

2017-09-16 18:47:33 +00:00
'use strict'
const Gpio = require('onoff').Gpio
2017-09-16 19:07:34 +00:00
const btn = Gpio(5, 'in', 'both')
2017-09-16 18:47:33 +00:00
2017-09-16 19:07:34 +00:00
console.log('Watching input on GPIO 05')
let saveTime = 0
let active = false
btn.watch((err, val) => {
const NOW = +new Date()
/* Button + 10K ohm resistor */
/* 1 = open */
/* 0 = closed */
2017-09-16 18:47:33 +00:00
if (err) {
return console.error(err)
}
2017-09-18 00:27:51 +00:00
console.log(`Release switch val: ${val}`)
if (val === 1) {
//console.log('open')
} else if (val === 0) {
//console.log('closed')
}
if (val === 0) {
if ((!active && saveTime === 0) || (active && NOW - saveTime > 10 * 1000)) {
saveTime = NOW
active = true //maybe unncecessary
} else {
//saveTime = 0
//active = false
}
} else if (val === 1) {
if (active) {
2017-09-17 23:57:38 +00:00
if (NOW - saveTime > 50 && NOW - saveTime < 1000) {
console.log('Started Frame')
2017-09-17 23:57:38 +00:00
} else if (NOW - saveTime >= 1000) {
console.log('Started Sequence')
}
console.log(`Release closed for ${NOW - saveTime}`)
saveTime = 0
active = false
}
}
})
/*setInterval(() => {
console.log(btn.readSync())
}, 1000)*/