Save devices state

Restore state
Switch to use intval as camera
This commit is contained in:
mmcwilliams 2018-02-01 14:52:47 -05:00
parent db47c4b524
commit 34a7d37b7b
4 changed files with 237 additions and 143 deletions

View File

@ -1,20 +1,16 @@
{
"version" : "2.0.0",
"ext_port" : 1111,
"arduino" : {
"baud" : 57600,
"board" : "uno",
"serialDelay" : 20,
"sequenceDelay" : 100,
"profiles" : {
"mcopy" : {
"label" : "Default mcopy profile",
"cam" : {
"time" : 630,
"time" : 850,
"delay" : 50,
"momentary" : 0
},
"proj" : {
"time" : 1300,
"time" : 1400,
"delay" : 50,
"momentary" : 0
},
@ -23,7 +19,25 @@
"after" : 0
}
},
"jk103" : {
"label" : "JK103",
"cam" : {
"time" : 600,
"delay" : 50,
"momentary" : 240
},
"proj" : {
"time" : 950,
"delay" : 50,
"momentary" : 240
},
"black" : {
"before" : 0,
"after" : 0
}
},
"jk_original" : {
"label" : "JK45 profile",
"cam" : {
"time" : 750,
"delay" : 50,
@ -39,25 +53,22 @@
"after" : 250
}
},
"jk103" : {
"intval3" : {
"label" : "INTVAL3",
"cam" : {
"time" : 600,
"time" : 630,
"delay" : 50,
"momentary" : 240
},
"proj" : {
"time" : 950,
"delay" : 50,
"momentary" : 240
},
"black" : {
"before" : 0,
"after" : 0
"momentary" : 0
}
}
},
"arduino" : {
"baud" : 57600,
"board" : "uno",
"serialDelay" : 20,
"sequenceDelay" : 100,
"cam" : {
"time" : 750,
"time" : 850,
"delay" : 50,
"momentary" : 300
},

View File

@ -12,6 +12,10 @@
padding: 6px 12px;
font-size: 21px;
min-width: 300px;
&.active{
border-color: @SELECTED;
color: @SELECTED;
}
}
button{
margin-top: -1px;

View File

@ -3,40 +3,65 @@ var devices = {};
devices.init = function () {
'use strict';
devices.listen();
devices.listen();;
devices.profiles();
gui.overlay(true);
gui.spinner(true, 'Checking for connected devices...');
};
devices.profiles = function () {
const keys = Object.keys(mcopy.cfg.profiles);
const elem = $('#profile')
let opt;
elem.empty();
for (let key of keys) {
opt = $('<option>');
opt.val(key);
opt.text(mcopy.cfg.profiles[key].label);
elem.append(opt);
}
elem.on('change', (t) => {
devices.profile($('#profile').val());
})
devices.profile(keys[0]);
};
devices.profile = function (profile) {
log.info(`Changed configuration profile to "${profile}"`, 'DEVICES', true, true);
const p = mcopy.cfg.profiles[profile];
const keys = Object.keys(p);
for (let key of keys) {
mcopy.cfg[key] = keys[key]
}
};
devices.listen = function () {
'use strict';
ipcRenderer.on('ready', devices.ready);
ipcRenderer.on('intval', devices.intvalCb);
};
devices.ready = function (event, arg) {
'use strict';
console.dir(arg)
let opt;
let devs = [];
let notify = 'Connected to '
let notify = 'Connected to ';
gui.spinner(false);
gui.overlay(false);
for (let i in arg) {
if (arg[i] !== '/dev/fake') {
devs.push(arg[i]);
devs.push(arg[i].arduino);
if (arg[i].arduino && arg[i].arduino !== '/dev/fake') {
if (notify === 'Connected to ') {
notify += arg[i] + ' '
notify += arg[i].arduino + ' '
} else {
notify += '& ' + arg[i]
notify += `& ${arg[i].arduino}`
}
}
opt = $('<option>');
opt.val(arg[i]);
opt.text(arg[i]);
opt.val(`ARDUINO_${arg[i].arduino}`);
opt.text(arg[i].arduino);
$(`#${i}_device`).empty();
$(`#${i}_device`).append(opt);
if (notify !== 'Connected to ') {
gui.notify()
}
if (notify !== 'Connected to ') {
gui.notify('DEVICES', notify)
}
if (devs.length > 0) {
$('#devices').empty();
@ -59,7 +84,7 @@ devices.intval = function () {
url : url
}
if ( url !== '' && typeof url !== 'undefined') {
proceed = confirm(`Are you sure you want to connect to INTVAL3 ${url}?`)
proceed = confirm(`Are you sure you want to connect to INTVAL3 @ ${url}?`)
} else {
alert('Cannot connect to INTVAL3 url as entered.')
}
@ -70,17 +95,28 @@ devices.intval = function () {
ipcRenderer.send('intval', obj)
} else {
$('#camera_type_arduino').prop('checked', 'checked');
$('#intval').removeClass('active');
}
};
devices.intvalCb = function (evt, args) {
'use strict';
let state;
gui.spinner(false);
gui.overlay(false);
if (args.connected && args.connected === true) {
//success state
state = JSON.parse(args.state);
mcopy.state.camera.pos = state.counter;
mcopy.state.camera.direction = state.frame.dir;
$('#intval').val(args.url);
$('#intval').addClass('active');
$('#camera_type_intval').prop('checked', 'checked');
gui.notify('DEVICES', `Connected to INTVAL3 @ ${args.url}`)
gui.updateState()
} else {
$('#camera_type_arduino').prop('checked', 'checked');
$('#intval').removeClass('active');
}
};

View File

@ -1,3 +1,6 @@
/* jshint esversion: 6, asi: true, strict: true*/
/* global require, setTimeout, process, console*/
'use strict'
const electron = require('electron')
@ -11,13 +14,18 @@ const events = require('events')
const async = require('async')
const path = require('path')
const ee = new events.EventEmitter()
const capture = require('./lib/capture')(ee)
//const capture = require('./lib/capture')(ee)
const settings = require('./lib/settings')
const Server = require('./lib/server')
const Intval = require('./lib/intval')
const mcopy = {}
const log = {}
const proj = {}
const cam = {}
const light = {}
const dev = {}
let mainWindow
let mscript
@ -27,35 +35,32 @@ let projector
let camera
let server
let menu
let log = {}
//console.log(process.version)
mcopy.cfg = require('./data/cfg.json')
mcopy.settings = {}
var enumerateDevices = function (err, devices) {
dev.enumerate = function (err, devices) {
if (err) {
log.info(err, 'SERIAL', false, true)
setTimeout(() =>{
distinguishDevices([])
dev.all([])
}, 1000)
} else {
log.info('Found ' + devices.length + ' USB devices', 'SERIAL', true, true)
console.dir(devices)
devices = favorDevices(devices)
console.dir(devices)
distinguishDevices(devices)
log.info(`Found ${devices.length} USB devices`, 'SERIAL', true, true)
devices = dev.favor(devices)
dev.all(devices)
}
}
var favorDevices = function (devices) {
const past = mcopy.settings.devices.filter(dev => {
if (dev.arduino) {
return dev
dev.favor = function (devices) {
const past = mcopy.settings.devices.filter(device => {
if (device.arduino) {
return device
}
}).map(dev => {
return dev.arduino
}).map(device => {
return device.arduino
})
if (past.length === 0) {
return devices
@ -71,7 +76,7 @@ var favorDevices = function (devices) {
return devices
}
var distinguishDevice = function (device, callback) {
dev.distinguish = function (device, callback) {
var connectCb = function (err, device) {
if (err) {
return console.error(err)
@ -94,7 +99,7 @@ var distinguishDevice = function (device, callback) {
if (err) {
return console.error(err)
}
rememberDevice(device, type)
dev.remember('arduino', device, type)
log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true)
if (callback) { callback(err, type); }
@ -103,7 +108,7 @@ var distinguishDevice = function (device, callback) {
};
//Cases for 1 or 2 arduinos connected
var distinguishDevices = function (devices) {
dev.all = function (devices) {
const connected = {
projector : false,
camera : false,
@ -185,7 +190,7 @@ var distinguishDevices = function (devices) {
checklist = devices.map(device => {
return next => {
distinguishDevice(device, (err, type) => {
dev.distinguish(device, (err, type) => {
if (err) {
console.error(err)
return next()
@ -197,42 +202,65 @@ var distinguishDevices = function (devices) {
async.series(checklist, () => {
//done checking devices
let c = {}
let p = {}
let l = {}
if (!connected.projector) {
fakeProjector()
}
p.arduino = connected.projector
if (!connected.camera) {
fakeCamera()
}
c.arduino = connected.camera
if (mcopy.settings.camera.intval) {
c.intval = mcopy.settings.camera.intval
console.dir(mcopy.settings.camera)
setTimeout(() => {
cam.connectIntval(null, { connect : true, url : c.intval })
}, 1000)
}
if (!connected.light) {
fakeLight()
}
devicesReady(connected.projector, connected.camera, connected.light)
l.arduino = connected.light
dev.ready(p, c, l)
})
};
var rememberDevice = function (device, type) {
dev.remember = function (which, device, type) {
let deviceEntry
let match = mcopy.settings.devices.filter(dev => {
if (dev.arduino && dev.arduino === device) {
const match = mcopy.settings.devices.filter(dev => {
if (dev[which] && dev[which] === device) {
return dev
}
})
if (match.length === 0) {
deviceEntry = {
arduino : device,
type : type
}
deviceEntry[which] = device
mcopy.settings.devices.push(deviceEntry)
settings.update('devices', mcopy.settings.devices)
settings.save()
}
}
};
var devicesReady = function (projector, camera, light) {
mainWindow.webContents.send('ready', {camera: camera, projector: projector, light: light })
settings.update('camera', { arduino : camera })
settings.update('projector', { arduino : projector })
settings.update('light', { arduino : light })
dev.ready = function (projector, camera, light) {
mainWindow.webContents.send('ready', {
camera: camera,
projector: projector,
light: light,
profile: mcopy.settings.profile
})
settings.update('camera', camera)
settings.update('projector', projector)
settings.update('light', light)
settings.save()
};
@ -250,38 +278,38 @@ var createWindow = function () {
minHeight : 600
})
mainWindow.loadURL('file://' + __dirname + '/index.html')
//mainWindow.webContents.openDevTools()
if (process.argv.indexOf('-d') !== -1 || process.argv.indexOf('--dev') !== -1) {
mainWindow.webContents.openDevTools()
}
mainWindow.on('closed', () => {
mainWindow = null
})
}
var light = {}
light.init = function () {
light.listen()
};
}
light.listen = function () {
ipcMain.on('light', (event, arg) => {
light.set(arg.rgb, arg.id)
event.returnValue = true
})
};
}
light.set = function (rgb, id) {
var str = rgb.join(',');
arduino.send('light', mcopy.cfg.arduino.cmd.light, (ms) => {
light.end(rgb, id, ms)
})
arduino.string('light', str)
};
}
light.end = function (rgb, id, ms) {
log.info('Light set to ' + rgb.join(','), 'LIGHT', true, true)
mainWindow.webContents.send('light', {rgb: rgb, id : id, ms: ms})
};
}
var proj = {}
proj.state = {
dir : true //default dir
};
}
proj.init = function () {
proj.listen()
}
@ -331,9 +359,7 @@ proj.end = function (cmd, id, ms) {
mainWindow.webContents.send('proj', {cmd: cmd, id : id, ms: ms})
}
var cam = {
intval : null
}
cam.intval = null
cam.state = {
dir : true //default dir
}
@ -348,34 +374,61 @@ cam.set = function (dir, id) {
cmd = mcopy.cfg.arduino.cmd.cam_backward
}
cam.state.dir = dir
arduino.send('camera', cmd, (ms) => {
if (cam.intval) {
cam.intval.setDir(dir, ms => {
cam.end(cmd, id, ms)
})
/*
intval.setDir('camera', dir, (ms) => {
} else {
arduino.send('camera', cmd, ms => {
cam.end(cmd, id, ms)
})
*/
}
}
cam.move = function (frame, id) {
let cmd = mcopy.cfg.arduino.cmd.camera
arduino.send('camera', cmd, (ms) => {
if (cam.intval) {
cam.intval.move(ms => {
cam.end(cmd, id, ms)
})
/*
intval.move('camera', (ms) => {
} else {
arduino.send('camera', cmd, ms => {
cam.end(cmd, id, ms)
})
*/
}
}
cam.exposure = function (exposure, id) {
intval.setDir('camera', exposure, (ms) => {
let cmd = 'E'
cam.intval.setDir('camera', exposure, ms => {
cam.end(cmd, id, ms)
})
}
cam.connectIntval = function (event, arg) {
if (arg.connect) {
cam.intval = new Intval(arg.url)
cam.intval.connect((err, ms, state) => {
if (err) {
mainWindow.webContents.send('intval', { connected : false })
log.info(`Cannot connect to ${arg.url}`, 'INTVAL', true, true)
cam.intval = null
delete cam.intval
} else {
mainWindow.webContents.send('intval', { connected : true, url : arg.url, state : state })
log.info(`Connected to INTVAL3 @ ${arg.url}`, 'INTVAL', true, true)
settings.update('camera', { intval : arg.url })
settings.save()
dev.remember('intval', arg.url, 'camera')
}
})
} else if (arg.disconnect) {
cam.intval = null
}
}
cam.listen = function () {
ipcMain.on('cam', (event, arg) => {
if (typeof arg.dir !== 'undefined') {
@ -385,22 +438,7 @@ cam.listen = function () {
}
event.returnValue = true
})
ipcMain.on('intval', (event, arg) => {
if (arg.connect) {
cam.intval = new Intval(arg.url)
cam.intval.connect((err, ms, state) => {
if (err) {
mainWindow.webContents.send('intval', { connected : false })
log.info(`Cannot connect to ${arg.url}`, 'INTVAL', true, true)
cam.intval = null
} else {
log.info()
}
})
} else if (arg.disconnect) {
cam.intval = null
}
})
ipcMain.on('intval', cam.connectIntval)
}
cam.end = function (cmd, id, ms) {
var message = ''
@ -464,7 +502,7 @@ log.info = function (action, service, status, display) {
log.display(obj)
}
}
/*
var transfer = {}
transfer.init = function () {
@ -472,7 +510,7 @@ transfer.init = function () {
};
transfer.listen = function () {
ipcMain.on('transfer', (event, arg) => {
var res = '';
let res = '';
//also turn on and off
if (arg.action === 'enable') {
capture.active = true
@ -488,11 +526,11 @@ transfer.listen = function () {
event.returnValue = res
})
}
*/
var init = function () {
createWindow()
//createMenu()
createMenu()
log.init()
light.init()
proj.init()
@ -509,7 +547,7 @@ var init = function () {
mcopy.settings = settings.all()
setTimeout( () => {
arduino.enumerate(enumerateDevices)
arduino.enumerate(dev.enumerate)
}, 1000)
}
@ -526,3 +564,8 @@ app.on('activate', () => {
createWindow();
}
});
mcopy.relaunch = function () {
app.relaunch({args: process.argv.slice(1).concat(['--relaunch'])})
app.exit(0)
}