Post by ukdave74 on Mar 13, 2024 12:50:10 GMT
I wanted a 7 segment display that didn't rely on pre-rendered images.
*Updated* Had a code tidy session and renamed variables to be meaningful
*Updated* Had a code tidy session and renamed variables to be meaningful
' 16X16 Pixel 7 Segment Display
'
' Created by: Dave Mayes
' Date: 2024-03-13
'
' Feel free to use and adapt.
Declare Sub DrawSeg (SSD_X as long, SSD_Y as long, _
SSD_Value as long,SSD_Digits as long, _
SSD_ForeGround as long, SSD_BackGround as long, _
SSD_ZeroFlag as long)
' SSD_X X position in pixels
' SSD_Y Y Position in pixels
' SSD_Value Integer value to display
' SSD_Digits Number of digits or 0 to show as many and needed
' SSD_ForeGround Colour for lit segments
' SSD_BackGround Colour for unlit segments
' SSD_ZeroFlag Leading zeros flag: 0 = unlit, non zero value for lit
' Create array to hold segment bit data
Dim Shared SSD_Byte%%(11)
' Segment data is represented as the decimal of a 7 bit number
' 1
' --
' 32 | | 2
' |64|
' --
' 16 | | 4
' | |
' --
' 8
' 0 1 2 3 4 5 6 7 8 9 0 -
Data 63,6,91,79,102,109,125,7,127,103,0,64
' Populate the array
For a = 0 To 11
Read SSD_Byte%%(a)
Next a
'start of the demo
Screen _NewImage(800, 600, 32)
_Title "7 Segment Display"
'Set demo colours
BG& = _RGB(50, 50, 50) ' Segment Off
FG1& = _RGB(250, 50, 50) ' Segment On
FG2& = _RGB(250, 250, 250) ' Segment On
FG3& = _RGB(50, 250, 50) ' Segemnt On
' Demo random numbers until a key is pressed
Do Until InKey$ <> ""
_Limit 2
Cls
' 8 digit fixed length leading zeros unlit
DrawSeg 160, 150, Int(Rnd * 999999) - 500000, 8, FG1&, BG&, 1
' 6 digit fixed length leading zeros lit
FG2& = _RGB(250, 250 - (Count& And 15) * 8, 250) ' Segment On
DrawSeg 160, 200, Count&, 6, FG2&, BG&, 0
' random length
DrawSeg 160, 250, Int(Rnd * 99999) - 50000, 0, FG3&, GB&, 0
Count& = Count& + 1
Loop
End
' Draw Number X pos, Y pos, Value, Digits, Lit Colour, Unlit Colour, Leading zeros lit
Sub DrawSeg (SSD_X, SSD_Y, SSD_Value, SSD_Digits, SSD_ForeGround, SSD_BackGround, SSD_ZeroFlag)
' print SSD_Value
Dim SSD_Colour(1) As Long, SSD_Bits As _Byte
SSD_Colour(0) = SSD_BackGround
SSD_Colour(1) = SSD_ForeGround
' Random Length
If SSD_Digits < 1 Then
' Use full string for negative numbers, or trim leading space for >= 0
If SSD_Value < 0 Then
SSD_StringValue$ = Str$(SSD_Value)
' print SSD_StringValue$, SSD_Digits
Else
SSD_StringValue$ = Mid$(Str$(SSD_Value), 2)
End If
SSD_Digits = Len(SSD_StringValue$)
' print SSD_StringValue$, SSD_Digits
Else
' Fixed length convert to padded string
SSD_StringValue$ = Right$(Space$(SSD_Digits) + Mid$(Str$(SSD_Value), 2), SSD_Digits)
' Handle negative number
If SSD_Value < 0 Then SSD_StringValue$ = "-" + Mid$(SSD_StringValue$, 2)
' print SSD_StringValue$, SSD_Digits
End If
' print SSD_StringValue$
' Loop through each digit
For SSD_Loop = 1 To SSD_Digits
' Get current digit
SSD_Digit$ = Mid$(SSD_StringValue$, SSD_Loop, 1)
' Turn leading spaces into 0's if flag set
If (SSD_ZeroFlag <> 0) And (SSD_Digit$ = " ") Then SSD_Digit$ = "10"
' Handle negative
If SSD_Digit$ = "-" Then SSD_Digit$ = "11"
SSD_Bits = Val(SSD_Digit$)
' print SSD_Bits, SSD_Byte%%(SSD_Bits) And 2 ' Debug segment
' Top Bar
Line (SSD_X + 3, SSD_Y + 1)-Step(9, 0), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 1))
Line (SSD_X + 4, SSD_Y + 2)-Step(7, 0), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 1))
' Top Right
Line (SSD_X + 14, SSD_Y + 2)-Step(0, 5), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 2))
Line (SSD_X + 13, SSD_Y + 3)-Step(0, 3), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 2))
' Bottom Right
Line (SSD_X + 14, SSD_Y + 9)-Step(0, 5), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 4))
Line (SSD_X + 13, SSD_Y + 10)-Step(0, 3), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 4))
' Bottom Bar
Line (SSD_X + 3, SSD_Y + 15)-Step(9, 0), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 8))
Line (SSD_X + 4, SSD_Y + 14)-Step(7, 0), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 8))
' Bottom Left
Line (SSD_X + 1, SSD_Y + 9)-Step(0, 5), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 16))
Line (SSD_X + 2, SSD_Y + 10)-Step(0, 3), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 16))
' Top Left
Line (SSD_X + 1, SSD_Y + 2)-Step(0, 5), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 32))
Line (SSD_X + 2, SSD_Y + 3)-Step(0, 3), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 32))
' Middle Bar
Line (SSD_X + 3, SSD_Y + 7)-Step(9, 0), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 64))
Line (SSD_X + 2, SSD_Y + 8)-Step(11, 0), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 64))
Line (SSD_X + 3, SSD_Y + 9)-Step(9, 0), SSD_Colour(Sgn(SSD_Byte%%(SSD_Bits) And 64))
' Next Digit
SSD_X = SSD_X + 16
Next SSD_Loop
End Sub