just finished my latest project ... a BIN clock ...
It just happens to coincide with the currently ongoing community discussion on BOOLEAN Type in FB (by coderJeff).
Reference: http://www.freebasic.net/forum/viewtopi ... 6&start=15
Some Long's in the code are 'misused' as BOOLEANS, see comments in code ...
BTW: our Server: r2-dallas went down whilst I've been writing this post! (obviously it's back up again ...)
Thanks to the Support People at Site5 ...
BIN-Clock.bas:
Code: Select all
' =================================================================== '
' BINARY Clock (incl. Digital & Help) 'MrSwiss made'™ Version is:
' newly coded, commented and tested: (details below)
' ------------------------------------------------------------------- '
' Copyright © 2015, MrSwiss
' ALL RIGHTS RESERVED! NO COMMERCIAL/EDUCATIONAL USE WHATSOEVER! THIS
' INCLUDES ANY FORM OF DISTRIBUTION IN ANY FORM AND BY ANY MEANS!
' NO WARRANTIES AT ALL ANYWHWERE IN THE WORLD! THE USER TAKES ANY AND
' ALL RISKS! THE AUTHOR DISCLAIMS ANY RESPONSIBILITY FOR ANY TYPE OF
' DAMAGE(S) CAUSED BY THIS CODE! IF IN DOUBT, DON'T USE IT AT ALL!
' FREE FOR PERSONAL, NONPROFIT USE ONLY. GOVERNMENTS: NOT PERMITTED!
' -------------------------------------------------------------------- '
' PLAIN TEXT: Don't LIKE it, don't USE it! It's as simple as that.
' =================================================================== '
' TESTED: with FreeBASIC Compiler Ver. 1.03.0, 32bit and 64bit (WIN)
' COMPILE WITH: -s gui (compiler switch), see manual for details
' COMPILER LANGUAGE: "fb" (only, no support for deprecated options!)
' ------------------------------------------------------------------- '
' CREDITS GO TO: fxm (explanations of calling Bload etc.)
' FB-Forum Topic: Programming/General/Freebasic 1.03 problems ...
' http://www.freebasic.net/forum/viewtopic.php?f=3&t=23728#p209888
' =================================================================== '
' define screen properties
ScreenRes 400, 200, 32 ' screen setup (32 bit depth, single buffer)
' declarations used by local procedures
Dim As Long PX(5), PY(2), bi(5) ' C-style arrays: PX(0 To 5), PY(0 to 2), bi(0 To 5)
' bi() an array of 'Long' forced to behave as 'BOOLEAN', see ConvToBIN & PaintTime Sub's
Dim As Long hh, mm, ss, ret ' var's for time & returned (error) codes (ret should be BOOLEAN too)
Dim Shared As UByte r=0, g=0, b=0, a=255' default color (black), opaque (can be used everywhere)
Dim As String TStr ' time string
' declares ... general init stuff
Dim As Any Ptr dclock = ImageCreate(300, 50, , 32) ' create 'clock face' buffer
ret = Bload (ExePath & "\DigiIF.bmp", dclock)' fill the buffer with the .BMP Graphic (from file)
If ret Then ' file NOT found, inform user and quit ...
' remember: background, at this point in time is still black ... default
Draw String (20, 80), "'DigiIF.bmp' NOT found, aborting ...", rgb(255,127,0) ' orange
Draw String (20,100), "Press any key to exit program!", rgb(255,127,0) ' orange
ImageDestroy dclock ' destroy image buffer
Sleep : End 1 ' quit with errorcode 1 (for batch processing)
EndIf
Color rgba(r,g,b,a), rgba(255,255,255,a)' black on white BG
' initialize the arrays PX, PY (without typing too much), defines _
' the Paint 'Axis-Points' (x, y) used later by 'PaintTime' Sub
For i As Long = 0 To 5
PX(i) = 105 + i*50 ' the 105 is the x-starting point (80+25) _
Next ' + i*50 is the increment by 50
For i As Long = 0 To 2
PY(i) = 50 + i*50 ' the 50 is the y-starting point (25+25) _
Next ' + i*50 is the increment by 50
' ----- local procedures ----- '
Sub GetTime(ByRef hh As Long, ByRef mm As Long, ByRef ss As Long, ByRef TStr As String)
TStr = Time ' get time as string "hh:mm:ss", system time
hh = ValInt(Left(TStr, 2)) ' ValInt returns a Long = Int<32>
mm = ValInt(Mid(TStr, 4, 2)) ' ATTN.: ValLng returns a LongInt = Int<64>
ss = ValInt(Right(TStr, 2))
End Sub
Sub ConvToBIN(ByVal Num As Long, bi() As Long)
' reset array to 0 (all positions) to have a defined start ...
For i As Long = 0 To 5 : bi(i) = 0 : Next ' single line 'For ... Next' loop
' we now only have to set the 'proper' bits (no error checks)
If Num >= 32 Then ' max. is 59 Min/Sec (max. 6 bits)
bi(0) = 1 : Num mod= 32 ' set bi() and get/set the 'remainder'
EndIf
If Num >= 16 Then bi(1) = 1 : Num mod= 16 : EndIf ' single line version (of above)
If Num >= 8 Then bi(2) = 1 : Num mod= 8 : EndIf ' if we had BOOLEANS: bi(n) = TRUE
If Num >= 4 Then bi(3) = 1 : Num mod= 4 : EndIf
If Num >= 2 Then bi(4) = 1 : Num mod= 2 : EndIf
If Num = 1 Then bi(5) = 1
End Sub
Sub PaintTime(ByVal idx As Long, PX() As Long, PY() As Long, bi() As Long)
For j As Long = 0 To 5 ' we have to always paint 6 fields (hh, mm, ss)
Select Case As Const idx ' hh: idx = 0, mm: idx = 1, ss: idx = 2 _
Case 0 ' there is, with every call, always only a 1/3 of the code run
Select Case As Const j ' every bi() instance once (0 --> 5), MS-Bit first ...
Case 0 ' this one is special to hh only (5 bits do the trick), 23 = max.
Paint (PX(j), PY(idx)), rgba(255,223,0,a), rgba(r,g,b,a) ' dark yellow
Draw String (PX(j)-12, PY(idx)-8), "NOT", rgba(r,g,b,a) ' default (black)
Draw String (PX(j)-16, PY(idx)+1), "used", rgba(r,g,b,a)
Case 1
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a) ' red (active)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a) ' dark grey (inactive)
EndIf
Case 2
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 3
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 4
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 5
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
End Select
Case 1
Select Case As Const j
Case 0
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 1
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 2
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 3
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 4
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 5
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
End Select
Case 2
Select Case As Const j
Case 0
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 1
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 2
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 3
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 4
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
Case 5
If bi(j) Then
Paint (PX(j), PY(idx)), rgba(255,0,0,a), rgba(r,g,b,a)
Else
Paint (PX(j), PY(idx)), rgba(63,63,63,127), rgba(r,g,b,a)
EndIf
End Select
End Select
Next
End Sub
' ----- end local procedures ----- '
' ===== MAIN ===== '
Dim As String Title = "Binary - Clock, MrSwiss made(TM)" ' define 'local var's' to main
Dim As String Help = "0/32 0/16 0/8 0/4 0/2 0/1" ' show binary 'weight' of digit
WindowTitle "BINARY - Clock" ' instead of "BIN-Clock", the .EXE name ...
Do
If Time = TStr Then Goto wait4TimeChange ' sleep until second increments
CLS ' clear screen buffer
GetTime(hh, mm, ss, TStr) ' once a second: get new values 24h, aka 'system time'
ScreenLock
'Paint (0, 0), rgba(255,255,191,127) ' paint BG bright yellow, to 'see' the loaded graphics
Put (80, 25), dclock, Pset ' graphic for the hour(s)
Put (80, 75), dclock, Pset ' graphic for the minute(s)
Put (80,125), dclock, Pset ' graphic for the second(s)
For i As Long = 0 To 2 ' the outlines/contours filling routine ...
Select Case As Const i
Case 0
ConvToBIN(hh, bi()) ' get BINARY for hours: hh (reset, then fill bi() array)
PaintTime(i, PX(), PY(), bi()) ' 'Paint' the graphics accordingly (uses all three arrays)
Case 1
ConvToBIN(mm, bi())
PaintTime(i, PX(), PY(), bi())
Case 2
ConvToBIN(ss, bi())
PaintTime(i, PX(), PY(), bi())
End Select
Next
' dynamically centered string on the .BMP (80+150 pixels = center of graphic [300 x 50 px])
Draw String (230-((Len(Title)+1)\2)*8, 13), Title, rgba(0,63,191,a) ' blue color
Draw String (10, 45), "Hour..: ", rgba(r,g,b,a) ' default color, absolute positioning
Draw String (90, 72), Help, rgba(r,g,b,a) ' display calculation help (bit-weight)
Draw String (10, 95), "Minute: ", rgba(r,g,b,a)
Draw String (90, 122), Help, rgba(r,g,b,a)
Draw String (10, 145), "Second: ", rgba(r,g,b,a)
Draw String (230-((Len(TStr)+1)\2)*8, 180), TStr, rgba(0,63,191,a) ' blue: Time-String, dyn. centered
ScreenUnLock
wait4TimeChange:
Sleep 200, 1
Loop Until InKey = Chr(255)+"k" ' top/right X clicked ... quit prog.
ImageDestroy dclock ' free allocated ressources (image buffer)
End 0 ' End closes prog. [errorcode=0], NO error
Focus on 'Job at hand' = YES, ABSOLUTELY (and only that, see above)
[Obsolete]I still don't know how to properly supply the needed *.bmp file, because my available Host is always asking for PWD?
Any suggestions on permissions etc.???[/Obsolete]
[Edit]try this LINK to my OneDrive shared stuff:
http://1drv.ms/1IqOzKA
hope it works ... you should be able to download right away[/Edit]