-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathmain.js
126 lines (106 loc) · 3.36 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
/*
* Copyright (c) 2016-2017 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK.
*
* This work is licensed under the
* Creative Commons Attribution 4.0 International License.
* To view a copy of this license, visit
* <http://creativecommons.org/licenses/by/4.0>.
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/
import config from "mc/config";
import Timer from "timer";
import parseBMF from "commodetto/parseBMF";
import parseBMP from "commodetto/parseBMP";
import Poco from "commodetto/Poco";
import Resource from "Resource";
import ILI9341 from "ili9341";
import LIS3DH from "lis3dh";
let pixelsOut = new ILI9341({});
const width = pixelsOut.width;
const height = pixelsOut.height;
let render = new Poco(pixelsOut);
let font = parseBMF(new Resource("OpenSans-Semibold-18.bf4"));
let ball = parseBMP(new Resource("ball-color.bmp"));
ball.alpha = parseBMP(new Resource("ball-alpha.bmp"));
ball.x = width >> 1;
ball.y = height >> 1;
ball.vx = 0;
ball.vy = 0;
ball.yMin = font.height * 3;
ball.backGroundColor = render.makeColor(0, 0, 0);
const textColor = render.makeColor(255, 255, 255);
const backgroundColor = render.makeColor(64, 64, 64);
const barColor = render.makeColor(128, 128, 128);
render.begin();
render.fillRectangle(backgroundColor, 0, 0, width, ball.yMin);
render.fillRectangle(ball.backgroundColor, 0, ball.yMin, width, height);
render.end();
let sensor = new LIS3DH({});
Timer.repeat(() => {
let values = sensor.sample();
if (180 === parseInt(config.orientation)) {
values.x = -values.x;
}
render.begin(0, 0, width, ball.yMin);
render.fillRectangle(backgroundColor, 0, 0, width, height);
drawBar("X", values.x, 0, 0, width, font.height);
drawBar("Y", values.y, 0, font.height, width, font.height);
drawBar("Z", values.z, 0, font.height * 2, width, font.height);
render.end();
ball.vx = (ball.vx + values.y) * 0.98;
ball.vy = (ball.vy + values.x) * 0.98;
let x = ball.x + ball.vx;
let y = ball.y + ball.vy;
if (x < 0) {
x = -x;
ball.vx = -ball.vx;
}
else if (x > (width - ball.width)) {
x = width - ball.width;
ball.vx = -ball.vx;
}
if (y < ball.yMin) {
y = ball.yMin;
ball.vy = -ball.vy;
}
else if (y > (height - ball.height)) {
y = height - ball.height;
ball.vy = -ball.vy;
}
moveBallTo(x, y)
}, 17);
function formatValue(value) {
if (!value)
return value;
if (value < 0)
return value.toFixed(3);
return "+" + value.toFixed(3);
}
function drawBar(label, value, x, y, width, height) {
const halfWidth = width >> 1;
const barWidth = (value * halfWidth) | 0;
if (value > 0)
render.fillRectangle(barColor, x + halfWidth, y, barWidth, height);
else
render.fillRectangle(barColor, x + halfWidth + barWidth, y, -barWidth, height);
render.drawText(label + " " + formatValue(value), font, textColor, x + 50, y);
}
function moveBallTo(x, y) {
const w = ball.width, h = ball.height;
if ((Math.abs(ball.x - x) <= w) && (Math.abs(ball.y - y) <= h))
render.begin(Math.min(ball.x, x), Math.min(ball.y, y), w << 1, h << 1); // often overdrawing
else {
render.begin(ball.x, ball.y, w, h);
render.fillRectangle(ball.backgroundColor, 0, 0, width, height);
render.continue(x, y, w, h);
}
render.fillRectangle(ball.backgroundColor, 0, 0, width, height);
render.drawMasked(ball, x, y, 0, 0, w, h, ball.alpha, 0, 0);
render.end();
ball.x = x;
ball.y = y;
}