'===========================================================================
' Subject: LED DISPLAY                        Date: 03-19-97 (19:16)       
'  Author: Mark Glenn                         Code: QB, QBasic, PDS        
'  Origin: fglenns@ix.netcom.com            Packet: EGAVGA.ABC
'===========================================================================
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''   LED Display - Can display about 300 LEDs per second in IDE on 486DX2   ''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''           Created as freeware by 4th Dimension Software                  ''
'' You may use this whenever you want, but please give some credit to the   ''
''                        us somewhere and somehow                          ''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''  Come to our homepage for more programming examples and free downloads   ''
''           Tons of stuff to do and great place to explore.                ''
''             http://www.geocities.com/TimesSquare/4940/                   ''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
DEFINT A-Z ' Went from 300 to 11000 LEDs per second when I added this
           ' So I would say this is very important on graphics based progs

DECLARE SUB LED (X%, Y%, colour%)
DECLARE SUB ScrollLED (Length%, X%, Y%)
DECLARE SUB ShowLEDDisplay (X%, Y%)
DECLARE SUB BoxDraw (x1%, y1%, x2%, y2%, UpDown%)
SCREEN 12

DIM SHARED Map(72, 10) ' LED Image map
DIM SHARED TempMap(72 * 2, 10)
LOCATE 10, 1
test1:
ScrollLED 72, 320 - INT(72 * 3 / 2), 120
END

'' The following is just tests on how fast your system can display LED lights
'' On my 486DX2, it can display 11000 per second.  Compiled is twice that.
DIM t AS LONG
t = TIMER
FOR a% = 1 TO 639 STEP 3
FOR b% = 1 TO 477 STEP 3
    TemCol% = TemCol% + 1
    IF TemCol% > 7 THEN TemCol% = 0
    LED a%, b%, TemCol%
NEXT
NEXT
LOCATE 2, 1
PRINT INT(1 / ((TIMER - t) / ((a% / 3) * (b% / 3)))); "LEDs can be displayed per second on your machine";
SLEEP

DIM TempLED AS LONG
Test2:
CLS
t = INT(TIMER): WHILE t = INT(TIMER): WEND 'Start Fresh
t = INT(TIMER)
FOR a = 1 TO 20000
    LED 10, 10, 0
    LED 10, 10, 4
    TempLED = TempLED + 2
NEXT
PRINT INT(TempLED / (TIMER - t)); "LED's were displayed in one second";
cool:
DATA 0,0,0,0,2,2,0,0,0,0
DATA 0,0,0,0,1,1,0,0,0,0
DATA 0,0,0,1,1,1,1,0,0,0
DATA 0,0,1,1,1,1,1,1,0,0
DATA 0,1,1,1,1,1,1,1,1,0
DATA 1,1,1,1,1,1,1,1,1,1
DATA 0,1,1,1,1,1,1,1,1,0
DATA 0,0,1,1,1,1,1,1,0,0
DATA 0,0,0,1,1,1,1,0,0,0
DATA 0,0,0,0,1,1,0,0,0,0
DATA 0,0,0,0,2,2,0,0,0,0

SUB BoxDraw (x1, y1, x2, y2, UpDown)
   SELECT CASE UpDown
      CASE 1
         LINE (x1, y1)-(x2, y2), 7, BF
         LINE (x1, y1)-(x2 - 1, y1), 15
         LINE (x1, y1)-(x1, y2 + 1), 15
         LINE (x1, y2 + 1)-(x2 + 1, y2 + 1), 8
         LINE (x2 + 1, y2 + 1)-(x2 + 1, y1), 8
      CASE 2
         LINE (x1, y1)-(x2, y2), 7, BF
         LINE (x1, y1)-(x2 - 1, y1), 8
         LINE (x1, y1)-(x1, y2 + 1), 8
         LINE (x1, y2 + 1)-(x2 + 1, y2 + 1), 15
         LINE (x2 + 1, y2 + 1)-(x2 + 1, y1), 15
   END SELECT
END SUB

' SUB LED (x, y, colour)
' x - x coordinate relative to LED display not screen (1-214)
'      (Screen coordinate)/3
' y - y coordinate relative to LED display not screen (1-60)
'      (Screen coordinate)/3
' Colour - From 0 to 7 (0 is off)
SUB LED (X%, Y%, colour%)
    NewX% = X% 'QB error fix
    NewY% = Y% 'QB error fix
   
    IF POINT(NewX%, NewY%) = colour% THEN EXIT SUB
    IF colour% = 0 THEN
        LINE (NewX%, NewY%)-(NewX% + 1, NewY% + 1), 0, BF
        EXIT SUB
    END IF
   
    PSET (NewX%, NewY%), colour% + 8
    PSET (NewX%, NewY% + 1), colour% + 8
    PSET (NewX% + 1, NewY%), colour% + 8
    PSET (NewX% + 1, NewY% + 1), colour%
END SUB

'ScrollLED: Change everything in here above the DO statement.  The stuff above
'           fills the array with the diamond figure.  Ignore it.
SUB ScrollLED (Length%, X%, Y%)
    TempVar = UBOUND(TempMap, 1)
    Height = UBOUND(TempMap, 2)
    LOCATE 11, 18: PRINT "Created as FREEWARE by 4th Dimension Software"
    LOCATE 13, 20: PRINT "http://www.geocities.com/TimesSquare/4940/"
    PAINT (0, 0), 4, 15
    BoxDraw X% - 2, Y% - 2, X% - 1 + (Length% * 3), Y% - 1 + Height * 3, 1
   
    LINE (X% - 1 + 1, Y% - 1 + 1)-(X% - 1 + (Length% * 3) - 1, Y% - 1 + Height * 3 - 1), 0, BF
    'LINE (199, 199)-(199 + (Length% * 3), 199 + 30), 15, B
    'Fill Array
    FOR Times = 0 TO 13
    RESTORE cool
    FOR a = 1 TO 11
    FOR b = 1 TO Height
        IF (a + (Times * 11) - 1) > (TempVar) THEN GOTO coolnews
        READ TempMap((a + (Times * 11) - 1), b - 1)
    NEXT
    NEXT
    NEXT
coolnews:
    Start = 0
'' Main scroller.  Don't modify if you don't understand it
DO
    Start = Start + 1
    IF Start >= TempVar THEN Start = 1
    FOR a = Start TO Length% + Start
        IF INKEY$ <> "" THEN END
        FOR b = 0 TO Height - 1
            IF a > TempVar THEN
                Map(a - Start, b) = TempMap(a - TempVar, b)
            ELSE
                Map(a - Start, b) = TempMap(a, b)
            END IF
        NEXT
    NEXT
    ShowLEDDisplay X%, Y%
LOOP
END SUB

'Converts the LED map to display on the screen
SUB ShowLEDDisplay (X%, Y%)
    FOR a = 0 TO (UBOUND(Map, 1) - 1)
        FOR b = 0 TO (UBOUND(Map, 2) - 1)
            LED a * 3 + X%, b * 3 + Y%, Map(a, b)
        NEXT
    NEXT
END SUB
