(* Menus and execution tables in EEPROM

 Attempt to implement a simple but universal EEPROM based
 menu system with added execution table.

*)

ecreate #LED  0 e,      \ Led tables
ecreate LED-XT   10 cells eallot
ecreate LED-TXT  10 0A * eallot

ecreate #MOVIE  0 e,    \ Movie tables
ecreate MOVIE-XT   20 cells eallot
ecreate MOVIE-TXT  20 0A * eallot

ecreate #MENU  0 e,     \ Main menu tables
ecreate MENU-XT    20 cells eallot
ecreate MENU-TXT   20 0A * eallot

value ITEM
: >ENTRY    ( a u eat xt eax ea# -- )
    >r  r@ e@ 2* +  e!      \ Store XT  ( - a u eat )
    r@ e@ 0A * +            \ Calc. text storage space
    09 over ec! 1+          \ Store string length
    dup 9 bounds            \ Fill entry
    do  bl i ec!  loop      \ with spaces
    swap bounds ?do         \ Store string
        count i ec!         \ Copy from a to ea
    loop
    drop  1 r> e+! ;        \ Increase number of used entries

: >LEDS     ( a u xt -- )   \ Make new LED entry
    led-txt swap  led-xt #led >entry ;

: >MOVIE    ( a u xt -- )   \ Make new MOVIE entry
    movie-txt swap  movie-xt #movie >entry ;

: >MENU     ( a u xt -- )   \ Make new MENU entry
    menu-txt swap  menu-xt #menu >entry ;

value #ADR
: .$        ( ea ea# -- )
    to #adr  item 0A * +  fat   \ Calc top entry
    #adr e@ 4 umin 0 do         \ Loop 4 or less times
        0 i 2* xy 2 &spaces     \ Place cursor
        dup ec@+ e&type  thin   \ Type string
        i 1+ item +  #adr e@ =  \ End of table reached?
        if
            #adr e@ 1- 0A * -   \ Yes, back to start
        else
            0A +    \ No, to next string
        then
    loop  drop ;

: SELECT    ( ea ea# -- )
    &page           \ Clear OLED screen
    begin
        2dup .$     \ Print menu entry
        s1? if  incr item  then \ To next entry
        dup e@ item = if  0 to item  then
    s2? until       \ Start/Stop pressed
    2drop ;         \ Remove EE-data

: EXEC      ( ea -- )
   &page  200 ms  item cells + e@ execute ;

: LEDMENU   ( -- )
    item >r  0 to item      \ Save & reset menu index
    led-txt #led select     \ Use LED menu
    led-xt exec  r> to item ; \ Exec. LED function, restore index

s" Chaser-1"  ' runner >leds    \ Fill LED tables

s" Chaser-2"  ' run2 >leds

s" Flash"     ' flash >leds

\ End
