Skip to content

Commit

Permalink
Merge pull request #3107 from pavelmachek/m_20_altimeter
Browse files Browse the repository at this point in the history
altimeter: use system variables to calibrate altimeter.
  • Loading branch information
bobrippling authored Nov 22, 2023
2 parents 0519375 + aad5aae commit 22e5fe8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
1 change: 1 addition & 0 deletions apps/altimeter/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
0.01: New App!
0.02: Actually upload correct code
0.03: Display sea-level pressure, too, and allow calibration
0.04: Switch to using system code for pressure calibration
71 changes: 33 additions & 38 deletions apps/altimeter/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,64 @@ Bangle.setBarometerPower(true, "altimeter");
g.clear(1);
Bangle.loadWidgets();
Bangle.drawWidgets();
var zero = 0;
var R = Bangle.appRect;
var y = R.y + R.h/2;
var MEDIANLENGTH = 20;
var avr = [], median;
var value = 0;
var avr = [];

function getStandardPressure(altitude) {
const P0 = 1013.25; // standard pressure at sea level in hPa
const T0 = 288.15; // standard temperature at sea level in K
const g0 = 9.80665; // standard gravitational acceleration in m/s^2
const R = 8.31432; // gas constant in J/(mol*K)
const M = 0.0289644; // molar mass of air in kg/mol
const L = -0.0065; // temperature lapse rate in K/m

const temperature = T0 + L * altitude; // temperature at the given altitude
const pressure = P0 * Math.pow((temperature / T0), (-g0 * M) / (R * L)); // pressure at the given altitude

return pressure;
}

function convertToSeaLevelPressure(pressure, altitude) {
return 1013.25 * (pressure / getStandardPressure(altitude));
function fmt(t) {
if ((t > -100) && (t < 1000))
t = t.toFixed(1);
else
t = t.toFixed(0);
return t;
}

Bangle.on('pressure', function(e) {
while (avr.length>MEDIANLENGTH) avr.pop();
avr.unshift(e.altitude);
median = avr.slice().sort();
let median = avr.slice().sort();
g.reset().clearRect(0,y-30,g.getWidth()-10,R.h);
if (median.length>10) {
var mid = median.length>>1;
value = E.sum(median.slice(mid-4,mid+5)) / 9;
t = value-zero;
if ((t > -100) && (t < 1000))
t = t.toFixed(1);
else
t = t.toFixed(0);
g.setFont("Vector",50).setFontAlign(0,0).drawString(t, g.getWidth()/2, y);
sea = convertToSeaLevelPressure(e.pressure, value-zero);
t = sea.toFixed(1) + " " + e.temperature.toFixed(1);
if (0) {
var value = E.sum(median.slice(mid-4,mid+5)) / 9;
} else {
var value = median[median.length>>1];
}
t = fmt(value);

g.setFont("Vector",50).setFontAlign(0,0).drawString(t, g.getWidth()/2, y);

let o = Bangle.getOptions();
let sea = o.seaLevelPressure;
t = sea.toFixed(1) + " " + e.temperature.toFixed(1);
if (0) {
print("alt raw:", value.toFixed(1));
print("temperature:", e.temperature);
print("pressure:", e.pressure);
print("sea pressure:", sea);
print("std pressure:", getStandardPressure(value-zero));
}
g.setFont("Vector",25).setFontAlign(-1,0).drawString(t,
10, R.y+R.h - 35);
}
g.setFont("Vector",25).setFontAlign(-1,0).drawString(t, 10, R.y+R.h - 35);
});

function setPressure(m, a) {
o = Bangle.getOptions();
print(o);
o.seaLevelPressure = o.seaLevelPressure * m + a;
Bangle.setOptions(o);
avr = [];
}

print(g.getFonts());
g.reset();
g.setFont("Vector:15");
g.setFontAlign(0,0);
g.drawString(/*LANG*/"ALTITUDE (m)", g.getWidth()/2, y-40);
g.drawString(/*LANG*/"SEA L (hPa) TEMP (C)", g.getWidth()/2, y+62);
g.flip();
g.setFont("6x8").setFontAlign(0,0,3).drawString(/*LANG*/"ZERO", g.getWidth()-5, g.getHeight()/2);
g.setFont("6x8").setFontAlign(0,0,3).drawString(/*LANG*/"STD", g.getWidth()-5, g.getHeight()/2);
Bangle.setUI("updown", btn=> {
if (!btn) zero=value;
if (btn<0) zero-=5;
if (btn>0) zero+=5;
if (!btn) setPressure(0, 1013.25);
if (btn<0) setPressure(1, 1);
if (btn>0) setPressure(1, -1);
});
2 changes: 1 addition & 1 deletion apps/altimeter/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ "id": "altimeter",
"name": "Altimeter",
"version":"0.03",
"version":"0.04",
"description": "Simple altimeter that can display height changed using Bangle.js 2's built in pressure sensor.",
"icon": "app.png",
"tags": "tool,outdoors",
Expand Down

0 comments on commit 22e5fe8

Please sign in to comment.