-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
62 lines (52 loc) · 2.64 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import board
from i2cslave import I2CSlave
# fake registers (think of these like real ones)
# this simulation only has 1 register with 16 parts
regs = [0] * 16
# next register to read from after i2cset
index = 0
## DEBUG TOGGLE - output on REPL
DEBUG = True
with I2CSlave(board.SCL, board.SDA, (0x40, 0x42)) as slave:
while True:
r = slave.request()
if not r:
# Maybe do some housekeeping
continue
with r: # Closes the transfer if necessary by sending a NACK or feeding the master dummy bytes
if DEBUG: print("BEGIN request")
# choose a I2C Address. We can simulate as many requests as we like for this.
if r.address == 0x40:
if DEBUG: print("ADDRESS 0x40")
# This is used by i2cset commands (or similar) and sets which register is next to be read from.
# No write requests can take place in this section.
if not r.is_read: # Master write which is Slave read
if DEBUG: print("slave read")
# Read in first byte - the 'fake' register location.
b = r.read(1)
if not b or b[0] > 15:
if DEBUG: print("empty byte")
continue
# set the 'fake' register location we want to right to next.
index = b[0]
# read in second byte of data. This corresponds to the specific data we want to
# put into the 'fake' register.
b = r.read(1)
if b:
if DEBUG: print("data: ", b[0])
regs[index] = b[0]
# This is used by i2cget commands (or similar) and reads back the register that is set as
# index. No read transactions can take place in this section.
elif r.is_restart: # Combined transfer: This is the Master read message
if DEBUG: print("combined transfer")
n = r.write(bytes([regs[index]]))
#else:
# A read transfer is not supported in this example
# If the Master tries, it will get 0xff byte(s) by the ctx manager (r.close())
elif r.address == 0x42:
if DEBUG: print("ADDRESS 0x42")
if not r.is_read:
b = r.read(1)
if b and b[0] == 0xde:
# do something
pass