const CompassApp = { StopTime:30, // seconds DarkTime:40, // seconds _stopInterval:0, _darkInterval:0, init : function() { console.log("init"); Bangle.on("touch", function() { compassApp.touch(); }); Bangle.setLCDTimeout(0); this.touch(); }, touch : function() { console.log("touch"); if (this._stopInterval != 0) { clearInterval(this._stopInterval); } this._stopInterval = setInterval(function() { compassApp.stop(); }, this.StopTime * 1000); if (this._darkInterval != 0) { clearInterval(this._darkInterval); } this._darkInterval = setInterval(function() { compassApp.dark(); }, this.DarkTime * 1000); Bangle.on('mag', function(mag) { compassApp.update(mag); }); Bangle.setCompassPower(1); Bangle.setLCDPower(1); }, stop : function() { console.log("stop"); clearInterval(this._stopInterval); this._stopInterval = 0; Bangle.setCompassPower(0); this.drawOff(); }, dark : function() { console.log("dark"); clearInterval(this._darkInterval); this._darkInterval = 0; Bangle.setLCDPower(0); }, update : function(mag) { if (this._stopInterval != 0) { this.drawOn(mag); } }, drawOff : function() { g.clear(); g.setFontVector(20); g.setColor(255,255,255); this.drawText("Power Saving\nTap for\ncompass"); g.flip(); }, drawOn : function(mag) { var heading = mag.heading; var radius = Math.min(g.getWidth(), g.getHeight()); var fullsize = radius * 0.47; var smallsize = radius * 0.43; var xc = g.getWidth()/2; var yc = g.getHeight()/2; g.setColor(0,0,0); g.clear(); g.setColor(0,0,255); g.fillCircle(xc, yc, fullsize); g.setColor(0,0,0); g.fillCircle(xc, yc, smallsize); g.setColor(255,255,255); g.drawCircle(xc, yc, fullsize); var w=0.1; var h=0.2; g.setColor(255,0,0); this.fillTriangle(heading-180, fullsize, 0,1, 0+2*w,1-2*h, 0-2*w,1-2*h); g.setColor(255,0,255); this.fillTriangle(heading-90, fullsize, 0,1, 0+w,1-h, 0-w,1-h); this.fillTriangle(heading, fullsize, 0,1, 0+w,1-h, 0-w,1-h); this.fillTriangle(heading+90, fullsize, 0,1, 0+w,1-h, 0-w,1-h); w/=2; this.fillTriangle(heading-315, fullsize, 0,1, 0+w,1-h, 0-w,1-h); this.fillTriangle(heading-225, fullsize, 0,1, 0+w,1-h, 0-w,1-h); this.fillTriangle(heading-135, fullsize, 0,1, 0+w,1-h, 0-w,1-h); this.fillTriangle(heading-45, fullsize, 0,1, 0+w,1-h, 0-w,1-h); this.fillTriangle(heading+45, fullsize, 0,1, 0+w,1-h, 0-w,1-h); this.fillTriangle(heading+135, fullsize, 0,1, 0+w,1-h, 0-w,1-h); this.fillTriangle(heading+225, fullsize, 0,1, 0+w,1-h, 0-w,1-h); this.fillTriangle(heading+315, fullsize, 0,1, 0+w,1-h, 0-w,1-h); g.setFontVector(20); g.setColor(0,255,0); var text = "NORTH"; if (this.nearest(heading, 22.5)) { text = "N-NW"; } else if (this.nearest(heading, 45)) { text = "NW"; } else if (this.nearest(heading, 67.5)) { text = "W-NW"; } else if (this.nearest(heading, 90)) { text = "WEST"; } else if (this.nearest(heading, 112.5)) { text = "W-SW"; } else if (this.nearest(heading, 135)) { text = "SW"; } else if (this.nearest(heading, 157.5)) { text = "S-SW"; } else if (this.nearest(heading, 180)) { text = "SOUTH"; } else if (this.nearest(heading, 202.5)) { text = "S-SE"; } else if (this.nearest(heading, 225)) { text = "SE"; } else if (this.nearest(heading, 247.5)) { text = "E-SE"; } else if (this.nearest(heading, 270)) { text = "EAST"; } else if (this.nearest(heading, 292.5)) { text = "E-NE"; } else if (this.nearest(heading, 315)) { text = "NE"; } else if (this.nearest(heading, 337.5)) { text = "N-NE"; } this.drawText(text); g.flip(); }, drawText : function(str) { var split = str.split('\n'); var lines = split.length; var yy = (g.getHeight() - g.getFontHeight() * lines)/2; for (var line in split) { var t = split[line]; g.drawString(t, (g.getWidth() - g.stringWidth(t))/2, yy); yy += g.getFontHeight(); } }, fillTriangle : function(heading, radius, x1,y1, x2,y2, x3,y3) { var xc = g.getWidth()/2; var yc = g.getWidth()/2; var verts = [ radius*x1,radius*y1, radius*x2,radius*y2, radius*x3,radius*y3 ]; console.log(verts); var rotated = g.transformVertices(verts, { x:xc, y:yc, scale:1, rotate:heading*3.1415/180 }); g.fillPoly(rotated); }, nearest : function(heading,angle) { return (heading > angle - 11.25) && (heading <= angle + 11.25); }, }; var compassApp = Object.create(CompassApp); compassApp.init();