Page 1 of 1

Character designer for an 8x8 LED matrix controlled by MAX7219.

Posted: Thu Nov 30, 2023 12:32 pm
by Fernando Perez

Code: [Local Link Removed for Guests]

' ESP32: CLK=GPIO18 - DIN=GPIO23
' ESP8266: CLK=GPIO14 - DIN=GPIO13
' In both, CS can be chosen

dim l(65)
file$ = "/set01"
pattern$ = "0,0,0,0,0,0,0,0"

CS = 15  ' CS=GPIO15 - CLK=GPIO14 - DIN=GPIO13
MAXSCROLL.SETUP 1, CS
writeMAX &H0A, 0   ' &H0A = brightness
clearMAX

onHTMLReload web
gosub web
WAIT

END

' ---------------------------------
web:
  cls
  cssExternal "/button.css"
  a$ = "" : a$ = a$ + |<center><div class="led">|

  led = 1
  for y = 1 to 8
    for x = 1 to 8
      command "a$=a$+button$("+chr$(34)+str$(led)+chr$(34)+",btn,"+chr$(34)+"bt"+str$(led)+chr$(34)+")"
      led = led + 1
    next x
    a$ = a$ + "<br>"
  next y 

  a$ = a$ + textBox$(pattern$, "pattern")
  a$ = a$ + |</div>|+button$("Save To",saveMAX,"btnSave")+textBox$(file$,"txtFile")
  html a$

return

' ---------------------------------
btn:
  b = val(htmlEventButton$)
  b$ = "bt" + htmlEventButton$
  
  if l(b) = 0 then
    l(b) = 1
    css CSSID$(b$, "background: yellow; color:black;")
  else
    l(b) = 0
    css CSSID$(b$, "background: black; color:white;")  
  endif
   
  displayMAX 
  updateLine   

return

' ---------------------------------
saveMAX:
  line$ = pattern$ + chr$(10)
  file.append file$, line$
  jscall |{alert("Saved character pattern");}|
return

' ---------------------------------
SUB clearMAX
LOCAL i
  for i = 1 to 8
    writeMAX i, 0
  next i  
END SUB

' ---------------------------------
SUB writeMAX(opcode, value)
LOCAL r
  pin(CS) = 0
  r = spi.byte(opcode)
  r = spi.byte(value)
  pin(CS) = 1
END SUB

' ---------------------------------
SUB displayMAX
LOCAL row, col, byte, pos, cell
  cell = 1
  for row = 1 to 8
    byte = 0
    for col = 1 to 8
      pos = 8 - col
      byte = byte + (l(cell) * (2^pos))
      cell = cell + 1
    next col
    writeMAX row, byte
  next row  
END SUB

' ---------------------------------
SUB updateLine
LOCAL line$, i, j, pos, cell, byte
  for i = 0 to 56 step 8
    for j = 1 to 8
      pos = 8 - j
      cell = i + j
      byte = byte + (l(cell) * (2^pos))
    next j
    line$ = line$ + str$(byte) + ","
    byte = 0
  next i
  line$ = left$(line$, len(line$) - 1)
  pattern$ = line$
  refresh
END SUB   

And the CSS file:

Code: [Local Link Removed for Guests]

.led button{
  margin:3px;
  background:black;
  color:white;
  width:50px;
  height:50px;
  border-radius:50%
}  

#pattern {
  margin:20px;
  width:450px;
  height:40px;
  padding:10px;
  font-size:1.5em;
  text-align:center;
  border:solid 2px;
}  

#btnSave {
  display: inline;
  width: 150px;
  margin: 0px 10px;
  padding: 10px 20px;
  border: none;
  border-radius: 16px;
  color: black;
  background-color: #ddd;
  text-align: center;
  font-size: 1.2rem;
  font-weight: 600; 
  cursor: pointer;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}

#txtFile {
  width:250px;
  height:40px;
  margin-left:20px;
  padding:10px;
  font-size:1.2em;
  border:solid 2px; 
}
Edited: For script needs, I have changed chr$(13) to chr$(10) in line 61

https://youtu.be/rifVOn8Cra4

While designing on the screen, the corresponding LEDs light up or turn off to easily see the real effect.
The generated patterns can be saved in a file for later use in a program that I will later publish.
I use it to view still images, not scrolling.
I hate scrolling! :evil:

Re: Character designer for an 8x8 LED matrix controlled by MAX7219.

Posted: Sat Dec 02, 2023 2:19 pm
by Fernando Perez
Once we have created our character pattern and saved it to a file, we have two possibilities:

A) Load program 1, save it and run it. Check that it works correctly.
- Check the "Advanced" box of the editor. Load the file with our character pattern. Copy content with the mouse. Uncheck the "Advanced" box.
- Paste what you copied at the end of the program 1. Add "DATA" at the beginning of each new line. Delete previous DATA.
- Execute the new pattern.

B) Load program 2, change the name of the file to the one we used to record our character pattern, save it and execute it.

https://youtu.be/AlRWDqv8340

Program 1:

Code: [Local Link Removed for Guests]

dim map(4) = 0, 2, 3, 4, 1  ' Module activation order
dim chars(9, 7) ' Declare the array that will contain the character pattern 
initCHARS       ' Fill it with READ/DATA

modules = 4  ' 4 modules of 8x8 LED matrix
CS = 15      ' GPIO that will control the CS pin
MAXSCROLL.SETUP modules, CS

brightness = 0
for module = 1 to modules  ' Adjust the brightness (0 to 10) of all modules
  writeMAX &H0A, brightness, module   ' &H0A = brightness
next module
' writeMAX &H0A, 5, 1   ' (Can be adjusted individually)  

clearMAX  ' Turn off all

' DEMO:
display 1, 1
display 9, 2
display 5, 3
display 0, 4
pause 3000
clearMAX

do
  for module = 1 to modules
    n = rnd(10)
    display n, module
  next module
  pause 3000
  clearMAX
loop    

END

' ---------------------------------
SUB writeMAX(opcode, value, module)
LOCAL i, r
  pin(CS) = 0
  for i = 1 to modules
    if i = map(module) then  
      r = spi.byte(opcode)
      r = spi.byte(value)
    else
      r = spi.byte(0)  ' MAX7219 NOOP
      r = spi.byte(0)
    endif
    pin(CS) = 1
  next i    
END SUB

' ---------------------------------
SUB display(char, module)
LOCAL i, byte
  for i = 1 to 8
    byte = chars(char, i-1)
    writeMAX i, byte, module
  next i
END SUB   

' ---------------------------------
SUB clearMAX
LOCAL i
  for module = 1 to modules
    for i = 1 to 8
      writeMAX i, 0, module
    next i
  next module    
END SUB

' ----------------------------------
SUB initCHARS
LOCAL i, j
  for i = 0 to 9
    for j = 0 to 7
      READ byte
      chars(i, j) = byte
    next j
  next i    
END SUB

' =========================
DATA 0,24,36,36,36,36,24,0
DATA 0,8,24,8,8,8,28,0
DATA 0,24,36,8,16,32,60,0
DATA 0,56,4,56,4,4,56,0
DATA 0,4,12,20,60,4,4,0
DATA 0,60,32,56,4,4,56,0
DATA 0,24,36,32,56,36,24,0
DATA 0,60,4,8,16,32,32,0
DATA 0,24,36,24,36,36,24,0
DATA 0,24,36,36,28,4,24,0

Program 2:

Code: [Local Link Removed for Guests]

file$ = "/set00"
lines = 1
numLines file$, lines ' Returns in the lines variable the number of lines of the file file$

dim chars(lines, 8)   ' Declare the array that will contain the character pattern 
fileToArray file$, lines, chars()  ' Fill it with the data from the file

dim map(4) = 0, 2, 3, 4, 1  ' Module activation order

modules = 4   ' 4 modules of 8x8 LED matrix
CS = 15       ' GPIO that will control the CS pin
MAXSCROLL.SETUP modules, CS

brightness = 1
for module = 1 to modules  ' Adjust the brightness (0 to 10) of all modules
  writeMAX &H0A, brightness, module   ' &H0A = brightness
next module  
' writeMAX &H0A, 5, 1   ' (Can be adjusted individually)

clearMAX  ' Turn off all

' DEMO:
n = 0 
do
  for module = 1 to 4
    char = n + module
    if char > lines then EXIT DO
    display char, module
  next module
  pause 2000
  clearMAX
  n = n + modules  
loop    

pause 2000
clearMAX

END

' ---------------------------------
SUB writeMAX(opcode, value, module)
LOCAL i, r
  pin(CS) = 0
  for i = 1 to modules
    if i = map(module) then  
      r = spi.byte(opcode)
      r = spi.byte(value)
    else
      r = spi.byte(0)  ' MAX7219 NOOP
      r = spi.byte(0)
    endif
    pin(CS) = 1
  next i    
END SUB

' ---------------------------------
SUB display(char, module)
LOCAL i, byte
  for i = 1 to 8
    byte = chars(char, i)
    writeMAX i, byte, module
  next i
END SUB   

' ---------------------------------
SUB clearMAX
LOCAL i
  for module = 1 to modules
    for i = 1 to 8
      writeMAX i, 0, module
    next i
  next module    
END SUB

' ========================
SUB numLines(file$, lines)
' ========================
LOCAL line$
  do
    line$ = file.read$(file$, lines)
    lines = lines + 1
  loop until line$ = "_EOF_"
  lines = lines-3
END SUB

' ===================================
SUB fileToArray(file$, lines, array()
' ===================================
LOCAL line, line$, cell
  for line = 1 to lines
    line$ = file.read$(file$, line)
    for cell = 1 to 8
      array(line, cell) = val(word$(line$, cell, ","))
    next cell
  next line
END SUB  
 

The attached zip includes the three programs and three character sets created with the editor.
max7219.zip