fl { Adafruit 16X32 Matrix (using 2pcs) PropForth 5.5 2014/07/02 14:53:15 Adafruit 16X32 Matrix Adafruit 16X32 Matrix [ C B A ] ------------------------- ------------------------- | b63 ...... b32| | b31 ...... b0 | [ 0 0 0 ] | upper matrix(8x32) | | upper matrix(8x32) | ... upper matrix(8x32) color[ B1 G1 R1 ] | | | | [ 1 1 1 ] |-------------------------|=====|-------------------------| <--------------Propeller P0-P11 | b63 ...... b32|cable| b31 ...... b0 | [ 0 0 0 ] | lower matrix(8x32) | | lower matrix(8x32) | lower matrix(8x32) color[ B2 G2 R2 ] | | | | [ 1 1 1 ] ------------------------- ------------------------- ^ ^ ^ ^ | | output signal side Input signal side | | output signal side Input signal side matrix_array2 64Word X 16 = 1024Word(2048bytes) --------------------------------------------------- 0 | b63 ...... b32| b31 ...... b0 | <-- 64Word .. | upper matrix(8x32) | upper matrix(8x32) | 7 | | | |-------------------------|-------------------------| 8 | b63 ...... b32| b31 ...... b0 | .. | lower matrix(8x32) | lower matrix(8x32) | 15 | | | --------------------------------------------------- propeller ROM-chip(GT21L16J1Y) P12 ----> sclk P13 ----> cs P14 ----> si P15 <---- so } \ =========================================================================== \ Variables \ =========================================================================== \ buffer for Adafrui matrix16X32 { Input before loading this code --------------------------------------------------------------------- reboot .. Prop0 Cog6 ok variable matrix_array2 Prop0 Cog6 ok matrix_array2 . 17616 Prop0 Cog6 ok <-- If Dev-kernel, it should be 17616. d17616 d2048 + here W! Prop0 Cog6 ok ---------------------------------------------------------------------- Loading this code variable matrix_array2 d17616 d2048 + here W! } \ variable for section wvariable n \ variable for demo wvariable color wvariable bg_color \ =========================================================================== \ Constants \ =========================================================================== \ Pin for Adafruit 16X32 Matrix (2pcs) 0 wconstant R1 \ 1 wconstant G1 \ 2 wconstant B1 3 wconstant R2 \ 4 wconstant G2 \ 5 wconstant B2 6 wconstant LnA \ 7 wconstant LnB \ 8 wconstant LnC \ 9 wconstant CLK d10 wconstant LE d11 wconstant EN \ Pin for ROM-chip(GT21L16J1Y) d12 wconstant SCK d13 wconstant CS d14 wconstant DO d15 wconstant DI 7 R1 lshift invert constant upper_m \ mask bit for upper-matrix RGB 7 R2 lshift invert constant lower_m \ mask bit for lower-matrix RGB 7 LnA lshift invert constant line_m \ mask bit for line 1 EN lshift constant en_m \ EN mask \ =========================================================================== \ Main \ =========================================================================== \------------------------------------------ \ Adafruit matrix16x32 2pcs \------------------------------------------ \ Set [C B A] to matrix_array2 \ ( -- ) : prepare_array2 \ upper section and lower section 2 0 do i n W! \ from line0 to line7 8 0 do \ from b0 to b31 d64 0 do j LnA lshift \ Get line number en_m or \ Add EN \ Calculate matrix-dot's position d64 8 u* n W@ u* \ section Word d64 j u* + \ Add line Word i + \ Add bit Word 2 u* \ bytes matrix_array2 + \ ( line line address ) W! \ Save line number to each dot loop loop loop ; \ Set each Long data of matrix_array2 to outa \ ( n1 -- n2 ) n1:address of matrix_array2 n2:n1+2 lockdict create a_matrix_16x32_2 forthentry $C_a_lxasm w, h118 h113 1- tuck - h9 lshift or here W@ alignl h10 lshift or l, z4ixZB l, z1bix[N l, z20yPO2 l, z1SV01X l, z80 l, freedict \ Scan matrix_array2 to display \ ( -- ) : scan2 hFFF dira COG! \ Set ports to output matrix_array2 begin dup d16 0 do d64 0 do a_matrix_16x32_2 loop LE pinhi LE pinlo EN pinlo loop drop fkey? swap drop until drop ; \------------------------------------------ \ ROM(JIS0208)-chip(GT21L16J1Y) \------------------------------------------ \ Initialize ports for ROM-chip(GT21L16J1Y) \ ( -- ) : initROM SCK pinout CS pinhi CS pinout DO pinout ; \ Set each port for ROM-chip(GT21L16J1Y) : do_h DO pinhi ; : do_l DO pinlo ; : sck_h SCK pinhi ; : sck_l SCK pinlo ; : di_m DI >m ; \ Multiply d32 : x32 d32 u* ; \ Get font(JIS0208:16x16) address [32bytes] \ ( n1 n2 -- n3 ) n1:ku[8bit] n2:ten[8bit] n3:address[24bit] : get_addr1 2dup 1 d94 between swap 1 d15 between and if 1- swap 1- d94 u* + x32 else 2dup 1 d94 between swap d16 d47 between and if 1- swap d16 - d94 u* + x32 d43584 + else 2dup 1 d94 between swap d48 d84 between and if 1- swap d48 - d94 u* + x32 d138463 + else 2dup 1 d94 between swap d85 = and if 1- swap d85 - d94 u* + x32 d246944 + else 1- swap d88 - d94 u* + x32 d249952 + thens ; \ Get font(8x16) address [16bytes] \ ( n1 -- n2 ) n1:ASCII code[h20 - h7F] n2:address[24bit] : get_addr2 h20 - d16 u* d255968 + ; \ Get font(8x16) address [16bytes] \ ( n1 -- n2 ) n1:ASCII code[h00 - hFF] n2:address[24bit] : get_addr3 d16 u* d257504 + ; \ Get font(8x16) address [16bytes] \ ( n1 -- n2 ) n1:ASCII code[h00 - hFF] n2:address[24bit] : get_addr4 d16 u* d261600 + ; \ READ ROM chip's font-data[2byte] for 1-row \ ( n1 n2 -- n3 n4 ) n1:address+16[24bit] n2:address[24bit] n3:upper for character n3:lower for character : read_ROM 2 0 do \ Select ROM chip CS pinlo \ Send instruction 3 h80 8 0 do 2dup and 0<> if do_h else do_l then sck_h sck_l 1 rshift loop 2drop \ Read char data \ Send address h800000 \ 1st:( n1 n2 800000 ) 2nd:( n3 n1 800000 ) d24 0 do sck_l 2dup and 0<> if do_h else do_l then sck_h 1 rshift loop 2drop \ 1st:( n1 ) 2nd:( n3 ) do_l sck_l \ Read data 0 8 0 do 1 lshift sck_h ina COG@ di_m and 0<> if 1 else 0 then or sck_l loop \ 1st:( n1 n3 ) 2nd:( n3 n4 ) i 0= if swap then \ 1st:( n3 n1 ) 2nd:( n3 n4 ) \ ." data" st? \ Deselect ROM chip CS pinhi loop ; \ Shift upper section and lower section of matrix_array2(16x64) to left(1Word) \ ( -- ) : shift_bit64 matrix_array2 d16 0 do dup dup \ ( addr addr addr ) d63 0 do dup 2+ \ ( addr addr addr addr+2 ) W@ swap W! \ ( addr addr ) 2+ dup \ ( addr addr+2 addr+2 ) loop 2drop \ ( addr ) d128 + \ next line loop drop ; \ Print out JIS0208's 1row to Adafruit matrix16x64 \ ( n1 -- ) n1:JIS0208's 1row data : prt_1row 1 \ ( n1 1 ) d16 0 do \ Save b0 value of each line 2dup and \ ( n1 1 t/f ) if color W@ else bg_color W@ then \ ( n1 1 color/bg_color ) matrix_array2 i d128 u* + d126 + \ b0 position dup \ ( n1 1 color/bg_color [address of b0] [address of b0] ) \ dup . W@ \ ( n1 1 color/bg_color [address of b0] [value of b0] ) over matrix_array2 - d1024 < \ Check if upper section or lower section if upper_m \ upper section else lower_m \ lower section then and \ ( n1 1 color/bg_color [address of b0] [masked value of b0] ) >r \ Push swap \ ( n1 1 [address of b0] color/bg_color ) over matrix_array2 - d1024 < \ Check if upper section or lower section if R1 \ upper section else R2 \ lower section then lshift \ ( n1 1 [address of b0] [shifted color/bg_color] ) r> \ Pop or swap \ ( n1 1 [value of b0] [address of b0] ) W! \ ( n1 1 ) 1 lshift \ 4 delms \ delay loop \ ( n1 h10000 ) shift_bit64 \ Shift b62-b0 of upper section to 1bit left 2drop \ ( ) ; \ Get data[2bytes] ROM-character's 1row and print it \ ( n1 -- n2 ) n1:address n2:address+1 : JIS0208 dup d16 + over \ ( n1 n1+16 n1 ) \ Get 1-row data[2bytes] read_ROM \ ( n1 byte[n] byte[n+1] ) 8 lshift or \ ( n1 Word ) \ Print out 1row to Adafruit matrix prt_1row 1+ \ ( n1+1 ) ; \ Print ASCII character \ ( n1 -- ) n1:address : ROM->matrix 8 0 do dup 8 + over \ ( addr addr+8 addr ) read_ROM \ ( addr byte[n] byte[n+1] ) 8 lshift or \ ( addr Word ) \ Print out 1row to Adafruit matrix prt_1row { \ Print out chacters to TeraTerm wondow. 1 d16 0 do 2dup and if h2A else bl then emit 1 lshift loop cr 2drop } 1+ \ ( addr+1 ) loop drop ; \ Display 8x16character to on adafruit matrix16x64 \ ASCII type 1 \ ( n1 -- ) n1:h20 - h7F : char_1 get_addr2 \ Get Font address ROM->matrix ; \ Display string by ASCII type 1 \ ( n1 n2 n3 -- ) n1:cstr n2:color n3:background : Type_1_str bg_color W! \ ( n1 n2 ) color W! \ ( n1 ) C@++ \ ( c-addr+1 c1 ) c-addr+1: string's first char addr c1:string length dup if bounds do i C@ char_1 loop \ Print string else 2drop then ; \ Display 8x16character to on adafruit matrix16x64 \ ASCII type 2 \ ( n1 -- ) n1:h20 - h7F : char_2 get_addr3 \ Get Font address ROM->matrix ; \ Display string by ASCII type 2 \ This convert only alphabet(26characters). \ To display other characters(number,!,#, etc), it need to add more if-else-then. \ ( n1 n2 n3 -- ) n1:cstr n2:color n3:background : Type_2_str bg_color W! \ ( n1 n2 ) color W! \ ( n1 ) C@++ \ ( c-addr+1 c1 ) c-addr+1: string's first char addr c1:string length dup if bounds do i C@ d128 + char_2 \ Print string loop else 2drop then ; \ Display 8x16character to on adafruit matrix16x64 \ These are meanibgless charcters. \ ASCII type 3 \ ( n1 -- ) n1:h20 - h7F : char_3 get_addr4 \ Get Font address ROM->matrix ; \ Display string by ASCII type 3 \ ( n1 n2 n3 -- ) n1:cstr n2:color n3:background : Type_3_str bg_color W! \ ( n1 n2 ) color W! \ ( n1 ) C@++ \ ( c-addr+1 c1 ) c-addr+1: string's first char addr c1:string length dup if bounds do i C@ char_3 loop \ Print string else 2drop then ; \ Inititialize to use ROM-chip on Adafruit : init_adafruit initROM prepare_array2 c" scan2" 0 cogx ; \ Print out ROM-character(JIS0208:7999pcs) to Adafruit matrix16x64(16x32 2pcs) \ ( -- ) : demo1 init_adafruit 0 bg_color W! 1 color W! 0 \ Top font address of ROM-chip begin d16 0 do JIS0208 loop \ Print out 1 character d16 + color W@ 1+ dup 8 = if drop 1 then color W! \ Check if end of JIS0208-table dup d255680 > if drop 0 then fkey? swap drop until drop 0 cogreset ; \ Print out ROM-character type1(8x16:96pcs) to Adafruit matrix16x64(16x32 2pcs) \ ( -- ) : demo2 init_adafruit 0 bg_color W! 1 color W! h80 h20 do i char_1 color W@ 1+ dup 8 = if drop 1 then color W! loop 0 cogreset ; \ Print out ROM-character type2(8x16:255pcs) to Adafruit matrix16x64(16x32 2pcs) \ ( -- ) : demo3 init_adafruit h100 0 do i char_2 color W@ 1+ dup 8 = if drop 1 then color W! loop 0 cogreset ; \ Print out ROM-character type3(8x16:255pcs) to Adafruit matrix16x64(16x32 2pcs) \ ( -- ) : demo4 init_adafruit h100 0 do i char_3 color W@ 1+ dup 8 = if drop 1 then color W! loop 0 cogreset ; \ Print out ROM-character(JIS0208:7999pcs) to Adafruit matrix16x64(16x32 2pcs) \ ( -- ) : demo5 init_adafruit c" May the Forth be with you. " 1 7 Type_1_str c" May the Forth be with you. " 0 4 Type_2_str 0 cogreset ; { fl \ ( n1 -- n2 ) n1:address of buffer build_BootOpt :rasm rdword outa , $C_stTOS or outa , __clk add $C_stTOS , # 2 jexit __clk h200 ;asm a_matrix_16x32_2 }