|
| 1 | +/* Widadjust auto adjuster */ |
| 2 | +/* fmt library v0.2.2 */ |
| 3 | +let fmt = { |
| 4 | + icon_alt : "\0\x08\x1a\1\x00\x00\x00\x20\x30\x78\x7C\xFE\xFF\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", |
| 5 | + icon_m : "\0\x08\x1a\1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", |
| 6 | + icon_km : "\0\x08\x1a\1\xC3\xC6\xCC\xD8\xF0\xD8\xCC\xC6\xC3\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", |
| 7 | + icon_kph : "\0\x08\x1a\1\xC3\xC6\xCC\xD8\xF0\xD8\xCC\xC6\xC3\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\xFF\x00\xC3\xC3\xFF\xC3\xC3", |
| 8 | + icon_c : "\0\x08\x1a\1\x00\x00\x60\x90\x90\x60\x00\x7F\xFF\xC0\xC0\xC0\xC0\xC0\xFF\x7F\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", |
| 9 | + icon_hpa : "\x00\x08\x16\x01\x00\x80\xb0\xc8\x88\x88\x88\x00\xf0\x88\x84\x84\x88\xf0\x80\x8c\x92\x22\x25\x19\x00\x00", |
| 10 | + icon_9 : "\x00\x08\x16\x01\x00\x00\x00\x00\x38\x44\x44\x4c\x34\x04\x04\x38\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", |
| 11 | + icon_10 : "\x00\x08\x16\x01\x00\x08\x18\x28\x08\x08\x08\x00\x00\x18\x24\x24\x24\x24\x18\x00\x00\x00\x00\x00\x00\x00", |
| 12 | + |
| 13 | + /* 0 .. DD.ddddd |
| 14 | + 1 .. DD MM.mmm' |
| 15 | + 2 .. DD MM'ss" |
| 16 | + */ |
| 17 | + geo_mode : 1, |
| 18 | + |
| 19 | + init: function() {}, |
| 20 | + fmtDist: function(km) { |
| 21 | + if (km >= 1.0) return km.toFixed(1) + this.icon_km; |
| 22 | + return (km*1000).toFixed(0) + this.icon_m; |
| 23 | + }, |
| 24 | + fmtSteps: function(n) { return this.fmtDist(0.001 * 0.719 * n); }, |
| 25 | + fmtAlt: function(m) { return m.toFixed(0) + this.icon_alt; }, |
| 26 | + fmtTemp: function(c) { return c.toFixed(1) + this.icon_c; }, |
| 27 | + fmtPress: function(p) { |
| 28 | + if (p < 900 || p > 1100) |
| 29 | + return p.toFixed(0) + this.icon_hpa; |
| 30 | + if (p < 1000) { |
| 31 | + p -= 900; |
| 32 | + return this.icon_9 + this.add0(p.toFixed(0)) + this.icon_hpa; |
| 33 | + } |
| 34 | + p -= 1000; |
| 35 | + return this.icon_10 + this.add0(p.toFixed(0)) + this.icon_hpa; |
| 36 | + }, |
| 37 | + draw_dot : 1, |
| 38 | + add0: function(i) { |
| 39 | + if (i > 9) { |
| 40 | + return ""+i; |
| 41 | + } else { |
| 42 | + return "0"+i; |
| 43 | + } |
| 44 | + }, |
| 45 | + fmtTOD: function(now) { |
| 46 | + this.draw_dot = !this.draw_dot; |
| 47 | + let dot = ":"; |
| 48 | + if (!this.draw_dot) |
| 49 | + dot = "."; |
| 50 | + return now.getHours() + dot + this.add0(now.getMinutes()); |
| 51 | + }, |
| 52 | + fmtNow: function() { return this.fmtTOD(new Date()); }, |
| 53 | + fmtTimeDiff: function(d) { |
| 54 | + if (d < 180) |
| 55 | + return ""+d.toFixed(0); |
| 56 | + d = d/60; |
| 57 | + return ""+d.toFixed(0)+"m"; |
| 58 | + }, |
| 59 | + fmtAngle: function(x) { |
| 60 | + switch (this.geo_mode) { |
| 61 | + case 0: |
| 62 | + return "" + x; |
| 63 | + case 1: { |
| 64 | + let d = Math.floor(x); |
| 65 | + let m = x - d; |
| 66 | + m = m*60; |
| 67 | + return "" + d + " " + m.toFixed(3) + "'"; |
| 68 | + } |
| 69 | + case 2: { |
| 70 | + let d = Math.floor(x); |
| 71 | + let m = x - d; |
| 72 | + m = m*60; |
| 73 | + let mf = Math.floor(m); |
| 74 | + let s = m - mf; |
| 75 | + s = s*60; |
| 76 | + return "" + d + " " + mf + "'" + s.toFixed(0) + '"'; |
| 77 | + } |
| 78 | + } |
| 79 | + return "bad mode?"; |
| 80 | + }, |
| 81 | + fmtPos: function(pos) { |
| 82 | + let x = pos.lat; |
| 83 | + let c = "N"; |
| 84 | + if (x<0) { |
| 85 | + c = "S"; |
| 86 | + x = -x; |
| 87 | + } |
| 88 | + let s = c+this.fmtAngle(x) + "\n"; |
| 89 | + c = "E"; |
| 90 | + if (x<0) { |
| 91 | + c = "W"; |
| 92 | + x = -x; |
| 93 | + } |
| 94 | + return s + c + this.fmtAngle(x); |
| 95 | + }, |
| 96 | + fmtFix: function(fix, t) { |
| 97 | + if (fix && fix.fix && fix.lat) { |
| 98 | + return this.fmtSpeed(fix.speed) + " " + |
| 99 | + this.fmtAlt(fix.alt); |
| 100 | + } else { |
| 101 | + return "N/FIX " + this.fmtTimeDiff(t); |
| 102 | + } |
| 103 | + }, |
| 104 | + fmtSpeed: function(kph) { |
| 105 | + return kph.toFixed(1) + this.icon_kph; |
| 106 | + }, |
| 107 | + radians: function(a) { return a*Math.PI/180; }, |
| 108 | + degrees: function(a) { return a*180/Math.PI; }, |
| 109 | + // distance between 2 lat and lons, in meters, Mean Earth Radius = 6371km |
| 110 | + // https://www.movable-type.co.uk/scripts/latlong.html |
| 111 | + // (Equirectangular approximation) |
| 112 | + // returns value in meters |
| 113 | + distance: function(a,b) { |
| 114 | + var x = this.radians(b.lon-a.lon) * Math.cos(this.radians((a.lat+b.lat)/2)); |
| 115 | + var y = this.radians(b.lat-a.lat); |
| 116 | + return Math.sqrt(x*x + y*y) * 6371000; |
| 117 | + }, |
| 118 | + // thanks to waypointer |
| 119 | + bearing: function(a,b) { |
| 120 | + var delta = this.radians(b.lon-a.lon); |
| 121 | + var alat = this.radians(a.lat); |
| 122 | + var blat = this.radians(b.lat); |
| 123 | + var y = Math.sin(delta) * Math.cos(blat); |
| 124 | + var x = Math.cos(alat) * Math.sin(blat) - |
| 125 | + Math.sin(alat)*Math.cos(blat)*Math.cos(delta); |
| 126 | + return Math.round(this.degrees(Math.atan2(y, x))); |
| 127 | + }, |
| 128 | +}; |
| 129 | + |
| 130 | +/* gps library v0.1.4 */ |
| 131 | +let gps = { |
| 132 | + emulator: -1, |
| 133 | + init: function(x) { |
| 134 | + this.emulator = (process.env.BOARD=="EMSCRIPTEN" |
| 135 | + || process.env.BOARD=="EMSCRIPTEN2")?1:0; |
| 136 | + }, |
| 137 | + state: {}, |
| 138 | + on_gps: function(f) { |
| 139 | + let fix = this.getGPSFix(); |
| 140 | + f(fix); |
| 141 | + |
| 142 | + /* |
| 143 | + "lat": number, // Latitude in degrees |
| 144 | + "lon": number, // Longitude in degrees |
| 145 | + "alt": number, // altitude in M |
| 146 | + "speed": number, // Speed in kph |
| 147 | + "course": number, // Course in degrees |
| 148 | + "time": Date, // Current Time (or undefined if not known) |
| 149 | + "satellites": 7, // Number of satellites |
| 150 | + "fix": 1 // NMEA Fix state - 0 is no fix |
| 151 | + "hdop": number, // Horizontal Dilution of Precision |
| 152 | + */ |
| 153 | + this.state.timeout = setTimeout(this.on_gps, 1000, f); |
| 154 | + }, |
| 155 | + off_gps: function() { |
| 156 | + clearTimeout(this.state.timeout); |
| 157 | + }, |
| 158 | + getGPSFix: function() { |
| 159 | + if (!this.emulator) |
| 160 | + return Bangle.getGPSFix(); |
| 161 | + let fix = {}; |
| 162 | + fix.fix = 1; |
| 163 | + fix.lat = 50; |
| 164 | + fix.lon = 14-(getTime()-this.gps_start) / 1000; /* Go West! */ |
| 165 | + fix.alt = 200; |
| 166 | + fix.speed = 5; |
| 167 | + fix.course = 30; |
| 168 | + fix.time = Date(); |
| 169 | + fix.satellites = 5; |
| 170 | + fix.hdop = 12; |
| 171 | + return fix; |
| 172 | + }, |
| 173 | + gps_start : -1, |
| 174 | + start_gps: function() { |
| 175 | + Bangle.setGPSPower(1, "libgps"); |
| 176 | + this.gps_start = getTime(); |
| 177 | + }, |
| 178 | + stop_gps: function() { |
| 179 | + Bangle.setGPSPower(0, "libgps"); |
| 180 | + }, |
| 181 | +}; |
| 182 | + |
| 183 | + |
| 184 | +var start_time = -5, start_delta; |
| 185 | + |
| 186 | +function updateTime(fix, now) { |
| 187 | + let s = fmt.fmtNow() + "\n"; |
| 188 | + if (!fix.time) |
| 189 | + return s + "???"; |
| 190 | + |
| 191 | + let delta = (now - fix.time.getTime()/1000); |
| 192 | + let tdelta = "" + delta.toFixed(4); |
| 193 | + |
| 194 | + let is_fix = 1; |
| 195 | + // = fix.fix |
| 196 | + |
| 197 | + if (start_time < -1) { |
| 198 | + start_time ++; |
| 199 | + } |
| 200 | + |
| 201 | + if (is_fix && start_time == -1) { |
| 202 | + start_time = now; |
| 203 | + start_delta = delta; |
| 204 | + } |
| 205 | + |
| 206 | + if (!is_fix) |
| 207 | + return s + "e " + tdelta + "s"; |
| 208 | + |
| 209 | + let ppm = (delta - start_delta) / (now - start_time); |
| 210 | + let pd = ppm * (3600*24); |
| 211 | + ppm *= 1000000; |
| 212 | + return s + "ppm " + ppm.toFixed(1) |
| 213 | + + "\n" + pd.toFixed(1) + "s/day" |
| 214 | + + "\ne " + tdelta + "s"; |
| 215 | +} |
| 216 | + |
| 217 | +var cancel_gps = 0; |
| 218 | + |
| 219 | +function on_gps(fix) { |
| 220 | + // Do this first so that we don't get extra jitter |
| 221 | + let now = getTime(); |
| 222 | + |
| 223 | + if (cancel_gps) |
| 224 | + return; |
| 225 | + |
| 226 | + let msg = ""; |
| 227 | + if (fix && fix.fix && fix.lat) { |
| 228 | + msg = "" + fix.speed.toFixed(1) + "km/h " + |
| 229 | + fix.alt.toFixed(0) + "m"; |
| 230 | + } else { |
| 231 | + msg = "N/FIX " |
| 232 | + + (getTime() - gps.gps_start).toFixed(0) + "s"; |
| 233 | + } |
| 234 | + |
| 235 | + msg += "\n" + updateTime(fix, now); |
| 236 | + |
| 237 | + g.reset().clear().setFont("Vector", 31) |
| 238 | + .setColor(1,1,1) |
| 239 | + .fillRect(0, 24, 176, 100) |
| 240 | + .setColor(0,0,0) |
| 241 | + .drawString(msg, 3, 25); |
| 242 | +} |
| 243 | + |
| 244 | +fmt.init(); |
| 245 | +gps.init(); |
| 246 | +gps.start_gps(); |
| 247 | +Bangle.on('GPS', on_gps); |
| 248 | + |
| 249 | +g.reset(); |
0 commit comments