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