Tuesday, October 27, 2020

Building a Garmin Watch Widget | Part 1: Getting Started

This past weekend, while on walk, I flipped over to the myABC widget on my Garmin Vivoactive 4 watch to see what direction we were headed. The compass (the 'C' in ABC) announced we were walking North-West. On our return, I once again checked my watch and once again, it read North-West. That can't be right I thought, and for not the first time I cursed my watch's compass.

I was disappointed, but not surprised. Like nearly every electronic device with a compass, I've found that at times it's wildly inaccurate. The irony that my watch can communicate with satellites in space to determine my location on the globe to a few meters, but can't do the job of a couple of magnets and dental floss isn't lost on me.

Yet, I've got a solution: I'm going to add a solar compass to my digital one. From earlier experiments, I know that with the current time and lat/long I can predict the direction and elevation of the sun. My watch has a GPS and a clock, so with a bit of math to figure out the azimuth of the Sun and dollop of graphics to render a dial, I should have an accurate navigation tool.

On a sunny day, with a locked in GPS, such a widget should be fast and reliable. There's only one minor detail: I've never written code for my Vivoactive. But not to worry, I've got a plan! All I need to do is:

  1. Figure out how to write a Hello World widget for the Vivoactive 4.
  2. Find useful examples that I can cheat with use as inspiration.
  3. Write code to display the input values I'll need for my calculations.
  4. Code the calculations to determine the Sun's position.
  5. Render this position information graphically using a dial.
  6. Publish the app to the Connect IQ store to give back to the community I've benefited from.

Step 1 was easy enough, I followed the instructions here. I'm not a fan of IDEs, but even I have to admit that the eclipse plugin made getting started a breeze.

I got lucky with Step 2, when I found this Garmin watch app: SunCalc. It's not quite the same solar calculations I'll be making, but there's definitely overlap. Just seeing that I can call Math.sin(...) is encouraging and gives me the sense that the calculations I want to make are doable.

And finally, I played with the API docs until I figured out Step 3: showing necessary inputs. Here's a screenshot of the simulator that shows the current time zone, lat/lng and day-of-year number for the current moment. It's not much to look at, but it's huge progress:

Find the code for this widget here.

Up next, I'll be working on coding the algorithms to power the compass. Stay tuned.

No comments:

Post a Comment