def ADS1115_GetVal(): i2c.open(ADS1115_ADDRESS) i2c.write([0x00]) (MSB, LSB) = i2c.read(2) i2c.close() Vel = (MSB << 8) + LSB if (Vel > 32767): Vel = 0 return Vel
def set_lidar_motor(speed, direction): i2c.open(ADR_slave) # The slave device address # set speed i2c.write([REG_speed]) # speed register, i2c.write([speed]) # speed a i2c.write([speed]) # speed b #i2c.write([0xaa, direction, 0x01]) #direction register, direction, pa$ i2c.close() #End communication with slave device
def readBmp180Id(addr=DEVICE): #Slave Device adress i2c.open(addr) # Register Address REG_ID=0xD0 #Set address at 0xD0 register i2c.write( [REG_ID] ) ( chip_id, chip_version ) = i2c.read(2) i2c.close() return (chip_id, chip_version)
def ReadBlock(self, address, key, value): """Reads a block from olimex mod-io. Args: key: interger, an address where to read data from. length: integer, how much data to read. """ try: i2c.open(address) i2c.write(key) value = i2c.read(value) i2c.close() return value except IOError: raise DeviceNotFoundException("Could not communicate with device")
def Write(self, address, payload): """Sends a request to olimex mod-io. Args: key: integer, an address where to wite data. value: """ data = [] for val in payload: data.append(val) print(data) try: i2c.open(address) i2c.write(data) i2c.close() except IOError: raise DeviceNotFoundException("Could not communicate with device")
def ReadADC(Address, channel, ref): config = 0b1000000111000011 config |= (channel << 12) config |= (ref << 9) lectura = 0 aux = 0x80 configH = (config >> 8) configL = config & 0x00FF i2c.init("/dev/i2c-0") i2c.open(Address) #Open ADC I2C i2c.write([0x01, configH, configL]) #Write to Adddress 1 (config register) i2c.close() while (lectura != aux): i2c.open(Address) high = i2c.read(2) i2c.close() lectura = high[0] & aux i2c.open(Address) i2c.write([0x00]) i2c.close() i2c.open(Address) read = i2c.read(2) i2c.close() value = (read[0] << 8) + read[1] return value
rx_speed = (rx - prev_rx) / 1024 prev_rx = rx line = '' if (rx_speed < 1024): line = "R%(rs) 5dK" % {'rs': rx_speed} if (rx_speed >= 1024): line = "R%(rs) 5.2fM" % {'rs': rx_speed / 1024} p = re.compile(r'(?<=TX\sbytes:)\d+') m = re.search(p, temp) tx = float(m.group()) tx_speed = (tx - prev_tx) / 1024 prev_tx = tx if (tx_speed < 1024): line = line + (" T%(rs) 5dK " % {'rs': tx_speed}) if (tx_speed >= 1024): line = line + (" T%(rs) 5.2fM " % {'rs': tx_speed / 1024}) WriteCommand(0xc0) WriteString(line) time.sleep(1) Setup() while True: Loop() i2c.close()
def read_byte_data(self, adr): i2c.open(self.address) i2c.write([adr]) res = i2c.read(1)[0] i2c.close() return res
print "Dump eeprom:" print "="*24 print " ", for i in xrange(16): print " %x" % i, print "\t", for i in xrange(16): print "%x" % i, print "" """Print data""" for i in xrange(128): page = i2c.read(16) print "%03x:" % (i*0x10), for j in xrange(0, 16): print "%02x" % page[j], """Print characters""" print "\t", for j in xrange(16): if page[j] <= 126 and page[j] >= 32: print chr(page[j]), else: print '.', print "" i2c.close()
def ADS1115_INIT(): i2c.open(ADS1115_ADDRESS) i2c.write([0x01, MSB_Config, LSB_Config]) i2c.close()
def readBmp180(addr=DEVICE): #Slave Device adress i2c.open(addr) # Register Addresses REG_CALIB = 0xAA REG_MEAS = 0xF4 REG_MSB = 0xF6 REG_LSB = 0xF7 # Control Register Address CRV_TEMP = 0x2E CRV_PRES = 0x34 # Oversample setting OVERSAMPLE = 0 # 0 - 3 # Read calibration data # Read calibration data from EEPROM i2c.write([REG_CALIB]) cal = i2c.read(22) # Convert byte data to word values AC1 = getShort(cal, 0) AC2 = getShort(cal, 2) AC3 = getShort(cal, 4) AC4 = getUshort(cal, 6) AC5 = getUshort(cal, 8) AC6 = getUshort(cal, 10) B1 = getShort(cal, 12) B2 = getShort(cal, 14) MB = getShort(cal, 16) MC = getShort(cal, 18) MD = getShort(cal, 20) print("AC1:",AC1) print("AC2:",AC2) print("AC3:",AC3) print("AC4:",AC4) print("AC5:",AC5) print("AC6:",AC6) print("B1:",B1) print("B2:",B2) print("MB:",MB) print("MC:",MC) print("MD",MD) # Read temperature i2c.write( [REG_MEAS, CRV_TEMP] ) time.sleep(0.015) i2c.write([0xF6]) ( msb, lsb ) = i2c.read( 2 ) UT = (msb << 8) + lsb print("msb,lsb",msb,lsb) print("UT",UT) # Read pressure i2c.write([REG_MEAS, CRV_PRES + (OVERSAMPLE << 6)]) time.sleep(0.14) i2c.write([REG_MSB]) (msb, lsb, xsb) = i2c.read(3) i2c.close() print("msb,lsb,xsb",msb,lsb,xsb) UP = ((msb << 16) + (lsb << 8) + xsb) >> (8 - OVERSAMPLE) print("UP",UP) # Refine temperature X1 = ((UT - AC6) * AC5) >> 15 X2 = (MC << 11) / (X1 + MD) B5 = X1 + X2 temperature = ( B5 + 8 ) >> 4 # Refine pressure B6 = B5 - 4000 B62 = B6 * B6 >> 12 X1 = (B2 * B62) >> 11 X2 = AC2 * B6 >> 11 X3 = X1 + X2 B3 = (((AC1 * 4 + X3) << OVERSAMPLE) + 2) >> 2 X1 = AC3 * B6 >> 13 X2 = (B1 * B62) >> 16 X3 = ((X1 + X2) + 2) >> 2 B4 = (AC4 * (X3 + 32768)) >> 15 B7 = (UP - B3) * (50000 >> OVERSAMPLE) P = (B7 * 2) / B4 X1 = ( P >> 8 ) * ( P >> 8 ) X1 = ( X1 * 3038 ) >> 16 X2 = ( -7357 * P ) >> 16 pressure = P + ( (X1 + X2 + 3791) >> 4 ) return ( temperature / 10.0, pressure/ 100.0 )
def write_byte(self, adr, byte): i2c.open(self.address) i2c.write([adr, byte]) i2c.close()
def close(self): i2c.close()
#!/usr/bin/env python from pyA20 import i2c i2c.init("/dev/i2c-0") #Initialize module to use /dev/i2c-0 res = i2c.open(0x20) #The slave device address is 0x2A i2c.write([0x21]) #Set address at 0x21 ADCL register value = i2c.read(1) #Read 1 byte valueL=[hex(x) for x in value] i2c.write([0x20]) #Set address at 0x20 ADCH register value = i2c.read(1) #Read 1 valueH=[int(x) for x in value] total=int(valueL[0],16)+(valueH[0]<<8) voltage=(total+3.737611)/76.250833 percentage=round((100*(voltage-10.4))/2.2) percentage=100 if percentage>100 else percentage percentage=0 if percentage<0 else percentage print str(percentage) i2c.close() #End communication with slave device