Chronobinometer

Share your advanced PureBasic knowledge/code with the community.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Chronobinometer

Post 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
;}
Last edited by BasicallyPure on Thu Nov 10, 2011 2:35 pm, edited 2 times in total.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Chronobinometer

Post by rsts »

Nice :)
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Chronobinometer

Post by idle »

just what every coder needs :D
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Chronobinometer

Post by BasicallyPure »

That's what happens when I get bored.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: Chronobinometer

Post by flaith »

Nice one :D
btw no am/pm ?
no 24 hours ? :wink:
“Fear is a reaction. Courage is a decision.” - WC
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Chronobinometer

Post 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. ;)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply