def find_boards(): """ A routine like this must be implemented by all board packages. :return: list of ADS1115 board objects (maximum of 4 boards) """ POSS_ADDR = (0x48, 0x49, 0x4A, 0x4B) boards = [] tmpmod = None try: I2Cbus = smbus.SMBus(1) except OSError: # no bus so there cannot be any boards. return boards for addr in POSS_ADDR: try: I2Cbus.read_byte(addr) except OSError: continue try: tmpmod = Adafruit_ADS1x15.ADS1115(address=addr) except RuntimeError as e: logger.debug(e) # print('No ADS1115 at: '+str(addr)) tmpmod = None if tmpmod: boards.append(Board_ADS1115(tmpmod)) return boards
def __init__(self, addr=DEVICE): self.addr = addr self.bus = smbus.SMBus(1) # self.running = False self.value = None self.old_value = None self.rising_thresholds = [] self.falling_thresholds = []
def __init__(self, bus_num, mode=MASTER, baudrate=None): if mode != self.MASTER: raise NotImplementedError("Only I2C Master supported!") _mode = self.MASTER #if baudrate != None: # print("I2C frequency is not settable in python, ignoring!") try: self._i2c_bus = smbus.SMBus(bus_num) except FileNotFoundError: raise RuntimeError("I2C Bus #%d not found, check if enabled in config!" % bus_num)
def __init__(self, channel=1, address=_HT16K33_DEFAULT_ADDRESS, auto_write=True, active=True, rotation=0): self.bus = smbus.SMBus() self.bus.open(channel) self._address = address self._temp = None self._buffer = [0] * 16 self.show() self._rotation = rotation self._auto_write = auto_write self.active = active self.blink_rate = 0 self.brightness = 15
def get_si7021_values(self): temperature = None humidity = None try: # Get I2C bus bus = smbus.SMBus(1) # SI7021 address, 0x40(64) # 0xF5(245) Select Relative Humidity NO HOLD master mode bus.write_byte(0x40, 0xF5) time.sleep(0.3) # SI7021 address, 0x40(64) # Read data back, 2 bytes, Humidity MSB first data0 = bus.read_byte(0x40) data1 = bus.read_byte(0x40) # Convert the data humidity = ((data0 * 256 + data1) * 125 / 65536.0) - 6 time.sleep(0.3) # SI7021 address, 0x40(64) # 0xF3(243) Select temperature NO HOLD master mode bus.write_byte(0x40, 0xF3) time.sleep(0.3) # SI7021 address, 0x40(64) # Read data back, 2 bytes, Temperature MSB first data0 = bus.read_byte(0x40) data1 = bus.read_byte(0x40) # Convert the data temperature = ((data0 * 256 + data1) * 175.72 / 65536.0) - 46.85 # Convert celsius to fahrenheit temperature = (temperature * 1.8) + 32 except Exception as e: logger.exception("Failed to get si7021 sensor data") return temperature, humidity
def __init__(self, address=BH1750_ADDRESS, bus=None, **kwargs): self.address = address if bus is None: self.bus = smbus.SMBus(1) else: self.bus = bus # self.running = False #if i2c is None: # import Adafruit_GPIO.I2C as I2C # i2c = I2C #self._device = i2c.get_i2c_device(address, **kwargs) self.bus.write_byte(address, CONTINUOUS_HIGH_RES_MODE) time.sleep(0.2) self.value = None self.old_value = None self.rising_thresholds = [] self.falling_thresholds = []
def __init__(self, hires=True, accel_address=LSM303_ADDRESS_ACCEL, mag_address=LSM303_ADDRESS_MAG, **kwargs): """Initialize the LSM303 accelerometer & magnetometer. The hires boolean indicates if high resolution (12-bit) mode vs. low resolution (10-bit, faster and lower power) mode should be used. """ # Setup I2C interface for accelerometer and magnetometer. # if i2c is None: # import Adafruit_GPIO.I2C as I2C # i2c = I2C # self._accel = i2c.get_i2c_device(accel_address, **kwargs) # self._mag = i2c.get_i2c_device(mag_address, **kwargs) self.accel_address = accel_address self.mag_address = mag_address self.smbus = smbus.SMBus(1) # Enable the accelerometer # self._accel.write8(LSM303_REGISTER_ACCEL_CTRL_REG1_A, 0x27) self.smbus.write_byte_data(self.accel_address, LSM303_REGISTER_ACCEL_CTRL_REG1_A, 0x27) # Select hi-res (12-bit) or low-res (10-bit) output mode. # Low-res mode uses less power and sustains a higher update rate, # output is padded to compatible 12-bit units. if hires: # self._accel.write8(LSM303_REGISTER_ACCEL_CTRL_REG4_A, 0b00001000) self.smbus.write_byte_data(self.accel_address, LSM303_REGISTER_ACCEL_CTRL_REG4_A, 0b00001000) else: # self._accel.write8(LSM303_REGISTER_ACCEL_CTRL_REG4_A, 0) self.smbus.write_byte_data(self.accel_address, LSM303_REGISTER_ACCEL_CTRL_REG4_A, 0) # Enable the magnetometer # self._mag.write8(LSM303_REGISTER_MAG_MR_REG_M, 0x00) self.smbus.write_byte_data(self.mag_address, LSM303_REGISTER_MAG_MR_REG_M, 0x00)
# Basic smbus test. This is pretty ugly and meant to be run against a ADS1x15 # and some output inspected by a Saleae logic analyzer. TODO: Refactor into # something that can test without hardware? import binascii import Adafruit_PureIO.smbus as smbus DEVICE_ADDR = 0x48 REGISTER = 0x01 # Test open and close. i2c = smbus.SMBus() i2c.open(1) val = i2c.read_byte(DEVICE_ADDR) print("read_byte from 0x{0:0X}: 0x{1:0X}".format(REGISTER, val)) i2c.close() # Test initializer open. i2c = smbus.SMBus(1) val = i2c.read_byte(DEVICE_ADDR) print("read_byte from 0x{0:0X}: 0x{1:0X}".format(REGISTER, val)) i2c.close() # Test various data reads. with smbus.SMBus(1) as i2c: val = i2c.read_byte(DEVICE_ADDR) print("read_byte from 0x{0:0X}: 0x{1:0X}".format(REGISTER, val)) val = i2c.read_byte_data(DEVICE_ADDR, REGISTER) print("read_byte_data from 0x{0:0X}: 0x{1:0X}".format(REGISTER, val)) val = i2c.read_word_data(DEVICE_ADDR, REGISTER) print("read_word_data from 0x{0:0X}: 0x{1:04X}".format(REGISTER, val))
#!/usr/bin/python import Adafruit_PureIO.smbus as smbus import t10_axp209 as pmu ########## ## MAIN ## ########## axp209 = smbus.SMBus(0, dangerous=True) pmu.adc_enable( axp209, pmu.BATT_V | pmu.BATT_C | pmu.ACIN_V | pmu.ACIN_C | pmu.VBUS_V | pmu.VBUS_C) adc = pmu.adc_status(axp209) print("ADC status=%s BV=%s BC=%s AV=%s AC=%s VV=%s VC=%s" % (bin(adc), pmu.ison(adc, pmu.BATT_V), pmu.ison(adc, pmu.BATT_C), pmu.ison(adc, pmu.ACIN_V), pmu.ison(adc, pmu.ACIN_C), pmu.ison(adc, pmu.VBUS_V), pmu.ison(adc, pmu.VBUS_C))) pwr = pmu.power_status(axp209) print("POWER status=%s VBUS=%s ACIN=%s BATT=%s CHARGING=%s\n" % (bin(pwr), pmu.ison(pwr, pmu.HAS_VBUS), pmu.ison(pwr, pmu.HAS_ACIN), pmu.ison(pwr, pmu.HAS_BATT), pmu.ison(pwr, pmu.CHARGING))) print("Temperature = %1.1f" % pmu.internal_temperature(axp209)) print("ACIN voltage = %1.4f" % pmu.acin_voltage(axp209)) print("ACIN current = %1.4f" % pmu.acin_current(axp209))
i2cbus.close() os.system("init 0") time.sleep(period) ########## ## MAIN ## ########## signal.signal(signal.SIGINT, bye) signal.signal(signal.SIGTERM, bye) syslog.openlog(DAEMONNAME, syslog.LOG_PID, syslog.LOG_DAEMON) syslog.syslog("init") i2cbus = smbus.SMBus(0) syslog.syslog("device=%s" % i2cbus._device) run() """ context = daemon.DaemonContext( signal_map = { signal.SIGINT:bye, signal.SIGTERM:bye, signal.SIGHUP:bye }, working_directory = "/var/local", umask = 0o002, pidfile = lockfile.pidlockfile.PIDLockFile("/run/%s.pid" % DAEMONNAME), files_preserve = [i2cbus._device] ) with context: run() """
def _write_data(self, data: int): with smbus.SMBus(self.bus_num) as i2c: i2c.write_byte_data(self.DEVICE_ADDR, CNTRBIT_RS, data) self._delay(WRITE_DELAY_MS * 0.001)
def _write_instruction(self, cmd: int): with smbus.SMBus(self.bus_num) as i2c: i2c.write_byte_data(self.DEVICE_ADDR, CNTRBIT_CO, cmd) self._delay(WRITE_DELAY_MS * 0.001)