Page 1 of 1

Chronobinometer

Posted: Thu Nov 10, 2011 3:03 am
by BasicallyPure
Yes it's just another binary clock with blinkin' lights.
A BCD or binary coded decimal clock to be more precise.
Chronobinometer - I made it so I get to name it. :lol:

Edit: It looks like this. The time shown is 07:56:47
Image

Code: Select all

;Chronobinometer, a BCD (binary coded decimal) clock
;
;by BasicallyPure
;11-09-2011
;
;tested ok on Windows and Linux

EnableExplicit

;{ constants
#MainWin      = 0
#ImgGad_Clock = 0
#Image_Clock  = 0
#ClockTimer   = 0
#HourColor_1   = $0080FF
#HourColor_0   = $003060
#MinuteColor_1 = $FFA000
#MinuteColor_0 = $704000
#SecondColor_1 = $00FF80
#SecondColor_0 = $006030
;}

;{ variables
   Define.l gadget, hour, minute, second, time, thisTime, lastTime
   Define.l color, x, flags, eventID, colorOff, colorOn
;}

flags = #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered
If Not OpenWindow(#MainWin, 0, 0, 302, 56, "Chronobinometer", flags) : End : EndIf
StickyWindow(#MainWin, #True)
ImageGadget(#ImgGad_Clock, 2, 2, 296, 50, 0, #PB_Image_Border)
CreateImage(#Image_Clock,296,50)

;{ Draw the clock and setup the clock's timer.
If StartDrawing(ImageOutput(#Image_Clock))
   color = #HourColor_0
   For x = 5 To 281 Step 12 ;draw all lights in the 'off' color
      Select x
         Case 17,125,233 ;skip over the digit separators
            Continue
         Case 89 ;switch to minute's color
            color = #MinuteColor_0
         Case 197 ;switch to second's color
            color = #SecondColor_0
      EndSelect
      Box(x,10,10,30,color)
   Next x
   Box(17, 20, 10, 10 , #HourColor_1)    ;hour's digit separator
   Box(125, 20, 10, 10 , #MinuteColor_1) ;minute's digit separator
   Box(233, 20, 10, 10 , #SecondColor_1) ;second's digit separator
   Box( 77,0,10,50,$808080) ;divider between hours & minutes
   Box(185,0,10,50,$808080) ;divider between minutes & seconds
   StopDrawing()
   SetGadgetState(#ImgGad_Clock,ImageID(#Image_Clock))
   AddWindowTimer(#MainWin,#ClockTimer,1000)
EndIf
;}

;{ event loop
Repeat
   eventID = WaitWindowEvent()
   Select eventID
      Case #PB_Event_CloseWindow
         End
      Case #PB_Event_Timer
         If EventTimer() = #ClockTimer ;update the clock display
            hour = Val(FormatDate("%hh" , Date()))
            minute = Val(FormatDate("%ii" , Date()))
            second = Val(FormatDate("%ss" , Date()))
            ;use 12 hour format
            If hour > 12 : hour - 12 : EndIf
            If hour < 1 : hour = 12 : EndIf
            
            ;encode current time as a 24 bit serial word as follows:
            ; 24 bit word looks like this: HZhhhhZMMMZmmmmZSSSZssss
            ; H = 1 or 0 for hours tens, h = 1 or 0 for hours ones
            ; M = 1 or 0 for minutes tens, m = 1 or 0 for minutes ones
            ; S = 1 or 0 for seconds tens, s = 1 or 0 for seconds ones
            ; Z = always zero for a space between digits
            
            time = hour   / 10 ;integer division gives hour 'tens' digit (x)
            time << 5 ;shift left to make room for hour 'ones' digit (Zhhhh)
            time + hour   % 10 ;modulo division gives hour 'ones' digit
            time << 4 ;shift left to make room for minutes 'tens' digit (ZMMM)
            time + minute / 10 ;integer division gives minutes 'tens' digit
            time << 5 ;shift left to make room for minutes 'ones' digit (Zmmmm)
            time + minute % 10 ;modulo division gives minutes 'ones' digit
            time << 4 ;shift left to make room for seconds 'tens' digit (ZSSS)
            time + second / 10 ;integer division gives seconds 'tens' digit
            time << 5 ;shift left to make room for seconds 'ones' digit (Zssss)
            time + second % 10 ;modulo division gives seconds 'ones' digit
            
            ;animate the clock display
            If StartDrawing(ImageOutput(#Image_Clock))
               thisTime = time
               colorOn  = #SecondColor_1
               colorOff = #SecondColor_0
               For x = 281 To 5 Step - 12
                  Select x
                     Case 185 ;switch to minute colors
                        colorOn = #MinuteColor_1
                        colorOff = #MinuteColor_0
                     Case 77 ;switch to hour colors
                        colorOn = #HourColor_1
                        colorOff = #HourColor_0
                  EndSelect
                  If (thisTime & 1) And Not(lastTime & 1)
                     Box(x,10,10,30,colorOn) ;turn light on
                  ElseIf (lastTime & 1) And Not(thisTime & 1)
                     Box(x,10,10,30,colorOff) ;turn light off
                  EndIf
                  thisTime >> 1 : lastTime >> 1
               Next x
               lastTime = time
               StopDrawing()
               SetGadgetState(#ImgGad_Clock,ImageID(#Image_Clock))
            EndIf
         EndIf
   EndSelect
ForEver
;}

Re: Chronobinometer

Posted: Thu Nov 10, 2011 3:25 am
by rsts
Nice :)

Re: Chronobinometer

Posted: Thu Nov 10, 2011 3:29 am
by idle
just what every coder needs :D

Re: Chronobinometer

Posted: Thu Nov 10, 2011 3:52 am
by BasicallyPure
That's what happens when I get bored.

Re: Chronobinometer

Posted: Thu Nov 10, 2011 11:11 am
by flaith
Nice one :D
btw no am/pm ?
no 24 hours ? :wink:

Re: Chronobinometer

Posted: Thu Nov 10, 2011 11:16 am
by MachineCode
You'd have to be such an ubergeek to read the time like that. :)

Having said that, however, I do actually one own of these watches:

Image

The time shown is 2:16 PM. So, I'm geekish, but not an ubergeek like BasicallyPure. ;)