def __init__(self, slave=None, family=0, extra=None): Bus.__init__(self, "ONEWIRE", "/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves", os.O_RDONLY) if self.fd > 0: os.close(self.fd) self.fd = 0 self.family = family if slave != None: addr = slave.split("-") if len(addr) == 1: self.slave = "%02x-%s" % (family, slave) elif len(addr) == 2: prefix = int(addr[0], 16) if family > 0 and family != prefix: raise Exception( "1-Wire slave address %s does not match family %02x" % (slave, family)) self.slave = slave else: devices = self.deviceList() if len(devices) == 0: raise Exception("No device match family %02x" % family) self.slave = devices[0] loadExtraModule(extra)
def __init__(self, chip=0, mode=0, bits=8, speed=0): Bus.__init__(self, "SPI", "/dev/spidev0.%d" % chip) self.chip = chip val8 = array.array('B', [0]) val8[0] = mode if fcntl.ioctl(self.fd, SPI_IOC_WR_MODE, val8): raise Exception("Cannot write SPI Mode") if fcntl.ioctl(self.fd, SPI_IOC_RD_MODE, val8): raise Exception("Cannot read SPI Mode") self.mode = struct.unpack('B', val8)[0] assert(self.mode == mode) val8[0] = bits if fcntl.ioctl(self.fd, SPI_IOC_WR_BITS_PER_WORD, val8): raise Exception("Cannot write SPI Bits per word") if fcntl.ioctl(self.fd, SPI_IOC_RD_BITS_PER_WORD, val8): raise Exception("Cannot read SPI Bits per word") self.bits = struct.unpack('B', val8)[0] assert(self.bits == bits) val32 = array.array('I', [0]) if speed > 0: val32[0] = speed if fcntl.ioctl(self.fd, SPI_IOC_WR_MAX_SPEED_HZ, val32): raise Exception("Cannot write SPI Max speed") if fcntl.ioctl(self.fd, SPI_IOC_RD_MAX_SPEED_HZ, val32): raise Exception("Cannot read SPI Max speed") self.speed = struct.unpack('I', val32)[0] assert((self.speed == speed) or (speed == 0))
def __init__(self, slave): self.channel = 0 if BOARD_REVISION > 1: self.channel = 1 Bus.__init__(self, "I2C", "/dev/i2c-%d" % self.channel) self.slave = slave if fcntl.ioctl(self.fd, I2C_SLAVE, self.slave): raise Exception("Error binding I2C slave 0x%02X" % self.slave)
def __init__(self, device="/dev/ttyAMA0", baudrate=9600): if not device.startswith("/dev/"): device = "/dev/%s" % device if isinstance(baudrate, str): baudrate = int(baudrate) aname = "B%d" % baudrate if not hasattr(termios, aname): raise Exception("Unsupported baudrate") self.baudrate = baudrate Bus.__init__(self, "UART", device, os.O_RDWR | os.O_NOCTTY) fcntl.fcntl(self.fd, fcntl.F_SETFL, os.O_NDELAY) #backup = termios.tcgetattr(self.fd) options = termios.tcgetattr(self.fd) # iflag options[0] = 0 # oflag options[1] = 0 # cflag options[2] |= (termios.CLOCAL | termios.CREAD) options[2] &= ~termios.PARENB options[2] &= ~termios.CSTOPB options[2] &= ~termios.CSIZE options[2] |= termios.CS8 # lflag options[3] = 0 speed = getattr(termios, aname) # input speed options[4] = speed # output speed options[5] = speed termios.tcsetattr(self.fd, termios.TCSADRAIN, options)
def __init__(self, slave=None, family=0, extra=None): Bus.__init__(self, "ONEWIRE", "/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves", os.O_RDONLY) if self.fd > 0: os.close(self.fd) self.fd = 0 self.family = family if slave != None: addr = slave.split("-") if len(addr) == 1: self.slave = "%02x-%s" % (family, slave) elif len(addr) == 2: prefix = int(addr[0], 16) if family > 0 and family != prefix: raise Exception("1-Wire slave address %s does not match family %02x" % (slave, family)) self.slave = slave else: devices = self.deviceList() if len(devices) == 0: raise Exception("No device match family %02x" % family) self.slave = devices[0] loadExtraModule(extra)
def write(self, data): Bus.write(self, data)
def read(self): if self.available() > 0: return Bus.read(self, self.available()).decode() return ""