|
| 1 | +// Tea Timer Application for Bangle.js 2 |
| 2 | +let timerDuration = (() => { |
| 3 | + let file = require("Storage").open("ateatimer.data", "r"); |
| 4 | + let data = file.read(4); // Assuming 4 bytes for storage |
| 5 | + return data ? parseInt(data, 10) : 4 * 60; // Default to 4 minutes |
| 6 | +})(); |
| 7 | +let timeRemaining = timerDuration; |
| 8 | +let timerRunning = false; |
| 9 | +let buzzInterval = null; // Interval for buzzing when timer reaches 0 |
| 10 | +let timerInterval = null; // Interval for timer countdown |
| 11 | + |
| 12 | +function saveDefaultDuration() { |
| 13 | + let file = require("Storage").open("ateatimer.data", "w"); |
| 14 | + file.write(timerDuration.toString()); |
| 15 | +} |
| 16 | + |
| 17 | +function drawTime() { |
| 18 | + g.clear(); |
| 19 | + g.setFont("Vector", 40); |
| 20 | + g.setFontAlign(0, 0); // Center align |
| 21 | + |
| 22 | + const minutes = Math.floor(Math.abs(timeRemaining) / 60); |
| 23 | + const seconds = Math.abs(timeRemaining) % 60; |
| 24 | + const sign = timeRemaining < 0 ? "-" : ""; |
| 25 | + const timeStr = `${sign}${minutes}:${seconds.toString().padStart(2, '0')}`; |
| 26 | + |
| 27 | + g.drawString(timeStr, g.getWidth() / 2, g.getHeight() / 2); |
| 28 | + |
| 29 | + // Draw Increase button (triangle pointing up) |
| 30 | + g.fillPoly([ |
| 31 | + g.getWidth() / 2, g.getHeight() / 2 - 80, // Top vertex |
| 32 | + g.getWidth() / 2 - 20, g.getHeight() / 2 - 60, // Bottom-left vertex |
| 33 | + g.getWidth() / 2 + 20, g.getHeight() / 2 - 60 // Bottom-right vertex |
| 34 | + ]); |
| 35 | + |
| 36 | + // Draw Decrease button (triangle pointing down) |
| 37 | + g.fillPoly([ |
| 38 | + g.getWidth() / 2, g.getHeight() / 2 + 80, // Bottom vertex |
| 39 | + g.getWidth() / 2 - 20, g.getHeight() / 2 + 60, // Top-left vertex |
| 40 | + g.getWidth() / 2 + 20, g.getHeight() / 2 + 60 // Top-right vertex |
| 41 | + ]); |
| 42 | + |
| 43 | + g.flip(); |
| 44 | +} |
| 45 | + |
| 46 | +function startTimer() { |
| 47 | + if (timerRunning) return; |
| 48 | + timerRunning = true; |
| 49 | + |
| 50 | + // Save the default duration on timer start |
| 51 | + timerDuration = timeRemaining; |
| 52 | + saveDefaultDuration(); |
| 53 | + |
| 54 | + timerInterval = setInterval(() => { |
| 55 | + timeRemaining--; |
| 56 | + drawTime(); |
| 57 | + |
| 58 | + if (timeRemaining === 0 && !buzzInterval) { |
| 59 | + // Start continuous vibration when timer reaches 0 |
| 60 | + buzzInterval = setInterval(() => Bangle.buzz(500), 1000); |
| 61 | + } |
| 62 | + }, 1000); |
| 63 | +} |
| 64 | + |
| 65 | +function resetTimer() { |
| 66 | + if (timerInterval) { |
| 67 | + clearInterval(timerInterval); |
| 68 | + timerInterval = null; |
| 69 | + } |
| 70 | + timerRunning = false; |
| 71 | + timeRemaining = timerDuration; |
| 72 | + stopBuzzing(); |
| 73 | + drawTime(); |
| 74 | +} |
| 75 | + |
| 76 | +function stopBuzzing() { |
| 77 | + if (buzzInterval) { |
| 78 | + clearInterval(buzzInterval); |
| 79 | + buzzInterval = null; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function adjustTime(amount) { |
| 84 | + if (!timerRunning) { |
| 85 | + timeRemaining += amount; |
| 86 | + timeRemaining = Math.floor(timeRemaining / 60) * 60; // Round to full minutes |
| 87 | + } else { |
| 88 | + timeRemaining += amount; // Allow adjustments during running |
| 89 | + } |
| 90 | + drawTime(); |
| 91 | +} |
| 92 | + |
| 93 | +function handleTouch(x, y) { |
| 94 | + const centerY = g.getHeight() / 2; |
| 95 | + |
| 96 | + if (y < centerY - 40) { |
| 97 | + // Increase button area |
| 98 | + adjustTime(60); |
| 99 | + } else if (y > centerY + 40) { |
| 100 | + // Decrease button area |
| 101 | + adjustTime(-60); |
| 102 | + } else { |
| 103 | + // Center area |
| 104 | + if (!timerRunning) { |
| 105 | + startTimer(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Handle physical button press for resetting timer |
| 111 | +setWatch(() => { |
| 112 | + resetTimer(); |
| 113 | +}, BTN1, { repeat: true, edge: "falling" }); |
| 114 | + |
| 115 | +// Handle touch |
| 116 | +Bangle.on("touch", (zone, xy) => { |
| 117 | + handleTouch(xy.x, xy.y, false); |
| 118 | +}); |
| 119 | + |
| 120 | +// Draw the initial timer display |
| 121 | +drawTime(); |
| 122 | + |
0 commit comments