Update app.js

master
Ben Jabituya 2023-10-16 17:08:16 +01:00 committed by GitHub
parent fef0fc0743
commit b673038680
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 670 additions and 587 deletions

View File

@ -1,7 +1,11 @@
setupcomplete_colour = "#ff3329";
default_colour = "#ffffff"; require("Font8x12").add(Graphics);
calc_display_colour = "#00FFFF"; require("Font7x11Numeric7Seg").add(Graphics);
display_colour = default_colour;
var setupcomplete_colour = "#ff3329";
var default_colour = "#ffffff";
var calc_display_colour = "#00FFFF";
var display_colour = default_colour;
var processing = false; var processing = false;
var all_extras_array = []; var all_extras_array = [];
@ -10,11 +14,19 @@ var mode = "planetary";
var modeswitch = false; var modeswitch = false;
var colours_switched = false; var colours_switched = false;
var sensorsOn = false;
// Load fonts // Load fonts
require("Font7x11Numeric7Seg").add(Graphics);
// position on screen // position on screen
const Xaxis = 150, Yaxis = 55; var screenSize = g.getHeight();
console.log(screenSize);
var Xaxis = 150, Yaxis = 55;
if (screenSize <= 176) {
setupcomplete_colour = "#00FFFF";
Xaxis = 110;
Yaxis = 40;
}
//lat lon settings loading //lat lon settings loading
var astral_settings; var astral_settings;
@ -443,7 +455,8 @@ function dec2str(x) {
var sgn = (x < 0) ? "-" : " "; var sgn = (x < 0) ? "-" : " ";
var d = Math.floor(dec); var d = Math.floor(dec);
var m = 60 * (dec - d); var m = 60 * (dec - d);
return sgn + cintstr(d, 2) + "° " + frealstr(m, 4, 1) + "'"; //return sgn + cintstr(d, 2) + "° " + frealstr(m, 4, 1) + "'";
return sgn + cintstr(d, 2) + "° ";
} }
// return the integer part of a number // return the integer part of a number
@ -534,7 +547,7 @@ function mean_sidereal_time(lon) {
var mst = 280.46061837 + 360.98564736629 * jd var mst = 280.46061837 + 360.98564736629 * jd
+ 0.000387933 * jt * jt - jt * jt * jt / 38710000 + lon; + 0.000387933 * jt * jt - jt * jt * jt / 38710000 + lon;
mst %=360; mst = mst % 360;
return mst; return mst;
} }
@ -552,7 +565,8 @@ function degr2str(x) {
var sgn = (x < 0) ? "-" : " "; var sgn = (x < 0) ? "-" : " ";
var d = Math.floor(dec); var d = Math.floor(dec);
var m = 60 * (dec - d); var m = 60 * (dec - d);
return sgn + cintstr(d, 3) + "° " + frealstr(m, 4, 1) + "'"; //return sgn + cintstr(d, 3) + "° " + frealstr(m, 4, 1) + "'";
return sgn + cintstr(d, 3) + "° ";
} }
// converts latitude in signed degrees into string // converts latitude in signed degrees into string
@ -561,7 +575,8 @@ function lat2str(x) {
var sgn = (x < 0) ? " S" : " N"; var sgn = (x < 0) ? " S" : " N";
var d = Math.floor(dec); var d = Math.floor(dec);
var m = 60 * (dec - d); var m = 60 * (dec - d);
return cintstr(d, 3) + "° " + frealstr(m, 4, 1) + "'" + sgn; //return cintstr(d, 3) + "° " + frealstr(m, 4, 1) + "'" + sgn;
return cintstr(d, 3) + "° ";
} }
// converts longitude in signed degrees into string // converts longitude in signed degrees into string
@ -570,7 +585,8 @@ function lon2str(x) {
var sgn = (x < 0) ? " W" : " E"; var sgn = (x < 0) ? " W" : " E";
var d = Math.floor(dec); var d = Math.floor(dec);
var m = 60 * (dec - d); var m = 60 * (dec - d);
return cintstr(d, 3) + "° " + frealstr(m, 4, 1) + "'" + sgn; //return cintstr(d, 3) + "° " + frealstr(m, 4, 1) + "'" + sgn;
return cintstr(d, 3) + "° ";
} }
// format two digits with leading zero if needed // format two digits with leading zero if needed
@ -642,9 +658,9 @@ function write_refresh_note(colour) {
if (!ready_to_compute) { if (!ready_to_compute) {
g.drawString("mode change:", Xaxis + 50, cursor, false); g.drawString("mode change:", Xaxis + 50, cursor, false);
cursor += 15; cursor += 15;
g.drawString("BTN1 to refresh", Xaxis + 50, cursor, true /*clear background*/); g.drawString("swipe up to refresh", Xaxis + 50, cursor, true /*clear background*/);
cursor += 15; cursor += 15;
g.drawString("BTN3 to cancel", Xaxis + 50, cursor, true /*clear background*/); g.drawString("swipe left/right to cancel", Xaxis + 50, cursor, true /*clear background*/);
} }
else else
g.drawString("updating, please wait", Xaxis + 50, cursor, false); g.drawString("updating, please wait", Xaxis + 50, cursor, false);
@ -652,42 +668,92 @@ function write_refresh_note(colour) {
function draw_moon(phase) { function draw_moon(phase) {
g.setColor(display_colour); g.setColor(display_colour);
var moonOffset = 12;
var mooonRadius = 25;
if (screenSize > 176) {
if (phase == 5) { if (phase == 5) {
g.fillCircle(200, Yaxis, 30); g.fillCircle(200, Yaxis + moonOffset, mooonRadius);
g.setColor("#000000"); g.setColor("#000000");
g.fillRect(220, 25, 240, 90); g.fillRect(220, 25, 240, 90);
} }
else if (phase == 6) { else if (phase == 6) {
g.fillCircle(200, Yaxis, 30); g.fillCircle(200, Yaxis + moonOffset, mooonRadius);
g.setColor("#000000"); g.setColor("#000000");
g.fillRect(200, 25, 240, 90); g.fillRect(200, 25, 240, 90);
} }
else if (phase == 1) { else if (phase == 1) {
g.fillCircle(200, Yaxis, 30); g.fillCircle(200, Yaxis + moonOffset, mooonRadius);
g.setColor("#000000"); g.setColor("#000000");
g.fillCircle(180, Yaxis, 30); g.fillCircle(180, Yaxis + moonOffset, mooonRadius);
} }
else if (phase == 4) else if (phase == 4)
g.fillCircle(200, Yaxis, 30); g.fillCircle(200, Yaxis + moonOffset, mooonRadius);
else if (phase == 3) { else if (phase == 3) {
g.fillCircle(200, Yaxis, 30); g.fillCircle(200, Yaxis + moonOffset, mooonRadius);
g.setColor("#000000"); g.setColor("#000000");
g.fillRect(160, 25, 180, 90); g.fillRect(160, 25, 180, 90);
} }
else if (phase == 2) { else if (phase == 2) {
g.fillCircle(200, Yaxis, 30); g.fillCircle(200, Yaxis + moonOffset, mooonRadius);
g.setColor("#000000"); g.setColor("#000000");
g.fillRect(160, 25, 200, 90); g.fillRect(160, 25, 200, 90);
} }
else if (phase == 7) { else if (phase == 7) {
g.fillCircle(200, Yaxis, 30); g.fillCircle(200, Yaxis + moonOffset, mooonRadius);
g.setColor("#000000"); g.setColor("#000000");
g.fillCircle(220, Yaxis, 30); g.fillCircle(220, Yaxis, 25);
}
}
else {
moonOffset = 12;
//var moonOffsetX = 150;
mooonRadius = 17;
g.setColor(display_colour);
if (phase != 0)
g.fillCircle(150, Yaxis + moonOffset, mooonRadius);
if (phase == 5) {
g.setColor("#000000");
g.fillRect(165, 25, 180, 90);
}
else if (phase == 6) {
g.setColor("#000000");
g.fillRect(150, 25, 240, 90);
}
else if (phase == 1) {
g.setColor("#000000");
g.fillCircle(140, Yaxis + moonOffset, mooonRadius);
}
else if (phase == 4)
g.fillCircle(150, Yaxis + moonOffset, mooonRadius);
else if (phase == 3) {
g.setColor("#000000");
g.fillRect(125, 25, 135, 90);
}
else if (phase == 2) {
g.setColor("#000000");
g.fillRect(125, 25, 150, 90);
}
else if (phase == 7) {
g.setColor("#000000");
g.fillCircle(160, Yaxis + moonOffset, mooonRadius);
}
} }
g.setColor(display_colour); g.setColor(display_colour);
} }
function autoUpdate() {
ready_to_compute = true;
g.setColor(display_colour);
g.fillCircle(15, 160, 5);
setTimeout(function () {
print("ready");
draw();
secondInterval = setInterval(draw, 1000);
}, 100);
}
function draw() { function draw() {
//print("drawing");
if (astral_settings.astral_default) if (astral_settings.astral_default)
display_colour = default_colour; display_colour = default_colour;
else if (!colours_switched) else if (!colours_switched)
@ -701,19 +767,26 @@ function draw() {
g.reset(); g.reset();
g.setColor(display_colour); g.setColor(display_colour);
// draw the current time (4x size 7 segment) // draw the current time (4x size 7 segment)
g.setFont("7x11Numeric7Seg", 5); g.setFont("7x11Numeric7Seg", 4);
g.setFontAlign(1, 1); // align right bottom g.setFontAlign(1, 1); // align right bottom
g.drawString(time, Xaxis + 20, Yaxis + 30, true /*clear background*/); g.drawString(time, Xaxis + 23, Yaxis + 34, true /*clear background*/);
g.setFont("6x8"); g.setFont("6x8");
g.setFontAlign(1, 1); // align center bottom g.setFontAlign(1, 1); // align center bottom
// pad the date - this clears the background if the date were to change length // pad the date - this clears the background if the date were to change length
var dateStr = " " + require("locale").date(d) + " "; var dateStr = " " + require("locale").date(d) + " ";
g.drawString(dateStr, Xaxis - 40, Yaxis - 40, true /*clear background*/);
if (screenSize < 177) {
g.drawString(dateStr, 100, 20, true /*clear background*/);
}
else {
//bangle 1
g.drawString(dateStr, 150, 40, true /*clear background*/);
}
//compute location of objects //compute location of objects
g.setFontAlign(1, 1); g.setFontAlign(1, 1);
g.setFont("6x8"); g.setFont("6x8", 1);
if (ready_to_compute) if (ready_to_compute)
g.setColor(calc_display_colour); g.setColor(calc_display_colour);
@ -722,25 +795,15 @@ function draw() {
g.setColor("#000000"); g.setColor("#000000");
cursor = Yaxis + 50; cursor = Yaxis + 50;
if (pstrings.length == 0) { if (pstrings.length != 0) {
if (ready_to_compute)
g.drawString("updating, please wait", Xaxis + 50, cursor, true);
else
g.drawString("press BTN1 to update", Xaxis + 50, cursor, true /*clear background*/);
}
else {
for (let i = 0; i < pstrings.length; i++) { for (let i = 0; i < pstrings.length; i++) {
g.drawString(pstrings[i], Xaxis + 50, cursor, true /*clear background*/); g.drawString(pstrings[i], Xaxis + 40, cursor, true /*clear background*/);
cursor += 15; cursor += 10;
} }
if (secondInterval) clearInterval(secondInterval);
secondInterval = undefined;
} }
if (modeswitch)
if (ready_to_compute)
write_refresh_note(calc_display_colour);
else
write_refresh_note(display_colour);
if (ready_to_compute) { if (ready_to_compute) {
processing = true; processing = true;
ready_to_compute = false; ready_to_compute = false;
@ -750,34 +813,43 @@ function draw() {
g.fillRect(Xaxis - 150, Yaxis + 40, Xaxis + 200, Yaxis + 200); g.fillRect(Xaxis - 150, Yaxis + 40, Xaxis + 200, Yaxis + 200);
modeswitch = false; modeswitch = false;
processing = false; processing = false;
Bangle.buzz(); //Bangle.buzz();
} }
current_moonphase = getMoonPhase(); current_moonphase = getMoonPhase();
all_extras_array = []; all_extras_array = [];
if (sensorsOn) {
g.setColor(display_colour);
g.fillCircle(160, 160, 5);
}
}
function SwitchSensorState() {
if (sensorsOn) {
print("turning sensors off");
Bangle.setCompassPower(0);
Bangle.setGPSPower(0);
sensorsOn = 0;
g.setColor("#000000");
g.fillCircle(screenSize - 15, screenSize - 15, 7);
//g.drawString(compass_heading, 30, 115, true /*clear background*/);
g.fillRect(0, 100, 30, 120);
}
else {
Bangle.setCompassPower(1);
Bangle.setGPSPower(1);
sensorsOn = 1;
g.setColor(display_colour);
g.fillCircle(screenSize - 15, screenSize - 15, 5);
}
} }
g.clear(); g.clear();
g.setColor("#000000");
g.setBgColor(0, 0, 0);
g.fillRect(0, 0, 175, 175);
current_moonphase = getMoonPhase(); current_moonphase = getMoonPhase();
Bangle.setUI("clockupdown", btn => {
if (btn==0) {
if (!processing) {
if (!modeswitch) {
modeswitch = true;
if (mode == "planetary") mode = "extras";
else mode = "planetary";
}
else
modeswitch = false;
}
} else {
if (!processing)
ready_to_compute = true;
}
});
// Load widgets // Load widgets
Bangle.loadWidgets(); Bangle.loadWidgets();
Bangle.drawWidgets(); Bangle.drawWidgets();
@ -785,59 +857,70 @@ Bangle.drawWidgets();
draw_moon(current_moonphase); draw_moon(current_moonphase);
draw(); draw();
var secondInterval = setInterval(draw, 1000); var updateInterval = setInterval(autoUpdate, 120000);
// Stop updates when LCD is off, restart when on //var magInterval = setInterval(updateMag, 50);
Bangle.on('lcdPower', on => {
if (secondInterval) clearInterval(secondInterval);
secondInterval = undefined;
Bangle.setCompassPower(0);
if (!astral_settings.astral_default)
Bangle.setGPSPower(0);
if (on) {
Bangle.setCompassPower(1);
Bangle.setGPSPower(1);
if (current_moonphase !== undefined) {
draw_moon(current_moonphase);
}
secondInterval = setInterval(draw, 1000);
draw(); // draw immediately
}
});
Bangle.setCompassPower(1); Bangle.setCompassPower(1);
Bangle.setGPSPower(1); Bangle.setGPSPower(1);
var secondInterval;
Bangle.setUI("clock");
autoUpdate();
setWatch(SwitchSensorState, BTN1, { repeat: true });
// Show launcher when button pressed // Show launcher when button pressed
Bangle.setClockMode(); //Bangle.setClockMode();
setWatch(function () { Bangle.on("swipe", function (directionLR, directionUD) {
if (!astral_settings.astral_default) { if (!processing) {
colours_switched = true; if (-1 == directionUD) {
if (display_colour == setupcomplete_colour) g.setColor(display_colour);
display_colour = default_colour; g.fillCircle(15, 160, 5);
else ready_to_compute = true;
display_colour = setupcomplete_colour; }
draw_moon(current_moonphase); else if (-1 == directionLR || directionLR == 1) {
print("attempting mode switch");
if (mode == "planetary") mode = "extras";
else mode = "planetary";
g.setColor(display_colour);
g.fillCircle(15, 160, 5);
ready_to_compute = true;
}
else if (directionUD == 1) {
SwitchSensorState();
} }
}, BTN4, { repeat: true });
setTimeout(function () {
print("ready");
draw();
secondInterval = setInterval(draw, 1000);
}, 100);
}
});
//events //events
Bangle.on('mag', function (m) { Bangle.on('mag', function (m) {
g.setFont("6x8",2); if (!isNaN(m.heading)) {
if (isNaN(m.heading))
compass_heading = "---";
else
compass_heading = Math.round(m.heading); compass_heading = Math.round(m.heading);
// g.setColor("#000000");
// g.fillRect(160, 10, 160, 20);
g.setColor(display_colour); g.setColor(display_colour);
if(compass_heading<100) if (compass_heading < 100)
compass_heading = " " + compass_heading; compass_heading = " " + compass_heading;
g.drawString(compass_heading, 150, 20, true /*clear background*/); if (sensorsOn) {
g.setFont("8x12");
g.setFontAlign(1, 1);
g.drawString(compass_heading, 25, 112, true /*clear background*/);
}
}
//var n = magArray.length;
//var mean = Math.round( magArray.reduce((a, b) => a + b) / n);
//compass_heading = mean;
}); });
Bangle.on('GPS', function (g) { Bangle.on('GPS', function (g) {
if (g.fix) { if (g.fix) {
display_colour = setupcomplete_colour;
astral_settings.lat = g.lat; astral_settings.lat = g.lat;
astral_settings.lon = g.lon; astral_settings.lon = g.lon;
astral_settings.astral_default = false; astral_settings.astral_default = false;