An obvious application is to enable a Pyboard to communicate with an ESP8266 while leaving the UART free for debugging via the REPL (or for other purposes). There is also scope for porting the master end of the interface to hardware such as the Raspberry Pi.
The Pyboard I2C slave code performs blocking reads and writes. You can't polish a ... oops ... rewind.
You can't make a silk purse out of a sow's ear. So some blocking occurs. Much effort went into into synchronisation to ensure that this is typically in single digits of milliseconds. The following is a usage example where the two participants exchange Python objects serialised with ujson. ESP8266 code:
Code: Select all
import uasyncio as asyncio
from machine import Pin, I2C
import asi2c
import ujson
i2c = I2C(scl=Pin(0),sda=Pin(2)) # software I2C
syn = Pin(5)
ack = Pin(4)
chan = asi2c.Responder(i2c, syn, ack)
async def receiver():
sreader = asyncio.StreamReader(chan)
while True:
res = await sreader.readline()
print('Received', ujson.loads(res))
async def sender():
swriter = asyncio.StreamWriter(chan, {})
txdata = [0, 0]
while True:
await swriter.awrite(''.join((ujson.dumps(txdata), '\n')))
txdata[1] += 1
await asyncio.sleep_ms(1500)
loop = asyncio.get_event_loop()
loop.create_task(receiver())
loop.create_task(sender())
loop.run_forever()