Project 08 - An Analog Stop Watch

<< Click to Display Table of Contents >>

Navigation:  Projects >

Project 08 - An Analog Stop Watch

This project uses the CleO primitives to create an analog stopwatch. It has the start/stop and the lap/reset functionality.

 

 

Code

 

The loop() routine controls the main flow and performs the following operation:

 

void loop()

{

touchControl();

drawWatchScreen();

updateTimers();

}

 

The code first checks for any touch draws the clock along with the laps and finally updates the timer values.

 

The touchControl() routine tracks the pen down and pen up events by following the dur and prevdur values. On pen up event, the tags are processed and flips the state of start/stop and the lap/reset buttons as shown in the code below:

 

void processTags(int16_t tag) {

 if (tag == 1) {

   if (startPressed == false)

     onStartButtonPress();

   else

     onStopButtonPress();

 

   startPressed = !startPressed;

 } else if (tag == 2) {

   if (startPressed == true)

     onLapButtonPress();

   else

     onResetButtonPress();

 }

}

 

void touchControl() {

 int16_t x, y, dur, tag;

 CleO.TouchCoordinates(x, y, dur, tag);

 

/* pen down */

 if (previousDur == 0 && dur != 0) {

   previousTag = tag;

   previousDur = 1;

 }

 /* pen up */

 else if (previousDur == 1 && dur == 0) {

   processTags(previousTag);

   previousDur = 0;

 }

}

 

The drawWatchScreen() routine builds three areas, namely the timer, lap, and buttons. Then it uses the tags while drawing start/stop and the lap/reset buttons. Finally,  it updates the lap area by pulling the last lap values using the drawLastLaps() function.

 

The following sample code walks through the drawWatchScreen() function -

 

void drawWatchScreen() {

 /* Start drawing the screen shot */

 CleO.Start();

 drawTimer();

 

 /* Draw lap area using lines and rectangle */

 CleO.RectangleExt(0, (0.60 * SCREEN_HEIGHT), SCREEN_WIDTH, SCREEN_HEIGHT, WHITE, TL, 0, 0);

 drawLastLapsLines();

 

 /* Draws button area using rectangle */

 drawButtonsArea();

 

 /* Selects curves at the corner for the rectangle */

  CleO.RectangleCorner((0.08 * SCREEN_HEIGHT) / 2);

  

  double button_width =  0.42 * SCREEN_WIDTH;

  double button_height = (0.08 * SCREEN_HEIGHT);

 CleO.Tag(1);

 CleO.RectangleExt(CENTER_X - button_width/2 - 5, (0.95 * SCREEN_HEIGHT),

                             button_width, button_height, WHITE, MM, 0, 0);

 

 /* Draw start/stop button using rectangles with corner and string */

 CleO.StringExt(FONT_SANS_2, (0.30 * SCREEN_WIDTH),

                (0.95 * SCREEN_HEIGHT), startButtonTextColor, MM, 0, 0, startButtonText);

 

 CleO.Tag(2);

 CleO.RectangleExt(CENTER_X + button_width/2 + 5, (0.95 * SCREEN_HEIGHT),

                   button_width, button_height, WHITE, MM, 0, 0);    

 /* Draw lap/reset button using rectangles with corner and string */

 CleO.StringExt(FONT_SANS_2, (0.70 * SCREEN_WIDTH),

                (0.95 * SCREEN_HEIGHT), lapButtonTextColor, MM, 0, 0, lapButtonText);

 

 CleO.Tag(3);

 /* Draw last laps only if lap button was pressed */

 if (lapPressed == true)

   drawLastLaps();

 

 /* Show the screenshot */

 CleO.Show();

}

 

The lastLaps globlal array is used to store the list of last laps.

 

 

Output

 

 

Project08-AnalogStopWatch