-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashlight.js
More file actions
120 lines (107 loc) · 4.26 KB
/
Flashlight.js
File metadata and controls
120 lines (107 loc) · 4.26 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
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
// Import required modules for display and keyboard functionality
const display = require("display");
const keyboard = require("keyboard");
// Define color palette for the flashlight
// Colors are defined using RGB values (red, green, blue)
const colours = [
display.color(0, 0, 0), // black
display.color(127, 127, 127), // grey
display.color(255, 255, 255), // white
display.color(255, 0, 0), // red
display.color(255, 165, 0), // orange
display.color(255, 255, 0), // yellow
display.color(0, 255, 0), // green
display.color(0, 255, 255), // cyan
display.color(0, 0, 255), // blue
display.color(75, 0, 130), // indigo
display.color(138, 43, 226), // violet
display.color(255, 0, 255) // magenta
];
// Application state variables
var currentColour = 2; // Start with white color (index 2)
var lightOn = false; // Flashlight starts in off state
var exitApp = false; // Controls main application loop
// Get display dimensions for responsive layout
const displayWidth = display.width();
const displayHeight = display.height();
// Scale font size based on display width (larger screens get bigger fonts)
const fontScale = (displayWidth > 300 ? 1 : 0);
// Show the initial standby screen
displayStandby();
/**
* Displays the standby/welcome screen with instructions
* Shows app title, controls, and author credit
*/
function displayStandby() {
// Clear screen with black background
display.fill(colours[0]);
// Center-align all text
display.setTextAlign('center', 'middle');
// Display app title in blue
display.setTextSize(2 + fontScale);
display.setTextColor(BRUCE_PRICOLOR);
display.drawText("Flashlight", displayWidth / 2, 10 + fontScale * 5);
// Display control instructions in grey
display.setTextSize(1 + fontScale);
display.setTextColor(colours[1]);
display.drawText("Select: On/Off", displayWidth / 2, displayHeight / 6 * 2);
display.drawText("Next/Prev: Change Colours", displayWidth / 2, displayHeight / 6 * 3);
display.drawText("Exit: Close Flashlight", displayWidth / 2, displayHeight / 6 * 4);
// Display author credit in white at bottom
display.setTextSize(1 + fontScale);
display.setTextColor(colours[2]);
display.drawText("By github.com/emericklaw", displayWidth / 2, displayHeight - 5 - fontScale * 5);
}
// Main application loop - continues until exitApp becomes true
while (!exitApp) {
// Check for exit button press (ESC key)
if (getEscPress()) {
exitApp = true;
break;
}
// Handle Select button press - toggle flashlight on/off
if (keyboard.getSelPress()) {
lightOn = !lightOn; // Toggle the light state
if (lightOn) {
// Turn on flashlight with current color
display.fill(colours[currentColour]);
} else {
// Turn off flashlight and show standby screen
displayStandby();
}
}
// Handle Previous button press - cycle to previous color or turn on light
if (keyboard.getPrevPress()) {
if (!lightOn) {
// If light is off, turn it on with current color
lightOn = true;
} else {
// If light is on, cycle to previous color
currentColour--;
// Wrap around to last color if we go below index 1 (skip black at index 0)
if (currentColour < 1) {
currentColour = colours.length - 1;
}
}
// Update display with new color
display.fill(colours[currentColour]);
}
// Handle Next button press - cycle to next color or turn on light
if (keyboard.getNextPress()) {
if (!lightOn) {
// If light is off, turn it on with current color
lightOn = true;
} else {
// If light is on, cycle to next color
currentColour++;
// Wrap around to first usable color if we exceed array bounds
if (currentColour > colours.length - 1) {
currentColour = 1; // Skip black (index 0), start from grey (index 1)
}
}
// Update display with new color
display.fill(colours[currentColour]);
}
// Small delay to prevent excessive CPU usage and button bouncing
delay(50);
}