def __init__(self, address): """ Initialize the module and the PCS9685 driver if this is not initialized at the moment. """ global _INITIALIZED_DRIVER self._address = address self._i2c = onionI2C.OnionI2C() # Initialize the PCA9685 for the first time. if self._address not in _INITIALIZED_DRIVER: self._setup() _INITIALIZED_DRIVER.add(self._address)
def __init__(self, address=PCA9685_ADDRESS, i2c=None, **kwargs): """Initialize the PCA9685.""" self._device = onionI2C.OnionI2C() self.set_all_pwm(0, 0) self._device.writeByte(0x40, MODE2, OUTDRV) self._device.writeByte(0x40, MODE1, ALLCALL) time.sleep(0.005) # wait for oscillator mode1 = self._device.readBytes(0x40, MODE1, 1) mode1 = mode1[0] # print(mode1) mode1 = mode1 & ~SLEEP # wake up (reset sleep) self._device.writeByte(0x40, MODE1, mode1) time.sleep(0.005) # wait for oscillator
def initialize(self): try: self.bus = onionI2C.OnionI2C(self.PORT) ver_major = self.bus.readBytes(self.ADDRESS, self.FIRMWARE_MAJOR, 1)[0] ver_minor = self.bus.readBytes(self.ADDRESS, self.FIRMWARE_MINOR, 1)[0] print(f"Firmware Version: {ver_major}.{ver_minor}") return 0 except Exception as ex: logging.error( "Initialization failed, check connection to the button") return 1
def __init__(self, gauss=5.6, declination=(10, 59)): # Get I2C bus self.i2c = onionI2C.OnionI2C() (degrees, minutes) = declination self._declDegrees = degrees self._declMinutes = minutes self._declination = (degrees + minutes / 60) * math.pi / 180 (reg, self._scale) = self._scales[gauss] self.i2c.writeByte(0x1E, 0x00, 0x70) # 8 Average, 15 Hz, Normal self.i2c.writeByte(0x1E, 0x01, reg << 5) #Scale self.i2c.writeByte(0x1E, 0x02, 0x00) # COntinuous Measurement Mode
def __init__(self, address=0x1E, gauss=1.3, declination=(0, 0)): self.i2c = onionI2C.OnionI2C() self.address = address (degrees, minutes) = declination self.__declDegrees = degrees self.__declMinutes = minutes self.__declination = (degrees + minutes / 60) * math.pi / 180 (reg, self.__scale) = self.__scales[gauss] self.i2c.writeByte(self.address, 0x00, 0x70) # 8 Average, 15 Hz, normal measurement self.i2c.writeByte(self.address, 0x01, reg << 5) # Scale self.i2c.writeByte(self.address, 0x02, 0x00) # Continuous measurement
def initialize(self): try: self.bus = onionI2C.OnionI2C(self.PORT) self.bus.writeByte(self.ADDRESS, self.REG_WATCHDOG, self.BYTE_WATCHDOG_STOP) self.bus.writeByte(self.ADDRESS, self.REG_ILIM, self.BYTE_ILIM) self.bus.writeByte(self.ADDRESS, self.REG_ICHG, self.BYTE_ICHG) self.bus.writeByte(self.ADDRESS, self.REG_BATFET, self.BYTE_BATFET) self.bus.writeByte(self.ADDRESS, self.REG_SYSMIN, self.BYTE_SYSMIN) logging.info("UPS initialized") return 0 except Exception as ex: logging.error( "Initialization failed, check connection to the UPS:" + str(ex)) return 1
def __init__(self, address=0x5c, precision=True): self.i2c = onionI2C.OnionI2C() self.address = address self.precision = precision self.temperature = 0.0 self.pressure = 0.0 if self.__deviceAdressCheck(self.address): print('Device found') self.__power_down(self.address) #for clean start if self.__working_check(self.address): raise EnvironmentError('Chip seems to be broken') else: print('Operation test - success!') else: raise ValueError('Wrong LPS331AP address')
def __init__(self, device=0, i2c=None, thresholds=None, thresholdSamples=3): super(SensorSHT25, self).__init__(thresholds=thresholds, thresholdSamples=thresholdSamples) # SHT25 address, 0x40(64) self.address = 0x40 if device != 0: raise Exception('Device number must be 0!') if i2c is not None: self.i2c = i2c else: self.i2c = onionI2C.OnionI2C()
def __init__(self, mode=SINGLE_HRES1, addr=0x23): if mode not in [Cont_HRES1, Cont_HRES2, Cont_LRES, SINGLE_HRES1, SINGLE_HRES2, SINGLE_LRES]: raise ValueError( 'Unexpected sample mode value {0}. Set mode to one of XXX'.format(mode)) Delay = 0.0 if mode == Cont_LRES: Delay = DELAY_LMODE elif mode == SINGLE_LRES: Delay = DELAY_LMODE else Delay = DELAY_HMODE self.mode = mode self.i2c = onionI2C.OnionI2C() self.addr = addr
def __init__(self, chip, i2cAddress, regScheme = '16bit'): self.i2cAddress = i2cAddress self.bus = onionI2C.OnionI2C(0) # set the verbosity self.bus.setVerbosity(-1) self.chip = chip if self.chip == 'MCP23008': self.bank = '8bit' else: self.bank = regScheme self.callBackFuncts=[] for i in range(0,17): self.callBackFuncts.append(['empty','emtpy']) return
def __init__(self, device=0, i2c=None, thresholds=None, thresholdSamples=3): super(SensorSHT31, self).__init__(thresholds=thresholds, thresholdSamples=thresholdSamples) # SHT31 address, 0x44(68) or 0x45(69) self.address = 0x44 if device == 1: self.address = self.address + 1 elif device not in [0, 1]: raise Exception('Device number must be 0 or 1!') if i2c is not None: self.i2c = i2c else: self.i2c = onionI2C.OnionI2C()
def __init__(self): self.i2c = onionI2C.OnionI2C() self.gscale = GFS_250DPS self.ascale = AFS_2G self.gres = 0 self.ares = 0 self.Self_test = array('f', [0, 0, 0, 0, 0, 0]) self.gx = 0 self.gy = 0 self.gz = 0 self.ax = 0 self.ay = 0 self.az = 0 self.g_bias = array('f', [0, 0, 0]) self.a_bias = array('f', [0, 0, 0])
def __init__(self, addr=0x76, tMode=T_OSample_1, pMode=P_OSample_1, hMode=H_OSample_1): #Check if the Modes are valid if tMode not in [ T_OSample_1, T_OSample_2, T_OSample_4, T_OSample_8, T_OSample_16 ]: raise ValueError( 'Unexpected Temperature mode value {0}. Set mode to one of BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or BME280_ULTRAHIGHRES' .format(mode)) self._tMode = tMode if pMode not in [ P_OSample_1, P_OSample_2, P_OSample_4, P_OSample_8, P_OSample_16 ]: raise ValueError( 'Unexpected Pressure mode value {0}. Set mode to one of BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or BME280_ULTRAHIGHRES' .format(mode)) self._pMode = pMode if hMode not in [ H_OSample_1, H_OSample_2, H_OSample_4, H_OSample_8, H_OSample_16 ]: raise ValueError( 'Unexpected Humidity mode value {0}. Set mode to one of BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or BME280_ULTRAHIGHRES' .format(mode)) self._hMode = hMode #I2C Bus init self._i2c = onionI2C.OnionI2C() #I2C Addr init self._addr = addr self._load_calibration() self._bme280_Setup() #self._i2c.writeByte(0x76, 0xF4, 0x3F) self.t_fine = 0.0
def __init__(self, address, port=0): self.address = address self.i2c = onionI2C.OnionI2C(port) # lcd defaults self.lcdbacklight = LCD_BACKLIGHT #default status self.line1 = "" self.line2 = "" self.line3 = "" self.line4 = "" self.lcdWrite(0x03) self.lcdWrite(0x03) self.lcdWrite(0x03) self.lcdWrite(0x02) self.lcdWrite(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE) self.lcdWrite(LCD_DISPLAYCONTROL | LCD_DISPLAYON) self.lcdWrite(lcdClearDISPLAY) self.lcdWrite(LCD_ENTRYMODESET | LCD_ENTRYLEFT) time.sleep(initSleep)
def __init__(self): super().__init__() self.metadata['Name'] = 'SHT31' self.metadata['Type'] = 'Temperature_RH' self.metadata['Mfg'] = 'ACME' self.metadata['Model'] = 'SHT31-D' print("Name: ", self.metadata['Name']) # Get I2C bus self.i2c = onionI2C.OnionI2C() rh = {} rh['value'] = -9999.0 rh['units'] = '%' rh['error'] = 2 self._set_rh(rh) temp = {} temp['value'] = -9999.0 temp['units'] = 'C' temp['error'] = 0.5 self._set_temperature(temp)
def __init__(self): super().__init__() self.metadata['Name'] = 'HumiChip' self.metadata['Type'] = 'Temperature_RH' self.metadata['Mfg'] = 'Samyoung S&C / ncd.com' self.metadata['Model'] = 'HCPA_5V_U3' print("Name: ", self.metadata['Name']) # Get I2C bus self.i2c = onionI2C.OnionI2C() rh = {} rh['value'] = -9999.0 rh['units'] = '%' rh['error'] = 3 self._set_rh(rh) temp = {} temp['value'] = -9999.0 temp['units'] = 'C' temp['error'] = 0.3 self._set_temperature(temp)
def __init__(self, SCALE_A=SCALE_A_8G, LSM_ACC_ADDR=0x19, SCALE_M=SCALE_M_81G, LSM_MAG_ADDR=0x1E): if SCALE_A not in [SCALE_A_2G, SCALE_A_4G, SCALE_A_8G, SCALE_A_16G]: raise ValueError( 'Unexpected Temperature mode value {0}. Set mode to one of XXXX' .format(mode)) self._Scale_A = SCALE_A if SCALE_M not in [ SCALE_M_13G, SCALE_M_19G, SCALE_M_25G, SCALE_M_40G, SCALE_M_47G, SCALE_M_56G, SCALE_M_81G ]: raise ValueError( 'Unexpected Temperature mode value {0}. Set mode to one of XXXX' .format(mode)) self._Scale_M = SCALE_M #I2C Bus init self._i2c = onionI2C.OnionI2C() self.setup_acc() self.setup_mag()
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. i2c = onionI2C.OnionI2C() self._accel = i2c.get_i2c_device(accel_address, **kwargs) self._mag = i2c.get_i2c_device(mag_address, **kwargs) # Enable the accelerometer self._accel.write8(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) else: self._accel.write8(LSM303_REGISTER_ACCEL_CTRL_REG4_A, 0) # Enable the magnetometer self._mag.write8(LSM303_REGISTER_MAG_MR_REG_M, 0x00)
def __init__(self, addr, port=0): self.addr = addr self.i2c = onionI2C.OnionI2C(port)
def __init__(self, address, port=0): self.address = address self.i2c = onionI2C.OnionI2C(port)
def __init__(self, twi=0, addr=0x68, at24c32_addr=0x56): # self._bus = smbus.SMBus(twi) self._i2c = onionI2C.OnionI2C(twi) self._addr = addr self._at24c32_addr = at24c32_addr
class mpu6050: # Global Variables GRAVITIY_MS2 = 9.80665 address = None bus = onionI2C.OnionI2C() # Scale Modifiers ACCEL_SCALE_MODIFIER_2G = 16384.0 ACCEL_SCALE_MODIFIER_4G = 8192.0 ACCEL_SCALE_MODIFIER_8G = 4096.0 ACCEL_SCALE_MODIFIER_16G = 2048.0 GYRO_SCALE_MODIFIER_250DEG = 131.0 GYRO_SCALE_MODIFIER_500DEG = 65.5 GYRO_SCALE_MODIFIER_1000DEG = 32.8 GYRO_SCALE_MODIFIER_2000DEG = 16.4 # Pre-defined ranges ACCEL_RANGE_2G = 0x00 ACCEL_RANGE_4G = 0x08 ACCEL_RANGE_8G = 0x10 ACCEL_RANGE_16G = 0x18 GYRO_RANGE_250DEG = 0x00 GYRO_RANGE_500DEG = 0x08 GYRO_RANGE_1000DEG = 0x10 GYRO_RANGE_2000DEG = 0x18 # MPU-6050 Registers PWR_MGMT_1 = 0x6B PWR_MGMT_2 = 0x6C SELF_TEST_X = 0x0D SELF_TEST_Y = 0x0E SELF_TEST_Z = 0x0F SELF_TEST_A = 0x10 ACCEL_XOUT0 = 0x3B ACCEL_XOUT1 = 0x3C ACCEL_YOUT0 = 0x3D ACCEL_YOUT1 = 0x3E ACCEL_ZOUT0 = 0x3F ACCEL_ZOUT1 = 0x40 TEMP_OUT0 = 0x41 TEMP_OUT1 = 0x42 GYRO_XOUT0 = 0x43 GYRO_XOUT1 = 0x44 GYRO_YOUT0 = 0x45 GYRO_YOUT1 = 0x46 GYRO_ZOUT0 = 0x47 GYRO_ZOUT1 = 0x48 ACCEL_CONFIG = 0x1C GYRO_CONFIG = 0x1B def __init__(self, address): self.address = address # Wake up the MPU-6050 since it starts in sleep mode self.bus.writeByte(self.address, self.PWR_MGMT_1, 0x00) # I2C communication methods def read_i2c_word(self, register): """Read two i2c registers and combine them. register -- the first register to read from. Returns the combined read results. """ # Read the data from the registers data = self.bus.readBytes(self.address, register, 2) high = data[0] low = data[1] value = (high << 8) + low if (value >= 0x8000): return -((65535 - value) + 1) else: return value # MPU-6050 Methods def get_temp(self): """Reads the temperature from the onboard temperature sensor of the MPU-6050. Returns the temperature in degrees Celcius. """ raw_temp = self.read_i2c_word(self.TEMP_OUT0) # Get the actual temperature using the formule given in the # MPU-6050 Register Map and Descriptions revision 4.2, page 30 actual_temp = (raw_temp / 340.0) + 36.53 return actual_temp def set_accel_range(self, accel_range): """Sets the range of the accelerometer to range. accel_range -- the range to set the accelerometer to. Using a pre-defined range is advised. """ # First change it to 0x00 to make sure we write the correct value later self.bus.writeByte(self.address, self.ACCEL_CONFIG, 0x00) # Write the new range to the ACCEL_CONFIG register self.bus.writeByte(self.address, self.ACCEL_CONFIG, accel_range) def read_accel_range(self, raw = False): """Reads the range the accelerometer is set to. If raw is True, it will return the raw value from the ACCEL_CONFIG register If raw is False, it will return an integer: -1, 2, 4, 8 or 16. When it returns -1 something went wrong. """ raw_data = self.bus.readBytes(self.address, self.ACCEL_CONFIG, 1)[0] if raw is True: return raw_data elif raw is False: if raw_data == self.ACCEL_RANGE_2G: return 2 elif raw_data == self.ACCEL_RANGE_4G: return 4 elif raw_data == self.ACCEL_RANGE_8G: return 8 elif raw_data == self.ACCEL_RANGE_16G: return 16 else: return -1 def get_accel_data(self, g = False): """Gets and returns the X, Y and Z values from the accelerometer. If g is True, it will return the data in g If g is False, it will return the data in m/s^2 Returns a dictionary with the measurement results. """ x = self.read_i2c_word(self.ACCEL_XOUT0) y = self.read_i2c_word(self.ACCEL_YOUT0) z = self.read_i2c_word(self.ACCEL_ZOUT0) accel_scale_modifier = None accel_range = self.read_accel_range(True) if accel_range == self.ACCEL_RANGE_2G: accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_2G elif accel_range == self.ACCEL_RANGE_4G: accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_4G elif accel_range == self.ACCEL_RANGE_8G: accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_8G elif accel_range == self.ACCEL_RANGE_16G: accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_16G else: print("Unkown range - accel_scale_modifier set to self.ACCEL_SCALE_MODIFIER_2G") accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_2G x = x / accel_scale_modifier y = y / accel_scale_modifier z = z / accel_scale_modifier if g is True: return {'x': x, 'y': y, 'z': z} elif g is False: x = x * self.GRAVITIY_MS2 y = y * self.GRAVITIY_MS2 z = z * self.GRAVITIY_MS2 return {'x': x, 'y': y, 'z': z} def set_gyro_range(self, gyro_range): """Sets the range of the gyroscope to range. gyro_range -- the range to set the gyroscope to. Using a pre-defined range is advised. """ # First change it to 0x00 to make sure we write the correct value later self.bus.writeBytes(self.address, self.GYRO_CONFIG, 0x00) # Write the new range to the ACCEL_CONFIG register self.bus.writeBytes(self.address, self.GYRO_CONFIG, gyro_range) def read_gyro_range(self, raw = False): """Reads the range the gyroscope is set to. If raw is True, it will return the raw value from the GYRO_CONFIG register. If raw is False, it will return 250, 500, 1000, 2000 or -1. If the returned value is equal to -1 something went wrong. """ raw_data = self.bus.readBytes(self.address, self.GYRO_CONFIG, 1)[0] if raw is True: return raw_data elif raw is False: if raw_data == self.GYRO_RANGE_250DEG: return 250 elif raw_data == self.GYRO_RANGE_500DEG: return 500 elif raw_data == self.GYRO_RANGE_1000DEG: return 1000 elif raw_data == self.GYRO_RANGE_2000DEG: return 2000 else: return -1 def get_gyro_data(self): """Gets and returns the X, Y and Z values from the gyroscope. Returns the read values in a dictionary. """ x = self.read_i2c_word(self.GYRO_XOUT0) y = self.read_i2c_word(self.GYRO_YOUT0) z = self.read_i2c_word(self.GYRO_ZOUT0) gyro_scale_modifier = None gyro_range = self.read_gyro_range(True) if gyro_range == self.GYRO_RANGE_250DEG: gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_250DEG elif gyro_range == self.GYRO_RANGE_500DEG: gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_500DEG elif gyro_range == self.GYRO_RANGE_1000DEG: gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_1000DEG elif gyro_range == self.GYRO_RANGE_2000DEG: gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_2000DEG else: print("Unkown range - gyro_scale_modifier set to self.GYRO_SCALE_MODIFIER_250DEG") gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_250DEG x = x / gyro_scale_modifier y = y / gyro_scale_modifier z = z / gyro_scale_modifier return {'x': x, 'y': y, 'z': z} def get_all_data(self): """Reads and returns all the available data.""" temp = get_temp() accel = get_accel_data gyro = get_gyro_data() return [accel, gyro, temp]
def __init__(self): self.i2c = onionI2C.OnionI2C(0) self.device = 0x5a self.tVOC = 0 self.CO2 = 0
from OmegaExpansion import onionI2C import sys devAddr = 0x48 if (len(sys.argv) >= 3): sys.exit ("127\nToo many arguments\n") if (len(sys.argv) == 2): devAddr = int(sys.argv[1],16) sensor = onionI2C.OnionI2C() rawData = sensor.readBytes(devAddr, 0x00, 2) data = rawData[0] * 1.0 + rawData[1]*(0.0625/16) print str(data)
def __init__(self, address): self.address = address self.i2c = onionI2C.OnionI2C() self.i2c.setVerbosity(1)
def __init__(self, address=0x68): self.address = address self.i2c = onionI2C.OnionI2C() # Wake up the MPU-6050 since it starts in sleep mode self.i2c.writeByte(self.address, self.PWR_MGMT_1, 0x00)
def __init__(self, optimize=False): super().__init__() self.i2c = onionI2C.OnionI2C() self.optimize = optimize
def __init__(self): self.i2c = onionI2C.OnionI2C() self.i2c.writeByte(0x39, 0x00 | 0x80, 0x03) self.i2c.writeByte(0x39, 0x01 | 0x80, 0x02) time.sleep(0.5) # not sure if this is necessary? self.previous_value = 0
from OmegaExpansion import AdcExp from OmegaExpansion import onionI2C import time #i2c initialization i2c = onionI2C.OnionI2C() LMP91000_STATUS_REG = (0x00) LMP91000_LOCK_REG = (0x01) LMP91000_TIACN_REG = (0x10) LMP91000_REFCN_REG = (0x11) LMP91000_MODECN_REG = (0x12) # LOCK register bitfield definition LMP91000_WRITE_LOCK = (0x01) LMP91000_WRITE_UNLOCK = (0x00) # STATUS register bitfield definition LMP91000_READY = (0x01) LMP91000_NOT_READY = (0x00) #TIACN register bitfield definition LMP91000_TIA_GAIN_EXT = (0x00) LMP91000_TIA_GAIN_2P75K = (0x04) LMP91000_TIA_GAIN_3P5K = (0x08) LMP91000_TIA_GAIN_7K = (0x0C) LMP91000_TIA_GAIN_14K = (0x10) LMP91000_TIA_GAIN_35K = (0x14) LMP91000_TIA_GAIN_120K = (0x18) LMP91000_TIA_GAIN_350K = (0x1C) LMP91000_RLOAD_10OHM = (0x00)
def __init__(self, address=ADS1x15_DEFAULT_ADDRESS, i2c=None, **kwargs): if i2c is None: from OmegaExpansion import onionI2C self._device = onionI2C.OnionI2C() self._devAddr = address self.continuousGain = 0