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 write_values(self, lt_value, rt_value, r_x, r_y, lb, rb, axis_b_l, axis_b_r): """ записывает данные в контроллер :param values: """ val_11, val_12 = divmod(lt_value, 256) val_21, val_22 = divmod(rt_value, 256) val_31, val_32 = divmod(r_x, 256) val_41, val_42 = divmod(r_y, 256) _values = [ ord('a'), ord('t'), val_11, val_12, val_21, val_22, val_31, val_32, val_41, val_42, lb, rb, axis_b_l, axis_b_r, 0, 0, ] try: i2c.write(_values) except IOError: self.set_state(OFF) else: self.set_state(ON)
def nibble(byte, mode): byte = (byte & 0xF0) | mode | BLACKLIGHT i2c.write([byte | ENABLE]) delay_ms(2.0) i2c.write([byte & ~ENABLE]) nibble(byte, mode) nibble((byte << 4), mode) delay_us(40.0)
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 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 Write4Bit(data): data = data << 4 i2c.write([C_LED | data]) time.sleep(0.00001) i2c.write([C_LED | C_WC | C_EN | data]) time.sleep(0.000001) i2c.write([C_LED | C_WC | data]) time.sleep(0.0001)
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
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
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 "" """Print data""" for i in xrange(128): page = i2c.read(16)
def sendCmd(cmd, *args): """ send command to oled display """ # TODO:optimize to one i2c write if possible i2c.write([0x80, cmd]) for arg in args: i2c.write([0x80, arg])
def sendData(data, *moreData): """ send data to oled RAM """ output = [0x40, data] for mData in moreData: output.append(mData) i2c.write(output)
def sendDataL(listData): """ send data to oled RAM """ output = [0x40] for mData in listData: output.append(mData) i2c.write(output)
# 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) AC3 = getShort(cal, 4) AC4 = getUshort(cal, 6) AC5 = getUshort(cal, 8)
def WriteData(data): tmp = data & 0xf0 i2c.write([C_LED | tmp]) time.sleep(0.00001) i2c.write([C_LED | C_WD | C_EN | tmp]) time.sleep(0.0000001) i2c.write([C_LED | C_WD | tmp]) time.sleep(0.00001) tmp = (data & 0x0f) << 4 i2c.write([C_LED | tmp]) time.sleep(0.00001) i2c.write([C_LED | C_WD | C_EN | tmp]) time.sleep(0.0000001) i2c.write([C_LED | C_WD | tmp]) time.sleep(0.00001)
def ADS1115_INIT(): i2c.open(ADS1115_ADDRESS) i2c.write([0x01, MSB_Config, LSB_Config]) i2c.close()
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()
def SetLed(): i2c.write([C_LED]) time.sleep(0.000001)
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 i2c.init("/dev/i2c-0") #Initialize module to use /dev/i2c-0 i2c.open(0x20) #The slave device address is 0x68 DS1371
def read_byte_data(self, adr): i2c.open(self.address) i2c.write([adr]) res = i2c.read(1)[0] i2c.close() return res
def write_byte(self, adr, byte): i2c.open(self.address) i2c.write([adr, byte]) i2c.close()
def set_heat(self, value): if value > 0: self.heat = 1.0 if gpio_available: if config.heater_invert: GPIO.output(config.gpio_heat, GPIO.LOW) time.sleep(self.time_step * value) GPIO.output(config.gpio_heat, GPIO.HIGH) else: GPIO.output(config.gpio_heat, GPIO.HIGH) time.sleep(self.time_step * value) GPIO.output(config.gpio_heat, GPIO.LOW) if i2c_gpio_available: ''' Send the PID to the IO spamming thread Cycle the elements in sequence of H----- = 8% H--L-- = 16% H-L-H- = 25% F--F-- = 33% FH-FL- = 43% FF-F-- = 50% FF-FH- = 58% FF-FF- = 67% FFHFF- = 75% FFHFFL = 83% FFFFFL = 92% FFFFFF = 100% F = full AC cycle, H = top half, L = bottom half, - = Off Side (bit 1) only enable during < 92% heat, as its bottom Rotate the sequence right every half AC cycle (or multiple of) ''' sequences = [ 0b10000000, 0b00000010, 0b10000000, 0b00010010, 0b10001000, 0b00100010, 0b10010000, 0b10010010, 0b11010000, 0b10011010, 0b11010000, 0b11010010, 0b11011010, 0b11010010, 0b11011010, 0b11011010, 0b11111010, 0b11011010, 0b11111010, 0b11011110, 0b11111000, 0b11111100, 0b11111100, 0b11111100 ] output = sequences[int((value * 24) - 1 - self.HEAT_SEQ)] if self.air > 0.0: output = output + 1 self.output = ~output self.HEAT_SEQ = (self.HEAT_SEQ + 1) % 2 log.info('Outputting %00X', self.output) i2c.write([10, self.output]) else: self.heat = 0.0 if i2c_gpio_available: if self.air > 0.0: output = 1 else: output = 0 self.output = ~output log.info('Outputting %00X', self.output) i2c.write([10, self.output]) if gpio_available: if config.heater_invert: GPIO.output(config.gpio_heat, GPIO.HIGH) else: GPIO.output(config.gpio_heat, GPIO.LOW)
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 WriteCommand(command): tmp = command & 0xf0 i2c.write([C_LED | tmp]) time.sleep(0.00001) i2c.write([C_LED | C_WC | C_EN | tmp]) time.sleep(0.0000001) i2c.write([C_LED | C_WC | tmp]) time.sleep(0.00001) tmp = (command & 0x0f) << 4 i2c.write([C_LED | tmp]) time.sleep(0.00001) i2c.write([C_LED | C_WC | C_EN | tmp]) time.sleep(0.0000001) i2c.write([C_LED | C_WC | tmp]) time.sleep(0.00001)
#!/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
__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 "" """Print data""" for i in xrange(128):