-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
executable file
ยท155 lines (130 loc) ยท 4.25 KB
/
main.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
#! /usr/bin/env node
const BBCMicrobit = require('bbc-microbit');
const robot = require('robotjs');
let microbit;
const {
clear,
drawPattern,
drawProgress,
MATRIX_SIZE
} = require('./draw');
let shuttingDown = false;
let showingProgress = false;
let slideCount = 0;
const heldButtons = {
left: false,
right: false
};
const BUZZER_PIN = 0; // pin the buzzer is connected to for MI:power board
const PERIOD = 150; // time to check accelerometer in ms
const BUTTON_ACTIONS = ['released', 'pressed', 'held'];
console.info('๐ ๐ฑ๏ธ micro:clicker\n');
function connectToMicrobit() {
console.info('๐ฎ Scanning for micro:bit...');
console.info(' Hold your micro:bit level!\n');
BBCMicrobit.discover(microbitDiscovered => {
console.info(`๐ค micro:bit found!\n`);
microbit = microbitDiscovered;
microbit.on('disconnect', _ => {
console.info('๐ค micro:bit disconnected.an');
});
console.info('๐ Connecting to micro:bit');
microbit.connectAndSetUp(_ => {
console.info('๐ค micro:bit connected!\n');
console.info(' โก๏ธ๏ธ Press right arrow to move right.');
console.info(' โฌ
๏ธ Press left arrow to move left.');
console.info(' โฌ
๏ธ โก๏ธ ๏ธ Hold both buttons to disconnect.');
console.info(' ๐ Tilt up to show current progress in slides.\n');
// listen for button presses
microbit.on('buttonAChange', value => handleButton('left', BUTTON_ACTIONS[value]));
microbit.on('buttonBChange', value => handleButton('right', BUTTON_ACTIONS[value]));
microbit.subscribeButtons();
// poll the accelerometer
microbit.on('accelerometerChange', (x, y, z) => handleAccelerometer(x, y, z));
microbit.writeAccelerometerPeriod(PERIOD, microbit.subscribeAccelerometer());
// set up the sounder on pin 0
microbit.pinOutput(BUZZER_PIN, _ => microbit.pinAnalog(BUZZER_PIN, playTune));
// show a pattern on load
microbit.writeLedMatrixState(drawPattern('noiseOne'), _ => {
setTimeout(_ => microbit.writeLedMatrixState(drawPattern('noiseTwo'), _ => {
setTimeout(_ => microbit.writeLedMatrixState(drawPattern('right')), 100);
}), 50);
});
});
});
}
function disconnectFromMicrobit() {
// time to wait for anyting to finish
setTimeout(_ => {
microbit.disconnect(_ => {
console.log('๐ Bye-bye\n');
process.exit(1);
});
}, 500);
}
function handleSlideCount(type) {
if (type === 'left') {
if (slideCount > 0) {
slideCount--;
}
} else {
if (slideCount < MATRIX_SIZE) {
slideCount++;
}
}
}
function handleButton(direction, action) {
// console.info(`โบ๏ธ Button ${direction} ${action}\n`);
if (shuttingDown) {
return;
}
if (action === 'released') {
heldButtons[direction] = false;
microbit.writeLedMatrixState(drawPattern('diamond'));
}
if (action === 'pressed') {
robot.keyTap(direction);
handleSlideCount(direction);
microbit.writeLedMatrixState(drawPattern(direction));
}
if (action === 'held') {
heldButtons[direction] = true;
// if both buttons are held
if (heldButtons['left'] && heldButtons['right']) {
shuttingDown = true;
console.log('\n๐ Disconnecting micro:bit!');
microbit.writeLedMatrixState(clear(), _ => {
microbit.writeLedMatrixState(drawPattern('heart'), _ => {
playTune();
setTimeout(_ => {
microbit.writeLedMatrixState(clear(), _ => disconnectFromMicrobit());
}, 500)
});
});
}
}
}
function handleAccelerometer(x, y, z) {
const tilted = z > 0;
if (tilted && !showingProgress) {
// console.info('๐ค micro:bit titled');
showingProgress = true;
microbit.writeLedMatrixState(drawProgress(slideCount), _ => {
setTimeout(_ => {
showingProgress = false;
microbit.writeLedMatrixState(drawPattern('diamond'));
}, 1000);
});
}
}
function playNote(note, duration) {
return new Promise(resolve => {
microbit.writePin(BUZZER_PIN, note, _ => {
setTimeout(_ => microbit.writePin(BUZZER_PIN, 0, resolve), duration);
});
});
}
function playTune() {
playNote(15, 50).then(_ => playNote(5, 100)).then(_ => playNote(1, 150));
}
connectToMicrobit();