def main(): parser = argparse.ArgumentParser(description='read sensor values from a Phidgets device') group = parser.add_mutually_exclusive_group(required=True) group.add_argument('-d', '--digital', metavar='<input id>', type=int, help='read from a digital input') group.add_argument('-a', '--analog', metavar='<sensor id>', type=int, help='read from an analog sensor') parser.add_argument('-t', '--temperature-sensor', action='store_const', const=True, default=False, help='device is a temperature sensor, output in degrees celcius. must be reading an analog sensor') parser.add_argument('-m', '--precision-light-sensor-multiplier', metavar='<multiplier>', type=float, help='\'m\' value from the underside of the precision light sensor. must be reading an analog sensor') parser.add_argument('-b', '--precision-light-sensor-boost', metavar='<boost>', type=float, help='\'b\' value from the underside of the precision light sensor. must be reading an analog sensor') cli_args = parser.parse_args() try: device = InterfaceKit() except RuntimeError as e: print("Runtime Error: %s" % e.message) exit(2) try: device.openPhidget() device.waitForAttach(1000) if cli_args.digital != None: if cli_args.digital < device.getInputCount(): print device.getInputState(cli_args.digital) else: parser.print_usage() print('%s: error: input id out of range (maximum is %d)' % (sys.argv[0], device.getInputCount()-1)) elif cli_args.analog != None: if cli_args.analog < device.getSensorCount(): if cli_args.temperature_sensor == True: print (float(device.getSensorValue(cli_args.analog)) * 0.2222) - 61.1111 elif (cli_args.precision_light_sensor_multiplier == None) != (cli_args.precision_light_sensor_boost == None): parser.print_usage() print('%s: error: you must supply both precision light sensor arguments') elif cli_args.precision_light_sensor_multiplier != None and cli_args.precision_light_sensor_boost != None: print (cli_args.precision_light_sensor_multiplier * float(device.getSensorValue(cli_args.analog)) + cli_args.precision_light_sensor_boost) else: print device.getSensorValue(cli_args.analog) else: parser.print_usage() print('%s: error: sensor id out of range (maximum is %d)' % (sys.argv[0], device.getSensorCount()-1)) device.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: device.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1) exit(0)
class PhidgetTextLCD(object): """ A class modelled after a Phidget InterfaceKit with an integrated LCD. It contains a list of objects, which are subclasses of Sensor object. Attributes: analog_sensors: a list containing objects of one of subclasses of Sensor data_dir: a string of directory path to save sensor reading data in """ def __init__(self, analog_sensors, data_dir): """Initializes the members.""" self.__interfaceKit = InterfaceKit() self.__textLCD = TextLCD() self.__data_dir = data_dir # instantiate classes according to actual sensor port configuration self.sensors = [] for sensor in analog_sensors: a_sensor = self.__Factory(sensor) self.sensors.append(a_sensor) try: # Interface Kit self.__interfaceKit.openPhidget() self.__interfaceKit.waitForAttach(10000) print "%s (serial: %d) attached!" % ( self.__interfaceKit.getDeviceName(), self.__interfaceKit.getSerialNum()) # get initial value from each sensor i = 0 for sensor in self.sensors: #print sensor.product_name #print sensor.value sensor.value = self.__interfaceKit.getSensorValue(i) i += 1 self.__interfaceKit.setOnSensorChangeHandler(self.__SensorChanged) # Text LCD self.__textLCD.openPhidget() self.__textLCD.waitForAttach(10000) print "%s attached!" % (self.__textLCD.getDeviceName()) self.__textLCD.setCursorBlink(False) except PhidgetException, e: print "Phidget Exception %i: %s" % (e.code, e.message) sys.exit(1)
try: ifk.waitForAttach(10000) except PhidgetException as e: errors = errors + "<h5>Phidget Exception on Attach: " + e.details + "</h5>\n" errors = errors + "<h5>If Phidget is 'Not Physically Attached' it may be in use</h5>\n" print('<table><tr><td>') # Generating the Sensor Value Table print('<b>Analog Sensor Values</b>') print('<table border="1"><tr><td>Sensor Number</td><td>Sensor Value</td></tr>\n') for i in range (0,8): try: print ('<tr><td>'+str(i)+'</td><td>'+str(ifk.getSensorValue(i))+'</td></tr>\n') except: pint('<tr><td>'+str(i)+'</td><td>Could not Read Censor Value </td></tr>\n') print('</table></td><td>') #Print the Digital Input values print('<b>Digital Input Values</b>') print('<table border="1"><tr><td>Input Number</td><td>Input Value</td></tr>\n') for i in range (0,8): try: print ('<tr><td>'+str(i)+'</td><td>'+str(ifk.getInputState(i))+'</td></tr>\n') except: pint('<tr><td>'+str(i)+'</td><td>Could not Read Digital Input State </td></tr>\n') print('</table></td><td>')
ser = serial.Serial('/dev/ttyUSB1', 9600, timeout=1) except: print "not connected" try: #Your program Code here interfaceKit.openPhidget() interfaceKit.waitForAttach(10000) print("%d attached!" % (interfaceKit.getSerialNum())) xold = 0 yold = 0 zold = 0 pold = 0 cmd = "\x82" ser.write(cmd) while True: x1 = interfaceKit.getSensorValue(0) y1 = interfaceKit.getSensorValue(1) z1 = interfaceKit.getSensorValue(2) p1 = interfaceKit.getSensorValue(3) sleep(0.2) x3 = interfaceKit.getSensorValue(0) y3 = interfaceKit.getSensorValue(1) z3 = interfaceKit.getSensorValue(2) p3 = interfaceKit.getSensorValue(3) if (-20 < (int(x3) - int(x1)) < 20 and 450 < z1 < 550): print(x1, y1, p1, z1) x2 = int(round((int(x1) - 500) / 7, 0)) if (-10 < x2 < 10 and x2 != xold): cmd1 = "\xaa\x04\x06" cmd2 = "\xaa\x04\x07" ser.write(cmd1)