-
Notifications
You must be signed in to change notification settings - Fork 1
/
testOS.js
70 lines (60 loc) · 1.44 KB
/
testOS.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
/* sample OS to test that the Grove is working */
var keys = {}
var text = ''
var cursorBlink = 0
var eventOutput = 'Start typing! Text is saved when you press ENTER.'
var error
function main(event, dataRecorder) {
var toSave = {}
if (event.type === 'startup') {
text = dataRecorder.read('myfile')
}
if (event.type === 'keyDown') {
keys[event.key] = true
switch (event.key) {
case 13:
// enter
text += '\n'
toSave['myfile'] = text
break
case 8:
// backspace
text = text.slice(0, text.length - 1)
break
case 192:
// tilde
// test that we can recover from an infinite loop
while (true) {}
case 27:
// escape
// test that errors are printed
error = error ? null : "You pressed escape. Reboot to recover."
default:
text += String.fromCharCode(event.key)
}
updateEventOutput(event)
}
if (event.type === 'keyUp') {
delete keys[event.key]
updateEventOutput(event)
}
if (event.type === 'clock') {
cursorBlink = (cursorBlink + 1) % 20
}
if (error) {
throw error
}
return {
screen: [eventOutput]
.concat(cursor(text).split('\n')),
records: toSave
}
}
function cursor(string) {
if (cursorBlink >= 10) return string + '\u2592'
return string
}
function updateEventOutput(event) {
eventOutput =
'' + event.type + ' ' + Object.keys(keys).join(', ')
}