|
| 1 | +// Tea Timer Application for Bangle.js 2 using sched library |
| 2 | + |
| 3 | +let timerDuration = (() => { |
| 4 | + let file = require("Storage").open("ateatimer.data", "r"); |
| 5 | + let data = file.read(4); // Assuming 4 bytes for storage |
| 6 | + return data ? parseInt(data, 10) : 4 * 60; // Default to 4 minutes |
| 7 | +})(); |
| 8 | +let timeRemaining = timerDuration; |
| 9 | +let timerRunning = false; |
| 10 | + |
| 11 | +function saveDefaultDuration() { |
| 12 | + let file = require("Storage").open("ateatimer.data", "w"); |
| 13 | + file.write(timerDuration.toString()); |
| 14 | +} |
| 15 | + |
| 16 | +function drawTime() { |
| 17 | + g.clear(); |
| 18 | + g.setFont("Vector", 40); |
| 19 | + g.setFontAlign(0, 0); // Center align |
| 20 | + |
| 21 | + const minutes = Math.floor(Math.abs(timeRemaining) / 60); |
| 22 | + const seconds = Math.abs(timeRemaining) % 60; |
| 23 | + const sign = timeRemaining < 0 ? "-" : ""; |
| 24 | + const timeStr = `${sign}${minutes}:${seconds.toString().padStart(2, '0')}`; |
| 25 | + |
| 26 | + g.drawString(timeStr, g.getWidth() / 2, g.getHeight() / 2); |
| 27 | + |
| 28 | + // Draw Increase button (triangle pointing up) |
| 29 | + g.fillPoly([ |
| 30 | + g.getWidth() / 2, g.getHeight() / 2 - 80, // Top vertex |
| 31 | + g.getWidth() / 2 - 20, g.getHeight() / 2 - 60, // Bottom-left vertex |
| 32 | + g.getWidth() / 2 + 20, g.getHeight() / 2 - 60 // Bottom-right vertex |
| 33 | + ]); |
| 34 | + |
| 35 | + // Draw Decrease button (triangle pointing down) |
| 36 | + g.fillPoly([ |
| 37 | + g.getWidth() / 2, g.getHeight() / 2 + 80, // Bottom vertex |
| 38 | + g.getWidth() / 2 - 20, g.getHeight() / 2 + 60, // Top-left vertex |
| 39 | + g.getWidth() / 2 + 20, g.getHeight() / 2 + 60 // Top-right vertex |
| 40 | + ]); |
| 41 | + |
| 42 | + g.flip(); |
| 43 | +} |
| 44 | + |
| 45 | +function startTimer() { |
| 46 | + if (timerRunning) return; |
| 47 | + timerRunning = true; |
| 48 | + |
| 49 | + // Save the default duration on timer start |
| 50 | + timerDuration = timeRemaining; |
| 51 | + saveDefaultDuration(); |
| 52 | + |
| 53 | + // Schedule a new timer using the sched library |
| 54 | + require("sched").setAlarm("mytimer", { |
| 55 | + msg: "Tea is ready!", |
| 56 | + timer: timeRemaining * 1000, // Convert to milliseconds |
| 57 | + vibrate: ".." // Default vibration pattern |
| 58 | + }); |
| 59 | + |
| 60 | + // Ensure the scheduler updates |
| 61 | + require("sched").reload(); |
| 62 | + |
| 63 | + // Start the secondary timer to update the display |
| 64 | + setInterval(updateDisplay, 1000); |
| 65 | +} |
| 66 | + |
| 67 | +function resetTimer() { |
| 68 | + // Cancel the existing timer |
| 69 | + require("sched").setAlarm("mytimer", undefined); |
| 70 | + require("sched").reload(); |
| 71 | + |
| 72 | + timerRunning = false; |
| 73 | + timeRemaining = timerDuration; |
| 74 | + drawTime(); |
| 75 | +} |
| 76 | + |
| 77 | +function adjustTime(amount) { |
| 78 | + timeRemaining += amount; |
| 79 | + timeRemaining = Math.max(1, timeRemaining); // Ensure time doesn't go negative |
| 80 | + print(timeRemaining); |
| 81 | + if (timerRunning) { |
| 82 | + // Update the existing timer with the new remaining time |
| 83 | + let alarm = require("sched").getAlarm("mytimer"); |
| 84 | + if (alarm) { |
| 85 | + // Cancel the current alarm |
| 86 | + require("sched").setAlarm("mytimer", undefined); |
| 87 | + |
| 88 | + // Set a new alarm with the updated time |
| 89 | + require("sched").setAlarm("mytimer", { |
| 90 | + msg: "Tea is ready!", |
| 91 | + timer: timeRemaining * 1000, // Convert to milliseconds |
| 92 | + vibrate: ".." // Default vibration pattern |
| 93 | + }); |
| 94 | + |
| 95 | + // Reload the scheduler to apply changes |
| 96 | + require("sched").reload(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + drawTime(); |
| 101 | +} |
| 102 | + |
| 103 | +function handleTouch(x, y) { |
| 104 | + const centerY = g.getHeight() / 2; |
| 105 | + |
| 106 | + if (y < centerY - 40) { |
| 107 | + // Increase button area |
| 108 | + adjustTime(60); |
| 109 | + } else if (y > centerY + 40) { |
| 110 | + // Decrease button area |
| 111 | + adjustTime(-60); |
| 112 | + } else { |
| 113 | + // Center area |
| 114 | + if (!timerRunning) { |
| 115 | + startTimer(); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// Function to update the display every second |
| 121 | +function updateDisplay() { |
| 122 | + if (timerRunning) { |
| 123 | + let alarm = require("sched").getAlarm("mytimer"); |
| 124 | + if (alarm) { |
| 125 | + timeRemaining = Math.ceil(require("sched").getTimeToAlarm(alarm) / 1000); |
| 126 | + } |
| 127 | + drawTime(); |
| 128 | + if (timeRemaining <= 0) { |
| 129 | + timeRemaining = 0 |
| 130 | + clearInterval(updateDisplay); |
| 131 | + timerRunning = false; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// Handle physical button press for resetting timer |
| 137 | +setWatch(() => { |
| 138 | + resetTimer(); |
| 139 | +}, BTN1, { repeat: true, edge: "falling" }); |
| 140 | + |
| 141 | +// Handle touch |
| 142 | +Bangle.on("touch", (zone, xy) => { |
| 143 | + handleTouch(xy.x, xy.y, false); |
| 144 | +}); |
| 145 | + |
| 146 | +// Draw the initial timer display |
| 147 | +drawTime(); |
0 commit comments