7-Segment LED Clock (using SevenSegment static lib)

Source-code only - please, don't post questions here.
MrSwiss
Posts: 2469
Joined: Jun 02, 2013 9:27
Location: Switzerland

7-Segment LED Clock (using SevenSegment static lib)

Postby MrSwiss » Feb 10, 2018 17:37

A Clock sporting a 7-Segment LED (graphical) display (pure FB code, for both: 32/64-bit's).

First: the needed lib (bi, bas -- source(s)), are found in Forum --> Projects.

SevenSegmentClock.bas:

Code: Select all

' SevenSegmentClock.bas -- 2017-08-19, by MrSwiss
'
' compile: -s gui

#Include "SevenSegment.bi"

' decimal splitter macro (range: 0 to 99), tens/units
#Define T_U(num)    ( CUShort(((num \ 10) Shl 8) + num Mod 10) )

' init code: clock specific positioning (spaces in between HR/MIN/SEC)
Dim As SevenSeg SS(5)       ' 6 elements SevenSeg (0 to 5)
Dim As UShort   startx, starty, endx, endy, sp_sec, sp_min, sp_hou

For i As UInteger = 0 To 5
    SS(i).initSS()          ' sizes SevenSeg (default = 6)
    SS(i).clr_SS(b_red, d_red, black, blue) ' bright/dark LED, bg, frame
    Select Case As Const i  ' positioning depending on prev. set position
        Case 0 :        SS(i).pos_SS(30, 30)        ' first position only
        Case 1, 3, 5 :  SS(i).pos_SS(endx + 1, 30)  ' to position, border
        Case 2, 4 :     SS(i).pos_SS(endx + 21, 30) ' pos. + space = 20
    End Select
    SS(i).gframe( ,, endx)  ' get end pos. x-axis (+1 = next start pos.)
    If i = 0 Then SS(i).gframe(startx, starty)  ' store start pos. SS(0)
    If i = 5 Then SS(i).gframe( ,,, endy)   ' on SS(5), endpos. y-axis
Next    ' we now have start/end coordinates of the whole SevenSeg block!

' seconds in a different color (than hours/minutes)
SS(4).clr_SS(b_blu, d_blu) : SS(5).clr_SS(b_blu, d_blu)
' done (init)

' ===== MAIN =====
' using obtained sizes (from init), to size the screen (no need to know them!)
ScreenRes(endx + startx, endy + starty, 32, 2, &h60) ' Alpha Primitives + allways on top
ScreenSet(1, 0)
WindowTitle "SevenSegmentClock  --  " + _   ' display (hpefully) useful info's
            "width: " + Str(endx + startx) + "   " + _
            "height: " + Str(endy + starty)
Color(white, &hFF003F00) : Cls      ' custom green (background)

Dim As String   cTime = Time, TM = "MrSwiss made(TM)", _
                title = "Digital 24 hour's clock, using SevenSegment-Library"

Do
    If Time <> cTime Then   ' run's once a second only, (> fails: on date change!)
        Cls
        cTime = Time                ' update time (below: use of the defined macro!)
        sp_hou = T_U(CUByte(Left(cTime, 2)))    ' split: tens digit, units digit
        sp_min = T_U(CUByte(Mid(cTime, 4, 2)))  ' as above (minutes)
        sp_sec = T_U(CUByte(Right(cTime, 2)))   ' as above (seconds)
        If HiByte(sp_hou) = 0 Then          ' switch OFF, leading zero (on Hour's, only)
            SS(0).dec_SS(16)                ' all segments OFF (on a SevenSeg)
        Else
            SS(0).dec_SS(HiByte(sp_hou))    ' decode it (LED's: ON/OFF status)
        End If : SS(0).dis_SS()             ' show it, no frame: default
        SS(1).dec_SS(LoByte(sp_hou)) : SS(1).dis_SS()   ' decode digit : display digit
        SS(2).dec_SS(HiByte(sp_min)) : SS(2).dis_SS()   ' as above ...
        SS(3).dec_SS(LoByte(sp_min)) : SS(3).dis_SS()
        SS(4).dec_SS(HiByte(sp_sec)) : SS(4).dis_SS()
        SS(5).dec_SS(LoByte(sp_sec)) : SS(5).dis_SS()
        Draw String ( 23, 12), title        ' using default foreground color
        Draw String (165,135), TM, &hFFFFCF00   ' custom yellow
        Flip
    End If
    If Len(InKey()) Then Exit Do            ' check for user input
    Sleep(100, 1)                           ' wait for 100 mS (no user interrupt)
Loop
' ===== END-MAIN =====  ' ----- EOF -----

For a simple test (without compiling the library first), replace the line:
#Include "SevenSegment.bi" with: #Include "SevenSegment[nn].bas"
(nn = either 32 or 64), place the included .bas next to SevenSegmentClock.bas,
then compile SevenSegmentClock.bas only ...
srvaldez
Posts: 1532
Joined: Sep 25, 2005 21:54

Re: 7-Segment LED Clock (using SevenSegment static lib)

Postby srvaldez » Feb 11, 2018 2:01

very nice MrSwiss, runs ok on my Mac
dodicat
Posts: 4821
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: 7-Segment LED Clock (using SevenSegment static lib)

Postby dodicat » Feb 11, 2018 11:37

Very nice Mr Swiss.
Luckily, in this neck of the woods we are allowed to produce cheap and cheerful timepieces.
(We no longer have the reputation for producing quality clocks)

Code: Select all


Sub digits(t As String,x As long,y As long,clr As Ulong,sz As single,gap as long=1,img As Any Pointer=0)
        x=x-2*sz
        dim as single s=any,c=any
        dim as single  d =iif(gap, sz/10,0)
        #macro thickline(x1,y1,x2,y2)
             s=(y1-y2)/10
             c=(x2-x1)/10
            Line img,(x1-s,y1-c)-(x2+s,y2+c),clr,bf
        #endmacro
        #macro display(_a,_b,_c,_d,_e,_f,_g)
        x=x+2*sz
        If _a=1 Then :thickline(x+d,y,(x-d+sz),y):End If
        If _b=1 Then :thickline((x+sz),y+d,(x+sz),(y-d+sz)):End If
        If _c=1 Then :thickline((x+sz),(y+d+sz),(x+sz),(y-d+2*sz)):End If
        If _d=1 Then :thickline((x-d+sz),(y+2*sz),x+d,(y+2*sz)):End If ''
        If _e=1 Then :thickline(x,(y-d+2*sz),x,(y+d+sz)):End If
        If _f=1 Then :thickline(x,(y-d+sz),x,y+d):End If
        If _g=1 Then :thickline(x+d,(y+sz),(x-d+sz),(y+sz)):End If
        #endmacro
        For z As long=0 To Len(t)-1
            Select Case As Const t[z]
            Case 48 :display(1,1,1,1,1,1,0)             '"0"
            Case 49 :display(0,1,1,0,0,0,0)             '"1"
            Case 50 :display(1,1,0,1,1,0,1)             '"2"
            Case 51 :display(1,1,1,1,0,0,1)             '"3"
            Case 52 :display(0,1,1,0,0,1,1)             '"4"
            Case 53 :display(1,0,1,1,0,1,1)             '"5"
            Case 54 :display(1,0,1,1,1,1,1)             '"6"
            Case 55 :display(1,1,1,0,0,0,0)             '"7"
            Case 56 :display(1,1,1,1,1,1,1)             '"8"
            Case 57 :display(1,1,1,1,0,1,1)            '"9"
            Case 58                                     '":"                   
                Circle img,((x+2*sz),(y+sz/2)),(sz/5),clr,,,,f
                Circle img,((x+2*sz),(y+1.5*sz)),(sz/5),clr,,,,f
                x=x+sz
            Case 45 :display(0,0,0,0,0,0,1)              '"-"                       
            Case 46                                      '"."                       
                Circle img,((x+2*sz),(y+1.9*sz)),(sz/5),clr,,,,f
                x=x+sz
            Case 32                                      '" "
                x=x+sz
            End Select
        Next z
    End Sub 
   

sub miniclock(x as long,y as long,size as long,Maincol as ulong,ShadowCol as ulong,Backcol as ulong,i as any ptr=0)
     line(x-size,y-size/2)-(x+13*size+size,(y-size/2)+2*size+size),BackCol,bf
     dim as string t=time
    digits "88 88 88",x,y,ShadowCol,size
    digits t,x,y,MainCol,size
end sub

sub minidate(x as long,y as long,size as long,Maincol as ulong,ShadowCol as ulong,Backcol as ulong)
     line(x-size,y-size/2)-(x+19*size+size,(y-size/2)+2*size+size),BackCol,bf
     dim as string t=date
    digits "88  88  8888",x,y,ShadowCol,size,0
    digits t,x,y,MainCol,size,0
    end sub
'=====================

screen 19,32,,64
color , rgb(100,100,100)

do
    screenlock
    cls
   
minidate(50,10,10,rgb(200,100,0),rgba(200,100,0,50),rgb(50,50,50))
draw string(50,75),"Raw times:",rgb(0,0,0)
digits(time,50,100,rgb(0,200,0),25,0)
digits(time+":"+ mid( str(frac(timer)),3,1) ,50,170,rgb(100,50,25),15,0)
draw string(50,255),"Times with bulbs:",rgb(0,0,0)
miniclock(50,300,25,rgb(250,250,250),rgba(250,250,0,50),rgb(0,100,255))
miniclock(50,400,40,rgb(0,100,255),rgba(0,100,255,30),rgb(250,250,250))
draw string(50,530),"Raw timer:",rgb(0,0,0)
digits(str(timer),50,550,rgb(100,0,0),15,0)

     screenunlock
   sleep 1,1
loop until  len(inkey)

sleep
     

The date viewer may not be correct (different regional formats)
MrSwiss
Posts: 2469
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: 7-Segment LED Clock (using SevenSegment static lib)

Postby MrSwiss » Feb 11, 2018 17:46

@srvaldez, thanks for the feedback (always good to know, about other OS's).

@dodicat, cool way (to make it similarly) too ...
dodicat wrote:The date viewer may not be correct (different regional formats)
That is easily fixed:

Code: Select all

sub minidate(x as long,y as long,size as long,Maincol as ulong,ShadowCol as ulong,Backcol as ulong)
    Line(x-size,y-size/2)-(x+19*size+size,(y-size/2)+2*size+size),BackCol,bf
    Dim as string t=Date, dd=Mid(t,4,2), mm=Left(t,2), yyyy=Right(t,4)  ' for format change <--
    t=dd+"-"+mm+"-"+yyyy    ' european date format, MrSwiss <--
    digits "88  88  8888",x,y,ShadowCol,size,0
    digits t,x,y,MainCol,size,0
End sub

Return to “Tips and Tricks”

Who is online

Users browsing this forum: MrSwiss and 1 guest