Use log library in intval module

This commit is contained in:
mmcwilliams 2017-09-25 23:26:13 -04:00
parent 82aa857ffd
commit 495e59eae2
1 changed files with 26 additions and 33 deletions

View File

@ -6,7 +6,7 @@ let Gpio
try {
Gpio = require('onoff').Gpio
} catch (e) {
console.warn('Failed including Gpio, using sim')
log.warn('Failed including Gpio, using sim')
Gpio = require('../../lib/onoffsim').Gpio
}
@ -55,11 +55,14 @@ class Intval {
primed : false //is ready to stop frame
}
}
this._releaseMin = 50
this._releaseSequence = 1000
this._microDelay = 10 // delay after stop signal before stopping motors
this._declarePins()
process.on('SIGINT', this._undeclarePins)
process.on('uncaughtException', this._undeclarePins)
}
/**
* (internal function) Declares all Gpio pins that will be used
@ -71,8 +74,8 @@ class Intval {
pin = PINS[p]
if (pin.edge) this._pin[p] = Gpio(pin.pin, pin.dir, pin.edge)
if (!pin.edge) this._pin[p] = Gpio(pin.pin, pin.dir)
log.info('_declarePins', { pin : pin.pin, dir : pin.dir, edge : pin.edge })
}
console.dir(this._pin)
this._pin.release.watch(this._watchRelease)
}
/**
@ -81,10 +84,16 @@ class Intval {
*
*/
_undeclarePins () {
if (!this._pin) return process.exit()
log.warn('_undeclarePins', { pin : PINS.fwd.pin, val : 0, reason : 'exiting'})
this._pin.fwd.writeSync(0)
log.warn('_undeclarePins', { pin : PINS.bwd.pin, val : 0, reason : 'exiting'})
this._pin.bwd.writeSync(0)
this._pin.fwd.unexport()
this._pin.bwd.unexport()
this._pin.micro.unexport()
this._pin.release.unexport()
process.exit()
}
/**
* Start motor in forward direction by setting correct pins in h-bridge
@ -113,7 +122,7 @@ class Intval {
let len = (+new Date()) - this._state.frame.start
console.log(`Frame stopped ${len}ms`)
log.info(`Frame stopped ${len}ms`)
this._pin.micro.unwatch()
this._state.frame.active = false
@ -138,21 +147,21 @@ class Intval {
_watchMicro (err, val) {
const NOW = +new Date()
if (err) {
console.error(err)
log.error('_watchMicro', err)
}
//determine when to stop
if (val === 0 && this._state.frame.active) {
if (!this._state.micro.primed) {
this._state.micro.primed = true
this._state.micro.time = NOW
console.log('Mircoswitch primed to stop motor')
log.info('Mircoswitch primed to stop motor')
}
} else if (val === 1 && this._state.frame.active) {
if (this._state.micro.primed) {
this._state.micro.primed = false
this._state.micro.time = 0
setTimeout( () => {
console.log(`Stopped frame after ${NOW - this._state.micro.time}ms`)
log.info(`Stopped frame after ${NOW - this._state.micro.time}ms`)
}, this._microDelay)
}
}
@ -178,9 +187,9 @@ class Intval {
const NOW = +new Date()
let press = 0
if (err) {
return console.error(err)
return log.error(err)
}
console.log(`Release switch val: ${val}`)
log.info(`Release switch val: ${val}`)
if (val === 0) {
//closed
if ((!this._state.release.active && this._state.release.time === 0) || (this._state.release.active && (NOW - this._state.release.time) > (this._releaseSequence * 10))
@ -197,7 +206,7 @@ class Intval {
} else if (press >= this._releaseSequence) {
this.sequence()
}
console.log(`Release closed for ${press}ms`)
log.info(`Release closed for ${press}ms`)
this._state.release.time = 0
this._state.release.active = false
}
@ -213,7 +222,7 @@ class Intval {
*/
setDir (val = true) {
if (typeof val !== 'boolean') {
return console.warn('Direction must be represented as either true or false')
return log.warn('Direction must be represented as either true or false')
}
this._state.dir = val
}
@ -222,10 +231,9 @@ class Intval {
*
* @param {?boolean} [dir="null"] (optional) Direction of the frame
* @param {?integer} [time="null"] (optional) Exposure time, 0 = minimum
* @param {?integer} [delay="null"] (optional) Delay after frame before another can be started
*
*/
frame (dir = null, time = null, delay = null) { //may be overloaded, delay is suspect
frame (dir = null, time = null) {
if (dir === true || (dir === null && this._state.dir === true) ) {
dir = true
} else {
@ -238,31 +246,16 @@ class Intval {
time = 0
}
if (delay === null && this._state.delay !== 0) {
delay = this._state.delay
} else {
delay = 0
}
this._state.frame.start = +new Date()
this._state.frame.active = true
this._pin.micro.watch(this._watchMicro)
if (delay !== 0) {
setTimeout(function () {
if (dir) {
this._startFwd()
} else {
this._startBwd()
}
}, delay)
log.info('frame', {dir : dir, time : time})
if (dir) {
this._startFwd()
} else {
if (dir) {
this._startFwd()
} else {
this._startBwd()
}
this._startBwd()
}
}
/**
@ -270,7 +263,7 @@ class Intval {
*
*/
sequence () {
console.log(`Started sequence`)
log.info('sequence', `Started sequence`)
}
status () {
return this._state