'
'LED-DISP.BAS: Version 1.0
'Scott Pessoni - August 1995
'
'These are some subroutines that I wrote to simulate different
'kinds of LED Displays.  This is the first version so it's not filled
'with to meny fetures but they are handy at some time.  The Led
'Display does not handle negitive numbers or decimals.  You also have
'to watch out for LedBar so that the formula doesn't overflow with large
'numbers.  The Leds aren't very pritty yet but I'm working on digitizing
'some!  The Leds are handy for showing the status of something because
'all you have to do is change the the led state and not remember the
'X and Y locations!  Have fun and tell me what you think.  Look for
'version 2 some time.
'-----------------------------------------------------------------------
DECLARE SUB Leds (LedNumber%, Status%)
DECLARE SUB LedBar (Number%)
DECLARE SUB LedDisplay (Number%)
DEFINT A-Z
DIM SHARED DisplayLedX, DisplayLedY, LedDigits
DIM SHARED GraphLedX, GraphLedY, GraphElements, GraphNum

SCREEN 13

'This sets the default colors to use for the Bright/Dim led panals:
'Red Leds:
PALETTE 16, 65536 * 15 + 256 * 15 + 57
PALETTE 17, 65536 * 2 + 256 * 2 + 19
'Green Leds:
'PALETTE 16, 65536 * 15 + 256 * 57 + 15
'PALETTE 17, 65536 * 2 + 256 * 19 + 2
'----------------

'-------- Led Digit Display Setup -----------------
DisplayLedX = 0 '|- Upper Left corner of
DisplayLedY = 0 '|  Led Digit display
LedDigits = 4   'Number of digits to have on display
'--------------------------------------------------
'-------- Led Bar Graph Display Setup -------------
GraphLedX = 0  '|- Upper Left corner of
GraphLedY = 20 '|  Led Graph display
GraphElements = 32  'Number of graph elements. Maximum 32
GraphNum = 1000 'The number when the graph is 100%
'--------------------------------------------------
'--------- Led Lights Setup -----------------------
TYPE Led
     x AS INTEGER '|- Upper Left corner of LED
     y AS INTEGER '|
     s AS INTEGER 'Current Status of LED (-1=Led not used 0=Off 1= On)
END TYPE
DIM SHARED Led(5) AS Led

FOR Temp = 1 TO 5 'Make LEDs unused until you assign them
     Led(Temp).s = -1
NEXT Temp

Led(1).x = 40: Led(1).y = 5: Led(1).s = 0  '|- Make some LED's
Led(2).x = 60: Led(2).y = 5: Led(2).s = 0  '|
'--------------------------------------------------

'============ DEMO:
LedDisplay -1 '|- Any negitive number will Clear/Create display
LedBar -1     '|
Leds 0, 0     '   Draw all LED's at there set states

FOR Count = 0 TO 1000
     LedDisplay Count   'Update Led Digits with current number
     LedBar Count       'Update Led Bar graph with current number
     SubCount = SubCount + 1                          '|- Every 10 numbers
     IF SubCount = 10 THEN Leds 1, -1: SubCount = 0   '|  toggle the Led
     IF Count > 500 THEN Leds 2, 1                    'After 500 turn Led ON
NEXT Count

DO: LOOP UNTIL INKEY$ <> ""
END

'LedBar: A simulated Led Bargraph
'-----------------------------------------------
'LedBar Number
'    Number = The current number you want to update the bar graph with
'-----------------------------------------------
SUB LedBar (Number)

IF Number < 0 THEN 'If Negitive then blank Bar Graph
     FOR MakeGraph = 1 TO GraphElements * 2 STEP 2 'Make the Bar graph
          LINE (GraphLedX + MakeGraph, GraphLedY)-(GraphLedX + MakeGraph, GraphLedY + 5), 17
     NEXT MakeGraph
     EXIT SUB
END IF

Elements = INT(Number * GraphElements / GraphNum) 'Calculate Number Elements
IF Elements > GraphElements THEN Elements = GraphElements 'Check limts

'----------------- Draw Bar Graph --------------------------------
FOR MakeGraph = 1 TO Elements * 2 STEP 2 'Make the Bar graph (Lit)
LINE (GraphLedX + MakeGraph, GraphLedY)-(GraphLedX + MakeGraph, GraphLedY + 5), 16
NEXT MakeGraph
FOR MakeGraph = Elements * 2 + 1 TO GraphElements * 2 STEP 2 'Make the Bar graph (Dim
LINE (GraphLedX + MakeGraph, GraphLedY)-(GraphLedX + MakeGraph, GraphLedY + 5), 17
NEXT MakeGraph
'------------------------------------------------------------------
END SUB

'LedDisplay: Generates a simulated Digital Led Display.
'------------------------------------------------------------
'LedDisplay (Number)
'    Number = The number you want to display on the Digital Display
'------------------------------------------------------------
SUB LedDisplay (Number%)
IF Number < 0 THEN  'Setup Led Display panel
     FOR PlotX = DisplayLedX TO DisplayLedX + ((LedDigits - 1) * 8) STEP 8
          '----------- One LED Matrix digit --------------------
          LINE (PlotX + 1, DisplayLedY)-(PlotX + 5, DisplayLedY), 17
          LINE (PlotX, DisplayLedY + 1)-(PlotX, DisplayLedY + 5), 17
          LINE (PlotX + 6, DisplayLedY + 1)-(PlotX + 6, DisplayLedY + 5), 17
          LINE (PlotX + 1, DisplayLedY + 6)-(PlotX + 5, DisplayLedY + 6), 17
          LINE (PlotX, DisplayLedY + 7)-(PlotX, DisplayLedY + 11), 17
          LINE (PlotX + 6, DisplayLedY + 7)-(PlotX + 6, DisplayLedY + 11), 17
          LINE (PlotX + 1, DisplayLedY + 12)-(PlotX + 5, DisplayLedY + 12), 17
          '------------------------------------------------------
     NEXT PlotX
     EXIT SUB
END IF

Number = FIX(Number) 'Get rid of the decimals incase there are some
Number = VAL(LEFT$(STR$(Number), LedDigits + 1)) 'Chop Number to LED size

PlotX = DisplayLedX

IF LEN(STR$(Number)) - 1 < LedDigits THEN  'Clear Unused digits
     FOR ClearEmptyDigits = 1 TO LedDigits - (LEN(STR$(Number)) - 1)
     LINE (PlotX + 1, DisplayLedY)-(PlotX + 5, DisplayLedY), 17
     LINE (PlotX, DisplayLedY + 1)-(PlotX, DisplayLedY + 5), 17
     LINE (PlotX + 6, DisplayLedY + 1)-(PlotX + 6, DisplayLedY + 5), 17
     LINE (PlotX + 1, DisplayLedY + 6)-(PlotX + 5, DisplayLedY + 6), 17
     LINE (PlotX, DisplayLedY + 7)-(PlotX, DisplayLedY + 11), 17
     LINE (PlotX + 6, DisplayLedY + 7)-(PlotX + 6, DisplayLedY + 11), 17
     LINE (PlotX + 1, DisplayLedY + 12)-(PlotX + 5, DisplayLedY + 12), 17
     PlotX = PlotX + 8
     NEXT ClearEmptyDigits
END IF


FOR PlotDigit = 1 TO LEN(STR$(Number)) - 1 'Plot each number to a LED

WorkDigit$ = MID$(STR$(Number), PlotDigit + 1, 1) 'Get 1 Digit

SELECT CASE WorkDigit$ 'Find and select which elements to turn on
     CASE "0"
          E1 = 16: E2 = 16: E3 = 16: E4 = 17: E5 = 16: E6 = 16: E7 = 16
   
     CASE "1"
          E1 = 17: E2 = 17: E3 = 16: E4 = 17: E5 = 17: E6 = 17: E7 = 16
  
     CASE "2"
          E1 = 17: E2 = 16: E3 = 16: E4 = 16: E5 = 16: E6 = 16: E7 = 17
  
     CASE "3"
          E1 = 17: E2 = 16: E3 = 16: E4 = 16: E5 = 17: E6 = 16: E7 = 16
  
     CASE "4"
          E1 = 16: E2 = 17: E3 = 16: E4 = 16: E5 = 17: E6 = 17: E7 = 16
 
     CASE "5"
          E1 = 16: E2 = 16: E3 = 17: E4 = 16: E5 = 17: E6 = 16: E7 = 16
  
     CASE "6"
          E1 = 16: E2 = 17: E3 = 17: E4 = 16: E5 = 16: E6 = 16: E7 = 16
  
     CASE "7"
          E1 = 17: E2 = 16: E3 = 16: E4 = 17: E5 = 17: E6 = 17: E7 = 16
  
     CASE "8"
          E1 = 16: E2 = 16: E3 = 16: E4 = 16: E5 = 16: E6 = 16: E7 = 16
  
     CASE "9"
          E1 = 16: E2 = 16: E3 = 16: E4 = 16: E5 = 17: E6 = 17: E7 = 16
END SELECT
'Plot the LEDs to the screen------------------------
LINE (PlotX, DisplayLedY + 1)-(PlotX, DisplayLedY + 5), E1
LINE (PlotX + 1, DisplayLedY)-(PlotX + 5, DisplayLedY), E2
LINE (PlotX + 6, DisplayLedY + 1)-(PlotX + 6, DisplayLedY + 5), E3
LINE (PlotX + 1, DisplayLedY + 6)-(PlotX + 5, DisplayLedY + 6), E4
LINE (PlotX, DisplayLedY + 7)-(PlotX, DisplayLedY + 11), E5
LINE (PlotX + 1, DisplayLedY + 12)-(PlotX + 5, DisplayLedY + 12), E6
LINE (PlotX + 6, DisplayLedY + 7)-(PlotX + 6, DisplayLedY + 11), E7
'--------------------------------------------------
PlotX = PlotX + 8
NEXT PlotDigit
END SUB

'Leds: Updates Leds
'--------------------------------------------------------------------
'     Leds (LedNumber, Status)
'LedNumber = Led to change (0 to Setup/Update ALL LEDS)
'Status = -1 Flip/Flop State  0 Led Off  1 Led On
'---------------------------------------------------------------------
SUB Leds (LedNumber, Status)

IF LedNumber = 0 THEN  'Update ALL Led's
     FOR MakeLeds = 1 TO 5
          IF Led(MakeLeds).s = 0 THEN  'Make dim Led's
               LINE (Led(MakeLeds).x, Led(MakeLeds).y)-(Led(MakeLeds).x + 2, Led(MakeLeds).y + 2), 0, BF
               PSET (Led(MakeLeds).x + 1, Led(MakeLeds).y + 1), 17
          ELSEIF Led(MakeLeds).s = 1 THEN 'Make Lit Led's
               LINE (Led(MakeLeds).x, Led(MakeLeds).y)-(Led(MakeLeds).x + 2, Led(MakeLeds).y + 2), 17, BF
               PSET (Led(MakeLeds).x + 1, Led(MakeLeds).y + 1), 16
          END IF
     NEXT MakeLeds
     EXIT SUB
END IF

IF Status = -1 THEN 'Flip/Flop the state of the Led.
     IF Led(LedNumber).s = 1 THEN Led(LedNumber).s = 0 ELSE Led(LedNumber).s = 1
ELSE       'Assign Led's Status if not Flip/Flop
     Led(LedNumber).s = Status
END IF
        
'---- Update current status of the selected LED
IF Led(LedNumber).s = 0 THEN 'Display Led OFF
     LINE (Led(LedNumber).x, Led(LedNumber).y)-(Led(LedNumber).x + 2, Led(LedNumber).y + 2), 0, BF
     PSET (Led(LedNumber).x + 1, Led(LedNumber).y + 1), 17
ELSEIF Led(LedNumber).s = 1 THEN 'Display Led ON
     LINE (Led(LedNumber).x, Led(LedNumber).y)-(Led(LedNumber).x + 2, Led(LedNumber).y + 2), 17, BF
     PSET (Led(LedNumber).x + 1, Led(LedNumber).y + 1), 16
END IF

END SUB