anchor.diff:  \ bit-banged i2c driver
   \ adapted from http://excamera.com/sphinx/article-forth-i2c.html
   \
  -\ This driver is master-only and won't support clock stretching.
  -\ There has to be a 1..10 kΩ resistor on SDA to pull it up in idle state.
  +\ This driver is master-only. It supports clock stretching.
  +\ There have to be 1..10 kΩ resistors on SDA and SCL to pull them up to idle state.
   
   [ifndef] SCL  PB6 constant SCL  [then]
   [ifndef] SDA  PB7 constant SDA  [then]
  
positive.code:   dup i2c.cnt !  if
      i2c-start i2c.adr @ 1+ i2c.prv ! i2c-flush
    else
      SCL ios!  \ i2c-stop
    then
    i2c.nak @
    dup if i2c-stop 0 i2c.cnt ! then  \ ignore reads if we had a nak
  
negative.code: 0 variable i2c.cnt
  
  : i2c-init ( -- )  \ initialise bit-banged I2C
    OMODE-OD SCL io-mode!
    OMODE-OD SDA io-mode!
  ;
  
  : i2c-half ( -- )  \ half-cycle timing delay for I2C
    [ifdef] I2C.DELAY  I2C.DELAY 0 do loop  [then] inline ;
  
  : SCL-ios! ( -- ) \ raise clock, wait until up
    SCL ios! 
    begin SCL io@ until
  ;
  
  : i2c-start ( -- )  \ with SCL high, change SDA from 1 to 0
    SDA ios! i2c-half SCL ios! i2c-half SDA ioc! i2c-half SCL ioc! ;
  