-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccelerometer.js
More file actions
89 lines (74 loc) · 2.46 KB
/
Accelerometer.js
File metadata and controls
89 lines (74 loc) · 2.46 KB
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
// Required modules
let vgm = require("vgm");
let widget = require("widget");
let dialog = require("dialog");
// Function to display the instructional screen
function showInstructionScreen() {
let instructions = [
"Welcome to Accelerometer app",
"Measurement Values:",
"Pitch: Tilt forward/backward",
"Roll: Tilt left/right",
"Yaw: Horizontal rotation",
"Press OK to start the app"
];
// Display each line of instructions as a separate dialog
for (let i = 0; i < instructions.length; i++) {
dialog.message("Instructions", instructions[i]);
}
}
// Initialize text element IDs
let pitchText, rollText, yawText;
// Capture initial values
let initialPitch = vgm.getPitch();
let initialRoll = vgm.getRoll();
let initialYaw = vgm.getYaw();
// Function to update and display VGM data
function displayVgmData() {
// Get current pitch, roll, and yaw data
let currentPitch = vgm.getPitch();
let currentRoll = vgm.getRoll();
let currentYaw = vgm.getYaw();
// Calculate relative values from initial position
let pitch = currentPitch - initialPitch;
let roll = currentRoll - initialRoll;
let yaw = currentYaw - initialYaw;
// Ensure roll, pitch, yaw are within the expected range
if (pitch < -180) {
pitch += 360;
} else if (pitch > 180) {
pitch -= 360;
}
if (roll < -180) {
roll += 360;
} else if (roll > 180) {
roll -= 360;
}
if (yaw < -180) {
yaw += 360;
} else if (yaw > 180) {
yaw -= 360;
}
// Update the data on the screen
widget.remove(pitchText);
widget.remove(rollText);
widget.remove(yawText);
pitchText = widget.addText(10, 30, "Secondary", "Pitch: " + to_string(pitch) + "°");
rollText = widget.addText(90, 30, "Secondary", "Roll: " + to_string(roll) + "°");
yawText = widget.addText(10, 50, "Secondary", "Yaw: " + to_string(yaw) + "°");
}
// Function to show the main app screen
function showAppScreen() {
// Initial setup
widget.addText(10, 10, "Primary", "VGM Data:");
widget.show();
// Main loop to update the display every 0.3 seconds
while (true) {
displayVgmData();
delay(300); // Update every 0.3 seconds
}
}
// Show instruction screen and wait for user confirmation
showInstructionScreen();
// Show the main app screen
showAppScreen();