\ 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" ;

: BRIGHT    ( -- )      ldr 0A / >power ;   \ Set brightness using LDR

: RUNNER    ( -- )          \ Running light in two directions
    bright
    begin
        forward? if  forward
        else    backward
        then
    s2? until ;

: FLASH     ( -- )          \ Flashing outside & inner lights
    bright
    begin
        2D to leds 100 ms
        12 to leds 100 ms
    sw? until ;

: RUN2      ( -- )          \ Opposite running lights
    bright
    begin
        09 to leds 60 ms
        12 to leds 60 ms
        24 to leds 60 ms
    sw? until ;

: LED-DEMO      ( -- )          \ Select one of three demo's
    begin
        runner  run2  flash
    s2? until  0 to leds ;

: .LIPO         ( -- )          \ Show LiPo voltage in decimal
    base @ >r  decimal          \ Uses a 4-digit & space format
    lipo dm 10364 dm 10000 */   \ Convert ADC to voltage
    0 <# # # ch . hold # #>     \ Make string
    &type  r> base !  &"  V " ; \ And print it

: .STATUS       ( -- )
    base @ >r  decimal
    0 0 xy" Unused : " ivecs chere - 0 d.str &type
    0 2 xy" Free EE: " efree 0 du.str &type
    0 4 xy" Light: " ldr 0 d.str &type &space
    0 6 xy" LiPo: " .lipo  r> base ! ;

: FLUCTUATE ( -- )
    3F to leds
    begin
        dm 70 0 do
            i >power  3 ms
            s2? if leave then
        loop
        0 dm 70 do
            i >power  3 ms
            s2? if leave then
        -1 +loop
        100 ms
    s2? until  0 to leds ;

: .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 ;

: ROLL          6 choose 1+ ;
: DO-DICE       roll dice ;     \ Print dice randomly
: ROLL-DICE     07 0 do  i 1+ 20 * ms  do-dice  loop ;

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

: TEST-DICE     ( -- )
    &page  thin
    0C 1 xy" Rolling dice"  fat
    30 4 xy" test"  800 ms  &page
    begin
        20 contrast
        20 choose 1+ 0 do  do-dice  C ms  loop
        roll-dice  C0 contrast  800 ms
    s2? until  &page ;

: EGELDEMO      ( -- )      egeltje 800 ms ;

: CLOCK         ( -- )      &page  10 4 xy" Tik Tak " 600 ms ;

: STARS         ( -- )
    begin
        &page
        100 0 do
            80 choose 8 choose  xy
            1 9 choose lshift row  50 ms
            s2? if  leave  then
        loop
    s2? until ;

\ Flying birds
: BIRDS         ( -- )
    &page
    begin
        D0 -20 do
            i b1  i 40 - b2
            i 38 < if 30 ms then
            i b2  i 40 - b3
            i 38 < if 30 ms then
            i b3  i 40 - b1
            i 38 < if 30 ms then
            s2? if  leave  then
        8 +loop
    s2? until  &page ;

\ Hack 42 logo's animated...
: H42       ( x y -- )
    2dup xy ch D g&emit ch E g&emit ch C g&emit
    2 + xy ch F g&emit ch G g&emit ch H g&emit ;

: G42A      ( -- )
    &page  graphic
    20 0 xy s" 00000110011000" &type
    20 1 xy s" 00000110011000" &type
    20 2 xy s" 01111111111111" &type
    20 3 xy s" 00001100110000" &type
    20 4 xy s" 11111111111110" &type
    20 5 xy s" 00011001100000" &type
    20 6 xy s" 00011001100000" &type
    thin  48 6 xy s" 42" &type ;

: G42       ( -- )
    &page pict
    2C 1 xy" IJKL"
    2C 3 xy" MNOP"
    2C 5 xy" QRST"
    48 6 xy thin s" 42" &type ;

: CLARITY   ( -- )
    FF 0 do
        s2? if leave then
        i contrast  20 ms
    10 +loop
    0 FF do
        s2? if leave then
        i contrast  20 ms
    -10 +loop
    C0 contrast ;

: HACK42    ( -- )
    g42a
    begin
        600 ms  g42
        6 0 do  clarity  loop  &page
        6 0 do
            s2? if leave then
            70 choose 5 choose h42 100 ms
        loop
    s2? until  &page ;

\ Menu structure to select function
value ITEM
: ENTRY     ( n1 n2 -- n1 )
    nip over = if
        ch * &emit &space  fat
    else
        bl &emit &space  thin
    then ;

: .STRING   ( +n -- )
    dup 09 > if  0A -  then
    dup  0= if  0 entry  &" Status   "  exit  then
    dup 1 = if  1 entry  &" Owner    "  exit  then
    dup 2 = if  2 entry  &" Egeltje  "  exit  then
    dup 3 = if  3 entry  &" Play dice"  exit  then
    dup 4 = if  4 entry  &" Dice demo"  exit  then
    dup 5 = if  5 entry  &" Led demo "  exit  then
    dup 6 = if  6 entry  &" Stars    "  exit  then
    dup 7 = if  7 entry  &" Clock    "  exit  then
    dup 8 = if  8 entry  &" Birds    "  exit  then
    dup 9 = if  9 entry  &" Hack42   "  exit  then
    ;

: MENU      ( +n -- )
    2 0 xy  dup .string
    2 2 xy  dup 1+ .string
    2 4 xy  dup 2 + .string
    2 6 xy  dup 3 + .string
    drop ;

: EXEC      ( -- )
    item 0 = if  &page  begin  .status  s2? until  then
    item 1 = if  .me  fluctuate                    then
    item 2 = if  begin  egeldemo  s2? until        then
    item 3 = if  play-dice                         then
    item 4 = if  test-dice                         then
    item 5 = if  led-demo                          then
    item 6 = if  stars                             then
    item 7 = if  clock                             then
    item 8 = if  birds                             then
    item 9 = if  hack42                            then
    200 ms ;

: SELECT    ( -- )
    begin  s2? 0= until     \ Switch released?
    begin
        item menu
        begin
            item
            s1? if  1 +to item  then
            item A = if  0 to item  then
        item <>  s2? or until  ( Leave when S1 or S2 is pressed )
    s2? until ;

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

' demo  to app
shield DEMO\  freeze

\ End



