-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
418 lines (358 loc) · 11.3 KB
/
api.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
NanoPlay OS
Copyright (C) Subnodal Technologies. All Rights Reserved.
https://nanoplay.subnodal.com
Licenced by the Subnodal Open-Source Licence, which can be found at LICENCE.md.
*/
exports._communicators = "";
/*
@name OS_VERSION
@type const <String>
The version string of NanoPlay OS.
*/
exports.OS_VERSION = require("config").OS_VERSION;
/*
@name OS_VERNUM
@type const <Number>
The comparable version number of NanoPlay OS.
*/
exports.OS_VERNUM = require("config").OS_VERNUM;
/*
@name fillShapes
@type var <Boolean>
Whether to fill in the insides of shapes.
*/
exports.fillShapes = false;
function typeAll(params, type) {
for (var i = 0; i < params.length; i++) {
if (typeof(params[i]) != type) {
throw new TypeError("Expected a parameter to be " + type + ", but got " + typeof(params[i]) + " instead");
}
}
}
function typePin(pin) {
if (typeof(pin) != "number" || pin != Math.floor(pin)) {
throw new TypeError("Pin specified is not an integer");
}
if (pin < 0 || pin > 5) {
throw new TypeError("NanoPlay doesn't have pin " + String(pin) + "; only has pins 0 to 5");
}
}
/*
@name close
Close the app and return to the home screen.
*/
exports._communicators += ";var close=" + (function() {
_shouldClose = true;
}).toString();
/*
@name statusBar
Enable/disable showing the status bar. The status bar is drawn over everything else.
@param enable <Boolean = false> Whether to show the status bar
*/
exports._communicators += ";var statusBar=" + (function(enable) {
_showStatusBar = !!enable;
}).toString();
/*
@name clear
Clear the screen.
*/
exports.clear = function() {
g.clear();
};
/*
@name fill
Enable/disable the filling in of subsequent shapes. If disabled, only shape outlines are drawn.
@param enable <Boolean = false> Whether to fill in subsequent shapes
*/
exports.fill = function(enable) {
exports.fillShapes = !!enable;
};
/*
@name invert
Enable/disable the inversion of subequent graphics. If enabled, filled-in
shapes will appear as off whereas they would appear as on when inversion is
disabled.
@param enable <Boolean = false> Whether to invert subsequent graphics
*/
exports.invert = function(enable) {
g.setBgColor(enable & 1);
g.setColor((!enable) & 1);
};
/*
@name line
Draw a line from one coordinate to another.
@param x1 <Number> The X component of the first coordinate
@param y1 <Number> The Y component of the first coordinate
@param x2 <Number> The X component of the second coordinate
@param y2 <Number> The Y component of the second coordinate
*/
exports.line = function(x1, y1, x2, y2) {
typeAll(arguments, "number");
g.drawLine(x1, y1, x2, y2);
};
/*
@name rect
Draw a rectangle from a given coordinate.
@param x <Number> The X component of the origin coordinate
@param y <Number> The Y component of the origin coordinate
@param w <Number> The width of the rectangle
@param h <Number> The height of the rectangle
*/
exports.rect = function(x, y, w, h) {
typeAll(arguments, "number");
if (exports.fillShapes) {
g.fillRect(x, y, x + w, y + h);
} else {
g.drawRect(x, y, x + w, y + h);
}
};
/*
@name circle
Draw a circle around a given coordinate with a specified radius.
@param x <Number> The X component of the origin coordinate
@param y <Number> The Y component of the origin coordinate
@param radius <Number> The radius of the circle
*/
exports.circle = function(x, y, radius) {
typeAll(arguments, "number");
if (exports.fillShapes) {
g.fillCircle(x, y, radius);
} else {
g.drawCircle(x, y, radius);
}
};
/*
@name ellipse
Draw an ellipse from a given coordinate.
@param x <Number> The X component of the origin coordinate
@param y <Number> The Y component of the origin coordinate
@param w <Number> The width of the ellipse
@param h <Number> The height of the ellipse
*/
exports.ellipse = function(x, y, w, h) {
typeAll(arguments, "number");
if (exports.fillShapes) {
g.fillEllipse(x, y, x + w, y + h);
} else {
g.drawEllipse(x, y, x + w, y + h);
}
};
/*
@name text
Draw a string of text from a given coordinate.
@param x <Number> The X component of the origin coordinate
@param y <Number> The Y component of the origin coordinate
@param text <*> The string to be drawn as text
@param mini <Boolean = false> Whether to draw the text in a smaller font
*/
exports.text = function(x, y, text) {
var mini = arguments[3] || false;
typeAll([x, y], "number");
if (mini) {
g.setFont("4x6");
} else {
g.setFont("6x8");
}
g.drawString(String(text), x, y);
};
/*
@name getTextWidth
Calculate the width of a given string of text
@param text <*> The string to calculate the textual width of
@param mini <Boolean = false> Whether the text is set in a smaller font
@returns <Number> The width of the text in pixels
*/
exports.getTextWidth = function(text) {
var mini = arguments[1] || false;
if (mini) {
g.setFont("4x6");
} else {
g.setFont("6x8");
}
return g.stringWidth(String(text));
};
/*
@name getPixel
Get a specified pixel's value.
@param x <Number> The X component of the pixel's coordinate
@param y <Number> The Y component of the pixel's coordinate
@returns <Boolean> Whether the pixel is on or off
*/
exports.getPixel = function(x, y) {
typeAll(arguments, "number");
return !!g.getPixel(x, y);
};
/*
@name setPixel
Set a specified pixel's value.
@param x <Number> The X component of the pixel's coordinate
@param y <Number> The Y component of the pixel's coordinate
@param on <Boolean = true> Whether the pixel is on or off
*/
exports.setPixel = function(x, y) {
var on = arguments[2] || true;
typeAll(arguments, "number");
return g.setPixel(x, y, on & 1);
};
exports.tl = {
/*
@name tl.pressed
Whether the top-left button is pressed.
@returns <Boolean> Whether the button is pressed
*/
pressed: function() {
return BTN1.read();
}
};
exports.tr = {
/*
@name tr.pressed
Whether the top-right button is pressed.
@returns <Boolean> Whether the button is pressed
*/
pressed: function() {
return BTN2.read();
}
};
exports.bl = {
/*
@name bl.pressed
Whether the bottom-left button is pressed.
@returns <Boolean> Whether the button is pressed
*/
pressed: function() {
return BTN4.read();
}
};
exports.br = {
/*
@name br.pressed
Whether the bottom-right button is pressed.
@returns <Boolean> Whether the button is pressed
*/
pressed: function() {
return BTN3.read();
}
};
/*
@name readPin
Read an analog pin's value (on the back of the NanoPlay).
@param pin <Number> Index of the pin: an integer within the bounds 0 to 5 inclusive
@returns <Number> The analog value of the pin: a real number within the bounds 0 to 1 inclusive
*/
exports.readPin = function(pin) {
typePin(pin);
return analogRead(pin + 14); // Analog pins start at 14
};
/*
@name writePin
Write an analog pin's value (on the back of the NanoPlay).
@param pin <Number> Index of the pin: an integer within the bounds 0 to 5 inclusive
@param value <Number> The The analog value of the pin: a real number within the bounds 0 to 1 inclusive
*/
exports.writePin = function(pin, value) {
typePin(pin);
if (typeof(value) == "boolean") {
value = value & 1;
} else if (typeof(value) != "number" || value < 0 || value > 1) {
throw new TypeError("Value must be a positive real number within the bounds 0 to 1 inclusive");
}
analogWrite(pin + 14, value); // Analog pins start at 14
};
/*
@name readFile
Read a data file from the NanoPlay's storage.
@param filename <String> The filename to read from: a string with only characters a-z, A-Z and 0-9, between 1 to 20 characters long
@returns <String> The contents of the data file that have been read
*/
exports.readFile = function(filename) {
typeAll([filename], "string");
if (!(/^[a-zA-Z0-9]*$/.test(filename))) {
throw new TypeError("Filename must only contain characters a-z, A-Z and 0-9");
}
if (filename.length < 1 || filename.length > 20) {
throw new TypeError("Filename must be non-empty and be at most 20 characters long");
}
return require("Storage").read(filename + ".npd");
};
/*
@name writeFile
Write a data file to the NanoPlay's storage. Any data files with the same
name will be overwritten.
@param filename <String> The filename to write to: a string with only characters a-z, A-Z and 0-9, between 1 to 20 characters long
@param data <String> The contents of the data file to write
*/
exports.writeFile = function(filename, data) {
typeAll([filename], "string");
if (!(/^[a-zA-Z0-9]*$/.test(filename))) {
throw new TypeError("Filename must only contain characters a-z, A-Z and 0-9");
}
if (filename.length < 1 || filename.length > 20) {
throw new TypeError("Filename must be non-empty and be at most 20 characters long");
}
if (require("Storage").getFree() < data + 1000) {
throw new Error("Insufficient storage to store file");
}
return require("Storage").write(filename + ".npd", String(data));
};
/*
@name getFileList
Get a list of data files in the NanoPlay's storage.
@returns <Object> A list of filenames of data files
*/
exports.getFileList = function() {
return require("Storage").list()
.filter((a) => a.endsWith(".npd"))
.map((a) => a.split(".")[0])
;
};
/*
@name nfcSet
Set the NanoPlay's NFC chip to emit the specified URL to nearby devices.
@param url <String> The URL to emit to nearby devices
*/
exports.nfcSet = function(url) {
typeAll([url], "string");
NRF.nfcURL(url);
};
/*
@name getBatteryPercentage
Get the battery's fullness percentage.
@returns <Number> The percentage fullness of the battery: a number within the bounds of 0 to 100 inclusive
*/
exports.getBatteryPercentage = function() {
return E.getBattery();
};
/*
@name getTemperatureCelsius
Get the current measured temperature in degrees Celsius.
@returns <Number> The temperature in degrees Celsius
*/
exports.getTemperatureCelsius = function() {
return E.getTemperature();
};
/*
@name getTemperatureFahrenheit
Get the current measured temperature in degrees Fahrenheit.
@returns <Number> The temperature in degrees Fahrenheit
*/
exports.getTemperatureFahrenheit = function() {
return (exports.getTemperatureCelsius() * 1.8) + 32;
};
/*
@name getTemperatureKelvin
Get the current measured temperature in Kelvin.
@returns <Number> The temperature in Kelvin
*/
exports.getTemperatureKelvin = function() {
return exports.getTemperatureCelsius() + 273.15;
};
/*
@name getLocaleCode
Get the NanoPlay's current locale code. For example, for English (United Kingdom), the locale code would be `"en_GB"`.
@returns <String> The locale code of the currently-set locale
*/
exports.getLocaleCode = function() {
return require("l10n").getLocaleCode();
};