\ require spi-base-avr.txt 
\ Words to drive the SPI module on the ATmega328P 
\ PJ 31-Jan-2016 
\ led-matrix-display.txt 
\ Drive a MAX7219 display chip with 8x8 LED matrix 
-disp-max7219 
marker -disp-max7219 
 
: max7219.send ( c1 c2 -- ) 
  swap spi.select spi.csend spi.csend spi.deselect 
; 
 
: disp.normal ( -- ) $0c $01 max7219.send ; 
: disp.shutdown ( -- ) $0c $00 max7219.send ; 
 
: disp.test.on ( -- ) $0f $01 max7219.send ; 
: disp.test.off ( -- ) $0f $00 max7219.send ; 
 
: disp.no.op ( -- ) $00 $00 max7219.send ; 
: disp.intensity ( c -- ) $0a swap max7219.send ; 
: disp.decode ( c -- ) $09 swap max7219.send ; 
: disp.scan.limit ( c -- ) $0b swap max7219.send ; 
: disp.set.digit ( cbits cdigit -- ) swap max7219.send ; 
 
: disp-test-1 ( -- ) \ all LEDs on full, 232mA needed 
  spi.init 
  disp.test.on 
  begin key? until 
  disp.test.off 
  spi.close 
; 
: disp-test-2 ( -- ) \ left 4 LEDs on first row, 42mA needed 
  spi.init 
  disp.normal 
  $03 disp.intensity 
  $00 disp.scan.limit 
  $f0 $01 disp.set.digit 
  begin key? until 
  disp.shutdown 
  spi.close 
; 
: disp-test-3 ( -- ) \ draw face, 18mA needed 
  spi.init 
  disp.normal 
  $01 disp.intensity 
  $07 disp.scan.limit 
  %00000000 $01 disp.set.digit 
  %01100110 $02 disp.set.digit 
  %00000000 $03 disp.set.digit 
  %00011000 $04 disp.set.digit 
  %00011000 $05 disp.set.digit 
  %10000001 $06 disp.set.digit 
  %01000010 $07 disp.set.digit 
  %00111100 $08 disp.set.digit 
  begin key? until 
  disp.shutdown 
  spi.close 
;