Shop OBEX P1 Docs P2 Docs Learn Events
I am posting my code for the LED_Matrix board — Parallax Forums

I am posting my code for the LED_Matrix board

HydraHackerHydraHacker Posts: 77
edited 2020-04-09 23:56 in Propeller 2
Hi everyone,
I have my new username scrolling on the LED Matrix board.

p2Man




https://youtu.be/izLHN4wvJnE

Comments

  • cgraceycgracey Posts: 14,155
    Neat. How did you write your code? You could post it here.
  • Chip, I will document my code first, then I will be glad to post my code.

    P2 Man
  • cgraceycgracey Posts: 14,155
    Chip, I will document my code first, then I will be glad to post my code.

    P2 Man

    Sounds good.
  • HydraHackerHydraHacker Posts: 77
    edited 2020-04-09 23:52
    Hello Everbody, I have posted my code to display scrolling strings on the LED Matrix 8x7 board, I had to remove all but two characters from the character set because it was too big. I have added a method to display numbers in different bases.

    P2Man
    CON
    	BASE2 = 2
            BASE8 =	8
    	BASE16 = 16
    	NULL = 0
           	PERIOD_ASCII_CDE = 46
    
    VAR
            stk[64]
            matrixCogID
    	byte fd, pf
    
    PUB LED_matrixStart()
    
       'start next free cog and if started get its ID
       matrixCogID := COGSPIN(NEWCOG, LED_matrix(), @stk)
    
    PUB LED_matrixStop()
    
       'terminates the cog that renders to the LED matrix
       COGSTOP(matrixCogID)
    
    PUB LED_matrix() | byte ph, ptv
    
       pf := TRUE
       repeat
    
          'pause frame from rendering while pf is 'FALSE'
          repeat until pf
    
          'fd set to 'FALSE' means rendering is about to start
          fd := FALSE
    
          'ph value drives pins 0-7 high and helps compute word[] and bit positions
          repeat ph from 0 to 7 
     
             'ptv values are usd to drive selected pin(s) low and for computations 
             repeat ptv from 0 to 6 
    
                'turns on LEDS according to one bits in bmp[].[15..8]
                if bmp[(ptv + ph * 7) / 8].[15 - (ptv + ph * 7) +// 8 ADDBITS 0]
    	       PINH(ph)
    	       PINL(plt[ptv + ph * 7])
    	       WAITCT(clkfreq / 56 / 50 + GETCT())
    	       DIRA.[7..0] := 0
    
         'fd set to'TRUE means rendering is finished
         fd := TRUE
    
    PUB displayScrollingStr(as, d) | byte c, l
    
       'write char bitmap to bmp[].[7..0] until 'NULL' terminator is reached
       repeat while byte[as]
          c := byte[as++]
          asciiChrToBmp(c, 0)
    
          'shift left eight times to get next char fully displayed
          repeat 8
              
              'shift left seven rows of bmp by one bit to make more columns of char visible
              repeat l from 0 to 6
                 bmp[l] <<= 1
    	  WAITCT(d + GETCT())
    
    PUB asciiChrToBmp(a, b) | byte l, long ca
    
       'pause rendering to allow changes to bmp
       stopRendering()
    
       'non-printable chars are displayed as a period
       if a < 32
          a := PERIOD_ASCII_CDE
    
       'calculate address of char
       ca := @space + ((a - 32) * 7)
    
       'writes char bitmap to either high/low byte of bmp
       repeat l from 0 to 6
          WORD[@bmp][l].[b * 8 addbits 7] := byte[ca++]
    
       'changes to bmp are done start rendering
       startRendering()
    
    PUB startRendering()
    
       'set pf to start rendering
       pf := TRUE
    
    PUB stopRendering()
    
       'set pf to stop rendering and wait to make sure rendering is done
       pf := FALSE
       repeat until fd
    
    PUB displayNumBaseToStr(n , b, d, s) | byte dg[32], i1, long dgt, i, t 
    
       'initialize indexes to are two arrays
       i := 0
       i1 := 0
    
       'n is negative place minus sign before number, make n positive-
       'so the number is displayed properly
       if n & $80000000 == $80000000 && (s == TRUE)
          numBaseStr[i1++] := "-"
          ABS= n
    
       'number base prefixes placed before number
       case b
          BASE2:
             numBaseStr[i1++] := "0"
             numBaseStr[i1++] := "b"
          BASE8:
             numBaseStr[i1++] := "0"
             numBaseStr[i1++] := "o"
          BASE16:
             numBaseStr[i1++] := "0"
             numBaseStr[i1++] := "x"
    
       'extract digits of number in reverse order
       repeat
          if s
             dgt := n // b
          else
             dgt := n +// b
          dg[i++] := ABS (dgt)
          if s
             n /= b  
          else
             n +/= b
       while n <> 0
    
       'write digits in proper order to output string
       repeat
          numBaseStr[i1++] := decHex[dg[--i]]
       while i > 0    
    
       'place null trminator at the end of string
       numBaseStr[i1] := NULL
    
       'scrolling number on LED_Matrix
       displayScrollingStr(@numBaseStr, d)
    
    DAT
    
    Plt                   		BYTE    1,2,3,4,5,6,7  
                          		BYTE    0,2,3,4,5,6,7   
                          		BYTE    0,1,3,4,5,6,7  
                          		BYTE    0,1,2,4,5,6,7   
                          		BYTE    0,1,2,3,5,6,7  
                          		BYTE    0,1,2,3,4,6,7   
                          		BYTE    0,1,2,3,4,5,7   
                          		BYTE    0,1,2,3,4,5,6   
    
    numBaseStr			BYTE	0[35]
    decHex				BYTE	"0123456789ABCDEF"
    
    bmp				WORD	%00000000_00000000			
    				WORD	%00000000_00000000	
    				WORD	%00000000_00000000
    				WORD	%00000000_00000000	
    				WORD	%00000000_00000000
    				WORD	%00000000_00000000
    				WORD	%00000000_00000000
    
    
    
    
    							'ASCII Code  	Character
    '							-----------  	---------
    space			BYTE	%00000000	'32	     	[space]	
    				BYTE	%00000000		
    				BYTE	%00000000
    				BYTE	%00000000			
    				BYTE	%00000000
    				BYTE	%00000000
    				BYTE	%00000000
    
    exclamation		BYTE	%00010000	'33	     	!				
    				BYTE	%00010000	
    				BYTE	%00010000
    				BYTE	%00010000		
    				BYTE	%00010000
    				BYTE	%00000000
    				BYTE	%00010000
    

    ******************************Below is code that I used to test the LED_Matrix object******************************
    CON
    
    OBJ
    
       	lm	: "JHB_LED_matrix_8x7_V1.0_Update"
    
    VAR
    
    PUB main() 
    
       lm.LED_matrixStart()
    
       repeat
          'display character immediatly to LED_Matrix
          lm.asciiChrToBmp(127, 1)
          WAITCT(clkfreq + GETCT())
    
          'example how to display a string between double quotes
          lm.displayScrollingStr(string("Man!!! "), clkfreq / 15)
    
          'example of how to display a string defined in a 'DAT' section
          lm.displayScrollingStr(@myName, clkfreq / 15)
    
          'display scrolling 'SPACE' to clear LED_Matrix 
          lm.displayScrollingStr(string(" "), clkfreq / 15)
    
          'method to display signed/unsigned numbers in different bases
          'lm.displayNumBaseToStr(number, base, delay, signed = TRUE/FALSE)
    
          lm.displayNumBaseToStr(4294967295 , 10, clkfreq / 15, FALSE)
          lm.displayScrollingStr(string(" "), clkfreq / 15)
    
    DAT
    
    'examples of how to define a 'NULL' terminated strings in a 'DAT' section
    myName			byte "Insert Name Here ", 0
    P2Logo			byte 127,32,  "Logo", 0	
    
  • HydraHackerHydraHacker Posts: 77
    edited 2020-04-10 20:15
    Hello Everbody, I have made some changes to my code, I have made the character set its own object and the rendering method is its own object and the method calls is its own object. So now there are three objects besides the program I wrote to test the LED_Matrix program. I'll attach four files so that you can experiment with it faster, just open up the Test_LED_matrix_object.spin2 in Pnut and compile.

    P2Man
    CON
    
    OBJ
    
    	cs : "LED_matrix_character_set"
    
    VAR
            stk[64]
            matrixCogID
    	byte fd, pf
    
    PUB start()
       'start next free cog and if started get its ID
       matrixCogID := COGSPIN(NEWCOG, LED_matrix(), @stk)
    
    PUB stop()
    
       'terminates the cog that renders to the LED matrix
       COGSTOP(matrixCogID)
    
    PUB LED_matrix() | byte ph, ptv
    
       pf := TRUE
       repeat
          'pause frame from rendering while pf is 'FALSE'
          repeat until pf
    
          'fd set to 'FALSE' means rendering is about to start
          fd := FALSE
    
          'ph value drives pins 0-7 high and helps compute word[] and bit positions
          repeat ph from 0 to 7 
     
             'ptv values are usd to drive selected pin(s) low and for computations 
             repeat ptv from 0 to 6 
                'turns on LEDS according to one bits in bmp[].[15..8]
                if bmp[(ptv + ph * 7) / 8].[15 - (ptv + ph * 7) +// 8 ADDBITS 0]
    	       PINH(ph)
    	       PINL(plt[ptv + ph * 7])
    	       WAITCT(clkfreq / 56 / 50 + GETCT())
    	       DIRA.[7..0] := 0
    
         'fd set to'TRUE means rendering is finished
         fd := TRUE
    
    PUB get_fd() : fdv
       return fd
    
    PUB get_numBaseStrAdr() : a
       return @numBaseStr
    
    PUB get_decHexAdr() : a
       return @decHex
    
    PUB get_bmpAdr() : a
       return @bmp
    
    PUB get_asciiAdr(asc) : a
       return cs.get_asciiAdr(asc)
    
    PUB set_pf(tf)
       pf := tf
    
    PUB set_fd(tf)
       fd := tf
    
    DAT
    
    Plt                   		BYTE    1,2,3,4,5,6,7  
                          		BYTE    0,2,3,4,5,6,7   
                          		BYTE    0,1,3,4,5,6,7  
                          		BYTE    0,1,2,4,5,6,7   
                          		BYTE    0,1,2,3,5,6,7  
                          		BYTE    0,1,2,3,4,6,7   
                          		BYTE    0,1,2,3,4,5,7   
                          		BYTE    0,1,2,3,4,5,6   
    
    numBaseStr			BYTE	0[35]
    decHex				BYTE	"0123456789ABCDEF"
    
    bmp				WORD	%00000000_00000000			
    				WORD	%00000000_00000000	
    				WORD	%00000000_00000000
    				WORD	%00000000_00000000	
    				WORD	%00000000_00000000
    				WORD	%00000000_00000000
    				WORD	%00000000_00000000
    


    *********************************Below method calls object******************************
    CON
    
    	BASE2 = 2
            BASE8 =	8
    	BASE16 = 16
    	NULL = 0
           	PERIOD_ASCII_CDE = 46
    
    OBJ
    
    	lm : "JHB_LED_matrix_8x7_V1.0_Update"
    
    PUB start()
       lm. start()
    
    PUB displayScrollingStr(as, d) | byte c, l, long ba
    
       'write char bitmap to bmp[].[7..0] until 'NULL' terminator is reached
       repeat while byte[as]
          c := byte[as++]
          asciiChrToBmp(c, 0)
          'shift left eight times to get next char fully displayed
          repeat 8
              'shift left seven rows of bmp by one bit to make more columns of char visible
              repeat l from 0 to 6
                 WORD[lm.get_bmpAdr()][l] <<= 1
    	  WAITCT(d + GETCT())
    
    PUB asciiChrToBmp(a, b) | byte l, long ca
    
       'pause rendering to allow changes to bmp
       stopRendering()
    
       'non-printable chars are displayed as a period
       if a < 32
          a := PERIOD_ASCII_CDE
    
       'calculate address of char
       ca := lm.Get_asciiAdr(a)
    
       'writes char bitmap to either high/low byte of bmp
       repeat l from 0 to 6
          WORD[lm.get_bmpAdr()][l].[b * 8 addbits 7] := byte[ca++]
    
       'changes to bmp are done start rendering
       startRendering()
    
    PUB startRendering()
    
       'set pf to start rendering
       lm.set_pf(TRUE)
    
    PUB stopRendering()
    
       'set pf to stop rendering and wait to make sure rendering is done
       lm.set_pf(FALSE)
       repeat until lm.get_fd()
    
    PUB displayNumBaseToStr(n , b, d, s) | byte dg[32], i1, long dgt, i, t 
    
       'initialize indexes to are two arrays
       i := 0
       i1 := 0
    
       'n is negative place minus sign before number, make n positive-
       'so the number is displayed properly
       if n & $80000000 == $80000000 && (s == TRUE)
          BYTE[lm.get_numBaseStrAdr()][i1++] := "-"
          ABS= n
    
       'number base prefixes placed before number
       case b
          BASE2:
             BYTE[lm.get_numBaseStrAdr()][i1++] := "0"
             BYTE[lm.get_numBaseStrAdr()][i1++] := "b"
          BASE8:
             BYTE[lm.get_numBaseStrAdr()][i1++] := "0"
             BYTE[lm.get_numBaseStrAdr()][i1++] := "o"
          BASE16:
             BYTE[lm.get_numBaseStrAdr()][i1++] := "0"
             BYTE[lm.get_numBaseStrAdr()][i1++] := "x"
    
       'extract digits of number in reverse order
       repeat
          if s
             dgt := n // b
          else
             dgt := n +// b
          dg[i++] := ABS (dgt)
          if s
             n /= b  
          else
             n +/= b
       while n <> 0
    
       'write digits in proper order to output string
       repeat
          BYTE[lm.get_numBaseStrAdr()][i1++] := BYTE[lm.get_decHexAdr()][dg[--i]]
       while i > 0    
    
       'place null trminator at the end of string
       BYTE[lm.get_numBaseStrAdr()][i1++] := NULL
    
       'scrolling number on LED_Matrix
       displayScrollingStr(lm.get_numBaseStrAdr(), d)
    
  • cgraceycgracey Posts: 14,155
    Looks good, HydraHacker. I need to find my LED array board so that I can try it.
  • Thanks Chip,
    I am going to try and add a little more functionality.

    HydraHacker
Sign In or Register to comment.