├── README.md └── irmcli.py /README.md: -------------------------------------------------------------------------------- 1 | ## Welcome to irMagician CLI 2 | 3 | Command Line Utility for irMagician 4 | 5 | ## Getting Started 6 | 7 | 1. Install Python(>2.7) pyserial: 8 | 9 | $ sudo apt-get install python-pip 10 | $ sudo pip install pyserial 11 | 12 | 2. Connect irMagician to your computer: 13 | 14 | 3. Start irmcli.py: 15 | 16 | $ sudo python irmcli.py -h 17 | 18 | ## License 19 | 20 | Released under the [MIT License](http://www.opensource.org/licenses/MIT). 21 | 22 | -------------------------------------------------------------------------------- /irmcli.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import sys 4 | import serial 5 | import time 6 | import json 7 | import argparse 8 | import os 9 | 10 | here = os.path.abspath(os.path.dirname(__file__)) 11 | ir_serial = serial.Serial("/dev/ttyACM0", 9600, timeout = 1) 12 | #ir_serial = serial.Serial("/dev/tty.usbmodem01231", 9600, timeout = 1) 13 | 14 | def captureIR(path): 15 | print "Capturing IR..." 16 | ir_serial.write("c\r\n") 17 | time.sleep(3.0) 18 | msg = ir_serial.readline() 19 | print msg 20 | if path and not 'Time Out' in msg: 21 | saveIR(path) 22 | 23 | def playIR(path): 24 | if path and os.path.isfile(path): 25 | print ("Playing IR with %s ..." % path) 26 | f = open(path) 27 | data = json.load(f) 28 | f.close() 29 | recNumber = len(data['data']) 30 | rawX = data['data'] 31 | 32 | ir_serial.write("n,%d\r\n" % recNumber) 33 | ir_serial.readline() 34 | 35 | postScale = data['postscale'] 36 | ir_serial.write("k,%d\r\n" % postScale) 37 | #time.sleep(1.0) 38 | msg = ir_serial.readline() 39 | #print msg 40 | 41 | for n in range(recNumber): 42 | bank = n / 64 43 | pos = n % 64 44 | if (pos == 0): 45 | ir_serial.write("b,%d\r\n" % bank) 46 | 47 | ir_serial.write("w,%d,%d\n\r" % (pos, rawX[n])) 48 | 49 | ir_serial.write("p\r\n") 50 | msg = ir_serial.readline() 51 | print msg 52 | #ir_serial.close() 53 | else: 54 | print "Playing IR..." 55 | ir_serial.write("p\r\n") 56 | time.sleep(1.0) 57 | msg = ir_serial.readline() 58 | print msg 59 | 60 | def saveIR(path): 61 | print ("Saving IR data to %s ..." % path) 62 | rawX = [] 63 | ir_serial.write("I,1\r\n") 64 | time.sleep(1.0) 65 | recNumberStr = ir_serial.readline() 66 | recNumber = int(recNumberStr, 16) 67 | 68 | ir_serial.write("I,6\r\n") 69 | time.sleep(1.0) 70 | postScaleStr = ir_serial.readline() 71 | postScale = int(postScaleStr, 10) 72 | 73 | #for n in range(640): 74 | for n in range(recNumber): 75 | bank = n / 64 76 | pos = n % 64 77 | if (pos == 0): 78 | ir_serial.write("b,%d\r\n" % bank) 79 | 80 | ir_serial.write("d,%d\n\r" % pos) 81 | xStr = ir_serial.read(3) 82 | xData = int(xStr, 16) 83 | rawX.append(xData) 84 | 85 | data = {'format':'raw', 'freq':38, 'data':rawX, 'postscale':postScale} 86 | 87 | f = open(path, 'w') 88 | json.dump(data, f) 89 | f.close() 90 | print "Done !" 91 | 92 | 93 | def measureTemperature(): 94 | #print "Sending T command..." 95 | ir_serial.write("T\r\n") 96 | 97 | #print "Reading raw temperature..." 98 | raw = ir_serial.readline() 99 | 100 | #print "Reading command status..." 101 | status = ir_serial.readline().rstrip() 102 | 103 | celsiusTemp = None 104 | try: 105 | celsiusTemp = ((5.0 / 1024.0 * float(raw)) - 0.4) / 0.01953 106 | print "Temperature: %s" % "{:4.1f}".format(celsiusTemp) 107 | except (ValueError, TypeError): 108 | print "TemperatureExcetion: raw => %s, status => %s" % (raw, status) 109 | 110 | 111 | def printFirmwareVer(): 112 | ir_serial.write("V\r\n") 113 | print ir_serial.readline().rstrip() 114 | ir_serial.readline() 115 | 116 | 117 | if __name__ == "__main__": 118 | # parse options 119 | parser = argparse.ArgumentParser(description='irMagician CLI utility.') 120 | parser.add_argument('-c', '--capture', action="store_true", dest="cap", help="capture IR data", default=False) 121 | parser.add_argument('-p', '--play', action="store_true", dest="play", help="play IR data", default=False) 122 | parser.add_argument('-s', '--save', action="store_true", dest="save", help="save IR data", default=False) 123 | parser.add_argument('-f', '--file', action="store", dest="file", help="IR data file (json)", default=False) 124 | parser.add_argument('-t', '--temperature', action="store_true", dest="temperature", help="measure ambient irMagicianT temperature in degrees Celsius", default=False) 125 | parser.add_argument('-v', '--version', action="store_true", dest="version", help="show firmware version", default=False) 126 | 127 | args = parser.parse_args() 128 | 129 | if args.version: 130 | printFirmwareVer() 131 | 132 | if args.temperature: 133 | measureTemperature() 134 | 135 | if args.play: 136 | playIR(args.file) 137 | 138 | if args.save and args.file: 139 | saveIR(args.file) 140 | 141 | if args.cap: 142 | captureIR(args.file) 143 | 144 | # release resources 145 | ir_serial.close() 146 | 147 | --------------------------------------------------------------------------------