def __init__(self, number=""): self.__remote = False if number: if os.path.isfile(number): self.__file = number # Simulation where number is the file used dor onewire stream emulation elif os.path.isfile(os.path.join(os.getcwd(), number)): self.__file = number # Same but in the current folder elif not isRaspberry: try: # Remote filesystem self.__file = "/sys/bus/w1/devices/%s/w1_slave" % number probe = host.Execute("open(\"%s\")" % self.__file) host.Execute("close()", probe) host.RemoveHandle(probe) self.__remote = True except: raise ValueError, "1-Wire Probe %s not detected in remote!" % number else: self.__file = "/sys/bus/w1/devices/%s/w1_slave" % number if not os.path.isfile(self.__file): raise ValueError, "1-Wire Probe %s not detected!" % number else: # Try autodetection self.__file = "" for number in os.listdir('/sys/bus/w1/devices'): if number != 'w1_bus_master1': self.__file = "/sys/bus/w1/devices/%s/w1_slave" % number break if not os.path.isfile(self.__file): raise ValueError, "1-Wire Probe not auto-detected!"
def setup(self, channel, direction, pull_up_down=None, initial=None): if direction == self.OUT: if initial == None: host.Execute("GPIO.setup(%s, %s)" % (str(channel), str(direction))) else: host.Execute("GPIO.setup(%s, %s, initial=%s)" % (str(channel), str(direction), str(initial))) else: if pull_up_down == None: host.Execute("GPIO.setup(%s, %s)" % (str(channel), str(direction))) else: host.Execute("GPIO.setup(%s, %s, pull_up_down=%s)" % (str(channel), str(direction), str(pull_up_down)))
def read(self): if self.__remote: probe = host.Execute("open(\"%s\")" % self.__file) result = host.Execute("read()", probe) host.Execute("close()", probe) host.RemoveHandle(probe) else: probe = open(self.__file) result = probe.read() probe.close() return float(result.split("\n")[1].split("=")[-1]) / 1000
def PWM(self, channel, frequency): handle = host.Execute("GPIO.PWM(%s, %s)" % (str(channel), str(frequency))) if self.__pwms.has_key(channel): pwm = self.__pwms.pop(channel) del pwm self.__pwms[channel] = Pmw(handle) return self.__pwms[channel]
def setmode(self, mode): host.Execute("GPIO.setmode(%s)" % str(mode))
def getmode(self): return host.Execute("GPIO.getmode()")
def __init__(self, address): self.__can = host.Execute("ADS1x15.ADS1115(%d)" % address)
def wait_for_edge(self, channel, edge, bouncetime=0, timeout=-1): return host.Execute("GPIO.wait_for_edge(%s, %s, %s, %s)" % (str(channel), str(edge), str(bouncetime), str(timeout)))
def event_detected(self, channel): return host.Execute("GPIO.event_detected(%s)" % str(channel))
def input(self, channel): return host.Execute("GPIO.input(%s)" % str(channel))
def __init__(self, busType): self.__bus = host.Execute("smbus.SMBus(%d)" % busType)
def close(self): host.Execute("close()", self.__bus) host.RemoveHandle(self.__bus) self.__bus = None
def write_byte_data(self, address, value, data): host.Execute("write_byte_data(%d, %d, %d)" % (address, value, data), self.__bus)
def write_byte(self, address, value): host.Execute("write_byte(%d, %d)" % (address, value), self.__bus)
def read_byte(self, address): return host.Execute("read_byte(%d)" % address, self.__bus)
def setwarnings(self, on): host.Execute("GPIO.setwarnings(%s)" % str(on))
def cleanup(self): # use *args host.Execute("GPIO.cleanup()") host.RemoveAllCallback("evtDetect")
def start(self, percent): host.Execute("start(%s)" % str(percent), self.__handle)
def output(self, channel, value): host.Execute("GPIO.output(%s, %s)" % (str(channel), str(value)))
def stop(self): host.Execute("stop()", self.__handle)
def add_event_detect(self, channel, edge, callback=None, bouncetime=50): if callback != None: cbItems = ("evtDetect%d" % channel, callback) else: cbItems == None host.Execute("GPIO.add_event_detect(%s, %s, %s, %s)" % (str(channel), str(edge), host.CB_KEYWORD, str(bouncetime)), callback=cbItems)
def ChangeFrequency(self, freq): host.Execute("ChangeFrequency(%s)" % str(freq), self.__handle)
def remove_event_detect(self, channel): host.Execute("GPIO.remove_event_detect(%s)" % str(channel)) host.RemoveCallback("evtDetect%d" % channel)
def ChangeDutyCycle(self, dc): host.Execute("ChangeDutyCycle(%s)" % str(dc), self.__handle)
def gpio_function(self, channel): return host.Execute("GPIO.gpio_function(%s)" % str(channel))
def read_adc(self, inputPin, gain): return host.Execute("read_adc(%d, %d)" % (inputPin, gain), self.__can)