-
Notifications
You must be signed in to change notification settings - Fork 0
/
v004
107 lines (94 loc) · 2.78 KB
/
v004
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
// Variables to store the microwave wattages and the input time
var wattageFrom = 800;
var wattageTo = 700;
var inputTime = 0; // in seconds
var selectedMinutes = 0;
var pressTime;
// Function to convert the time based on wattage
function convertTime(time, fromWatt, toWatt) {
return time * (fromWatt / toWatt);
}
// Function to convert seconds into minutes and seconds format
function formatTime(seconds) {
var min = Math.floor(seconds / 60);
var sec = seconds % 60;
return min + " min " + sec + " s";
}
// Function to display the main screen
function drawMainScreen() {
g.clear();
g.setFont("6x8", 1);
g.drawString("Convert Microwave Time", 5, 5);
g.drawString("", 5, 20); // Empty line
g.drawString("From: " + wattageFrom + "W", 5, 35);
g.drawString("Current time: " + formatTime(inputTime), 5, 50);
g.drawString("", 5, 65); // Empty line
g.drawString("To: " + wattageTo + "W", 5, 80);
g.drawString("Converted time: " + formatTime(Math.round(convertTime(inputTime, wattageFrom, wattageTo))), 5, 95);
g.drawString("", 5, 110); // Empty line
g.drawString("Short Press to choose time", 5, 125);
g.drawString("Long Press to choose Wattage", 5, 140);
g.flip();
}
// Function to show the menu for minute selection
function showMinuteMenu() {
var minuteOptions = {
"": { "title": "Choose Minutes" }
};
for (var i = 0; i <= 99; i++) {
minuteOptions[i + "m"] = (function(minute) {
return function() {
selectedMinutes = minute;
showSecondMenu();
};
})(i);
}
E.showMenu(minuteOptions);
}
// Function to show the menu for second selection
function showSecondMenu() {
var secondOptions = {
"": { "title": "Choose Seconds" }
};
for (var i = 0; i < 60; i++) {
secondOptions[i + "s"] = (function(second) {
return function() {
inputTime = (selectedMinutes * 60) + second;
drawMainScreen();
};
})(i);
}
E.showMenu(secondOptions);
}
// Function to show the menu for wattage selection
function showWattageMenu() {
var wattageOptions = {
"": { "title": "Choose Conversion" },
"800W to 700W": function() {
wattageFrom = 800;
wattageTo = 700;
drawMainScreen();
},
"700W to 800W": function() {
wattageFrom = 700;
wattageTo = 800;
drawMainScreen();
}
};
E.showMenu(wattageOptions);
}
// Function to handle button presses
setWatch(function(e) {
if (e.state) { // Button pressed down
pressTime = e.time;
} else { // Button released
var duration = e.time - pressTime;
if (duration > 1) { // if button is held for more than 1 second
showWattageMenu();
} else {
showMinuteMenu();
}
}
}, BTN, { repeat: true, edge: "both" }); // monitor both button down and up events
// Draw the main screen on startup
drawMainScreen();