|
| 1 | +(function() { |
| 2 | + var img; |
| 3 | + var v; |
| 4 | + var data; |
| 5 | + var hrLeft = 0; |
| 6 | + var daysLeft = 0; |
| 7 | + var showPercent=false; |
| 8 | + //updates values |
| 9 | + function updateValues(){ |
| 10 | + v = E.getBattery(); |
| 11 | + data = require("smartbatt").get(); |
| 12 | + hrLeft = data.hrsLeft; |
| 13 | + daysLeft = hrLeft / 24; |
| 14 | + |
| 15 | + hrLeft = Math.round(hrLeft); |
| 16 | + daysLeft = Math.round(daysLeft); |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + //draws battery icon and fill bar |
| 21 | + function drawBatt(){ |
| 22 | + v =E.getBattery(); |
| 23 | + var s=24,g=Graphics.createArrayBuffer(24,24,1,{msb:true}); |
| 24 | + g.fillRect(0,6,s-3,18).clearRect(2,8,s-5,16).fillRect(s-2,10,s,15).fillRect(3,9,3+v*(s-9)/100,15); |
| 25 | + g.transparent=0; |
| 26 | + img=g.asImage("string"); |
| 27 | + } |
| 28 | + |
| 29 | + //calls both updates for values and icons. |
| 30 | + //might split in the future since values updates once every five minutes so we dont need to call it in every minute while the battery can be updated once a minute. |
| 31 | + function updateDisplay(){ |
| 32 | + updateValues(); |
| 33 | + drawBatt(); |
| 34 | + } |
| 35 | + |
| 36 | + return { |
| 37 | + name: "SmartBatt", |
| 38 | + items: [ |
| 39 | + { name : "BattStatus", |
| 40 | + get : () => { |
| 41 | + updateValues(); |
| 42 | + drawBatt(); |
| 43 | + |
| 44 | + //update clock info according to batt state |
| 45 | + if (Bangle.isCharging()) { |
| 46 | + return { text: v+"%", img : img}; |
| 47 | + } |
| 48 | + else if(daysLeft >= 1) { |
| 49 | + return { text: daysLeft+"d", img : img}; |
| 50 | + } |
| 51 | + |
| 52 | + else { |
| 53 | + return {text: hrLeft+"h", img: img}; |
| 54 | + } |
| 55 | + }, |
| 56 | + |
| 57 | + show : function() { |
| 58 | + this.interval = setInterval(()=>{ |
| 59 | + updateDisplay(); |
| 60 | + this.emit('redraw'); |
| 61 | + }, 60000); |
| 62 | + }, |
| 63 | + |
| 64 | + hide : function() { |
| 65 | + clearInterval(this.interval); |
| 66 | + this.interval = undefined; |
| 67 | + } |
| 68 | + }, |
| 69 | + { name : "AvgDrainage", |
| 70 | + get : () => { |
| 71 | + drawBatt() |
| 72 | + var data=require("smartbatt").get(); |
| 73 | + return { text: data.avgDrainage.toFixed(2)+"/h", img : img}; |
| 74 | + }, |
| 75 | + |
| 76 | + show : function() { |
| 77 | + this.interval = setInterval(()=>{ |
| 78 | + this.emit('redraw'); |
| 79 | + }, 60000); |
| 80 | + }, |
| 81 | + |
| 82 | + hide : function() { |
| 83 | + clearInterval(this.interval); |
| 84 | + this.interval = undefined; |
| 85 | + } |
| 86 | + } |
| 87 | + ] |
| 88 | + }; |
| 89 | +}) |
0 commit comments