def __init__(self, am2315Addr = 0x5c, i2cBusID = 1): # Set up I2C libraries self.__i2c = qI2c self.__i2cMaster = qI2c.I2CMaster(1) # Set global address var self.__addr = am2315Addr # AM2315 Registers self.regRhMSB = 0x00 self.regRhLSB = 0x01 self.regTmpMSB = 0x02 self.regTmpLSB = 0x03 self.regModelHi = 0x08 self.regModelLo = 0x09 self.regVersion = 0x0a self.regIDA = 0x0b self.regIDB = 0x0c self.regIDD = 0x0d self.regIDE = 0x0e self.regStat = 0x0f self.regUsrAMSB = 0x10 self.regUsrALSB = 0x11 self.regUsrBMSB = 0x12 self.regUsrBLSB = 0x13 # Commands self.cmdReadReg = 0x03
def __init__(self): self.i2c = smbus.SMBus(1) self.bus = i2clib.I2CMaster() self.add = 0x60 # I2C address circuit self.freq = 101.9 print("FM Radio Module TEA5767")
def getSamples(self): results = {} varDivisior = self._divisor() varMultiplier = (2.4705882 / varDivisior) / 1000 try: with i2c.I2CMaster() as bus: for channel in self._channels: (address, adcConfig) = self._adcConfig(channel) bus.transaction(i2c.writing_bytes(address, adcConfig)) h, m, l, s = bus.transaction(i2c.reading(address, 4))[0] while (s & 128): h, m, l, s = bus.transaction(i2c.reading(address, 4))[0] # shift bits to product result t = ((h & 0b00000001) << 16) | (m << 8) | l # check if positive or negative number and invert if needed if (h > 128): t = ~(0x020000 - t) results[channel] = t * varMultiplier except Exception as e: print(("There was a problem", e)) print(("address", address, isinstance(address, int))) print(("adcConfig", adcConfig, isinstance(adcConfig, int))) return results
def getFullLuminosity(self): self.enable() self.wait() with i2c.I2CMaster(self.i2cbus) as bus: read_results = bus.transaction( i2c.writing_bytes( address, self.COMMAND_BIT | self.WORD_BIT | self.REGISTER_CHAN1_LOW), i2c.reading(address, 2), i2c.writing_bytes( address, self.COMMAND_BIT | self.WORD_BIT | self.REGISTER_CHAN0_LOW), i2c.reading(address, 2)) self.disable() full = read_results[0][1] # print("---- full: %#08x" % full) full = full << 8 full += read_results[0][0] # print("---- full: %#08x" % full) full = full << 8 full += read_results[1][1] # print("---- full: %#08x" % full) full = full << 8 full += read_results[1][0] # print("---- full: %#08x" % full) return full
def __init__(self, address=0x5C, debug=False): self.channel = self.pi_i2c_bus_number() # 0 for pi Rev-1, 1 for pi Rev-2 self.address = address # Default address 0x5C self.bus = i2c.I2CMaster() # quick2wire master self.lastError = None # Contains last error string self.debug = debug # Debug flag
def readAccel(self): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.POST_ACCEL)) sleep(self.TD_POST_DATA) readValues = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_POST_DATA)) accelX = (256*readValues[0][0] + readValues[0][1]) if(accelX & 0x01<<15 != 0x00): accelX = (-(self.MAX_16_BIT+1) + accelX) accelX = accelX/1024.0 accelY = (256*readValues[0][2] + readValues[0][3]) if(accelY & 0x01<<15 != 0x00): accelY = (-(self.MAX_16_BIT+1) + accelY) accelY = accelY/1024.0 accelZ = (256*readValues[0][4] + readValues[0][5]) if(accelZ & 0x01<<15 != 0x00): accelZ = (-(self.MAX_16_BIT+1) + accelZ) accelZ = accelZ/1024.0 print("AccelX = %f" %accelX) print("AccelY = %f" %accelY) print("AccelZ = %f" %accelZ)
def resetProc(self): sleep(self.TD_DEFAULT) print("Resetting the HMC6343 processor") with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.RESET)) sleep(self.TD_RESET) print("HMC 6343 Processor Reset")
def setOrientation(self, orientation): sleep(self.TD_DEFAULT) successFlag = 1 with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, orientation)) sleep(self.TD_SET_ORIENTATION) OPMode1 = self.readOPMode1() if(orientation == self.ORIENT_LEVEL): if(OPMode1 & 0x01 == 0): successFlag = 0 elif(orientation == self.ORIENT_SIDEWAYS): if(OPMode1 & 0x02 == 0): successFlag = 0 elif(orientation == self.ORIENT_FLATFRONT): if(OPMode1 & 0x04 == 0): successFlag = 0 else: print("Orientation value not valid") if(successFlag == 0): print("Failed to set orientation") else: print("Orientation set successfully")
def selectMode(self, mode): sleep(self.TD_DEFAULT) successFlag = 1 if(mode == self.ENTER_SLEEP): enterSleep() return else: with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, mode)) sleep(self.TD_ENTER_MODE) OPMode1 = self.readOPMode1() if(mode == self.ENTER_RUN): if(OPMode1 & 0x10 == 0): successFlag = 0 elif(mode == self.ENTER_STANDBY): if(OPMode1 & 0x08 == 0): successFlag = 0 else: print("Mode value not valid") if(successFlag == 0): print("Failed to set mode") else: print("Mode set successfully")
def getTuned(self): with i2c.I2CMaster() as bus: results = bus.transaction(reading(self.address, self.numReadBytes)) elem = results[0][0] print("0 bits", int(get_bit(elem, 0)), int(get_bit(elem, 1)), int(get_bit(elem, 2)), int(get_bit(elem, 3)), int(get_bit(elem, 4)), int(get_bit(elem, 5)), int(get_bit(elem, 6)), int(get_bit(elem, 7))) elem = results[0][1] print("1 bits", int(get_bit(elem, 0)), int(get_bit(elem, 1)), int(get_bit(elem, 2)), int(get_bit(elem, 3)), int(get_bit(elem, 4)), int(get_bit(elem, 5)), int(get_bit(elem, 6)), int(get_bit(elem, 7))) elem = results[0][2] print("2 bits", int(get_bit(elem, 0)), int(get_bit(elem, 1)), int(get_bit(elem, 2)), int(get_bit(elem, 3)), int(get_bit(elem, 4)), int(get_bit(elem, 5)), int(get_bit(elem, 6)), int(get_bit(elem, 7))) elem = results[0][3] print("3 bits", int(get_bit(elem, 0)), int(get_bit(elem, 1)), int(get_bit(elem, 2)), int(get_bit(elem, 3)), int(get_bit(elem, 4)), int(get_bit(elem, 5)), int(get_bit(elem, 6)), int(get_bit(elem, 7))) return int(get_bit(elem, 7))
def values(self): i=0 errcode=0 hum = 999 temp = 999 while i <= MAXTRYS: with i2c.I2CMaster() as bus: try: bus.transaction(i2c.writing_bytes(AM2315_I2CADDR, FUNCTION_CODE_READ,*readBytes )) time.sleep(AM2315_WAITTIME) read_results = bus.transaction(i2c.reading(AM2315_I2CADDR, 8)) # print(read_results) break except: i = i+1 if i > MAXTRYS: errcode=1 else: s=bytearray(read_results[0]) crc = 256*s[7]+s[6] t = bytearray([s[0],s[1],s[2],s[3],s[4],s[5]]) c = self.crc16(t) if crc != c: errcode=2 else: hum = (256*s[2]+s[3])/10 temp = (256*s[4]+s[5])/10 return hum,temp,errcode
def setTiming(self, timing): self.timing = timing with i2c.I2CMaster(self.i2cbus) as bus: bus.transaction( i2c.writing_bytes(self.address, self.COMMAND_BIT | self.REGISTER_TIMING, self.gain | self.timing))
def initSensor(self): """ initalizes the first channel if the MAX1164 device in single endded mode with vdd as ref """ with i2c.I2CMaster(1) as bus: bus.transaction(i2c.writing_bytes(self.address, 0x8a )) bus.transaction(i2c.writing_bytes(self.address, 0x01 ))
def readTilt(self): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.POST_TILT)) sleep(self.TD_POST_DATA) readValues = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_POST_DATA)) pitch = (256*readValues[0][0] + readValues[0][1]) if(pitch & 0x01<<15 != 0x00): pitch = (-(self.MAX_16_BIT+1) + pitch) pitch = pitch/10.0 roll = (256*readValues[0][2] + readValues[0][3]) if(roll & 0x01<<15 != 0x00): roll = (-(self.MAX_16_BIT+1) + roll) roll = roll/10.0 temp = (256*readValues[0][4] + readValues[0][5]) if(temp & 0x01<<15 != 0x00): temp = (-(self.MAX_16_BIT+1) + temp) temp = temp/10.0 print("Pitch = %f" %pitch) print("Roll = %f" %roll) print("Temperature = %f" %temp)
def readMag(self): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.POST_MAG)) sleep(self.TD_POST_DATA) readValues = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_POST_DATA)) magX = (256*readValues[0][0] + readValues[0][1]) if(magX & 0x01<<15 != 0x00): magX = (-(self.MAX_16_BIT+1) + magX) magX = magX/10.0 magY = (256*readValues[0][2] + readValues[0][3]) if(magY & 0x01<<15 != 0x00): magY = (-(self.MAX_16_BIT+1) + magY) magY = magY/10.0 magZ = (256*readValues[0][4] + readValues[0][5]) if(magZ & 0x01<<15 != 0x00): magZ = (-(self.MAX_16_BIT+1) + magZ) magZ = magZ/10.0 print("MagX = %f" %magX) print("MagY = %f" %magY) print("MagZ = %f" %magZ)
def read_card(self): logging.info("Fetching card id from %0X" % self.i2c_address) # Fetch the card ID by sending 1/1 to the SL030 card reader with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(0x50, 0x1, 0x1)) time.sleep(0.1) # SL030 requires this time to respond reliably read_results = bus.transaction(i2c.reading(0x50, 10)) returned_len = read_results[0][0] status = read_results[0][2] if returned_len == 0: logging.info("Error fetching from card reader") return (None) if status == 0x1: # No Tag logging.info("No tag detected") return (None) else: # Format the read card ID as a hex string card_as_hex = [] for x in range(3, returned_len): card_as_hex.append('{1:02x}'.format( x, read_results[0][x]).upper()) logging.debug("Card presented: " % card_as_hex) return (''.join(card_as_hex))
def readEEPROM(self, reg): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.READ_EEPROM, reg)) sleep(self.TD_READ_EEPROM) readValue = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_EEPROM_REG)) print("Value at Reg %02x = %d" %(reg,readValue[0][0])) print("Value at Reg %02x in hex = 0x%02x" %(reg,readValue[0][0]))
def __init__(self, busnr=1, address=0x20): self.iodir_register = 0x00 self.gpio_register = 0x0A self.addr = address self.bus = self.bus = i2c.I2CMaster(busnr) self.ioDir = self.__getReg(self.iodir_register) self.ioData = self.__getReg(self.gpio_register) return
def send(sendStr): byteList = [] for i in sendStr: byteList.append(ord(i)) byteList.append(0x0A) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing(address, bytes(byteList)))
def getTempRaw(self): """ gets the raw hex values from the i2c device """ with i2c.I2CMaster(1) as bus: x = (bus.transaction(i2c.reading(self.address, 2))) h = int(binascii.hexlify(x[0]), 16) #mask for 10 bit ADC return (h & 0b0000001111111111)
def readOPMode1(self): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.POST_OPMODE1)) sleep(self.TD_POST_DATA) readValues = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_EEPROM_REG)) print("Value of OpMode1= 0x%02x" %readValues[0][0]) return readValues[0][0]
def change_output(self): try: with i2c.I2CMaster(1) as bus: bus.transaction( i2c.writing(self.__address, bytearray([2, self.__bit_register[0], self.__bit_register[1]]))) return self.__bit_register except IOError: # print("Err: No hybridIO detected") return -1
def __init__(self): self.logger = logging.getLogger('sensor(MS5803)') self.logger.setLevel(logging.ERROR) self.bus = i2c.I2CMaster(1) self.address = 0x77 self.C = [0 for i in range(7)] self.D = [0 for i in range(3)] for x in range(1, 7): self.C[x] = self.__readPROM(x)
def check_mcp23017_loopback(chip_class, checker): with i2c.I2CMaster() as master: chip = chip_class(master, 0x20) chip.reset() checker(chip, Topology) chip.reset() checker(chip, inverse(Topology))
def __init__(self, address=0x6E, bus=None): # This is a kind of trick so we can generate the documentation # on other platforms try: self.bus = bus or i2c.I2CMaster() except FileNotFoundError: self.bus = None self.address = address
def calibrateSensor(self): sleep(self.TD_DEFAULT) print("Entering Calibration Mode") with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.ENTER_CAL)) sleep(self.TD_ENTER_CAL) input("Press Enter to exit calibration mode") bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.EXIT_CAL)) sleep(self.TD_EXIT_CAL) print("Exited Calibration Mode")
def update(self): try: with i2c.I2CMaster(1) as bus: data = bus.transaction( i2c.writing_bytes(self.__address, 0), i2c.reading(self.__address, 2))[0] self.__bit_register = data return self.__bit_register except IOError: # print("Err: No hybridIO detected") return -1
def foundSensor(self): with i2c.I2CMaster(self.i2cbus) as bus: read_results = bus.transaction( i2c.writing_bytes(self.address, self.REGISTER_ID), i2c.reading(self.address, 1)) state = read_results[0][0] # print("%02x" % state) if state == 0x0A: return True return False
def exitSleep(self): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.EXIT_SLEEP)) sleep(self.TD_EXIT_SLEEP) OPMode1 = self.readOPMode1() if(OPMode1 & 0x18 == 0): print("Failed to exit sleep mode") else: print("Exited sleep mode")
def __init__(self, bus="", address="", warnings=1): if (bus == ""): # create connection self.bus = i2c.I2CMaster(1) else: self.bus = bus if (address == ""): self.address = 0x04 else: self.address = address self.warnings = warnings self.reset_config() # reset controller and vars