Share this page to your:

Clock

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.

Clock

clock

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.

The 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.

Notice that there are three copies of the Button objects which 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.

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.

The 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.

Set Clock

clock

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.

The 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.

Summary

This described the clock and setclock apps and gave more details on how apps in general are designed.