Add better state object structure

And use in preliminary logic
This commit is contained in:
mmcwilliams 2017-08-29 22:27:56 -04:00
parent cf537af849
commit c87d425c57
1 changed files with 16 additions and 4 deletions

View File

@ -35,8 +35,12 @@ class Intval {
this._pin = {} this._pin = {}
this._state = { this._state = {
dir : true, //forward dir : true, //forward
running : false, frame : {
startFrame : 0 start : 0,
active : false,
val : 0,
expected : 0
}
} }
this._declarePins() this._declarePins()
} }
@ -61,6 +65,8 @@ class Intval {
if (err) { if (err) {
console.error(err) console.error(err)
} }
this._state.frame.val = val
//determine when to stop
} }
_watchRelease (err, val) { _watchRelease (err, val) {
if (err) { if (err) {
@ -74,7 +80,8 @@ class Intval {
this._state.dir = val this._state.dir = val
} }
frame (dir = true, time = 0, delay = 0) { frame (dir = true, time = 0, delay = 0) {
this._state.running = true this._state.frame.start = +new Date()
this._state.frame.active = true
this._pin.micro.watch(this._watchMicro) this._pin.micro.watch(this._watchMicro)
if (delay !== 0) { if (delay !== 0) {
setTimeout(function () { setTimeout(function () {
@ -96,8 +103,13 @@ class Intval {
_stop () { _stop () {
this._pin.fwd.set(0) this._pin.fwd.set(0)
this._pin.bwd.set(0) this._pin.bwd.set(0)
let len = (+new Date()) - this._state.frame.start
console.log(`Frame stopped`, { ms : len })
this._pin.micro.unwatch() this._pin.micro.unwatch()
this._state.running = false this._state.frame.active = false
} }
status () { status () {
return this._state return this._state