Project 09 - An Analog Timer

<< Click to Display Table of Contents >>

Navigation:  Projects >

Project 09 - An Analog Timer

This project creates an analog stopwatch using the CleO primitives.

 

 

Code

 

In the Setup() Arduino routine, it invokes the SetDateTime() widget which prompts the users to select the date and time. This information is stored in the epoch time. The Epoch time is converted to year, month, day of month, hour, minute, second and day of week using the EpochToDate() routine. The timer values are initialized using these values so that the timer will start at these values.

 

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

 

void loop()

{

touchControl();

drawWatchScreen();

updateTimer();

}

 

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

 

The touchControl() routine tracks the pen down events. On pen down event, it checks for any button press and handles the functions of pressing the start/stop, cancel and setting timer buttons as shown in code  below:

 

void processTags(int16_t tag) {

/* On start/pause button press */

 if (tag == START_PAUSE_BUTTON_TAG) {

   if (isTimeOut() == false) {

    /* Start button was not press earlier */

     if (startPressed == false)

       onStartButtonPress();

     else

       onPauseButtonPress();

 

   /* Alternate button press event */

     startPressed = !startPressed;

   } else {

     onSetTimerButtonPress();

     startPressed = true;

   }

 } else if (tag == CANCEL_BUTTON_TAG) {

   if (startPressed == true || isTimeOut() == false)

     onCancelButtonPress();

 }

}

 

The touchcontrol() routine also handles the Pen up event. Here is the sample code snippet:

 

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 draws the watch screen by first drawing the timer and button. Then it uses the tags while drawing the start/pause and cancel buttons. Following sample code shows this routine:

 

void drawWatchScreen() {

 CleO.Start();

 

 /* Draw timer using circle, needle, line, point */

 drawTimer();

 

/* Draws rectangle button area at the bottom of the screen */

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

  double button_width =  0.42 * SCREEN_WIDTH;

  double button_height = (0.08 * SCREEN_HEIGHT);

 

/* Assign touch tag for start/stop button */

 CleO.Tag(START_PAUSE_BUTTON_TAG);

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

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

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

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

 

/* Assign touch tag for cancel/reset button */

 CleO.Tag(CANCEL_BUTTON_TAG);

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

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

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

                (0.95 * SCREEN_HEIGHT), CancelButtonTextColor, MM, 0, 0, CancelButtonText);

 

 CleO.Show();

}

 

 

Output

 

 

Project 9-AnalogTimer