#!/usr/bin/rtb -fm0 // clock: // Example analogue clock program // Gordon Henderson for RTB Basic proc setup nTime = time + 1000 // Loop, waiting for a second, then drawing a new clock // The wait/delay algorithm isn't perfect, but it's good enough for now... cycle proc drawClockFace proc drawClockHands update wait ((nTime - time) / 1000) nTime = nTime + 1000 repeat END // setup: // Initialise our clock and variuos dimensions to make // it work over different display sizes (within reason!) def proc setup local t$ local i cls fontScale (2, 2) rd = gHeight / 2 - 1 thickness = gHeight / 48 barLen = thickness * 4 origin (gWidth / 2, gHeight / 2) deg t$ = time$ while time$ = t$ cycle // Wait for the next whole second repeat endproc // drawClockFace: // Display our clock face // The strategy is to clear the screen then draw it all from scratch def proc drawClockFace local m, px1, py1, px2, py2, d cls2 // Outer dial colour = White circle (0, 0, rd, 1) colour = Black circle (0, 0, rd - thickness, 1) // The four big indicators for 12,15,30 and 45 colour = White rect (0 - thickness / 2, rd - barLen, thickness, barLen, 1) rect (rd - barLen, 0 - thickness / 2, barLen, thickness, 1) rect (0 - thickness / 2, 0 - rd, thickness, barLen, 1) rect (0 - rd, 0 - thickness / 2, barLen, thickness, 1) // Smaller 5 and 1 minute ticks for m = 0 to 59 cycle px1 = sin (m * 6) * (rd - 2) // Outer py1 = cos (m * 6) * (rd - 2) if (m MOD 5) = 0 THEN d = barLen else d = barLen / 2 endif px2 = sin (m * 6) * (rd - d) // Inner py2 = cos (m * 6) * (rd - d) line (px1, py1, px2, py2) repeat endproc // drawClockHands: // Output the 3 hands def proc drawClockHands local t$, h, m, s local sAngle, x, y t$ = time$ // HH:MM:SS h = VAL (LEFT$ (t$, 2)) m = VAL (MID$ (t$, 3, 2)) s = VAL (RIGHT$ (t$, 2)) if (h > 12) then h = h - 12 // Fix for 24-hour proc drawHand (Blue, 0.75, (h * 30) + (m * 0.5), 5) // Hour proc drawHand (Green, 0.85, m * 6, 3) // Minute proc drawHand (Red, 0.95, s * 6, 1) // Second // Decoration sAngle = s * 6 x = sin (sAngle) * (rd * 0.2) y = cos (sAngle) * (rd * 0.2) line (0,0, -x,-y) circle (0, 0, rd * 0.1, 0) circle (0, 0, rd * 0.05, 1) // Text hvTab (tWidth / 2 - 4, tHeight / 8) print t$; endproc // proc drawHand: // Draw our clock hand def proc drawHand (c, length, angle, thick) local x, y, l9 l9 = length * 0.9 colour = c polyStart polyPlot (0,0) x = sin (angle - thick) * (rd * l9) y = cos (angle - thick) * (rd * l9) polyPlot (x, y) x = sin (angle ) * (rd * length) y = cos (angle ) * (rd * length) polyPlot (x, y) x = sin (angle + thick) * (rd * l9) y = cos (angle + thick) * (rd * l9) polyPlot (x, y) polyEnd endproc