-
Notifications
You must be signed in to change notification settings - Fork 1
/
power_glove.js
102 lines (83 loc) · 1.73 KB
/
power_glove.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var sys = require('sys');
var child = require('child_process');
var five = require ('/Users/omnijinx/Documents/repos/johnny-five/lib/johnny-five.js');
var board = new five.Board();
var mode;
var middleTrigger = 0.25;
var middleMin = 0.15;
var middleRange = 0.17;
var ledCount = 8;
function setVolume(level) {
child.exec("osascript -e 'set Volume " + level + "'");
lighting = cycle[Math.round(level)];
lc.row(0, 0, lighting);
};
function getVolume() {
cmd = child.spawn("osascript -e 'get volume settings'");
cmd.stdout.on('data', function(data) {
console.log(data);
});
}
board.on('ready', function() {
led = new five.Led({
pin: 13
});
led.on();
lc = new five.LedControl({
pins: {
data: 2,
clock: 3,
cs: 4
},
devices: 1,
isMatrix: true
});
dial = new five.Sensor({
pin: 'A0',
freq: 100
});
middleFinger = new five.Sensor({
pin: 'A1',
freq: 100
});
ringFinger = new five.Sensor({
pin: 'A2',
freq: 100
});
hwSwitch = new five.Button({
pin: 13
});
board.repl.inject({
led: led,
lc: lc,
dial: dial,
middleFinger: middleFinger,
ringFinger: ringFinger,
hwSwitch: hwSwitch,
setVolume: setVolume,
getVolume: getVolume
});
function monitorSwitch() {
if (hwSwitch.isDown) {
mode = 'dial';
} else {
mode = 'glove';
}
setTimeout(monitorSwitch, 250);
}
monitorSwitch();
idx = 0;
cycle = [8, 12, 14, 15, 31, 63, 127, 255];
dial.scale([0, 7]).on('read', function() {
if (mode === 'dial') {
setVolume(Math.abs(this.scaled - 7));
}
});
middleFinger.scale([0, 1]).on('read', function() {
if (mode === 'glove') {
newVolume = (this.scaled - middleMin) * (7 / middleRange);
newVolume = Math.max(0, Math.min(newVolume, 10));
setVolume(newVolume);
}
});
});