|
| 1 | +(function(){ |
| 2 | + |
| 3 | + var showPercent = false; |
| 4 | + const width = 40; |
| 5 | + const height = 24; |
| 6 | + |
| 7 | + let COLORS = { |
| 8 | + 'bg': g.theme.bg, |
| 9 | + 'fg': g.theme.fg, |
| 10 | + 'charging': "#08f", |
| 11 | + 'high': g.theme.dark ? "#fff" : "#000", |
| 12 | + 'low': "#f00", |
| 13 | + }; |
| 14 | + |
| 15 | + const levelColor = (l) => { |
| 16 | + if (Bangle.isCharging()) return COLORS.charging; |
| 17 | + if (l >= 30) return COLORS.high; |
| 18 | + return COLORS.low; |
| 19 | + }; |
| 20 | + |
| 21 | + function draw() { |
| 22 | + let batt=E.getBattery(); |
| 23 | + let data = require("smartbatt").get(); |
| 24 | + let hrsLeft=data.hrsLeft; |
| 25 | + let days = hrsLeft / 24; |
| 26 | + |
| 27 | + let txt = showPercent |
| 28 | + ? batt |
| 29 | + : (days >= 1 |
| 30 | + ? Math.round(Math.min(days, 99)) + "d" |
| 31 | + : Math.round(hrsLeft) + "h"); |
| 32 | + if(Bangle.isCharging()) txt=E.getBattery(); |
| 33 | + let s = 29; |
| 34 | + let x = this.x, y = this.y; |
| 35 | + let xl = x + 4 + batt * (s - 12) / 100; |
| 36 | + |
| 37 | + // Drawing code follows... |
| 38 | + g.setColor(COLORS.bg); |
| 39 | + g.fillRect(x + 2, y + 5, x + s - 6, y + 18); |
| 40 | + |
| 41 | + g.setColor(levelColor(batt)); |
| 42 | + g.fillRect(x + 1, y + 3, x + s - 5, y + 4); |
| 43 | + g.fillRect(x + 1, y + 19, x + s - 5, y + 20); |
| 44 | + g.fillRect(x, y + 4, x + 1, y + 19); |
| 45 | + g.fillRect(x + s - 5, y + 4, x + s - 4, y + 19); |
| 46 | + g.fillRect(x + s - 3, y + 8, x + s - 2, y + 16); |
| 47 | + g.fillRect(x + 4, y + 15, xl, y + 16); |
| 48 | + |
| 49 | + g.setColor(COLORS.fg); |
| 50 | + g.setFontAlign(0, 0); |
| 51 | + g.setFont('6x8'); |
| 52 | + g.drawString(txt, x + 14, y + 10); |
| 53 | + |
| 54 | + |
| 55 | +} |
| 56 | + WIDGETS["widsmartbatt"] = { |
| 57 | + area: "tr", |
| 58 | + width: 30, |
| 59 | + draw: draw |
| 60 | + }; |
| 61 | + |
| 62 | + // Touch to temporarily show battery percent |
| 63 | + Bangle.on("touch", function (_btn, xy) { |
| 64 | + if (WIDGETS["back"] || !xy) return; |
| 65 | + |
| 66 | + var oversize = 5; |
| 67 | + var w = WIDGETS["widsmartbatt"]; |
| 68 | + var x = xy.x, y = xy.y; |
| 69 | + |
| 70 | + if (w.x - oversize <= x && x < w.x + width + oversize |
| 71 | + && w.y - oversize <= y && y < w.y + height + oversize) { |
| 72 | + E.stopEventPropagation && E.stopEventPropagation(); |
| 73 | + showPercent = true; |
| 74 | + setTimeout(() => { |
| 75 | + showPercent = false; |
| 76 | + w.draw(w); |
| 77 | + }, 3000); |
| 78 | + w.draw(w); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + // Update widget on charging state change |
| 83 | + Bangle.on('charging', function () { |
| 84 | + WIDGETS["widsmartbatt"].draw(); |
| 85 | + }); |
| 86 | + |
| 87 | + setInterval(() => WIDGETS["widsmartbatt"].draw(), 60000); |
| 88 | + |
| 89 | + |
| 90 | +})(); |
0 commit comments