-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.py
76 lines (59 loc) · 1.79 KB
/
i2c.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from time import sleep
import pigpio
# The variable which stores i2c "register" values.
# Update this with current values and information in the code that calls this library.
DATAOUT={
'h':0x1680,
'p':0xb40,
'default':None
}
# Set default I2C address
I2C_ADDR=0x42
# Connect GPIO
pi=pigpio.pi()
if not pi.connected:
exit()
def initialize(addr=I2C_ADDR):
global clientEvent,address
address=addr
# When pigpio has an interrupt on EVENT_BSC, execute the i2c function.
clientEvent = pi.event_callback(pigpio.EVENT_BSC, i2c)
# Configure as I2C client at specified address, default 0x42.
pi.bsc_i2c(address)
print("initialized i2c client")
def stop():
clientEvent.cancel() # Cancel the interrupt callback.
pi.bsc_i2c(0) # Disable peripheral.
pi.stop() # Disable pigpio.
def i2c(id, tick):
global pi, address
status, length, data = pi.bsc_i2c(address) # Grab the data from BSC.
if length:
#print(data[0])
#print(type(data[0]))
#print(str(data[0]))
print(chr(data[0]))
print(DATAOUT.get(chr(data[0])))
# Set tx data to rx matched register in DATAOUT using rx data[0]
data=DATAOUT.get(chr(data[0]))
print("sent={} FR={} received={} [{}]".format(status>>16, status&0xfff,length,data))
# If data[0] didn't match a register value, DATAOUT returns None.
# Therefore, if (data) only executes if we have a matched register value in DATAOUT.
if (data):
# Send the tx data to BSC buffer.
status, length, data = pi.bsc_i2c(address,"{}*".format(data))
else:
# If no register match, clear the BSC buffer.
status,length,data = pi.bsc_i2c(address,"{}*")
#if __name__ == "__main__":
def main():
# One-time setup.
initialize()
# Run for awhile. The bus is interrupt driven so nothing is happening here.
i=0
while(i<500):
sleep(1)
i+=1
# Stop the bus.
stop()
main()