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