def __init__(self, name, hostname): ''' ''' self.name = name self.hostname = hostname # topic = 'pump/'+name # client.message_callback_add(topic,self.on_message) # # wiringpi.wiringPiSetupGpio() # # wiringpi.pinMode(PORT,1) # # wiringpi.digitalWrite(PORT,not data['status']) # gpio.init() # gpio.setcfg(port.PA10, gpio.OUTPUT) i2c.init("/dev/i2c-0") #Initialize module to use /dev/i2c-2 i2c.open(0x48) #The slave device address is 0x55 #If we want to write to some register #i2c.write([0xAA, 0x20]) #Write 0x20 to register 0xAA #i2c.write([0xAA, 0x10, 0x11, 0x12]) #Do continuous write with start address 0xAA #If we want to do write and read data = [0x84,0xc2] i2c.write([0x01, 0xc2, 0x85 ]) #Set address at 0xAA register i2c.write([0x00]) threading.Timer(9, publish).start() threading.Timer)
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 lcd_init(DispSet, EntryMode): i2c.init("/dev/i2c-0") i2c.open(I2C_ADDR) delay_ms(20) nibble(0x30, CMD) delay_ms(5) nibble(0x30, CMD) delay_us(110) nibble(0x30, CMD) nibble(0x20, CMD) write(DispSet, CMD) write(0x08, CMD) write(0x01, CMD) write(0x06, CMD) write(EntryMode, CMD)
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
#!/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
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 )
#!/usr/bin/env python from pyA20 import i2c """Initialize i2c bus""" i2c.init("/dev/i2c-2") i2c.open(0x77) #Slave Device adress """Set address pointer to the first""" i2c.write([0x00]) print "Dump eeprom:" i2c.close()
def init(interface, addr): """ Initalize i2c interface """ i2c.init("/dev/i2c-" + str(interface)) i2c.open(addr)
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 <<<<<<< HEAD i2c.open(addr) #Slave Device adress time.sleep(0.05) i2c.write([REG_CALIB]) cal = i2c.read(22) time.sleep(0.05) #cal = bus.read_i2c_block_data(addr, REG_CALIB, 22) ======= i2c.write([REG_CALIB]) cal = i2c.read(22) >>>>>>> 2066dfbee270812bf435b6e815ff896003e6ccf1 # Convert byte data to word values AC1 = getShort(cal, 0) AC2 = getShort(cal, 2)
def __init__(self, device_address, port): i2c.init(device_address) i2c.open(port) self.state = None
import time import os import re from pyA20 import i2c #Initialize module to use /dev/i2c-0 # i2c-0 is is the upper i2c port on your orangepi GPIO ports #Physical: # 3 - SDA.0 # 5 - SCL.0 i2c.init("/dev/i2c-0") #the address of PCF8574 i2c.open(0x27) #i2c 1602 module pin defination #LEDK is the K pin for LCD background light LED #PCF8574 LCD1602 #D0 RS #D1 RW #D2 E #D3 LEDK #D4 D4 #D5 D5 #D6 D6 #D7 D7 C_LED = 0x08 * 1
def write_byte(self, adr, byte): i2c.open(self.address) i2c.write([adr, byte]) i2c.close()
def read_byte_data(self, adr): i2c.open(self.address) i2c.write([adr]) res = i2c.read(1)[0] i2c.close() return res
def readBmp180(addr=DEVICE): #Slave Device adress i2c.open(addr)
The i2c address can be different, but on this specific board is 0x50. The text will be big mess if python3 is used. """ from pyA20 import i2c __author__ = "Stefan Mavrodiev" __copyright__ = "Copyright 2014, Olimex LTD" __credits__ = ["Stefan Mavrodiev"] __license__ = "GPL" __version__ = "2.0" __maintainer__ = __author__ __email__ = "*****@*****.**" eeprom_address = 0x28 """Initialize i2c bus""" i2c.init("/dev/i2c-1") i2c.open(eeprom_address) """while loop between 0 and 255""" n = 255 li = range(1, 255 + 1) + range(n, 0, -1) ti = cycle(li) while True: sleep(0.01) i2c.write([0xAA, next(ti)]) i2c.close()
log.warning(msg) gpio_available = False except RuntimeError: msg = "Could not load RPi.GPIO library, trying OrangePiZero library" log.warning(msg) gpio_available = False try: from pyA20.gpio import gpio from pyA20.gpio import port from pyA20 import i2c log.warning("Hello") i2c.init("/dev/i2c-1") #Initialize module to use /dev/i2c-1 i2c.open(0x68) #The slave device address is 0x68 DS1371 i2c.write([0x04, 0xFF, 0xFF, 0xFF]) #If we want to write to some register i2c.write([0x07, 0x4F]) #Write 0x0E to register 0x07 # INTCN 1 WDALM 0 AIE 1 #Initialise the GPIO output gpio.init() #Initialize module. Always called first # gpio.setcfg(config.gpio_watchdog, gpio.OUTPUT) # Configure the watchdog gpio.setcfg(config.gpio_cool, gpio.OUTPUT) # Configure the cooling output gpio.setcfg(config.gpio_reset, gpio.OUTPUT) # Configure Reset as OUTPUT gpio.output(config.gpio_reset, gpio.HIGH) # Set the i2c GPIO reset line High
from pyA20 import i2c __author__ = "Stefan Mavrodiev" __copyright__ = "Copyright 2014, Olimex LTD" __credits__ = ["Stefan Mavrodiev"] __license__ = "GPL" __version__ = "2.0" __maintainer__ = __author__ __email__ = "*****@*****.**" eeprom_address = 0x50 """Initialize i2c bus""" i2c.init("/dev/i2c-1") i2c.open(eeprom_address) """Set address pointer to the first""" i2c.write([0x00]) 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 ""
def ADS1115_INIT(): i2c.open(ADS1115_ADDRESS) i2c.write([0x01, MSB_Config, LSB_Config]) i2c.close()