Now that our O-Watches can keep and display accurate time, let's go one step further and use the TinyScreen graphics to draw a round analog face!
It is strongly recommended that you get the previous Simple RTC Watch working before trying this.
Note: This watch face is now included in Mult-O-Watch.
We'll use the drawCircle() code from the TinyScreen Demo lesson to draw a circle for the clock face, then use some old-fashioned code that uses some tricks and a look-up table to draw the hands on the face using sine() and cosine() without using any costly floating point math--it's all done with integers.
We'll keep the text display of the date, time, and day of the week too, but in a more compact form.
Because our (re)drawing commands for the clock hands are very simple, there's a bit of flicker as they're drawn, completely erased, then redrawn. A little extra code to keep track of the previous time and hand positions and only redraw the minute and hour hand when really needed would cut down on the flicker.
Also, the hour, minute, and second hands are all simple lines. It could be fun to see what other sorts of shapes you can use for them.
What Is A Look-up Table?
A look-up table (LUT) is simply a list of pairs of numbers. The first number in the pair is the one that you use to look up the second number. It works just like speed-dial on a phone: you push speed-dial #3, and the phone looks up that 3 corresponds to the phone number 213-123-4567, then dials it for you.
LUTs are useful when you have a bunch of numbers that are hard to remember, take a lot of time or effort to figure out, and/or have a special meaning.
For example, early digital computers used LUTs all the time. The guidance computers on the Apollo space rockets used LUTs to store pre-computed values for all of the complex math functions needed to go to the moon. The Apollo computers were very limited in their capabilities, so it would take them a long time to calculate the complex math involved in steering the spacecraft. Since the corrections to the rockets' paths sometimes had to happen within a fraction of a second, the computers would be too slow to figure out how big the corrections needed to be. Plus, the spacecraft ran on batteries, so the astronauts had to save every bit of battery energy they could. The solution was to use LUTs, and to look up the numbers needed, which the Apollo computers could do very quickly.
Before calculators and personal computers were common, people used LUTs too. These were big books just filled LUTs that would go on for pages and pages. It was much easier and faster for people to look up values in a book rather than try to figure them out with pencil and paper, slide-rule, or abacus. It was also really easy to make a mistake in a calculation, while it was much harder to make a mistake while looking for a number in a table.
A special kind of LUT used by computers is called a jump table or branch table. This table stores a list of addresses (locations) in the computer's memory, which are the starting points of various functions. For example, an app only needs to remember that branch #2 has to do with reading data from the hard drive, while #3 is for writing data to the hard drive. The app looks up branch address #2 in the LUT, then jumps right to it, and runs the code to read from the hard drive. If a driver module wants to replace the standard code for reading from disk with its own faster smarter code, it only has to change the address in the #2 entry of the branch table to its own address, and the app will jump to the driver's code instead. This is a very fast and efficient way to give apps a standard method for accessing functions, and used to be very common in computer BIOS code.