\ keyboard

needs toolkit
needs tools-ext
needs 1802io
needs 1802
needs skins
needs pref
decimal

: input-nibble  ( n -- )
    input-nibble
    view view.v-init @  execute ;

: flip-load-toggle  ( -- )
    load-switch? 0= set-load-switch
    init-load-toggle ;

: flip-run-toggle  ( -- )
    run-switch? 0= set-run-switch
    init-run-toggle ;

: flip-mp-toggle  ( -- )
    memprotect? 0= set-memprotect
    init-mp-toggle ;

: do-hexkey  ( char -- )
    >upper        \ char to uppercase
    dup >r
    (hex) 0A = if
        \ process cr
        strobe-EF4
    else
    r@ [char] 0 [char] 9 1+ within if
        r@ [char] 0 -  input-nibble
    else
    r@ [char] A [char] G within if
        r@ [ char A (hex) 0A - ] literal  -
        input-nibble
    else
    r@ [char] L = if
        flip-load-toggle
    else
    r@ [char] R = if
        flip-run-toggle
    else
    r@ [char] M = if
        flip-mp-toggle
    then then then then then then
    r> drop ;

\ We input ASCII keys to all unused
\ input ports (2, 3, 5, 6, & 7), and
\ strobe both EF2 & EF3.
: do-asciikey  ( char -- )
    dup 10 = if drop 13 then
    dup 2 input!
    dup 3 input!
    dup 5 input!
    dup 6 input!
    7 input!
    strobe-EF2/3 ;

: keypress  ( char -- )
    graffiti-hexpad? if
       do-hexkey
    else
       do-asciikey
    then ;
    
