
\ Prints -1 if device with address 'a' is present on I2C-bus otherwise 0.
\ : I2C?      ( a -- )            \ Result is true when device 'a' is on I2C bus
\     >dev setup-i2c {i2ack?} . ; \ Address device, present?


(* Display tools

: >PICTURE      ( addr -- )     \ Copy picture from mem. address to display
    40 {ol  dm 400 0 do
        i + c@  i2out           \ One row
    loop
    drop  i2stop} ;

value DELAY                     \ Frame hold time
: FRAME         ( -- )      delay ms ;

\ Picture and movies from EEPROM

: EE>PICTURE    ( eeaddr -- )   \ Copy picture from EE address to display
    0 {ol  dm 400 0 do
        i + ec@  i2out          \ One row
    loop
    drop  i2stop} ;

\ Note that the first cell of a movie must contain the
\ full length of the stored picture frames in bytes!!
: EEMOVIE       ( eeaddr -- )   \ Play movie from EEPROM
    @+ bounds ?do               \ Get length of movie
        i ee>picture  frame     \ Play one frame at a time
    dm 400 +loop ;              \ To next picture

shield DISPLAY\  freeze

*)

(*
: 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
*)


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


\ Set DCO to 1 MHz, current use of CPU without leds = 0,35mA
: 1MHZ      ( -- )  \ Baudrate 38K4
    int-off
    10FF c@ 0057 c! \ BCSCTL1   Set DCO = 1MHz
    10FE c@ 0056 c! \ DCOCTL
    0000 0058 c!    \ UCA0CTL2  DCO on
    int-on ;

\ Set DCO to 16 MHz, current use of CPU without leds = 5,01mA
: 16MHZ     ( -- )  \ Baudrate 38K4
    int-off
    10F9 c@ 0057 c! \ BCSCTL1   Set DCO = 16MHz
    10F8 c@ 0056 c! \ DCOCTL
    0000 0058 c!    \ DCO on
    int-on ;

: 32KHZ     ( -- )
    int-off
    0000 0057 c!    \ BCSCTL1   Switch low freq. mode on
    000C 0053 c!    \ BCSCTL3   32kHz with 12pF
    begin           \ LFXT osc. running?
        02 002 *bic \ IFG1      Clear bit 2
        noop  noop  \ Wait, Bit 2 remains clear
    02 002 bit* 0= until \ when osc. runs fine!
    00C8 0058 c! ;  \ BCSCTL2   LFXT1CLK on SMCLK

: 12KHZ     ( -- )
    int-off
    0000 0057 c!    \ BCSCTL1   Switch low freq. mode on
    0020 0053 c!    \ BCSCTL3   12kHz VLOCLK on
    00C8 0058 c!    \ BCSCTL2   SMCLK & MCLK are VLOCLK
    int-on ;

