forked from AtlasScientific/Raspberry-Pi-sample-code
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathi2c.py
150 lines (126 loc) · 5.53 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python
import io
import sys
import fcntl
import time
import copy
import string
from AtlasI2C import (
AtlasI2C
)
def print_devices(device_list, device):
for i in device_list:
if(i == device):
print("--> " + i.get_device_info())
else:
print(" - " + i.get_device_info())
#print("")
def get_devices():
device = AtlasI2C()
device_address_list = device.list_i2c_devices()
device_list = []
for i in device_address_list:
device.set_i2c_address(i)
response = device.query("I")
try:
moduletype = response.split(",")[1]
response = device.query("name,?").split(",")[1]
except IndexError:
print(">> WARNING: device at I2C address " + str(i) + " has not been identified as an EZO device, and will not be queried")
continue
device_list.append(AtlasI2C(address = i, moduletype = moduletype, name = response))
return device_list
def print_help_text():
print('''
>> Atlas Scientific I2C sample code
>> Any commands entered are passed to the default target device via I2C except:
- Help
brings up this menu
- List
lists the available I2C circuits.
the --> indicates the target device that will receive individual commands
- xxx:[command]
sends the command to the device at I2C address xxx
and sets future communications to that address
Ex: "102:status" will send the command status to address 102
- all:[command]
sends the command to all devices
- Poll[,x.xx]
command continuously polls all devices
the optional argument [,x.xx] lets you set a polling time
where x.xx is greater than the minimum %0.2f second timeout.
by default it will poll every %0.2f seconds
>> Pressing ctrl-c will stop the polling
''' % (AtlasI2C.LONG_TIMEOUT, AtlasI2C.LONG_TIMEOUT))
def main():
print_help_text()
device_list = get_devices()
device = device_list[0]
print_devices(device_list, device)
real_raw_input = vars(__builtins__).get('raw_input', input)
while True:
user_cmd = real_raw_input(">> Enter command: ")
# show all the available devices
if user_cmd.upper().strip().startswith("LIST"):
print_devices(device_list, device)
# print the help text
elif user_cmd.upper().startswith("HELP"):
print_help_text()
# continuous polling command automatically polls the board
elif user_cmd.upper().strip().startswith("POLL"):
cmd_list = user_cmd.split(',')
if len(cmd_list) > 1:
delaytime = float(cmd_list[1])
else:
delaytime = device.long_timeout
# check for polling time being too short, change it to the minimum timeout if too short
if delaytime < device.long_timeout:
print("Polling time is shorter than timeout, setting polling time to %0.2f" % device.long_timeout)
delaytime = device.long_timeout
try:
while True:
print("-------press ctrl-c to stop the polling")
for dev in device_list:
dev.write("R")
time.sleep(delaytime)
for dev in device_list:
print(dev.read())
except KeyboardInterrupt: # catches the ctrl-c command, which breaks the loop above
print("Continuous polling stopped")
print_devices(device_list, device)
# send a command to all the available devices
elif user_cmd.upper().strip().startswith("ALL:"):
cmd_list = user_cmd.split(":")
for dev in device_list:
dev.write(cmd_list[1])
# figure out how long to wait before reading the response
timeout = device_list[0].get_command_timeout(cmd_list[1].strip())
# if we dont have a timeout, dont try to read, since it means we issued a sleep command
if(timeout):
time.sleep(timeout)
for dev in device_list:
print(dev.read())
# if not a special keyword, see if we change the address, and communicate with that device
else:
try:
cmd_list = user_cmd.split(":")
if(len(cmd_list) > 1):
addr = cmd_list[0]
# go through the devices to figure out if its available
# and swith to it if it is
switched = False
for i in device_list:
if(i.address == int(addr)):
device = i
switched = True
if(switched):
print(device.query(cmd_list[1]))
else:
print("No device found at address " + addr)
else:
# if no address change, just send the command to the device
print(device.query(user_cmd))
except IOError:
print("Query failed \n - Address may be invalid, use list command to see available addresses")
if __name__ == '__main__':
main()