Rev 4944 Rev 4945
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  
-   8 from mlabutils import ejson
-   9  
-   10 parser = ejson.Parser()
-   11  
-   12 #### Script Arguments ###############################################
-   13  
-   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)
-   18  
-   19 value = parser.parse_file(sys.argv[1])
-   20 path = value['data_path']
-   21 stationName = value['origin']
-   22  
7 def init(): 23 def main():
-   24  
8 global h 25 while True:
-   26  
9 print "Opening device" 27 print "Opening device"
10 h = hid.device() 28 h = hid.device()
11 h.open(0x10C4, 0xEA90) 29 h.open(0x10C4, 0xEA90)
12   30  
13 print "Manufacturer: %s" % h.get_manufacturer_string() 31 print "Manufacturer: %s" % h.get_manufacturer_string()
Line 15... Line 33...
15 print "Serial No: %s" % h.get_serial_number_string() 33 print "Serial No: %s" % h.get_serial_number_string()
16   34  
17 h.write([0x02, 0b00000110, 0x00, 0x00, 0x00]) # setup io pin direction 35 h.write([0x02, 0b00000110, 0x00, 0x00, 0x00]) # setup io pin direction
18 sleep( 1.00 ) 36 sleep( 1.00 )
19   37  
20   -  
21 def light(timea): -  
22 h.write([0x04, 0xFF, 0xFF]) -  
23 time.sleep(timea) -  
24   -  
25 def dark(timea): -  
26 h.write([0x04, 0x00, 0xFF]) -  
27 time.sleep(timea) -  
28   -  
29 def blik(): -  
30 dark(0.8) -  
31 light(1) -  
32   -  
33 def check_button(): -  
34 response = h.get_feature_report(0x03,2) 38 response = h.get_feature_report(0x03,2)
35 return response[1] 39 previous_inputs = response[1]
36   40  
37 def main(): -  
38 init() -  
39 while True: -  
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__":