GLCD Analogue Clock

Assume you have a LCD_write_pixel (x, y, ink)
where ink is on/black or off/white

Analogue Clock is centred in a 128x64 GLCD panel. Top left is 0, 0
bottom right is 127,63

to position any place from 32 to 95, subtract 31 off the x co-ordinates and have a constant added of the X-Offset.

The co-ordinates minutes and hours dots and the end points of the clock hands are created in a spreadsheet using
R * cos (minutes * 6 -90) and
R *sin (minutes * 6 -90)
as 1 minute is 6 degrees and 12 o'clock is 0 minutes = -90 degrees.
You'll need to convert to radians.

Assume you have a regular interrupt to count with:

first draw the clock face

              if  lastkey == "*" then
                    DrawClockFace()
                    clock_on = true
<

in some initialisation or menu code

Main "tick" driven code fragment:

counter somehow gets incremented every 2ms in this example

    
     if (counter == 0) then
    	     counter = 500
          
             if clock_on then
                DrawSecondHand (seconds, off)
                DrawMinuteHand (minutes,off)
                DrawHourHand (hours, off)
             end if
             seconds = seconds +1
             if seconds > 59 then
                seconds = 0
                minutes = minutes +1
                if minutes > 59 then
                    minutes =0
                    hours = hours +1
                    if hours > 23 then
                       hours = 0
                    end if
                end if
             end if
             if clock_on then
                DrawHourHand (hours, on)
                DrawMinuteHand (minutes,on)
                DrawSecondHand (seconds, on)
             end if
         end if <

The supporting cast:

procedure LCD_Line(byte in x0, byte in y0, byte in x1, byte in y1, bit in ink) is
var bit steep, yinc
var sword deltax, deltay, err
var byte px, py
    deltax = sword(x1)-sword(x0)
    deltay = sword(y1)-sword(y0)
    if (deltax < 0) then
       deltax = -deltax
    end if
    if (deltay < 0) then
       deltay = -deltay
    end if
    steep = (deltay > deltax)
    if steep then   -- swap(x0,y0) swap(x1,y1)
       px = x0
       x0 = y0
       y0 = px
       
       px = x1
       x1 = y1
       y1 = px
    end if
    if (x0 > x1) then  -- swap(x0,x1) swap(y0,y1)
       px = x0
       x0 = x1
       x1 = px
       
       py = y0
       y0 = y1
       y1 = py
    end if
    deltax = sword(x1)-sword(x0)               -- we might have swapped
    err = deltax / 2              -- assume the compiler does a shift
    yinc =  (y1 > y0)
    py = y0
    deltay = sword(y1)-sword(y0)    -- we might have swapped
     if (deltay < 0) then
       deltay = -deltay
    end if
    for deltax using px loop
       if steep then
           lcd_write_pixel (py, x0+px, ink)
       else
           lcd_write_pixel (x0+px, py, ink)
       end if
       err = err - deltay
       if (err < 0) then
           if yinc then
              py = py + 1
           else
              py = py -1
           end if
           err = err + deltax
       end if
    end loop
end procedure

procedure Lcd_DrawRectangle (byte in x0 , byte in y0 , byte in x1 , byte in y1, bit in ink ) is
   x1 = x0 + x1 - 1
   y1 = y0 + y1 - 1
   lcd_orth_line ( x0,y0, x1,y0 )
   lcd_orth_line ( x1,y0, x1,y1 )
   lcd_orth_line ( x1,y1, x0,y1 )
   lcd_orth_line ( x0,y1, x0,y0 )
end procedure


procedure lcd_orth_line(byte in x0, byte in y0, byte in x1, byte in y1) is
 var byte xi,yi , xfark,yfark, fark , xx

   xi = x0
   yi = y0
   if x1 >= x0  then  xfark = x1 - x0 else  xfark = x0 - x1 end if
   if y1 >= y0  then  yfark = y1 - y0 else  yfark = y0 - y1 end if

   if xfark >= yfark then fark = xfark else fark = yfark end if
   xx = 0
   for fark loop
      lcd_write_pixel ( xi, yi ,1)
      if xx < xfark then
          if x1 >= x0 then xi = xi + 1 else xi = xi - 1 end if
      end if
      if xx < yfark then
          if y1 >= y0 then yi = yi + 1 else yi = yi - 1 end if
      end if
      xx = xx + 1
   end loop
end procedure
<
-- Title: graphic_utils
procedure DrawClockFace() is
    lcd_fill(0)
    LCD_DrawRectangle (62, 31, 2, 2, True)
--draw minutes dots
    lcd_write_pixel (65, 0, True)   -- 1
    lcd_write_pixel (68, 1, True)   -- 2
    lcd_write_pixel (72, 2, True)   -- 3
    lcd_write_pixel (75, 3, True)   -- 4
    lcd_write_pixel (78, 4, True)   -- 5
    lcd_write_pixel (80, 6, True)   -- 6
    lcd_write_pixel (83, 8, True)   -- 7
    lcd_write_pixel (85, 10, True)   -- 8
    lcd_write_pixel (87, 13, True)   -- 9
    lcd_write_pixel (89, 15, True)   -- 10
    lcd_write_pixel (90, 18, True)   -- 11
    lcd_write_pixel (91, 21, True)   -- 12
    lcd_write_pixel (92, 25, True)   -- 13
    lcd_write_pixel (93, 28, True)   -- 14
    lcd_write_pixel (93, 31, True)   -- 15
    lcd_write_pixel (93, 34, True)   -- 16
    lcd_write_pixel (92, 37, True)   -- 17
    lcd_write_pixel (91, 41, True)   -- 18
    lcd_write_pixel (90, 44, True)   -- 19
    lcd_write_pixel (89, 47, True)   -- 20
    lcd_write_pixel (87, 49, True)   -- 21
    lcd_write_pixel (85, 52, True)   -- 22
    lcd_write_pixel (83, 54, True)   -- 23
    lcd_write_pixel (80, 56, True)   -- 24
    lcd_write_pixel (78, 58, True)   -- 25
    lcd_write_pixel (75, 59, True)   -- 26
    lcd_write_pixel (72, 60, True)   -- 27
    lcd_write_pixel (68, 61, True)   -- 28
    lcd_write_pixel (65, 62, True)   -- 29
    lcd_write_pixel (62, 62, True)   -- 30
    lcd_write_pixel (59, 62, True)   -- 31
    lcd_write_pixel (56, 61, True)   -- 32
    lcd_write_pixel (52, 60, True)   -- 33
    lcd_write_pixel (49, 59, True)   -- 34
    lcd_write_pixel (46, 58, True)   -- 35
    lcd_write_pixel (44, 56, True)   -- 36
    lcd_write_pixel (41, 54, True)   -- 37
    lcd_write_pixel (39, 52, True)   -- 38
    lcd_write_pixel (37, 49, True)   -- 39
    lcd_write_pixel (35, 47, True)   -- 40
    lcd_write_pixel (34, 44, True)   -- 41
    lcd_write_pixel (33, 41, True)   -- 42
    lcd_write_pixel (32, 37, True)   -- 43
    lcd_write_pixel (31, 34, True)   -- 44
    lcd_write_pixel (31, 31, True)   -- 45
    lcd_write_pixel (31, 28, True)   -- 46
    lcd_write_pixel (32, 25, True)   -- 47
    lcd_write_pixel (33, 21, True)   -- 48
    lcd_write_pixel (34, 18, True)   -- 49
    lcd_write_pixel (35, 15, True)   -- 50
    lcd_write_pixel (37, 13, True)   -- 51
    lcd_write_pixel (39, 10, True)   -- 52
    lcd_write_pixel (41, 8, True)   -- 53
    lcd_write_pixel (44, 6, True)   -- 54
    lcd_write_pixel (46, 4, True)   -- 55
    lcd_write_pixel (49, 3, True)   -- 56
    lcd_write_pixel (52, 2, True)   -- 57
    lcd_write_pixel (56, 1, True)   -- 58
    lcd_write_pixel (59, 0, True)   -- 59
    lcd_write_pixel (62, 0, True)   -- 60

 --draw hours dots

    LCD_DrawRectangle (78, 4, 2, 2, True )
    LCD_DrawRectangle (89, 15, 2, 2, True )
    LCD_DrawRectangle (93, 31, 2, 2, True )
    LCD_DrawRectangle (89, 47, 2, 2, True)
    LCD_DrawRectangle (78, 58, 2, 2, True )
    LCD_DrawRectangle (62, 62, 2, 2, True )
    LCD_DrawRectangle (46, 58, 2, 2, True )
    LCD_DrawRectangle (35, 47, 2, 2, True )
    LCD_DrawRectangle (31, 31, 2, 2, True )
    LCD_DrawRectangle (35, 15, 2, 2, True )
    LCD_DrawRectangle (46, 4, 2, 2, True )
    LCD_DrawRectangle (62, 0, 2, 2, True )
End procedure


procedure DrawMinuteHand(byte in minutes, bit in ink) is
    case minutes of
     1  :
        LCD_line (62, 31, 65, 7, ink)   --  1
     2  :
        LCD_line (62, 31, 67, 8, ink)   --  2
     3  :
        LCD_line (62, 31, 69, 8, ink)   --  3
     4  :
        LCD_line (62, 31, 72, 9, ink)   --  4
     5  :
        LCD_line (62, 31, 74, 10, ink)   --  5
     6  :
        LCD_line (62, 31, 76, 12, ink)   --  6
     7  :
        LCD_line (62, 31, 78, 13, ink)   --  7
     8  :
        LCD_line (62, 31, 80, 15, ink)   --  8
     9  :
        LCD_line (62, 31, 81, 17, ink)   --  9
     10 :
        LCD_line (62, 31, 83, 19, ink)   --  10
     11 :
        LCD_line (62, 31, 84, 21, ink)   --  11
     12 :
        LCD_line (62, 31, 85, 24, ink)   --  12
     13 :
        LCD_line (62, 31, 85, 26, ink)   --  13
     14 :
        LCD_line (62, 31, 86, 28, ink)   --  14
     15 :
        LCD_line (62, 31, 86, 31, ink)   --  15
     16 :
        LCD_line (62, 31, 86, 34, ink)   --  16
     17 :
        LCD_line (62, 31, 85, 36, ink)   --  17
     18 :
        LCD_line (62, 31, 85, 38, ink)   --  18
     19 :
        LCD_line (62, 31, 84, 41, ink)   --  19
     20 :
        LCD_line (62, 31, 83, 43, ink)   --  20
     21 :
        LCD_line (62, 31, 81, 45, ink)   --  21
     22 :
        LCD_line (62, 31, 80, 47, ink)   --  22
     23 :
        LCD_line (62, 31, 78, 49, ink)   --  23
     24  :
        LCD_line (62, 31, 76, 50, ink)   --  24
     25 :
        LCD_line (62, 31, 74, 52, ink)   --  25
     26 :
        LCD_line (62, 31, 72, 53, ink)   --  26
     27 :
        LCD_line (62, 31, 69, 54, ink)   --  27
     28 :
        LCD_line (62, 31, 67, 54, ink)   --  28
     29 :
        LCD_line (62, 31, 65, 55, ink)   --  29
     30 :
        LCD_line (62, 31, 62, 55, ink)   --  30
     31 :
        LCD_line (62, 31, 59, 55, ink)   --  31
     32 :
        LCD_line (62, 31, 57, 54, ink)   --  32
     33 :
        LCD_line (62, 31, 55, 54, ink)   --  33
     34 :
        LCD_line (62, 31, 52, 53, ink)   --  34
     35 :
        LCD_line (62, 31, 50, 52, ink)   --  35
     36 :
        LCD_line (62, 31, 48, 50, ink)   --  36
     37 :
        LCD_line (62, 31, 46, 49, ink)   --  37
     38 :
        LCD_line (62, 31, 44, 47, ink)   --  38
     39 :
        LCD_line (62, 31, 43, 45, ink)   --  39
     40 :
        LCD_line (62, 31, 41, 43, ink)   --  40
     41 :
        LCD_line (62, 31, 40, 41, ink)   --  41
     42 :
        LCD_line (62, 31, 39, 38, ink)   --  42
     43 :
        LCD_line (62, 31, 39, 36, ink)   --  43
     44 :
        LCD_line (62, 31, 38, 34, ink)   --  44
     45 :
        LCD_line (62, 31, 38, 31, ink)   --  45
     46 :
        LCD_line (62, 31, 38, 28, ink)   --  46
     47 :
        LCD_line (62, 31, 39, 26, ink)   --  47
     48 :
        LCD_line (62, 31, 39, 24, ink)   --  48
     49 :
        LCD_line (62, 31, 40, 21, ink)   --  49
     50 :
        LCD_line (62, 31, 41, 19, ink)   --  50
     51 :
        LCD_line (62, 31, 43, 17, ink)   --  51
     52  :
        LCD_line (62, 31, 44, 15, ink)   --  52
     53 :
        LCD_line (62, 31, 46, 13, ink)   --  53
     54  :
        LCD_line (62, 31, 48, 12, ink)   --  54
     55 :
        LCD_line (62, 31, 50, 10, ink)   --  55
     56 :
        LCD_line (62, 31, 52, 9, ink)   --  56
     57 :
        LCD_line (62, 31, 55, 8, ink)   --  57
     58 :
        LCD_line (62, 31, 57, 8, ink)   --  58
     59 :
        LCD_line (62, 31, 59, 7, ink)   --  59
     0 :
        LCD_line (62, 31, 62, 7, ink)   --  0
    End case
End procedure

procedure DrawSecondHand(byte in seconds, bit in ink ) is
    case seconds of
    0 :
        LCD_line (62, 31, 62, 3, ink)    --  0
    1 :
        LCD_line (62, 31, 65, 3, ink)    --  1
    2 :
        LCD_line (62, 31, 68, 4, ink)    --  2
    3 :
        LCD_line (62, 31, 71, 4, ink)    --  3
    4 :
        LCD_line (62, 31, 73, 5, ink)    --  4
    5 :
        LCD_line (62, 31, 76, 7, ink)    --  5
    6 :
        LCD_line (62, 31, 78, 8, ink)    --  6
    7 :
        LCD_line (62, 31, 81, 10, ink)    --  7
    8 :
        LCD_line (62, 31, 83, 12, ink)    --  8
    9 :
        LCD_line (62, 31, 85, 15, ink)    --  9
    10 :
        LCD_line (62, 31, 86, 17, ink)    --  10
    11 :
        LCD_line (62, 31, 88, 20, ink)    --  11
    12 :
        LCD_line (62, 31, 89, 22, ink)    --  12
    13 :
        LCD_line (62, 31, 89, 25, ink)    --  13
    14 :
        LCD_line (62, 31, 90, 28, ink)    --  14
    15 :
        LCD_line (62, 31, 90, 31, ink)    --  15
    16 :
        LCD_line (62, 31, 90, 34, ink)    --  16
    17 :
        LCD_line (62, 31, 89, 37, ink)    --  17
    18 :
        LCD_line (62, 31, 89, 40, ink)    --  18
    19 :
        LCD_line (62, 31, 88, 42, ink)    --  19
    20 :
        LCD_line (62, 31, 86, 45, ink)    --  20
    21 :
        LCD_line (62, 31, 85, 47, ink)    --  21
    22 :
        LCD_line (62, 31, 83, 50, ink)    --  22
    23 :
        LCD_line (62, 31, 81, 52, ink)    --  23
    24 :
        LCD_line (62, 31, 78, 54, ink)    --  24
    25 :
        LCD_line (62, 31, 76, 55, ink)    --  25
    26 :
        LCD_line (62, 31, 73, 57, ink)    --  26
    27 :
        LCD_line (62, 31, 71, 58, ink)    --  27
    28 :
        LCD_line (62, 31, 68, 58, ink)    --  28
    29 :
        LCD_line (62, 31, 65, 59, ink)    --  29
    30 :
        LCD_line (62, 31, 62, 59, ink)    --  30
    31 :
        LCD_line (62, 31, 59, 59, ink)    --  31
    32 :
        LCD_line (62, 31, 56, 58, ink)    --  32
    33 :
        LCD_line (62, 31, 53, 58, ink)    --  33
    34 :
        LCD_line (62, 31, 51, 57, ink)    --  34
    35 :
        LCD_line (62, 31, 48, 55, ink)    --  35
    36 :
        LCD_line (62, 31, 46, 54, ink)    --  36
    37 :
        LCD_line (62, 31, 43, 52, ink)    --  37
    38 :
        LCD_line (62, 31, 41, 50, ink)    --  38
    39 :
        LCD_line (62, 31, 39, 47, ink)    --  39
    40 :
        LCD_line (62, 31, 38, 45, ink)    --  40
    41 :
        LCD_line (62, 31, 36, 42, ink)    --  41
    42 :
        LCD_line (62, 31, 35, 40, ink)    --  42
    43 :
        LCD_line (62, 31, 35, 37, ink)    --  43
    44 :
        LCD_line (62, 31, 34, 34, ink)    --  44
    45 :
        LCD_line (62, 31, 34, 31, ink)    --  45
    46 :
        LCD_line (62, 31, 34, 28, ink)    --  46
    47 :
        LCD_line (62, 31, 35, 25, ink)    --  47
    48 :
        LCD_line (62, 31, 35, 22, ink)    --  48
    49 :
        LCD_line (62, 31, 36, 20, ink)    --  49
    50 :
        LCD_line (62, 31, 38, 17, ink)    --  50
    51 :
        LCD_line (62, 31, 39, 15, ink)    --  51
    52 :
        LCD_line (62, 31, 41, 12, ink)    --  52
    53 :
        LCD_line (62, 31, 43, 10, ink)    --  53
    54 :
        LCD_line (62, 31, 46, 8, ink)    --  54
    55 :
        LCD_line (62, 31, 48, 7, ink)    --  55
    56 :
        LCD_line (62, 31, 51, 5, ink)    --  56
    57 :
        LCD_line (62, 31, 53, 4, ink)    --  57
    58 :
        LCD_line (62, 31, 56, 4, ink)    --  58
    59 :
        LCD_line (62, 31, 59, 3, ink)    --  59
    end case
    LCD_DrawRectangle (62, 31, 2, 2, on) --centre
end procedure

procedure DrawHourHand(byte in hours, bit in ink) is
   case hours of
   1, 13:
    LCD_line (62, 31, 70, 17, ink)
   2, 14:
    LCD_line (62, 31, 76, 23, ink)
   3, 15 :
    LCD_line (62, 31, 78, 31, ink)
   4, 16 :
    LCD_line (62, 31, 76, 39, ink)
   5, 17 :
    LCD_line (62, 31, 70, 45, ink)
   6, 18 :
    LCD_line (62, 31, 62, 47, ink)
   7, 19 :
    LCD_line (62, 31, 54, 45, ink)
   8, 20 :
    LCD_line (62, 31, 48, 39, ink)
   9, 21 :
    LCD_line (62, 31, 46, 31, ink)
   10, 22 :
    LCD_line (62, 31, 48, 23, ink)
   11, 23 :
    LCD_line (62, 31, 54, 17, ink)
   12, 0, 24 :
    LCD_line (62, 31, 62, 15, ink)
    end case

end Procedure


procedure  LCD_DrawButton(byte in px , byte in py , byte in wx , byte in wy, bit in ink ) is
var byte ind
    For wx using ind loop
        lcd_write_pixel ( ind+wx, py, ink)
        lcd_write_pixel ( ind+wx, py + wy - 1, ink)
        lcd_write_pixel ( ind+wx + 1, py + wy, ink)
    end loop
    For wy using ind loop
        lcd_write_pixel ( px, ind+py, ink)
        lcd_write_pixel ( px + wx - 1, ind+py, ink)
        lcd_write_pixel ( px + wx, ind +py + 1, ink)
    end loop
end procedure
<