-
Notifications
You must be signed in to change notification settings - Fork 1
/
BraillePad.js
200 lines (187 loc) · 6.05 KB
/
BraillePad.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
dojo.declare('BraillePad', null, {
constructor: function() {
var self = this;
var s = new io.Socket('localhost', {port: 8008, rememberTransport: false});
s.connect();
s.on('connect', function() {
console.log('connected');
self.message('pad connected');
});
s.on('message', function(e) {
if (e.event == 'down') {
self.padDown(e);
} else if(e.event == 'up') {
self.padUp(e);
}
});
// default mapping from pad buttons to Braille dots (can be changed with setup pad)
this.kmap = { 0: 3, // LL
15: 2, // ML
2: 1, // UL
1: 4, // UR
13: 5, // MR
3: 6 }; // LR
// finite state machine transition table, maps state and input to new state or action
this.state_table = {
idle: {
down: { state: 'p1' }
},
p1: {
timer: { state: 'p2' }
},
p2: {
down: { state: 'p1' },
up: { state: 'p1' },
timer: { action: 'preview' }
},
holding: {
down: { state: 'p1' },
up: { state: 'p1' },
timer: { action: 'holding' }
},
releasing: {
up: { action: 'isClear' },
timer: { action: 'isClear' }
},
setup: {
down: { state: 'setup' },
up: { state: 'setup' },
timer: { state: 'setup1' }
},
setup1: {
down: { state: 'setup' },
up: { state: 'setup' },
timer: { action: 'isFull' }
},
clearing: {
up: { action: 'isClear' },
down: { state: 'setup' }
}
};
this.state = 'idle';
this.dots = [ ];
this.current_letter = '';
this.timing = true; // false to disable the timer while speaking
setInterval(dojo.hitch(this, 'onTimer'), 100);
},
fsm: function(input) {
var entry = this.state_table[this.state][input];
//console.log('fsm', input, this.state);
if (!entry) {
return;
}
if (entry.state) {
this.state = entry.state;
}
if (entry.action == 'preview') {
if (this.dots.length > 0) {
this.state = 'holding';
this.holdCount = 0;
this.preview();
} else {
this.state = 'idle';
}
} else if (entry.action == 'holding') {
this.holdCount += 1;
if (this.holdCount > 10) {
this.state = 'releasing';
this.write();
}
} else if (entry.action == 'isFull') {
if (this.dots.length == 6) { // holding 6 down
this.state = 'clearing';
this.message('Keys noted, now release them all.');
}
} else if (entry.action == 'isClear') {
if (this.dots.length === 0) { // all released
this.state = 'idle';
//this.message();
}
}
},
preview: function() {
var letter = dotsToLetter(this.dots);
var msg = '';
if (this.dots.length == 1) {
msg = 'dot ' + this.dots;
} else {
msg = 'dots ' + this.dots.join(' ');
}
if (letter) {
msg = msg + '. Makes ' + letter + '.';
console.log('preview', letter);
}
this.timing = false;
this.message(msg, 'preview', dojo.hitch(this, function() {
this.timing = true; }));
this.current_letter = letter;
},
write: function() {
if (this.current_letter) {
console.log('write', this.current_letter);
dojo.publish('letter', [ this.current_letter ]);
console.log('published');
}
},
startSetup: function() {
this.kmap = {};
this.state = 'setup';
this.message('Press and hold dots 1 through 6 in order.');
},
padDown: function(evt) {
//console.log('down', evt);
var dot = this.kmap[evt.button];
if (! dot && this.state.match(/setup/) ) {
dot = (this.dots.length + 1) + '';
this.kmap[evt.button] = dot;
}
if (dot && dojo.indexOf(this.dots, dot) == -1) {
console.log('down', evt, dot, this.dots);
this.dots.push(dot);
this.dots.sort();
this.display();
this.fsm('down');
}
},
padUp: function(evt) {
//console.log('up', evt);
var dot = this.kmap[evt.button];
if (dot && dojo.indexOf(this.dots, dot) > -1) {
//console.log('up', evt, dot, this.dots);
this.dots = dojo.filter(this.dots, function(d) { return d != dot; });
this.display();
this.fsm('up');
if (this.state.match(/setup/)) {
delete this.kmap[evt.button];
}
}
},
onTimer: function() {
if (this.timing) {
this.fsm('timer');
}
},
display: function() {
console.log('display', this.dots);
dojo.publish('dots', [this.dots]);
},
displayKeyMap: function() {
//console.log('displayKeyMap', keyNames);
var dots = [ ];
for (var kc in this.kmap) {
dots[this.kmap[kc]] = keyName(kc);
}
//console.log('dots', dots);
var kmsg = [ ];
for (var i = 1; i <= 6; i++) {
this.dmap[i].innerHTML = dots[i];
kmsg.push(dots[i] + "→Dot " + i);
}
//console.log(kmsg);
dojo.byId('keymap').innerHTML = kmsg.join(', ');
},
message: function(text, channel, after) {
console.log('message', text, channel);
dojo.publish('message', [text, channel, after]);
}
});