intval3/experiments/gpio.js

119 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-09-16 18:47:33 +00:00
'use strict'
const Gpio = require('onoff').Gpio
2017-09-24 19:06:04 +00:00
let release
let micro
let fwd
let bwd
process.on('SIGINT', () => {
if (fwd && fwd.writeSync) {
console.log(`Setting fwd to 0`)
fwd.writeSync(0)
}
if (bwd && bwd.writeSync) {
console.log(`Setting bwd to 0`)
bwd.writeSync(0)
}
2017-09-24 20:05:35 +00:00
process.exit()
2017-09-24 19:06:04 +00:00
})
2017-09-22 04:29:31 +00:00
function releaseTest () {
2017-09-24 19:06:04 +00:00
const PIN = 6
release = Gpio(PIN, 'in', 'both')
2017-09-23 12:53:48 +00:00
console.log(`Watching input on GPIO 0${PIN}`)
2017-09-22 04:29:31 +00:00
let saveTime = 0
let active = false
2017-09-24 19:06:04 +00:00
release.watch((err, val) => {
2017-09-22 04:29:31 +00:00
const NOW = +new Date()
/* Button + 10K ohm resistor */
/* 1 = open */
/* 0 = closed */
if (err) {
return console.error(err)
}
2017-10-19 21:57:28 +00:00
//console.log(`Release switch val: ${val}`)
2017-10-19 23:22:21 +00:00
//console.log(`RELEASE: ${val} ${active} ${NOW} ${saveTime}`)
2017-09-22 04:29:31 +00:00
if (val === 0) {
//console.log('closed')
} else if (val === 1) {
//console.log('open')
}
if (val === 0) {
//closed
if ((!active && saveTime === 0) || (active && NOW - saveTime > 10 * 1000)) {
saveTime = NOW
active = true //maybe unncecessary
} else {
//saveTime = 0
//active = false
}
} else if (val === 1) {
//open
if (active) {
if (NOW - saveTime > 50 && NOW - saveTime < 1000) {
console.log('Started Frame')
} else if (NOW - saveTime >= 1000) {
console.log('Started Sequence')
}
2017-10-19 23:22:21 +00:00
//console.log(`Release closed for ${NOW - saveTime}`)
2017-09-22 04:29:31 +00:00
saveTime = 0
active = false
}
}
2017-09-22 04:29:31 +00:00
})
}
function microTest () {
2017-09-24 19:06:04 +00:00
const PIN = 5
micro = Gpio(PIN, 'in', 'both')
2017-09-23 12:53:48 +00:00
console.log(`Watching input on GPIO 0${PIN}`)
2017-09-22 04:29:31 +00:00
let saveTime = 0
2017-09-23 12:53:48 +00:00
let frameActive = true //this._state.frame.active
2017-09-22 04:29:31 +00:00
let primed = false //this._state.primed
2017-09-24 19:06:04 +00:00
micro.watch((err, val) => {
2017-09-22 04:29:31 +00:00
const NOW = +new Date()
if (err) {
return console.error(err)
}
console.log(`Micro switch val: ${val}`)
if (val === 0) {
//console.log('closed')
} else if (val === 1) {
//console.log('open')
}
2017-09-23 12:53:48 +00:00
if (val === 0 && frameActive) {
if (!primed) {
primed = true
saveTime = NOW
console.log('Primed')
}
} else if (val === 1 && frameActive) {
if (primed) {
primed = false
setTimeout( () => {
console.log(`Stop Frame after ${NOW - saveTime}`)
}, 10)
}
2017-09-22 04:29:31 +00:00
}
})
}
2017-09-23 21:56:13 +00:00
//test stepping up of 3.3V RPI logic via
2017-09-24 19:06:04 +00:00
//Sparkfun PRT-10968 (NPC1402)
2017-09-23 21:56:13 +00:00
function stepupTest () {
const FWD = 13 // RPIO PIN 13
2017-09-23 21:56:13 +00:00
const BWD = 19
2017-09-24 19:06:04 +00:00
fwd = Gpio(FWD, 'out')
bwd = Gpio(BWD, 'out')
2017-09-23 21:56:13 +00:00
2017-10-20 00:26:37 +00:00
console.log(`Setting pin ${BWD} high`)
fwd.writeSync(0)
bwd.writeSync(1)
2017-09-23 21:56:13 +00:00
}
releaseTest()
2017-10-19 23:12:15 +00:00
microTest()
//stepupTest()