There are two Clock apps. One just displays the clock, the other sets the date and time. The VM is not connected to the internet so it cannot sync its time with anything external. It also doesn't know about daylight savings and timezones. So you will likely need to adjust the clock time now and then. But we can start by looking at the Clock app itself.
The bulk of the code for the clock is in the
display() method which is called about once second. This is the default display interval and it is determined in App.h. The Clock app could override this interval but it doesn't need to.
But before we get to the display() method we should look at the
setup() method. This is called when we invoke the app from the menu and it clears the screen and draws the two buttons.
display() method checks to see if the m_noise is true, that means we are sending a click to the speaker and flashing the LED.
The rest of the code blanks out the section of the screen which displayed the previous date and time and writes new values to it. But there is a little more. The clock can show Mars time if the
m_mars flag is set. This flag is toggled by the MARS button.
The time zone is currently hard coded to Auckland, New Zealand, ie where I live. The timezone is initialised in Hardware.cpp and uses the Jack Christensen library to allow me to define timezone rules to handle daylight savings. It means the underlying clock value stays and GMT and only converted when I display it. The time display on the menu uses the same mechanism.
Notice that the Button objects draw themselves, otherwise all the screen operations are done using the Graphics library directly. The Graphics library is a variant of the Adafruit library, optimised for Teensy. The MARS and EARTH buttons change their visibility depending on the mode.
Mars time is calculated for the zero longitude on Mars, effectively its GMT. The exact zero longitude and zero lattitude point is a crater named Airy-0 in Sinus Meridiani so that is what I use as the time zone name. While there are times of day there are no dates yet on Mars so none displayed here. When landers arrive on Mars they assign their landing date as SOL 0 and count forward from there, so every lander has its own date sequence.
Other calls for the speaker and LEDs are through the Hardware library, which is just another VM class. We will see more calls to Hardware in the other apps.
touch(TS_Point p) method is called when a touch event is detected. The method checks the touch point with each button to see if the touch point is on that button. If it is then the button is flashed to give the user feedback, and the
m_noise flag is set or cleared, depending on which button. Other touch points are ignored.
The second app to mention here is the one that sets the time on the clock. It is worth refreshing that the clock is on board the Teensy and it has a crystal soldered underneath the Teensy as well as a backup coin cell battery so it will keep time even when the main battery is disconnected.
setup() method in this app loads all the buttons with values and draws them. The buttons here are a lot like the buttons we already saw on the clock, but they are actually implemented in the Incrementor class. The Incrementor takes a numeric value to display and displays it as a wide button. If you press the right hand side it will increment the numeric value. If you press the left hand side it will decrement the value. The Increment class also has min and max values it enforces.
Using an Incrementor button for hours. minutes, days, months and years allows us to indicate a time, and an OK button takes that time and sets the clock to it. It is actually a two step setting because we set the CPU clock and then set the Teensy internal clock to the same value. That way we can have the boot sequence pull the value from the Teensy clock (the one that is backed up by that coin cell battery) and copy it to the CPU clock.
The display() method in this app does nothing. There are no dynamic updates to do here so no need to refresh the display.
This described the clock and setclock apps and gave more details on how apps in general are designed.