def test_page_bytes(self): i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56) self.assertEqual(i2c.page_bytes, 8) i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56, page_bytes=16) self.assertEqual(i2c.page_bytes, 16) with self.assertRaises(TypeError): i2c.page_bytes = "1" with self.assertRaises(ValueError): i2c.page_bytes = -1 with self.assertRaises(ValueError): i2c.page_bytes = 0 with self.assertRaises(ValueError): i2c.page_bytes = 4 with self.assertRaises(ValueError): i2c.page_bytes = 10 with self.assertRaises(ValueError): i2c.page_bytes = 2048 i2c.page_bytes = 32 self.assertEqual(i2c.page_bytes, 32) i2c.page_bytes = 64 self.assertEqual(i2c.page_bytes, 64)
def test_init(self): with self.assertRaises(TypeError): pylibi2c.I2CDevice() with self.assertRaises(TypeError): pylibi2c.I2CDevice(1, 2) with self.assertRaises(TypeError): pylibi2c.I2CDevice("1", "2") with self.assertRaises(TypeError): pylibi2c.I2CDevice("/dev/i2c-1") with self.assertRaises(IOError): pylibi2c.I2CDevice("/dev/i2c-100", 0x56)
async def open(self, ws, data): # Parse request device = I2CDevice(**data).__dict__ device.pop("handle") # First open i2c bus self.__device = pylibi2c.I2CDevice(**device)
def test_setattr(self): i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56) with self.assertRaises(AttributeError): i2c.bus = "" with self.assertRaises(AttributeError): i2c.addr = ""
def __init__(self, i2c_chan = 1, i2c_base_addr = 0x3B): self.i2c_chan = i2c_chan self.i2c_base_addr = i2c_base_addr self.i2c_bus = pylibi2c.I2CDevice( '/dev/i2c-'+str(self.i2c_chan), self.i2c_base_addr, iaddr_bytes=2 )
def __init__(self): logger.info('MPU: Opening i2c device @/dev/i2c-1, addr 0x68') self.device = pylibi2c.I2CDevice('/dev/i2c-1', 0x68) self.device.delay = 10 self.device.page_bytes = 8 self.device.flags = pylibi2c.I2C_M_IGNORE_NAK time.sleep(3) logger.info('MPU: disabling sleep mode') i2cdev.setSleepMode(self.device, enabled=0) self.gyro_cal_x = 0 self.gyro_cal_y = 0 self.gyro_cal_z = 0
def test_tenbit(self): i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56) self.assertEqual(i2c.tenbit, False) i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56, tenbit=1) self.assertEqual(i2c.tenbit, True) with self.assertRaises(TypeError): i2c.tenbit = 0 with self.assertRaises(TypeError): i2c.tenbit = 100 with self.assertRaises(TypeError): i2c.tenbit = "True" i2c.tenbit = False self.assertEqual(i2c.tenbit, False) i2c.tenbit = True self.assertEqual(i2c.tenbit, True)
def test_iaddr_bytes(self): i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56) self.assertEqual(i2c.iaddr_bytes, 1) i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56, iaddr_bytes=2) self.assertEqual(i2c.iaddr_bytes, 2) with self.assertRaises(TypeError): i2c.iaddr_bytes = "1" with self.assertRaises(ValueError): i2c.iaddr_bytes = -1 with self.assertRaises(ValueError): i2c.iaddr_bytes = 5 i2c.iaddr_bytes = 0 self.assertEqual(i2c.iaddr_bytes, 0) i2c.iaddr_bytes = 1 self.assertEqual(i2c.iaddr_bytes, 1) i2c.iaddr_bytes = 3 self.assertEqual(i2c.iaddr_bytes, 3)
def test_delay(self): i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56) self.assertEqual(i2c.delay, 1) i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56, delay=0) self.assertEqual(i2c.delay, 0) with self.assertRaises(TypeError): i2c.delay = "100" with self.assertRaises(TypeError): i2c.delay = 0.1 with self.assertRaises(ValueError): i2c.delay = -1 with self.assertRaises(ValueError): i2c.delay = 101 i2c.delay = 10 self.assertEqual(i2c.delay, 10) i2c.delay = 100 self.assertEqual(i2c.delay, 100)
def test_flags(self): i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56) self.assertEqual(i2c.flags, 0) i2c = pylibi2c.I2CDevice("/dev/i2c-1", 0x56, flags=1) self.assertEqual(i2c.flags, 1) with self.assertRaises(TypeError): i2c.flags = "100" with self.assertRaises(TypeError): i2c.flags = 0.1 with self.assertRaises(ValueError): i2c.flags = -1 i2c.flags = 0 self.assertEqual(i2c.flags, 0) i2c.flags = pylibi2c.I2C_M_NOSTART self.assertEqual(i2c.flags, pylibi2c.I2C_M_NOSTART) i2c.flags = pylibi2c.I2C_M_IGNORE_NAK self.assertEqual(i2c.flags, pylibi2c.I2C_M_IGNORE_NAK)
page_bytes = args.get('page_bytes') iaddr_bytes = args.get('iaddr_bytes') data = args.get('data') size = args.get('size') ioctl = args.get('ioctl') if data is None and size is None: raise RuntimeError( "'data' or 'size' must specified one, 'data' for write, 'size' for read" ) # Create a i2c device i2c = pylibi2c.I2CDevice(bus=bus, addr=device, page_bytes=page_bytes, iaddr_bytes=iaddr_bytes, delay=delay) if data: write_handle = i2c.ioctl_write if ioctl else i2c.write ret = write_handle(iaddr, bytes(data.encode("ascii"))) print("Write: '{0:s}' to address: 0x{1:x}".format(data, iaddr)) print("Result:{}".format(ret)) else: read_handle = i2c.ioctl_read if ioctl else i2c.read data = read_handle(iaddr, size) print("Read: {0:d} bytes data from address: 0x{1:x}".format( size, iaddr)) print("Result:'{}'".format(data.decode("ascii"))) except (TypeError, IOError, ValueError, RuntimeError) as err:
def setUp(self): self.i2c_size = 256 # 24C04 E2PROM test self.i2c = pylibi2c.I2CDevice(bus="/dev/i2c-1", addr=0x56, page_bytes=16)
import ctypes import pylibi2c import time #------CONNECT TO NZ AT GIVEN ADDRESS (e.g. 0x40) AND GIVE IT A NAME (e.g. "wheels")---- wheels = pylibi2c.I2CDevice('/dev/i2c-1', 0x40) # Open i2c device at address 0x3 wheels.delay = 10 # Set delay wheels.page_bytes = 16 # Set page_bytes wheels.flags = pylibi2c.I2C_M_IGNORE_NAK # Set flags #------CONNECT TO ANOTHER NZ WITH A DIFFERENT ADDRESS AND NAME--------------------------- #head = pylibi2c.I2CDevice('/dev/i2c-1', 0x41) # Open i2c device at address 0x4 #head.delay = 10 # Set delay #head.page_bytes = 16 # Set page_bytes #head.flags = pylibi2c.I2C_M_IGNORE_NAK # Set flags #------READ FROM THE NEARZERO------------------------------------------------------------ while True: data1 = wheels.read( 0x0, 12 ) #The last part is the buffer size; enough for 6 digits from each encoder print(data1) #data2 = head.read(0x0,12) #print(data2) #The first 6 digits give the encoder value from channel 1 #The second 6 digits give the encoder value from channel 2 #Digits 1 and 7 indicate sign, where 0 = positive and - = negative.
def i2cSetup(self): self.address = pylibi2c.I2CDevice('/dev/i2c-1', self.address) self.address.delay = 10 self.address.page_bytes = 16 self.address.flags = pylibi2c.I2C_M_IGNORE_NAK
import ctypes import pylibi2c import time #------CONNECT TO NZ AT GIVEN ADDRESS (e.g. 0x40) AND GIVE IT A NAME (e.g. "wheels")------ wheels = pylibi2c.I2CDevice('/dev/i2c-1', 0x40) wheels.delay = 10 wheels.page_bytes = 16 wheels.flags = pylibi2c.I2C_M_IGNORE_NAK #------CONNECT TO ANOTHER NZ WITH A DIFFERENT ADDRESS AND NAME---------------------------- #head = pylibi2c.I2CDevice('/dev/i2c-1', 0x41) #head.delay = 10 #head.page_bytes = 16 #head.flags = pylibi2c.I2C_M_IGNORE_NAK #------WRITE COMMANDS TO NEARZERO--------------------------------------------------------- #(0x0, 'CHAN(1/2) MODE(v/p/s) SIGN(+/-) CMD CMD CMD CMD CMD "C" CMD CMD CMD CMD CMD') wheels.write( 0x0, '1v+00100c00200') #Drive channel 1 at velocity=100 with 200mA of current wheels.write( 0x0, '2v+00250c00320') #Drive channel 2 at velocity=250 with 320mA of current #head.write(0x0, '1p+00000c00000') #Drive channel 1 at position=0 with 0mA of current #head.write(0x0, '2p+01000c00600') #Drive channel 2 at position=1000 and 600mA of current
import ctypes import pylibi2c import time import datetime from firebase import firebase # Open i2c device @/dev/i2c-0, addr 0x00, 16bits internal address i2c = pylibi2c.I2CDevice('/dev/i2c-1', 0x08, iaddr_bytes=2) # Set delay i2c.delay = 10 # Set page_bytes i2c.page_bytes = 16 # Set flags i2c.flags = pylibi2c.I2C_M_IGNORE_NAK # Inicializando buffer buf = bytes(256) firebase = firebase.FirebaseApplication( 'https://surveillaceathenaiot.firebaseio.com/', None) while True: #endereco I2C -> 8 data = i2c.ioctl_read(0x0, 8) if data != '99999999':
import ctypes import pylibi2c import time from log_setup import logger from utils import i2cdev logger.info('Opening i2c device @/dev/i2c-1, addr 0x68') i2c = pylibi2c.I2CDevice('/dev/i2c-1', 0x68) i2c.delay = 10 logger.info('Set delay = 10') i2c.page_bytes = 8 logger.info('Set page bytes = 8') i2c.flags = pylibi2c.I2C_M_IGNORE_NAK logger.info('Set flags to ignore i2c device nak signal') #i2cdev.setSleepMode(i2c, 0) time.sleep(3) pwr_reg = i2cdev.readByte(i2c, 0x6B) #print(i2c.read(0x0, 256)) print(pwr_reg) #logger.debug('Reading PWR MGMT register field') #print(pwr_reg) while True: data = i2c.read(0x43, 6)