From e6070465e5c552fd68eadbbff5223812e1cf4a13 Mon Sep 17 00:00:00 2001 From: mmcw-dev Date: Wed, 20 Dec 2017 18:40:51 -0500 Subject: [PATCH] Sequence logic ready for testing --- index.js | 86 ++++++++++++++++++++++++++++++++++++++++++- lib/sequence/index.js | 84 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 163 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 193cd2c..e2091fd 100644 --- a/index.js +++ b/index.js @@ -50,6 +50,8 @@ function createBLE () { ble.on('exposure', bExposure) ble.on('delay', bDelay) ble.on('counter', bCounter) + ble.on('sequence', bSequence) + ble.on('reset', bReset) } //Restify functions @@ -246,6 +248,17 @@ function rSequence (req, res, next) { let dir = true let exposure = 0 let delay = 0 + + if (intval._state.frame.dir !== true) { + dir = false + } + if (intval._state.frame.exposure !== 0) { + exposure = intval._state.frame.exposure + } + if (intval._state.frame.delay !== 0) { + delay = intval._state.frame.delay + } + if (req.query && typeof req.query.dir !== 'undefined') { if (typeof req.query.dir === 'string') { dir = (req.query.dir === 'true') @@ -274,13 +287,24 @@ function rSequence (req, res, next) { exposure = req.body.exposure } } - if (sequence.active) { + if (sequence._state.active) { return sequence.stop(() => { res.send({ stopped : true }) return next() }) } else { - return sequence.start({}, (seq) => { + return sequence.start({ + loop : [ (next) => { + intval.frame(dir, exposure, (len) => { + next() + }), + }, (next) => { + setTimeout(() => { + next() + }, delay) + } + ] + }, (seq) => { res.send(seq) return next() }) @@ -379,6 +403,64 @@ function bCounter (obj, cb) { return cb() } +function bSequence (obj, cb) { + let dir = true + let exposure = 0 + let delay = 0 + + if (intval._state.frame.dir !== true) { + dir = false + } + if (intval._state.frame.exposure !== 0) { + exposure = intval._state.frame.exposure + } + if (intval._state.frame.delay !== 0) { + delay = intval._state.frame.delay + } + + if (typeof obj.dir !== 'undefined') { + if (typeof obj.dir === 'string') { + dir = (obj.dir === 'true') + } else { + dir = obj.dir + } + } + if (typeof obj.exposure !== 'undefined') { + if (typeof obj.exposure === 'string') { + exposure = parseInt(obj.exposure) + } else { + exposure = obj.exposure + } + } + if (sequence._state.active) { + return sequence.stop(() => { + return cb() + }) + } else { + console.time('sequence time') + sequence.start({ + loop : [ (next) => { + intval.frame(dir, exposure, (len) => { + next() + }), + }, (next) => { + setTimeout(() => { + next() + }, delay) + } + ] + }, (seq) => { + console.timeEnd('sequence time') + }) + return cb() + } +} + +function bReset (obj, cb) { + intval.reset() + setTimeout(cb, 10) +} + function index (req, res, next) { fs.readFile(INDEXPATH, 'utf8', (err, data) => { if (err) { diff --git a/lib/sequence/index.js b/lib/sequence/index.js index 98d371a..10849a4 100644 --- a/lib/sequence/index.js +++ b/lib/sequence/index.js @@ -1,17 +1,91 @@ 'use strict' -const sequence = { +const log = require('../log')('seq') + +/** Object sequence features */ +const sequence = {}; + +sequence._state = { + arr : [], active : false, + paused : false, frame: false, - delay : false + delay : false, + count : 0, + stop : null +} + +sequence._loop = { + arr : [], + count : 0, + max : 0 } sequence.start = function (options, cb) { - sequence.active = true + sequence._state.active = true + sequence._state.count = 0 + + if (options.arr) { + sequence._state.arr = options.arr + } + + if (options.loop) { + sequence._loop.arr = options.loop + sequence._loop.count = 0 + } + + if (options.maxLoop) { + sequence._loop.max = options.maxLoop + } else { + sequence._loop.max = 0 + } + + sequence.step() } -sequence.stop = function (cb) { - sequence.active = false +sequence.stop = function (next) { + sequence._state.active = false + sequence._state.count = 0 + sequence._loop.count = 0 + sequence._state.stop() + next() +} + +sequence.pause = function () { + sequence._state.paused = true +} + +sequence.resume = function () { + sequence._state.paused = false + sequence.step() +} + +sequence.step = function () { + if (sequence._state.active && !sequence._state.paused) { + if (sequence._state.arr.length > 0 && typeof sequence._state.arr[sequence._state.count] !== 'undefined') { + return sequence._state.arr[sequence._state.count](() => { + sequence._state.count++ + sequence.step() + }) + } else if (sequence._loop.arr.length > 0 && typeof sequence._loop.arr[sequence._state.count] !== 'undefined') { + if (sequence._state.count > sequence._loop.arr.length) { + sequence._state.count = 0 + sequence._loop.count++ + } + if (sequence._loop.count > sequence._loop.max) { + return sequence.stop() + } + return sequence._loop.arr[sequence._state.count](() => { + sequence._state.count++ + }) + } else{ + return sequence.stop() + } + } else if (sequence._state.paused) { + log.info('Sequence paused', { loop : sequence._loop.count, count : sequence._state.count }) + } else if (!sequence._state.active) { + log.info('Sequence stopped', { loop : sequence._loop.count, count : sequence._state.count }) + } } module.exports = sequence \ No newline at end of file