I2C_class module

class I2C_class.I2C(H)[source]

Methods to interact with the I2C port. An instance of Labtools.Packet_Handler must be passed to the init function

Example:: Read Values from an HMC5883L 3-axis Magnetometer(compass) [GY-273 sensor] connected to the I2C port
>>> ADDRESS = 0x1E
>>> from Labtools import interface
>>> I = interface.Interface() 
#Alternately, you may skip using I2C as a child instance of Interface, 
#and instead use I2C=Labtools.I2C_class.I2C(Labtools.packet_handler.Handler())

# writing to 0x1E, set gain(0x01) to smallest(0) >>> I.I2C.bulkWrite(ADDRESS,[0x01,0])

# writing to 0x1E, set mode conf(0x02), continuous measurement(0) >>> I.I2C.bulkWrite(ADDRESS,[0x02,0])

# read 6 bytes from addr register on I2C device located at ADDRESS >>> vals = I.I2C.bulkRead(ADDRESS,addr,6)

>>> from numpy import int16
#conversion to signed datatype
>>> x=int16((vals[0]<<8)|vals[1])
>>> y=int16((vals[2]<<8)|vals[3])
>>> z=int16((vals[4]<<8)|vals[5])
>>> print x,y,z
init()[source]
enable_smbus()[source]
pullSCLLow(uS)[source]

Hold SCL pin at 0V for a specified time period. Used by certain sensors such as MLX90316 PIR for initializing.

Arguments  
uS Time(in uS) to hold SCL output at 0 Volts
config(freq, verbose=True)[source]

Sets frequency for I2C transactions

Arguments  
freq I2C frequency
start(address, rw)[source]

Initiates I2C transfer to address via the I2C port

Arguments  
address I2C slave address
rw Read/write. - 0 for writing - 1 for reading.
stop()[source]

stops I2C transfer

Returns:Nothing
wait()[source]

wait for I2C

Returns:Nothing
send(data)[source]

SENDS data over I2C. The I2C bus needs to be initialized and set to the correct slave address first. Use I2C.start(address) for this.

Arguments  
data Sends data byte over I2C bus
Returns:Nothing
send_burst(data)[source]

SENDS data over I2C. The function does not wait for the I2C to finish before returning. It is used for sending large packets quickly. The I2C bus needs to be initialized and set to the correct slave address first. Use start(address) for this.

Arguments  
data Sends data byte over I2C bus
Returns:Nothing
restart(address, rw)[source]

Initiates I2C transfer to address

Arguments  
address I2C slave address
rw Read/write. * 0 for writing * 1 for reading.
simpleRead(addr, numbytes)[source]

Read bytes from I2C slave without first transmitting the read location.

Arguments  
addr Address of I2C slave
numbytes Total Bytes to read
read(length)[source]

Reads a fixed number of data bytes from I2C device. Fetches length-1 bytes with acknowledge bits for each, +1 byte with Nack.

Arguments  
length number of bytes to read from I2C bus
read_repeat()[source]
read_end()[source]
read_status()[source]
readBulk(device_address, register_address, bytes_to_read)[source]
writeBulk(device_address, bytestream)[source]

write bytes to I2C slave

Arguments  
device_address Address of I2C slave
bytestream List of bytes to write
scan(frequency=100000, verbose=False)[source]

Scan I2C port for connected devices

Arguments  
Frequency I2C clock frequency
Returns:Array of addresses of connected I2C slave devices

Previous topic

SPI_class module

Next topic

NRF24L01_class module

This Page