Binaryish Clock

User projects written in or related to FreeBASIC.
rdc
Posts: 1725
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Binaryish Clock

Postby rdc » Aug 16, 2006 21:46

I took a break from the book and was playing around with my Glyph32 program.

Code: Select all

Option Explicit

#include once "vbcompat.bi"

Type gl32type
    pixel As Integer
    clr As Integer
End Type

Sub PrintGlyph(obj() As gl32type, grow As Integer, gcol As Integer)
    Dim As Integer i, j, row, col, scrw
    Dim framebuffer As Integer Ptr

    row = (grow - 1) * (Ubound(obj, 1) + 1)
    col = (gcol - 1) * (Ubound(obj, 2) + 1)


    framebuffer = Screenptr
    If framebuffer Then
        Screeninfo scrw
        Screenlock
        For i = Lbound(obj, 1) To Ubound(obj, 1)
            For j = Lbound(obj, 2) To Ubound(obj, 2)
                If obj(i, j).pixel = 1 Then
                    Poke Integer, framebuffer + ((row + i) * scrw) + (col + j), obj(i, j).clr
                End If
            Next
        Next
        Screenunlock
    End If
End Sub

Dim As gl32type ledred(15, 15)  => { _
{(0,0),(0,0),(0,0),(0,0),(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)}, _
{(0,0),(0,0),(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0),(0,0),(0,0)}, _
{(0,0),(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,16711680),(1,16711680),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0),(0,0)}, _
{(0,0),(1,8421481),(1,8421481),(1,8421481),(1,12632256),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0)}, _
{(0,0),(1,8421481),(1,8421481),(1,12632256),(1,12632256),(1,12632256),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0)}, _
{(1,8421481),(1,8421481),(1,8421481),(1,16711680),(1,12632256),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(0,0)}, _
{(1,8421481),(1,8421481),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,8421481),(1,8421481),(1,8421481),(0,0)}, _
{(1,8421481),(1,8421481),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,8421481),(1,8421481),(1,8421481),(0,0)}, _
{(1,8421481),(1,8421481),(1,8421481),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(0,0)}, _
{(1,8421481),(1,8421481),(1,8421481),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(0,0)}, _
{(0,0),(1,8421481),(1,8421481),(1,8421481),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,16711680),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0)}, _
{(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,16711680),(1,16711680),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(0,0),(0,0)}, _
{(0,0),(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(0,0),(0,0),(0,0)}, _
{(0,0),(0,0),(0,0),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(1,8421481),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(0,0),(0,0),(0,0),(0,0)}, _
{(0,0),(0,0),(0,0),(0,0),(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)}, _
{(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)} _
}

Dim As gl32type ledblack(15, 15)  => { _
{(0,0),(0,0),(0,0),(0,0),(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)}, _
{(0,0),(0,0),(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0),(0,0),(0,0)}, _
{(0,0),(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0),(0,0)}, _
{(0,0),(1,8421481),(1,8421481),(1,8421481),(1,12632256),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0)}, _
{(0,0),(1,8421481),(1,8421481),(1,12632256),(1,12632256),(1,12632256),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0)}, _
{(1,8421481),(1,8421481),(1,8421481),(1,6579300),(1,12632256),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(0,0)}, _
{(1,8421481),(1,8421481),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(1,8421481),(0,0)}, _
{(1,8421481),(1,8421481),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(1,8421481),(0,0)}, _
{(1,8421481),(1,8421481),(1,8421481),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(0,0)}, _
{(1,8421481),(1,8421481),(1,8421481),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(0,0)}, _
{(0,0),(1,8421481),(1,8421481),(1,8421481),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0)}, _
{(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(0,0),(0,0)}, _
{(0,0),(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(0,0),(0,0),(0,0)}, _
{(0,0),(0,0),(0,0),(1,8421481),(1,8421481),(1,6579300),(1,8421481),(1,8421481),(1,6579300),(1,6579300),(1,8421481),(1,8421481),(0,0),(0,0),(0,0),(0,0)}, _
{(0,0),(0,0),(0,0),(0,0),(0,0),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(1,8421481),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)}, _
{(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)} _
}

Dim As Double ctime
Dim As String bhour, bmin, bsec, ttime
Dim As Integer i, row, col

Screen 14, 32
Windowtitle "Tick Tock, Binary Clock"
Do
    ctime = Now
    'Get the binary time values
    bhour = Bin(Hour(ctime))
    If Len(bhour) < 6 Then
        bhour = String(6 - Len(bhour), "0") & bhour
    End If
    bmin = Bin(Minute(ctime))
    If Len(bmin) < 6 Then
        bmin = String(6 - Len(bmin), "0") & bmin
    End If
    bsec = Bin(Second(ctime))
    If Len(bsec) < 6 Then
        bsec = String(6 - Len(bsec), "0") & bsec
    End If
    'Get the formatted time
    ttime = Format(ctime, "hh:mm:ss AM/PM") & " (" & Format(ctime, "hh:mm:ss") & ")"
    Locate 2, 20 - Len(ttime) / 2
    Print ttime
    'Print the binary digits
    row = 5
    col = 8
    For i = 1 To Len(bhour)
        If Mid(bhour, i, 1) = "1" Then
            PrintGlyph ledred(), row, col
        Else
            PrintGlyph ledblack(), row, col
        End If
        col += 1
    Next
    row += 1
    col = 8
    For i = 1 To Len(bmin)
        If Mid(bmin, i, 1) = "1" Then
            PrintGlyph ledred(), row, col
        Else
            PrintGlyph ledblack(), row, col
        End If
        col += 1
    Next
    row += 1
    col = 8
    For i = 1 To Len(bsec)
        If Mid(bsec, i, 1) = "1" Then
            PrintGlyph ledred(), row, col
        Else
            PrintGlyph ledblack(), row, col
        End If
        col += 1
    Next
    Sleep 1
Loop Until Inkey <> ""   


Image
hartnell
Posts: 170
Joined: Aug 15, 2006 3:21
Contact:

Postby hartnell » Aug 16, 2006 21:58

Neato. You have inspired me :) -hartnell
RayBritton
Posts: 306
Joined: Jun 02, 2005 7:11
Contact:

Postby RayBritton » Aug 17, 2006 12:46

i also made a while back now, anyway for those who like ascii: an ascii binary clock
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Postby MystikShadows » Aug 17, 2006 14:08

awesome clocks guys....ray, happen to have to code to the ascii version?
E.K.Virtanen
Posts: 785
Joined: May 28, 2005 9:19
Location: Finland

Postby E.K.Virtanen » Aug 17, 2006 14:09

RayBritton wrote:i also made a while back now, anyway for those who like ascii: an ascii binary clock


I love that too :D
RayBritton
Posts: 306
Joined: Jun 02, 2005 7:11
Contact:

Postby RayBritton » Aug 17, 2006 18:22

i do, but its sooooooo bad i didn't want to show it to anyone, buthere you go
E.K.Virtanen
Posts: 785
Joined: May 28, 2005 9:19
Location: Finland

Postby E.K.Virtanen » Aug 17, 2006 18:31

RayBritton wrote:i do, but its sooooooo bad i didn't want to show it to anyone, buthere you go


Blah, its BASIC, and after i show some of my codes, you know what is BAD :D
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Postby MystikShadows » Aug 17, 2006 18:36

hehe, code is not "that" bad by the way....but hey, it works, that's all that counts....you should have seen some of the code I've had to work with in my career....makes your code look no less than divine. ;-) hehe
blahboybang
Posts: 385
Joined: Oct 16, 2005 0:15
Location: USA
Contact:

Postby blahboybang » Aug 18, 2006 2:16

Awesome! It works on linux, too! What would be more awesome is if you could make it a gnome panel applet!
NegativePlazma
Posts: 4
Joined: Aug 10, 2006 2:36

Postby NegativePlazma » Aug 18, 2006 2:20

Wow, this kicks the crap out of my first little app. Whats even cooler is that if done in a different language he probably would have TONS more lines of code.. amazing.


Nice work dude.
coderJeff
Site Admin
Posts: 3342
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Postby coderJeff » Aug 18, 2006 2:57

Slightly different approach using Draw String and a custom font.

Code: Select all

option explicit
#include "vbcompat.bi"

Screen 14, 32

dim led as string = _
chr(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) + _
chr(0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0) + _
chr(0,0,0,1,1,2,2,2,2,2,1,1,0,0,0,0) + _
chr(0,0,1,2,2,2,3,2,3,2,2,2,1,0,0,0) + _
chr(0,0,1,2,2,4,4,4,2,3,2,2,1,0,0,0) + _
chr(0,1,2,2,4,4,4,2,2,2,3,2,2,1,0,0) + _
chr(0,1,2,3,4,4,2,2,2,2,2,3,2,1,0,0) + _
chr(0,1,2,2,4,2,2,2,2,2,3,2,2,1,0,0) + _
chr(0,1,2,3,2,2,2,2,2,2,2,3,2,1,0,0) + _
chr(0,1,2,2,3,2,2,2,2,2,3,2,2,1,0,0) + _
chr(0,0,1,2,2,3,2,3,2,3,2,2,1,0,0,0) + _
chr(0,0,1,2,2,2,3,2,3,2,2,2,1,0,0,0) + _
chr(0,0,0,1,1,2,2,2,2,2,1,1,0,0,0,0) + _
chr(0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0) + _
chr(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) + _
chr(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

dim pal( 0 to 4, 0 to 1 ) as uinteger = { _
  {rgb( 255,  0,255 ), rgb( 255,  0,255 )}, _
  {rgb( 127,127,127 ), rgb( 127,127,127 )}, _
  {rgb(   0,  0,  0 ), rgb( 255,  0,  0 )}, _
  {rgb(  63, 63, 63 ), rgb( 127,  0,  0 )}, _
  {rgb( 255,255,255 ), rgb( 255,255,255 )} }

dim fnt as any ptr = imagecreate( 32, 17, 0 )
dim p as ubyte ptr = fnt + 4
dim as integer i,j,k
dim as double t1

'' set font header
p[0] = 0  '' version = 0
p[1] = 48 '' first char = "0"
p[2] = 49 '' last char = "1"
p[3] = 16 '' width of "0"
p[4] = 16 '' width of "1"

'' load led images in to custom font
for k = 0 to 1
  for i = 0 to 15
    for j = 0 to 15
      pset fnt,( i + k * 16, j + 1), pal( led[ i * 16 + j ], k )
    next j
  next i
next k

t1 = timer
while multikey(1) = 0

  dim as double d = now()

  locate 4, 10
  print format( d, "yyyy-mm-dd hh:mm:ss" )

  Draw String (7*16,3*16), bin(year(d) - 2000,6), ,fnt
  Draw String (9*16,4*16), bin(month(d),4), ,fnt
  Draw String (8*16,5*16), bin(day(d),5), ,fnt
  Draw String (8*16,7*16), bin(hour(d),5), ,fnt
  Draw String (7*16,8*16), bin(minute(d),6), ,fnt
  Draw String (7*16,9*16), bin(second(d),6), ,fnt

  '' Just for fun - 100ths since started
  Draw String (0, 12*16), bin(cuint((timer-t1)*100), 20), , fnt

  Sleep 25

wend

ImageDestroy fnt
rdc
Posts: 1725
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Postby rdc » Aug 18, 2006 3:37

That is damn cool Jeff. :)
coderJeff
Site Admin
Posts: 3342
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Postby coderJeff » Aug 18, 2006 4:05

Thanks, draw string is neat statement that I haven't seen used too much since it was added in ver 0.16. I would have practically killed for it doing presentation graphics in qb years ago. The custom font images can be bsave'd and bload'ed also.

Your glyph32 program looks good. Maybe could also be a font editor for draw string also? Just an idea ... for the next time you need a recreational break from book writing, of course :)
rdc
Posts: 1725
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Postby rdc » Aug 18, 2006 12:27

Yeah, that is a good idea.

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 3 guests