\ ********************************************************************* 
\ Dice roll on 8x8 LED matrix 
\    Date:          30 jun 2022 
\    Updated:       10 july 2022 
\    File Version:  0.0 
\    MCU:           ESP32-WROOM-32 
\    Forth:         ESP32forth all versions 7.x++ 
\    Copyright:     Marc PETREMANN 
\    Author:        Marc PETREMANN 
\    GNU General Public License 
\ ********************************************************************* 
 
\ require SPI vocabulary: 
\ link: https://esp32.arduino-forth.com/article/SPI_implementation 
\ link: https://esp32.arduino-forth.com/article/SPI_words 
 
DEFINED? --MAX7219 [if] forget --MAX7219  [then] 
create --MAX7219 
 
\ define VSPI pins 
19 constant VSPI_MISO 
23 constant VSPI_MOSI 
18 constant VSPI_SCLK 
05 constant VSPI_CS 
 
\ define SPI port frequency 
4000000 2 / constant SPI_FREQ 
 
\ select SPI vocabulary 
only FORTH  SPI also 
 
\ initialize HSPI port 
: init.VSPI ( -- ) 
    VSPI_CS OUTPUT pinMode 
    VSPI_SCLK VSPI_MISO VSPI_MOSI VSPI_CS SPI.begin 
    SPI_FREQ SPI.setFrequency 
  ; 
 
\ send two bytes to MAX7219 thru SPI port 
: MAX7219.send ( c1 c2 -- ) 
    1 SPI.setHwCs 
    swap 8 lshift + SPI.write16 
    0 SPI.setHwCs 
  ; 
 
: MAX7219.normal   ( -- )  
    $0c $01 MAX7219.send  
  ; 
 
: MAX7219.shutdown ( -- )  
    $0c $00 MAX7219.send 
  ; 
 
: MAX7219.intensity  ( c -- )   
    $0a swap MAX7219.send  
  ; 
 
: MAX7219.decode     ( c -- )   
    $09 swap MAX7219.send  
  ; 
 
: MAX7219.scan.limit ( c -- )   
    $0b swap MAX7219.send  
  ; 
 
: MAX7219.set.digit  ( cbits cdigit -- )  
    swap MAX7219.send  
  ; 
 
: MAX7219.test.on  ( -- )  
    $0f $01 MAX7219.send  
  ; 
 
: MAX7219.test.off ( -- )  
    $0f $00 MAX7219.send  
  ; 
 
 
DEFINED? --TTT [if] forget --TTT  [then] 
create --TTT 
 
\ *** Array with alphanumerics characters *** 
\ caracters ..0..9A..Z in array 
create DICE_SPRITES 
  $00 c, $00 c, $00 c, $18 c, $18 c, $00 c, $00 c, $00 c,   \ 1 
  $00 c, $00 c, $00 c, $c3 c, $c3 c, $00 c, $00 c, $00 c,   \ 2 
  $00 c, $c0 c, $c0 c, $18 c, $18 c, $03 c, $03 c, $00 c,   \ 3 
  $00 c, $c3 c, $c3 c, $00 c, $00 c, $c3 c, $c3 c, $00 c,   \ 4 
  $00 c, $c3 c, $c3 c, $18 c, $18 c, $c3 c, $c3 c, $00 c,   \ 5 
  $00 c, $db c, $db c, $00 c, $00 c, $db c, $db c, $00 c,   \ 6 
 
 
: getNum ( n ---)          \ get nth caracters from CHARACTERS table 
    8 * DICE_SPRITES + 
  ; 
 
\ Only for test encoding characters 
: tstChar ( n ---) 
    getNum { addr } 
    8 0 do 
        addr i + c@  dup 
        hex <# # # #> cr type ."  - " 
        binary <# # # # # # # # # #> type 
        decimal 
    loop 
  ; 
 
 
 
\ ****** TEST section ********************************* 
 
DEFINED? --tx [if] forget --tx  [then] 
create --tx 
 
\ display a character from it address 
: disp.char { addr -- } 
    8 0 do 
        addr i + c@             \ fetch character byte 
        i 1+ MAX7219.set.digit  \ send byte at addr i 
    loop 
  ; 
 
: dice ( n -- ) 
    getNum disp.char 
  ; 
 
 
\ Random number data  
$3FF75144 constant RNG_DATA_REG  
  
\ get 32 bits random b=number  
: rnd  ( -- x )  
    RNG_DATA_REG UL@  
  ;  
  
\ get random number in interval [0..n-1]  
: random ( n -- 0..n-1 )  
    rnd swap mod  
  ;  
 
: before-dice ( -- ) 
    5 0 do 
        8 0 do 
            $ff i 1+ MAX7219.set.digit j 10 * ms 
            $00 i 1+ MAX7219.set.digit 
        loop 
        8 0 do 
            $ff 7 i - 1+ MAX7219.set.digit j 10 * ms 
            $00 7 i - 1+ MAX7219.set.digit 
        loop 
    loop 
  ; 
 
: MAX7219.init ( -- ) 
    init.VSPI 
    MAX7219.normal 
    $01 MAX7219.intensity 
    $07 MAX7219.scan.limit 
    $00 MAX7219.decode 
  ; 
 
MAX7219.init 
 
: dice-roll ( -- ) 
    before-dice 
    6 random dice 
  ; 
 
 
 

Legal: site web personnel sans commerce / personal site without seling