\ 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
        10 choose  2 choose     \ Create pattern
        if  10 *  then  i2out   \ Output pattern
    1- ?dup 0= until  i2stop} ; \ End stream

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

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

: .ME       ( -- )
    &page   8 0 >xy s" HCC Forth-gg" &&type
            0 2 >xy 80 28 &fill
           28 4 >xy s" Willem" &&type
           1C 6 >xy s" Ouwerkerk" &&type ;

(*
Schuifregister waarvan de uitgangen genummerd zijn van 1 t/m 'm'
en de uitgangen 'm' en 'n' geXORed worden en het resultaat
daarvan weer op bit 1 ingevoerd wordt.

    m   n       cyclus-lengte       woordlengte
 ---------------------------------------------
    15  14          32767           16 bits
    16  4,13,15     65535           16 bits

code RANDOM         ( -- u )    \ Rnd Make random number u
    tos sp -) mov
    adr rnd & tos mov           \ Get RND value
    tos day mov                 \ Work copy
    day day add                 \ Bit 15 to bit 16
    day sun mov                 \ Copy to SUN
    8000 # sun bia              \ Mask bit 15
    day day add                 \ Shift 14 up
    8000 # day bia              \ Mask bit 14
    sun day bix                 \ XOR bit 14 & 15
    tos tos addc                \ Shift result to RND value
    tos adr rnd & mov           \ Save result
    next
end-code

*)

value RND                       \ Random work area
code RANDOM
8324 ,  4784 ,  0 ,  4217 ,  adr rnd ,
4708 ,  5808 ,  480A ,  F03A ,
8000 ,  5808 ,  F038 ,  8000 ,
EA08 ,  6707 ,  4782 ,  adr rnd ,
4F00 ,  end-code

: DEMO      ( -- )
    setup-adc  -1 to forward?
    display-setup  setup-random
    begin
        looplicht  egeltje  600 ms  .me
    key? until ;

' demo  to app
shield DEMO\  freeze

\ End


