\ noForth button OLED demo

\ Generating pseudo random numbers
\ CHOOSE - Make pseudo random number u2 the range of u2 is 0 to u1-1

hex
: /MS           0 ?do  30 0 do loop  loop ;  \ Wait u times 0,1 ms

value RND   \ Work location for pseudo random numbers
decimal     \ Make pseudo random number ud
: RANDOM        ( -- ud )       rnd 31421 *  6927 +  dup to rnd ;
: CHOOSE        ( u1 -- u2 )    random  um* nip ;
: SETUP-RANDOM  ( -- )          31414 to rnd ;
hex

: RFILL         ( +n -- )       \ Fill with random pattern +n columns
    40 {ol                      \ Start oled-data stream
    begin                       \ Whole screen buffer
        2 choose  8 choose      \ Create pattern numbers
        lshift  i2out           \ Make and output bit pattern
    1- ?dup 0= until  i2stop} ; \ End stream

: EGELTJE   ( -- )
    &page                       \ Init. and show Hedgehog
    38 00 do
        80 i 2* - 1 egel
        s2? if  leave then
        >dir<  5 ms
    loop
    80 ms  fat  0 6 xy" Egel project" ;

: LOOPLICHT ( -- )
    4 0 do
       forward? if  forward
       else    backward
       then
       s2? if  leave  0 29 c!  then
    loop   0 29 c! ;

: .ME       ( -- )
    &page  fat
     3 0 xy" HCC Forth-gg"  graphic

     0 2 xy 80 28 &fill  thin
    28 4 xy" Willem"
    1C 6 xy" Ouwerkerk" ;

: DOT       ( y x -- )      \ Eight character dot
    6 umin >r  70 umin  graphic
        dup  r@    xy" Q11P"
             r> 1+ xy" O11N" ;

: ONE       3C 3 dot ;      \ Six sides of a dice
: TWO       24 6 dot  54 0 dot ;
: THREE     one  two ;
: FOUR      two  24 0 dot  54 6 dot ;
: FIVE      one  four ;
: SIX       four 24 3 dot  54 3 dot ;

: DICE      ( +n -- )       \ Print dice
    &page
    dup 1 = if  one     then
    dup 2 = if  two     then
    dup 3 = if  three   then
    dup 4 = if  four    then
    dup 5 = if  five    then
        6 = if  six     then ;

: DO-DICE       6 choose 1+ dice ; \ Print dice randomly
: ROLL-DICE     07 0 do  i 1+ 10 * ms  do-dice  loop ;
: SW?           s1? s2? or ;

: PLAY-DICE     ( -- )
    begin
        20 contrast  200 ms
        begin  do-dice  sw? until
        roll-dice  C0 contrast
        begin  sw? until
    s2? until  &page ;

: EGELDEMO      ( -- )      looplicht  egeltje  200 ms  .me ;

\ : READY     ( -- )      begin  s2? until ;

: ENTRY     ( n1 n2 -- n1 )
    over = if
        ch * &emit &space  fat
    else
        bl &emit &space  thin
    then ;

: MENU      ( +n -- )
    2 0 xy  0 entry  &" Status "
    2 2 xy  1 entry  &" Owner "
    2 4 xy  2 entry  &" Egel "
    2 6 xy  3 entry  &" Dice "
    drop ;

value ITEM
: EXEC      ( -- )
    item 0 = if  &page  begin  .status  s2? until  then
    item 1 = if  .me  begin  s2? until             then
    item 2 = if  begin  egeldemo  s2? until  then
    item 3 = if  play-dice                         then
    200 ms ;

: SELECT    ( -- )
    begin  s2? 0= until
    begin
        item menu
        s1? if  1 +to item  then
        item 4 = if  0 to item  then
    s2? until ;

: DEMO      ( -- )
    setup-adc  -1 to forward?  0 to item
    setup-display  setup-random
    .me  400 ms
    begin
        &page  select  200 ms  exec
    key? until ;

' demo  to app
shield DEMO\  freeze

\ End


