Jump to content

Binary Coded Decimal (BCD) Clock


BrewManNH
 Share

Recommended Posts

Following along with the odd clock theme that seems to be popular lately, I have come up with a BCD clock to add to the collection. It's a 24 hour clock, you can modify it to a 12 hour clock, but it's not as interesting that way.

This clock can be changed to display milliseconds by changing one variable at the start of the Main function, there are comments describing that. In my opinion I wouldn't have it display the mseconds because they change too fast to keep track of, it looks terrible, and flickers BADLY.

The way the clock is set up currently, it displays one icon for the light being "off" and another for it being "on". You can use whatever icons for these that you'd like instead of the ones in the zip file. Change the variables in the Global declaration at the start of the script to whatever you'd like. Another variation that you can use is to set the "off" icon to a blank file name and then it will only turn on the lights that are supposed to be on, and there will be empty spaces for the off icons.

If nothing else, at least you'll learn binary and/or BCD numbers. :)

Updated: Corrected the issue with the seconds for the number 7.

 

BCD Clock.zip

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

funny clock

p.s.
it seems to me that there is an issue in the digit 7 of the seconds.

Here is a shorter way to turn on/off bits (Rearranging the GUICtrlCreateIcon positions on the GUI and within the array it can be shorten even much more)

;*****************************************
;BCD_Clock.au3 by BrewManNH
;*****************************************
#include <GUIConstantsEx.au3>

Global $aIcons[2] = [@ScriptDir & "\MyAutoIt3_Red.ico", @ScriptDir & "\MyAutoIt3_Green.ico"] ; [0] -> off , [1] -> on

Main()
Func Main()
    Local $GUIWidth = 470, $iNum_Columns = 5
;~     Note if you set this to true it will display the MS on the clock, but be warned it looks horrible and flickers badly
    Local $bShowMS = False ; False = No MS display, True = Display MS
    Local $aControlArray[21], $Counter = 0
    If $bShowMS Then
        $GUIWidth = 670
        $iNum_Columns = 8
        ReDim $aControlArray[32]
    EndIf
    $BCD_GUI = GUICreate("BCD Clock", $GUIWidth, 270, -1, -1)
    For $Column = 0 To $iNum_Columns
        For $Row = 3 To 0 Step -1
            If $Column = 0 And $Row < 2 Then ContinueLoop
            If $Column = 2 Or $Column = 4 And $Row < 1 Then ContinueLoop
            $aControlArray[$Counter] = GUICtrlCreateIcon($aIcons[0], -1, ($Column * 70) + 30, ($Row * 55) + 25, 48, 48, -1, -1)
            $Counter += 1
        Next
    Next
    GUICtrlCreateLabel(" Hours ", 70, 250)
    GUICtrlSetFont(-1, 8.5, 600)
    GUICtrlCreateLabel(" Minutes ", 200, 250)
    GUICtrlSetFont(-1, 8.5, 600)
    GUICtrlCreateLabel(" Seconds ", 340, 250)
    GUICtrlSetFont(-1, 8.5, 600)
    GUICtrlCreateLabel(" ms ", 530, 250)
    GUICtrlSetFont(-1, 8.5, 600)
    GUICtrlCreateLabel("8", 10, 35)
    GUICtrlSetFont(-1, 8.5, 600)
    GUICtrlCreateLabel("4", 10, 90)
    GUICtrlSetFont(-1, 8.5, 600)
    GUICtrlCreateLabel("2", 10, 150)
    GUICtrlSetFont(-1, 8.5, 600)
    GUICtrlCreateLabel("1", 10, 210)
    GUICtrlSetFont(-1, 8.5, 600)
    GUISetState()
    While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
        _SetHour(@HOUR, $aControlArray)
        _Set10sMin(@MIN, $aControlArray)
        _Set10sSec(@SEC, $aControlArray)
        If $bShowMS Then _Set100sMSec(@MSEC, $aControlArray)
    WEnd
EndFunc   ;==>Main
Func _SetHour($Hour, $aArray)
    Local Static $LastHour
    If $Hour = $LastHour Then Return
    $LastHour = $Hour
    Local $sBCD = _Dec_To_Binary(Int($Hour / 10), 2) & _Dec_To_Binary(StringRight($Hour, 1), 4)
    GUICtrlSetImage($aArray[0], $aIcons[StringMid($sBCD, 2, 1)])
    GUICtrlSetImage($aArray[1], $aIcons[StringMid($sBCD, 1, 1)])
    GUICtrlSetImage($aArray[2], $aIcons[StringMid($sBCD, 6, 1)])
    GUICtrlSetImage($aArray[3], $aIcons[StringMid($sBCD, 5, 1)])
    GUICtrlSetImage($aArray[4], $aIcons[StringMid($sBCD, 4, 1)])
    GUICtrlSetImage($aArray[5], $aIcons[StringMid($sBCD, 3, 1)])
EndFunc   ;==>_SetHour
Func _Set10sMin($iMin, $aArray)
    Local Static $LastMin
    If $iMin = $LastMin Then Return
    $LastMin = $iMin
    Local $sBCD = _Dec_To_Binary(Int($iMin / 10), 3) & _Dec_To_Binary(StringRight($iMin, 1), 4)
    GUICtrlSetImage($aArray[6], $aIcons[StringMid($sBCD, 3, 1)])
    GUICtrlSetImage($aArray[7], $aIcons[StringMid($sBCD, 2, 1)])
    GUICtrlSetImage($aArray[8], $aIcons[StringMid($sBCD, 1, 1)])
    GUICtrlSetImage($aArray[9], $aIcons[StringMid($sBCD, 7, 1)])
    GUICtrlSetImage($aArray[10], $aIcons[StringMid($sBCD, 6, 1)])
    GUICtrlSetImage($aArray[11], $aIcons[StringMid($sBCD, 5, 1)])
    GUICtrlSetImage($aArray[12], $aIcons[StringMid($sBCD, 4, 1)])
EndFunc   ;==>_Set10sMin
Func _Set10sSec($iSec, $aArray)
    Local Static $LastSec
    If $iSec = $LastSec Then Return
    $LastSec = $iSec
    Local $sBCD = _Dec_To_Binary(Int($iSec / 10), 3) & _Dec_To_Binary(StringRight($iSec, 1), 4)
    GUICtrlSetImage($aArray[13], $aIcons[StringMid($sBCD, 3, 1)])
    GUICtrlSetImage($aArray[14], $aIcons[StringMid($sBCD, 2, 1)])
    GUICtrlSetImage($aArray[15], $aIcons[StringMid($sBCD, 1, 1)])
    GUICtrlSetImage($aArray[16], $aIcons[StringMid($sBCD, 7, 1)])
    GUICtrlSetImage($aArray[17], $aIcons[StringMid($sBCD, 6, 1)])
    GUICtrlSetImage($aArray[18], $aIcons[StringMid($sBCD, 5, 1)])
    GUICtrlSetImage($aArray[19], $aIcons[StringMid($sBCD, 4, 1)])
EndFunc   ;==>_Set10sSec

Func _Set100sMSec($iMSec, $aArray)
    Local Static $LastMSec
    If $iMSec = $LastMSec Then Return
    $LastMSec = $iMSec
    Local $iMilli = StringFormat('%03i', $iMSec)
    Local $sBCD = _Dec_To_Binary(StringMid($iMilli, 1, 1), 4) & _Dec_To_Binary(StringMid($iMilli, 2, 1), 4) & _Dec_To_Binary(StringMid($iMilli, 3, 1), 4)
    GUICtrlSetImage($aArray[20], $aIcons[StringMid($sBCD, 4, 1)])
    GUICtrlSetImage($aArray[21], $aIcons[StringMid($sBCD, 3, 1)])
    GUICtrlSetImage($aArray[22], $aIcons[StringMid($sBCD, 2, 1)])
    GUICtrlSetImage($aArray[23], $aIcons[StringMid($sBCD, 1, 1)])
    GUICtrlSetImage($aArray[24], $aIcons[StringMid($sBCD, 8, 1)])
    GUICtrlSetImage($aArray[25], $aIcons[StringMid($sBCD, 7, 1)])
    GUICtrlSetImage($aArray[26], $aIcons[StringMid($sBCD, 6, 1)])
    GUICtrlSetImage($aArray[27], $aIcons[StringMid($sBCD, 5, 1)])
    GUICtrlSetImage($aArray[28], $aIcons[StringMid($sBCD, 12, 1)])
    GUICtrlSetImage($aArray[29], $aIcons[StringMid($sBCD, 11, 1)])
    GUICtrlSetImage($aArray[30], $aIcons[StringMid($sBCD, 10, 1)])
    GUICtrlSetImage($aArray[31], $aIcons[StringMid($sBCD, 9, 1)])
EndFunc   ;==>_Set100sMSec

Func _Dec_To_Binary($iNumber, $iBits) ; from decimal to binary
    ; http://www.autoitscript.com/forum/topic/90056-decimal-to-binary-number-converter/?p=647505
    Local $sBinString = ""
    While $iNumber
        $sBinString = BitAND($iNumber, 1) & $sBinString
        $iNumber = BitShift($iNumber, 1)
    WEnd
    Return StringFormat('%0' & $iBits & 'i', $sBinString)
EndFunc   ;==>_Dec_To_Binary

 

hope that some others funny clocks will appear on the forum

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

I fixed the issue with the original script to correct the bug with the number 7 in the seconds.

Here is a shorter way to turn on/off bits (Rearranging the GUICtrlCreateIcon positions on the GUI and within the array it can be shorten even much more)

 Nice update to the display method. Not as easy to read but definitely a good approach I wouldn't have thought of. :)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...