A tiny no-frills speedometer progressive web app based on Geolocation API, AmbientLightSensor API, and WakeLock API.
Because the Combination Meter on my 2005 Prius is on the fritz, and I need a speedometer until I can tear apart and repair the faulty caps. :-)
The GeoLocation API has a Position
interface, which contains a Coordinates
object, coords
. This Coordinates
interface contains not only things you might expect (latitude, longitude), but also contains a property called speed
, which is represents the device velocity in meters per second. Fun fact, Chrome on Android returns this prop with very little effort, at which point it's just a matter of some quick math to do the mph/kmh conversion.
I implemented AmbientLightSensor API to enable a dark mode while driving at night. To get this working in Chrome on Android you have to enable Generic Sensor extra classes, which is behind a flag: chrome://flags#enable-generic-sensor-extra-classes
Since M71, the WakeLock API has been enabled for testing purposes (see the current status on Chrome Platform Status). This ideally allows us to keep the screen from going into power saving mode and generally an off state. I've enabled this (you'll need M71 Chrome for Android) for testing.
Hop into the src
folder and run the local web server of your choice. No specific tool required.
I didn't really wrap this with build tooling, but I did crunch it down to 1.7K gzip'ed for fun using closure compiler and http-minifier. The rest is just some fancy Linux CLI jumping and sed (and I almost just used a make file instead ;-).
$ yarn install
$ yarn dist
- How accurate is this?
When the dash is working on the Prius, it's pretty accurate from the looks of it. You're going to be constainted on any number of know problems with GPS (e.g., when the Coordinates.accuracy
is not very good because of lock or assist issues).
- Why didn't you use build tools/a framework/something I like.
Didn't feel the need.
- Couldn't you have done this with the Generic Sensors API and the accelerometer?
Yeah, probably.