Last active
August 29, 2015 14:16
-
-
Save bombarie/19a534603c403f220530 to your computer and use it in GitHub Desktop.
Node.JS Myo script reflecting the detected hand poses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Node version 0.10.33 | |
// Myo SDK version 0.8.1 | |
//- - - - - - - - - - - - - - - - - - - - | |
// | |
// NODE.JS EXPRESS START | |
// | |
// | |
var express = require('express'); | |
var path = require('path'); | |
var favicon = require('serve-favicon'); | |
var logger = require('morgan'); | |
var cookieParser = require('cookie-parser'); | |
var bodyParser = require('body-parser'); | |
var routes = require('./routes/index'); | |
var users = require('./routes/users'); | |
// | |
// | |
// NODE.JS EXPRESS START | |
// | |
//- - - - - - - - - - - - - - - - - - - - | |
//- - - - - - - - - - - - - - - - - - - - | |
// | |
// MYO PART | |
// | |
// | |
// more info: https://github.com/stolksdorf/myo.js | |
var Myo = require('myo'); | |
// I found these options somewhere online. | |
// Haven't fooled around with them extensively but on first glance they don't seem to have any influence... | |
var myo = Myo.create(0, { | |
armbusy_threshold : 200, | |
doubleTap: { | |
time: [200, 300], | |
threshold: 2.5 | |
} | |
}); | |
var fistOn = false; // used to hold fist state | |
var spreadOn = false; // used to hold fingers_spread state | |
myo.on('connected', function() { | |
console.log("myo connected"); | |
}); | |
myo.on('unlock', function(){ | |
console.log("unlocked"); | |
}); | |
myo.on('lock', function(){ | |
console.log("locked"); | |
}); | |
// Why doesn't double_tap register when using it to unlock? | |
// I know, chicken and egg, but feels more natural that it *would* trigger when unlocking | |
myo.on('double_tap', function (edge) { | |
if (edge) { | |
console.log("double tap ON"); | |
} else { | |
console.log("double tap OFF"); | |
} | |
}); | |
myo.on('fingers_spread', function(edge){ | |
if (edge) { | |
console.log("fingers_spread ON"); | |
spreadOn = true; | |
} else { | |
console.log("fingers_spread OFF"); | |
spreadOn = false; | |
} | |
}); | |
myo.on('fist', function(edge){ | |
if (edge) { | |
console.log("fist ON"); | |
fistOn = true; | |
} else { | |
console.log("fist OFF"); | |
fistOn = false; | |
} | |
}); | |
myo.on('wave_in', function(edge){ | |
if (edge) { | |
console.log("wave_in ON"); | |
} else { | |
console.log("wave_in OFF"); | |
} | |
}); | |
myo.on('wave_out', function(edge){ | |
if (edge) { | |
console.log("wave_out ON"); | |
} else { | |
console.log("wave_out OFF"); | |
} | |
}); | |
var checkKeepUnlocked = function() { | |
if (spreadOn || fistOn) { | |
myo.unlock(500); | |
} | |
} | |
var keepMyoUnlockedInterval = setInterval(checkKeepUnlocked, 1000 / 25); | |
// | |
// | |
// MYO PART | |
// | |
//- - - - - - - - - - - - - - - - - - - - | |
//- - - - - - - - - - - - - - - - - - - - | |
// | |
// EXPRESS CONFIGURING | |
// | |
// | |
var app = express(); | |
// view engine setup | |
app.set('views', path.join(__dirname, 'views')); | |
app.set('view engine', 'jade'); | |
// uncomment after placing your favicon in /public | |
//app.use(favicon(__dirname + '/public/favicon.ico')); | |
app.use(logger('dev')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(cookieParser()); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
app.use('/', routes); | |
app.use('/users', users); | |
// catch 404 and forward to error handler | |
app.use(function(req, res, next) { | |
var err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); | |
// error handlers | |
// development error handler | |
// will print stacktrace | |
if (app.get('env') === 'development') { | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: err | |
}); | |
}); | |
} | |
// production error handler | |
// no stacktraces leaked to user | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: {} | |
}); | |
}); | |
// | |
// | |
// EXPRESS CONFIGURING | |
// | |
//- - - - - - - - - - - - - - - - - - - - | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment