-
Notifications
You must be signed in to change notification settings - Fork 481
/
PlaygroundComponents.js
288 lines (257 loc) · 7.77 KB
/
PlaygroundComponents.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Utilities for initializing Circuit Playground board components with APIs
* conforming to Maker API droplet blocks.
*/
import {
N_COLOR_LEDS,
CP_COMMAND,
CP_ACCEL_STREAM_ON,
} from './PlaygroundConstants';
import _ from 'lodash';
import five from '@code-dot-org/johnny-five';
import PlaygroundIO from 'playground-io';
import PlaygroundButton from './Button';
import PlaygroundThermometer from './Thermometer';
import TouchSensor from './TouchSensor';
import Piezo from './Piezo';
import NeoPixel from './NeoPixel';
import Led from './Led';
import Switch from './Switch';
/**
* Initializes a set of Johnny-Five component instances for the currently
* connected Circuit Playground board.
*
* @param {five.Board} board - the johnny-five board object that needs new
* components initialized.
* @returns {Promise.<Object.<String, Object>>} board components
*/
export function createCircuitPlaygroundComponents(board) {
// Must initialize sound sensor BEFORE left button, otherwise left button
// will not respond to input. This has something to do with them sharing
// pin 4 on the board.
return Promise.all([
initializeSoundSensor(board),
initializeLightSensor(board),
initializeThermometer(board),
]).then(([soundSensor, lightSensor, tempSensor]) => {
return {
colorLeds: initializeColorLeds(board),
led: new Led({board, pin: 13}),
toggleSwitch: new Switch(board),
buzzer: new Piezo({
board,
pin: 5,
controller: PlaygroundIO.Piezo,
}),
soundSensor,
lightSensor,
tempSensor,
accelerometer: initializeAccelerometer(board),
buttonL: new PlaygroundButton({board, pin: 4}),
buttonR: new PlaygroundButton({board, pin: 19}),
};
});
}
/**
* De-initializes any Johnny-Five components that might have been created
* by createCircuitPlaygroundComponents
* @param {Object} components - map of components, as originally returned by
* createCircuitPlaygroundComponents. This object will be mutated: Destroyed
* components will be removed. Additional members of this object will be
* ignored.
* @param {boolean} shouldDestroyComponents - whether or not to fully delete the
* components, or just reset to their initial state.
*/
export function cleanupCircuitPlaygroundComponents(
components,
shouldDestroyComponents
) {
if (components.colorLeds) {
components.colorLeds.forEach(led => {
led.color('white');
led.intensity(100);
led.off();
});
}
if (components.led) {
components.led.intensity(0);
components.led.off();
}
if (components.buzzer) {
components.buzzer.off();
components.buzzer.stop();
}
//Disable and clear sensors
if (components.soundSensor) {
components.soundSensor.disable();
components.soundSensor._events = {};
}
if (components.lightSensor) {
components.lightSensor.disable();
components.lightSensor._events = {};
}
if (components.tempSensor) {
components.tempSensor.disable();
components.tempSensor._events = {};
}
if (components.accelerometer) {
components.accelerometer.stop();
components.accelerometer._events = {};
}
if (shouldDestroyComponents) {
delete components.colorLeds;
delete components.led;
delete components.toggleSwitch;
delete components.buzzer;
delete components.soundSensor;
delete components.lightSensor;
delete components.tempSensor;
delete components.accelerometer;
delete components.buttonL;
delete components.buttonR;
}
}
/**
* Re-initializes sensor components and accelerometer
* @param {Object} components - map of components, as originally returned by
* createCircuitPlaygroundComponents.
*/
export function enableCircuitPlaygroundComponents(components) {
if (components.soundSensor) {
components.soundSensor.enable();
}
if (components.lightSensor) {
components.lightSensor.enable();
}
if (components.tempSensor) {
components.tempSensor.enable();
}
if (components.accelerometer) {
components.accelerometer.start();
}
}
/**
* Set of classes used by interpreter to understand the type of instantiated
* objects, allowing it to make methods and properties of instances available.
*/
export const componentConstructors = {
Led,
Board: five.Board,
NeoPixel,
PlaygroundButton,
Switch,
Piezo,
Sensor: five.Sensor,
Thermometer: five.Thermometer,
Pin: five.Pin,
Accelerometer: five.Accelerometer,
Animation: five.Animation,
/**
* @link https://en.wikipedia.org/wiki/Three_Laws_of_Robotics
* 1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.
* 2. A robot must obey orders given it by human beings except where such orders would conflict with the First Law.
* 3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Law.
*/
Servo: five.Servo,
TouchSensor,
};
function initializeColorLeds(board) {
return _.range(N_COLOR_LEDS).map(i => initializeColorLed(board, i));
}
function initializeColorLed(board, pin) {
return new NeoPixel({
board,
controller: PlaygroundIO.Pixel,
pin,
});
}
function initializeSoundSensor(board) {
return new Promise(resolve => {
const sensor = new five.Sensor({
board,
pin: 'A4',
freq: 100,
});
addSensorFeatures(five.Board.fmap, sensor);
sensor.once('data', () => resolve(sensor));
});
}
function initializeLightSensor(board) {
return new Promise(resolve => {
const sensor = new five.Sensor({
board,
pin: 'A5',
freq: 100,
});
addSensorFeatures(five.Board.fmap, sensor);
sensor.once('data', () => resolve(sensor));
});
}
/**
* Set scale for setScale block, which records for later use to a five.Sensor instance.
* @param {five.Board.fmap} fmap mapping function
* @param {five.Sensor} sensor
*/
function addSensorFeatures(fmap, sensor) {
/**
* Cache scale setting locally (cannot grab after the fact from five.Sensor).
* Scale is a 2-element array of [low, high].
* @type {Array.<number>|undefined}
*/
sensor.setScale = (low, high) => {
sensor.scale(low, high);
};
}
function initializeThermometer(board) {
return new Promise(resolve => {
const thermometer = new five.Thermometer({
board,
controller: PlaygroundThermometer,
pin: 'A0',
freq: 100,
});
thermometer.once('data', () => resolve(thermometer));
});
}
function initializeAccelerometer(board) {
const accelerometer = new five.Accelerometer({
board,
controller: PlaygroundIO.Accelerometer,
});
accelerometer.start = function () {
accelerometer.io.sysexCommand([CP_COMMAND, CP_ACCEL_STREAM_ON]);
};
accelerometer.getOrientation = function (orientationType) {
if (undefined === orientationType) {
return [
accelerometer.getOrientation('x'),
accelerometer.getOrientation('y'),
accelerometer.getOrientation('z'),
];
}
// Accelerometer on the express board is rotated 90 degrees from classic board.
// Conditional ensures consistent output of 'pitch'/'roll' across both boards
if (board.isExpressBoard) {
if (orientationType === 'pitch') {
return accelerometer['roll'];
} else if (orientationType === 'roll') {
return -1 * accelerometer['pitch'];
}
}
return accelerometer[orientationType];
};
accelerometer.getAcceleration = function (accelerationDirection) {
if (undefined === accelerationDirection) {
return [
accelerometer.getAcceleration('x'),
accelerometer.getAcceleration('y'),
accelerometer.getAcceleration('z'),
];
}
if (accelerationDirection === 'total') {
return accelerometer.acceleration;
}
return accelerometer[accelerationDirection];
};
return accelerometer;
}