1 |
#!/usr/bin/python |
1 |
#!/usr/bin/python |
2 |
import hid |
2 |
import hid |
3 |
import time |
3 |
import time |
4 |
import datetime |
4 |
import datetime |
5 |
from time import sleep |
5 |
from time import sleep |
6 |
import os, sys |
6 |
import os, sys |
7 |
|
7 |
|
8 |
from mlabutils import ejson |
8 |
from mlabutils import ejson |
9 |
|
9 |
|
10 |
parser = ejson.Parser() |
10 |
parser = ejson.Parser() |
11 |
|
11 |
|
12 |
#### Script Arguments ############################################### |
12 |
#### Script Arguments ############################################### |
13 |
|
13 |
|
14 |
if len(sys.argv) != 2: |
14 |
if len(sys.argv) != 2: |
15 |
sys.stderr.write("Invalid number of arguments.\n") |
15 |
sys.stderr.write("Invalid number of arguments.\n") |
16 |
sys.stderr.write("Usage: %s CONFIG_FILE\n" % (sys.argv[0], )) |
16 |
sys.stderr.write("Usage: %s CONFIG_FILE\n" % (sys.argv[0], )) |
17 |
sys.exit(1) |
17 |
sys.exit(1) |
18 |
|
18 |
|
19 |
value = parser.parse_file(sys.argv[1]) |
19 |
value = parser.parse_file(sys.argv[1]) |
20 |
path = value['data_path'] |
20 |
path = value['data_path'] |
21 |
stationName = value['origin'] |
21 |
stationName = value['origin'] |
22 |
|
22 |
|
23 |
def main(): |
23 |
def main(): |
24 |
|
24 |
|
25 |
while True: |
25 |
while True: |
26 |
|
26 |
|
27 |
print "Opening device" |
27 |
print "Opening device" |
28 |
h = hid.device() |
28 |
h = hid.device() |
29 |
h.open(0x10C4, 0xEA90) |
29 |
h.open(0x10C4, 0xEA90) |
30 |
|
30 |
|
31 |
print "Manufacturer: %s" % h.get_manufacturer_string() |
31 |
print "Manufacturer: %s" % h.get_manufacturer_string() |
32 |
print "Product: %s" % h.get_product_string() |
32 |
print "Product: %s" % h.get_product_string() |
33 |
print "Serial No: %s" % h.get_serial_number_string() |
33 |
print "Serial No: %s" % h.get_serial_number_string() |
34 |
|
34 |
|
35 |
h.write([0x02, 0b00000110, 0x00, 0x00, 0x00]) # setup io pin direction |
35 |
h.write([0x02, 0b00000110, 0x00, 0x00, 0x00]) # setup io pin direction |
36 |
sleep( 1.00 ) |
36 |
sleep( 1.00 ) |
37 |
|
37 |
|
38 |
response = h.get_feature_report(0x03,2) |
38 |
response = h.get_feature_report(0x03,2) |
39 |
previous_inputs = response[1] |
39 |
previous_inputs = response[1] |
40 |
|
40 |
|
41 |
try: |
41 |
try: |
42 |
|
42 |
|
43 |
while True: |
43 |
while True: |
44 |
response = h.get_feature_report(0x03,2) |
44 |
response = h.get_feature_report(0x03,2) |
45 |
inputs = response[1] |
45 |
inputs = response[1] |
46 |
|
46 |
|
47 |
print bin(inputs) |
47 |
print bin(inputs) |
48 |
|
48 |
|
49 |
now = datetime.datetime.now() |
49 |
now = datetime.datetime.now() |
50 |
filename = path + time.strftime("%Y%m%d%H", time.gmtime())+"0000_"+stationName+"_freq.csv" |
50 |
filename = path + time.strftime("%Y%m%d%H", time.gmtime())+"0000_"+stationName+"_meta.csv" |
51 |
|
51 |
|
52 |
if not os.path.exists(filename): |
52 |
if not os.path.exists(filename): |
53 |
with open(filename, "a") as f: |
53 |
with open(filename, "a") as f: |
54 |
f.write('#timestamp,IO_state \n') |
54 |
f.write('#timestamp,IO_state \n') |
55 |
f.write("%.1f,%s\n" % (time.time(), hex(inputs))) |
55 |
f.write("%.1f,%s\n" % (time.time(), hex(inputs))) |
56 |
|
56 |
|
57 |
if (previous_inputs != inputs): |
57 |
if (previous_inputs != inputs): |
58 |
with open(filename, "a") as f: |
58 |
with open(filename, "a") as f: |
59 |
f.write("%.1f,%s\n" % (time.time(), hex(inputs))) |
59 |
f.write("%.1f,%s\n" % (time.time(), hex(inputs))) |
60 |
previous_inputs = inputs |
60 |
previous_inputs = inputs |
61 |
|
61 |
|
62 |
if not (inputs & 0b00000001) and (0b00000001): |
62 |
if not (inputs & 0b00000001) and (0b00000001): |
63 |
h.write([0x04, 0x00, 0xFF]) |
63 |
h.write([0x04, 0x00, 0xFF]) |
64 |
time.sleep(0.8) |
64 |
time.sleep(0.8) |
65 |
h.write([0x04, 0xFF, 0xFF]) |
65 |
h.write([0x04, 0xFF, 0xFF]) |
66 |
time.sleep(0.5) |
66 |
time.sleep(0.5) |
67 |
|
67 |
|
68 |
time.sleep(0.5) |
68 |
time.sleep(0.5) |
69 |
|
69 |
|
70 |
except IOError, ex: |
70 |
except IOError, ex: |
71 |
print ex |
71 |
print ex |
72 |
|
72 |
|
73 |
except KeyboardInterrupt: |
73 |
except KeyboardInterrupt: |
74 |
print "Closing device" |
74 |
print "Closing device" |
75 |
h.close() |
75 |
h.close() |
76 |
exit() |
76 |
exit() |
77 |
|
77 |
|
78 |
print "Done" |
78 |
print "Done" |
79 |
|
79 |
|
80 |
|
80 |
|
81 |
if __name__ == "__main__": |
81 |
if __name__ == "__main__": |
82 |
main() |
82 |
main() |