-
Notifications
You must be signed in to change notification settings - Fork 46
/
gyro.js
58 lines (50 loc) · 2.11 KB
/
gyro.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
// Module dependencies.
var Utilities = require('./utilities'),
Smoothing = require('./smoothing'),
config = require('./config');
//Proccess button events.
var motionProcessor = function(eventEmmiter) {
var varianceThreshhold = 1,
outputSmoothing = new Smoothing(smoothInput),
utilities = new Utilities(),
motionInputConfiguration = config.getControllerConfig().motionInputs,
smoothInput = config.getOptions().accelerometerSmoothing;
//Private methods
//data corrections so that each dirrection has a 0 throug x value
var correctData = function(motionAxis, data) {
var value;
//ensuring that both directions start from 0 and move to -x or x;
if (data[motionAxis.directionPin] === 1) {
//we need the values to be from 0 to x.
value = 255 - data[motionAxis.valuePin];
} else if (data[motionAxis.directionPin] === 2) {
//going in the oposite direction, we need to values to be from 0 to -x;
value = data[motionAxis.valuePin] * -1;
}
//return an object with both value and dirrection.
return {
direction: data[motionAxis.directionPin],
value: value
};
};
//process the axis movement.
var processAxis = function(motionAxis, data) {
//every motion will have a dirrection and a value
var motionValue = correctData(motionAxis, data),
lastPosition = outputSmoothing.readLastPosition(motionAxis.name);
//check if the values are within variance
if (utilities.isWithinVariance(lastPosition, motionValue.value, varianceThreshhold)) {
motionValue.value = outputSmoothing.smooth(motionAxis.name, motionValue.value);
eventEmmiter.emit(motionAxis.name + ':motion', motionValue);
}
};
// Public methods
//process all configured motion inputs.
this.process = function(data) {
for (var i = 0; i < motionInputConfiguration.length; i++) {
processAxis(motionInputConfiguration[i], data);
}
};
};
module.exports = motionProcessor;