def init_i2c(self):
print('* initializing i2c')
self.bus = I2C(scl=self.pin_scl,
sda=self.pin_sda)
python类I2C的实例源码
def __init__(self):
self.d = None
try:
import ssd1306
from machine import I2C, Pin
i2c = I2C(sda=Pin(4), scl=Pin(5))
self.d = ssd1306.SSD1306_I2C(64, 48, i2c, 60)
print ("Display available")
except Exception as e:
print ("Display just print")
self.d = None
def __init__(self, i2c, address=0x20 ):
"""Initialize MCP230xx at specified I2C address on the I2C Bus."""
self.address = address
self.i2c = i2c
# Assume starting in ICON.BANK = 0 mode (sequential access).
# Compute how many bytes are needed to store count of GPIO.
self.gpio_bytes = self.NUM_GPIO//8
# Buffer register values so they can be changed without reading.
self.iodir = bytearray(self.gpio_bytes) # Default direction to all inputs.
self.gppu = bytearray(self.gpio_bytes) # Default to pullups disabled.
self.gpio = bytearray(self.gpio_bytes)
# Write current direction and pullup buffer state.
self.write_iodir()
self.write_gppu()
def writeList(self, register, data):
"""Introduced to match the writeList implementation of the Adafruit I2C _device member"""
return self.i2c.writeto_mem(self.address, register, data)
def readList(self, register, length):
"""Introduced to match the readList implementation of the Adafruit I2C _device member"""
return self.i2c.readfrom_mem(self.address, register, length)
def __init__( self, i2c=None, addr=AM2315_I2CADDR ):
self.addr = addr
self.rbuf = bytearray(8) # sensor response
if i2c != None:
self.i2c = i2c
else:
# WARNING this default bus does not work
# on feather ESP8266 with external power supply
# see test.py using sda on pin 4, scl on pin 2
self.i2c = I2C( sda=Pin(4), scl=Pin(5), freq=20000 )
def __init__(self, scl_pin=5, sda_pin=4, delta_temp = 0, delta_hum = 0, i2c_address=DEFAULT_I2C_ADDRESS):
self.i2c = I2C(scl=Pin(scl_pin), sda=Pin(sda_pin))
self.i2c_addr = i2c_address
self.set_delta(delta_temp, delta_hum)
time.sleep_ms(50)
def init(self, scl_pin=5, sda_pin=4):
"""
Init the I2C bus using the new pin values
"""
self.i2c.init(scl=Pin(scl_pin), sda=Pin(sda_pin))
def writeList(self, register, data):
"""Introduced to match the writeList implementation of the Adafruit I2C _device member"""
return self.i2c.writeto_mem(self.address, register, data)
def readList(self, register, length):
"""Introduced to match the readList implementation of the Adafruit I2C _device member"""
return self.i2c.readfrom_mem(self.address, register, length)
def __init__(self, connect=None, *, pwr=None, i2c=None, spi=None, i2c_addr=98):
if connect in ('X', 'Y', 'XY', 'YX'):
i = connect[-1]
j = connect[0]
y = j + '4'
elif connect == 'C':
i = 2
j = 2
y = 'A7'
else:
if pwr is None or i2c is None or spi is None:
raise ValueError('must specify valid "connect" or all of "pwr", "i2c" and "spi"')
if pwr is None:
pwr = machine.Pin(y, machine.Pin.OUT)
if i2c is None:
i2c = machine.I2C(i, freq=1000000)
if spi is None:
spi = machine.SPI(j, baudrate=13500000, polarity=0, phase=0)
if not pwr.value():
pwr(1)
sleep_ms(10)
# else:
# alread have power
# lets be optimistic...
# set connections
self.pwr = pwr
self.i2c = i2c
self.spi = spi
self.i2c_addr = i2c_addr
# create temp buffers and memoryviews
self.buf16 = bytearray(16)
self.buf19 = bytearray(19)
self.buf = [None] * 10
for i in range(1, 10):
self.buf[i] = memoryview(self.buf16)[0:i]
self.buf1 = self.buf[1]
self.array4 = [0, 0, 0, 0]
# set default orientation and window
self.set_orient(PORTRAIT)
self._fcmd2b('<BBBBBB', 0x76, 0, 0, self.w, self.h) # viewport 'v'
self._fcmd2b('<BBBBBB', 0x79, 0, 0, self.w, self.h) # window 'y'