class CCS811: """CCS811 gas sensor driver. :param ~busio.I2C i2c: The I2C bus. :param int addr: The I2C address of the CCS811. """ # set up the registers error = i2c_bit.ROBit(0x00, 0) """True when an error has occured.""" data_ready = i2c_bit.ROBit(0x00, 3) """True when new data has been read.""" app_valid = i2c_bit.ROBit(0x00, 4) fw_mode = i2c_bit.ROBit(0x00, 7) hw_id = i2c_bits.ROBits(8, 0x20, 0) int_thresh = i2c_bit.RWBit(0x01, 2) interrupt_enabled = i2c_bit.RWBit(0x01, 3) drive_mode = i2c_bits.RWBits(3, 0x01, 4) temp_offset = 0.0 """Temperature offset.""" def __init__(self, i2c_bus, address=0x5A): self.i2c_device = I2CDevice(i2c_bus, address) # check that the HW id is correct if self.hw_id != _HW_ID_CODE: raise RuntimeError( "Device ID returned is not correct! Please check your wiring.") # try to start the app buf = bytearray(1) buf[0] = 0xF4 with self.i2c_device as i2c: i2c.write(buf, end=1) time.sleep(0.1) # make sure there are no errors and we have entered application mode if self.error: raise RuntimeError( "Device returned a error! Try removing and reapplying power to " "the device and running the code again.") if not self.fw_mode: raise RuntimeError( "Device did not enter application mode! If you got here, there may " "be a problem with the firmware on your sensor.") self.interrupt_enabled = False # default to read every second self.drive_mode = DRIVE_MODE_1SEC self._eco2 = None # pylint: disable=invalid-name self._tvoc = None # pylint: disable=invalid-name @property def error_code(self): """Error code""" buf = bytearray(2) buf[0] = 0xE0 with self.i2c_device as i2c: i2c.write_then_readinto(buf, buf, out_end=1, in_start=1) return buf[1] def _update_data(self): if self.data_ready: buf = bytearray(9) buf[0] = _ALG_RESULT_DATA with self.i2c_device as i2c: i2c.write_then_readinto(buf, buf, out_end=1, in_start=1) self._eco2 = (buf[1] << 8) | (buf[2]) self._tvoc = (buf[3] << 8) | (buf[4]) if self.error: raise RuntimeError("Error:" + str(self.error_code)) @property def tvoc(self): # pylint: disable=invalid-name """Total Volatile Organic Compound in parts per billion.""" self._update_data() return self._tvoc @property def eco2(self): # pylint: disable=invalid-name """Equivalent Carbon Dioxide in parts per million. Clipped to 400 to 8192ppm.""" self._update_data() return self._eco2 @property def temperature(self): """ .. deprecated:: 1.1.5 Hardware support removed by vendor Temperature based on optional thermistor in Celsius.""" buf = bytearray(5) buf[0] = _NTC with self.i2c_device as i2c: i2c.write_then_readinto(buf, buf, out_end=1, in_start=1) vref = (buf[1] << 8) | buf[2] vntc = (buf[3] << 8) | buf[4] # From ams ccs811 app note 000925 # https://download.ams.com/content/download/9059/13027/version/1/file/CCS811_Doc_cAppNote-Connecting-NTC-Thermistor_AN000372_v1..pdf rntc = float(vntc) * _REF_RESISTOR / float(vref) ntc_temp = math.log(rntc / 10000.0) ntc_temp /= 3380.0 ntc_temp += 1.0 / (25 + 273.15) ntc_temp = 1.0 / ntc_temp ntc_temp -= 273.15 return ntc_temp - self.temp_offset def set_environmental_data(self, humidity, temperature): """Set the temperature and humidity used when computing eCO2 and TVOC values. :param int humidity: The current relative humidity in percent. :param float temperature: The current temperature in Celsius.""" # Humidity is stored as an unsigned 16 bits in 1/512%RH. The default # value is 50% = 0x64, 0x00. As an example 48.5% humidity would be 0x61, # 0x00. humidity = int(humidity * 512) # Temperature is stored as an unsigned 16 bits integer in 1/512 degrees # there is an offset: 0 maps to -25C. The default value is 25C = 0x64, # 0x00. As an example 23.5% temperature would be 0x61, 0x00. temperature = int((temperature + 25) * 512) buf = bytearray(5) buf[0] = _ENV_DATA struct.pack_into(">HH", buf, 1, humidity, temperature) with self.i2c_device as i2c: i2c.write(buf) def set_interrupt_thresholds(self, low_med, med_high, hysteresis): """Set the thresholds used for triggering the interrupt based on eCO2. The interrupt is triggered when the value crossed a boundary value by the minimum hysteresis value. :param int low_med: Boundary between low and medium ranges :param int med_high: Boundary between medium and high ranges :param int hysteresis: Minimum difference between reads""" buf = bytearray([ _THRESHOLDS, ((low_med >> 8) & 0xF), (low_med & 0xF), ((med_high >> 8) & 0xF), (med_high & 0xF), hysteresis, ]) with self.i2c_device as i2c: i2c.write(buf) def reset(self): """Initiate a software reset.""" # reset sequence from the datasheet seq = bytearray([_SW_RESET, 0x11, 0xE5, 0x72, 0x8A]) with self.i2c_device as i2c: i2c.write(seq)
class Encoder: def __init__(self, i2c, device_address): self.i2c_device = I2CDevice(i2c, device_address) def __enter__(self): return self def __exit__(self, *exc): return False # GCONF gconf_dtype = i2c_bit.RWBit(_REG_GCONF, 0x00) gconf_wrape = i2c_bit.RWBit(_REG_GCONF, 0x01) gconf_dire = i2c_bit.RWBit(_REG_GCONF, 0x02) gconf_ipud = i2c_bit.RWBit(_REG_GCONF, 0x03) gconf_rmod = i2c_bit.RWBit(_REG_GCONF, 0x04) gconf_etype = i2c_bit.RWBit(_REG_GCONF, 0x05) gconf_mbank = i2c_bit.RWBit(_REG_GCONF, 0x06) gconf_rst = i2c_bit.RWBit(_REG_GCONF, 0x07) # GCONF2 gconf2_cksrc = i2c_bit.RWBit(_REG_GCONF, 0x00) gconf2_relmod = i2c_bit.RWBit(_REG_GCONF, 0x01) # GP1CONF gp1conf = i2c_bits.RWBits(8, _REG_GP1CONF, 0x00) gp1conf_mode = i2c_bits.RWBits(2, _REG_GP1CONF, 0x00) gp1conf_pul = i2c_bit.RWBit(_REG_GP1CONF, 0x02) gp1conf_int = i2c_bits.RWBits(2, _REG_GP1CONF, 0x03) # GP2CONF gp2conf = i2c_bits.RWBits(8, _REG_GP2CONF, 0x00) gp2conf_mode = i2c_bits.RWBits(2, _REG_GP2CONF, 0x00) gp2conf_pul = i2c_bit.RWBit(_REG_GP2CONF, 0x02) gp2conf_int = i2c_bits.RWBits(2, _REG_GP2CONF, 0x03) # GP3CONF gp3conf = i2c_bits.RWBits(8, _REG_GP3CONF, 0x00) gp3conf_mode = i2c_bits.RWBits(2, _REG_GP3CONF, 0x00) gp3conf_pul = i2c_bit.RWBit(_REG_GP3CONF, 0x02) gp3conf_int = i2c_bits.RWBits(2, _REG_GP3CONF, 0x03) # INTCONF intconf_ipushr = i2c_bit.RWBit(_REG_INTCONF, 0x00) intconf_ipushp = i2c_bit.RWBit(_REG_INTCONF, 0x01) intconf_ipushd = i2c_bit.RWBit(_REG_INTCONF, 0x02) intconf_irinc = i2c_bit.RWBit(_REG_INTCONF, 0x03) intconf_irdec = i2c_bit.RWBit(_REG_INTCONF, 0x04) intconf_irmax = i2c_bit.RWBit(_REG_INTCONF, 0x05) intconf_irmin = i2c_bit.RWBit(_REG_INTCONF, 0x06) intconf_int2 = i2c_bit.RWBit(_REG_INTCONF, 0x07) # ESTATUS - TODO: Break this into a set of flags estatus = i2c_bits.ROBits(8, _REG_ESTATUS, 0x00) # I2STATUS - TODO: Break this into a set of flags i2status = i2c_bits.ROBits(8, _REG_I2STATUS, 0x00) # FSTATUS - TODO: Break this into a set of flags fstatus = i2c_bits.ROBits(8, _REG_FSTATUS, 0x00) # CVAL FLOAT cval_float = i2c_struct.UnaryStruct(_REG_CVAL, ">f") # CVAL LONG cval_long = i2c_struct.UnaryStruct(_REG_CVAL, ">l") # CMAX FLOAT cmax_float = i2c_struct.UnaryStruct(_REG_CMAX, ">f") # CMAX LONG cmax_long = i2c_struct.UnaryStruct(_REG_CMAX, ">l") # CMIN FLOAT cmin_float = i2c_struct.UnaryStruct(_REG_CMIN, ">f") # CMIN LONG cmin_long = i2c_struct.UnaryStruct(_REG_CMIN, ">l") # ISTEP FLOAT istep_float = i2c_struct.UnaryStruct(_REG_ISTEP, ">f") # ISTEP INT istep_long = i2c_struct.UnaryStruct(_REG_ISTEP, ">l") # R/G/B LED rled = i2c_bits.RWBits(8, _REG_RLED, 0x00) gled = i2c_bits.RWBits(8, _REG_GLED, 0x00) bled = i2c_bits.RWBits(8, _REG_BLED, 0x00) # GP 1/2/3 gp1 = i2c_bits.RWBits(8, _REG_GP1, 0x00) gp2 = i2c_bits.RWBits(8, _REG_GP2, 0x00) gp3 = i2c_bits.RWBits(8, _REG_GP3, 0x00) # ANTI BOUNCE antbounc = i2c_bits.RWBits(8, _REG_ANTBOUNC, 0x00) # DP PERIOD dpperiod = i2c_bits.RWBits(8, _REG_DPPERIOD, 0x00) # FADE RGB fadergb = i2c_bits.RWBits(8, _REG_FADERGB, 0x00) # FADE GP fadegp = i2c_bits.RWBits(8, _REG_FADEGP, 0x00) # GAMMA R/G/B gamrled = i2c_bits.RWBits(3, _REG_GAMRLED, 0x00) gamgled = i2c_bits.RWBits(3, _REG_GAMGLED, 0x00) gambled = i2c_bits.RWBits(3, _REG_GAMBLED, 0x00) # GAMMA GP 1/2/3 gammagp1 = i2c_bits.RWBits(3, _REG_GAMMAGP1, 0x00) gammagp2 = i2c_bits.RWBits(3, _REG_GAMMAGP2, 0x00) gammagp3 = i2c_bits.RWBits(3, _REG_GAMMAGP3, 0x00) # ID CODE idcode = i2c_bits.ROBits(8, _REG_IDCODE, 0x00) # VERSION version = i2c_bits.ROBits(8, _REG_VERSION, 0x00) # EEPROM eeprom = i2c_bits.ROBits(1024, _REG_EEPROM, 0x00, register_width=128)
class CCS811: """CCS811 gas sensor driver. :param ~busio.I2C i2c: The I2C bus. :param int addr: The I2C address of the CCS811. """ # set up the registers error = i2c_bit.ROBit(0x00, 0) """True when an error has occured.""" data_ready = i2c_bit.ROBit(0x00, 3) """True when new data has been read.""" app_valid = i2c_bit.ROBit(0x00, 4) fw_mode = i2c_bit.ROBit(0x00, 7) hw_id = i2c_bits.ROBits(8, 0x20, 0) int_thresh = i2c_bit.RWBit(0x01, 2) interrupt_enabled = i2c_bit.RWBit(0x01, 3) drive_mode = i2c_bits.RWBits(3, 0x01, 4) def __init__(self, i2c_bus, address=0x5A): self.i2c_device = I2CDevice(i2c_bus, address) # check that the HW id is correct if self.hw_id != _HW_ID_CODE: raise RuntimeError( "Device ID returned is not correct! Please check your wiring.") # reset before starting Print("Reseting") self.reset() time.sleep(10) # try to start the app buf = bytearray(1) buf[0] = 0xF4 with self.i2c_device as i2c: i2c.write(buf, end=1) time.sleep(10) # make sure there are no errors and we have entered application mode if self.error: raise RuntimeError( "Device returned a error! Try removing and reapplying power to " "the device and running the code again.") if not self.fw_mode: raise RuntimeError( "Device did not enter application mode! If you got here, there may " "be a problem with the firmware on your sensor.") self.interrupt_enabled = False # default to read every minute self.drive_mode = DRIVE_MODE_60SEC self._eco2 = None # pylint: disable=invalid-name self._tvoc = None # pylint: disable=invalid-name @property def error_code(self): """Error code""" buf = bytearray(2) buf[0] = 0xE0 with self.i2c_device as i2c: i2c.write_then_readinto(buf, buf, out_end=1, in_start=1) return buf[1] def _update_data(self): if self.data_ready: buf = bytearray(9) buf[0] = _ALG_RESULT_DATA with self.i2c_device as i2c: i2c.write_then_readinto(buf, buf, out_end=1, in_start=1) self._eco2 = (buf[1] << 8) | (buf[2]) self._tvoc = (buf[3] << 8) | (buf[4]) if self.error: raise RuntimeError("Error:" + str(self.error_code)) @property def baseline(self): """ The propery reads and returns the current baseline value. The returned value is packed into an integer. Later the same integer can be used in order to set a new baseline. """ buf = bytearray(3) buf[0] = _BASELINE with self.i2c_device as i2c: i2c.write_then_readinto(buf, buf, out_end=1, in_start=1) return struct.unpack("<H", buf[1:])[0] @baseline.setter def baseline(self, baseline_int): """ The property lets you set a new baseline. As a value accepts integer which represents packed baseline 2 bytes value. """ buf = bytearray(3) buf[0] = _BASELINE struct.pack_into("<H", buf, 1, baseline_int) with self.i2c_device as i2c: i2c.write(buf) @property def tvoc(self): # pylint: disable=invalid-name """Total Volatile Organic Compound in parts per billion.""" self._update_data() return self._tvoc @property def eco2(self): # pylint: disable=invalid-name """Equivalent Carbon Dioxide in parts per million. Clipped to 400 to 8192ppm.""" self._update_data() return self._eco2 def set_mode(self, mode): # change operating mode print("Idle for 20min before changing to new mode") self.drive_mode = DRIVE_MODE_IDLE time.sleep(1200) self.drive_mode = mode def set_environmental_data(self, humidity, temperature): """Set the temperature and humidity used when computing eCO2 and TVOC values. :param int humidity: The current relative humidity in percent. :param float temperature: The current temperature in Celsius.""" # Humidity is stored as an unsigned 16 bits in 1/512%RH. The default # value is 50% = 0x64, 0x00. As an example 48.5% humidity would be 0x61, # 0x00. humidity = int(humidity * 512) # Temperature is stored as an unsigned 16 bits integer in 1/512 degrees # there is an offset: 0 maps to -25C. The default value is 25C = 0x64, # 0x00. As an example 23.5% temperature would be 0x61, 0x00. temperature = int((temperature + 25) * 512) buf = bytearray(5) buf[0] = _ENV_DATA struct.pack_into(">HH", buf, 1, humidity, temperature) with self.i2c_device as i2c: i2c.write(buf) def set_interrupt_thresholds(self, low_med, med_high, hysteresis): """Set the thresholds used for triggering the interrupt based on eCO2. The interrupt is triggered when the value crossed a boundary value by the minimum hysteresis value. :param int low_med: Boundary between low and medium ranges :param int med_high: Boundary between medium and high ranges :param int hysteresis: Minimum difference between reads""" buf = bytearray([ _THRESHOLDS, ((low_med >> 8) & 0xF), (low_med & 0xF), ((med_high >> 8) & 0xF), (med_high & 0xF), hysteresis, ]) with self.i2c_device as i2c: i2c.write(buf) def reset(self): """Initiate a software reset.""" # reset sequence from the datasheet seq = bytearray([_SW_RESET, 0x11, 0xE5, 0x72, 0x8A]) with self.i2c_device as i2c: i2c.write(seq)
class Adafruit_CCS811: #set up the registers #self.status = Adafruit_bitfield([('ERROR' , 1), ('unused', 2), ('DATA_READY' , 1), ('APP_VALID', 1), ('unused2' , 2), ('FW_MODE' , 1)]) error = i2c_bit.ROBit(0x00, 0) data_ready = i2c_bit.ROBit(0x00, 3) app_valid = i2c_bit.ROBit(0x00, 4) fw_mode = i2c_bit.ROBit(0x00, 7) hw_id = i2c_bits.ROBits(8, 0x20, 0) #self.meas_mode = Adafruit_bitfield([('unused', 2), ('INT_THRESH', 1), ('INT_DATARDY', 1), ('DRIVE_MODE', 3)]) int_thresh = i2c_bit.RWBit(0x01, 2) interrupt_enabled = i2c_bit.RWBit(0x01, 3) drive_mode = i2c_bits.RWBits(3, 0x01, 4) #self.error_id = Adafruit_bitfield([('WRITE_REG_INVALID', 1), ('READ_REG_INVALID', 1), ('MEASMODE_INVALID', 1), ('MAX_RESISTANCE', 1), ('HEATER_FAULT', 1), ('HEATER_SUPPLY', 1)]) TVOC = 0 eCO2 = 0 tempOffset = 0.0 def __init__(self, i2c, addr=0x5A): self.i2c_device = I2CDevice(i2c, addr) #check that the HW id is correct if self.hw_id != CCS811_HW_ID_CODE: raise RuntimeError( "Device ID returned is not correct! Please check your wiring.") #try to start the app buf = bytearray(1) buf[0] = 0xF4 self.i2c_device.write(buf, end=1, stop=True) time.sleep(.1) #make sure there are no errors and we have entered application mode if self.checkError(): raise RuntimeError( "Device returned an Error! Try removing and reapplying power to the device and running the code again." ) if not self.fw_mode: raise RuntimeError( "Device did not enter application mode! If you got here, there may be a problem with the firmware on your sensor." ) self.interrupt_enabled = False #default to read every second self.setDriveMode(CCS811_DRIVE_MODE_1SEC) def setDriveMode(self, mode): self.drive_mode = mode def available(self): return self.data_ready def readData(self): if not self.data_ready: return False else: buf = bytearray(9) buf[0] = CCS811_ALG_RESULT_DATA self.i2c_device.write(buf, end=1, stop=False) self.i2c_device.read_into(buf, start=1) self.eCO2 = (buf[1] << 8) | (buf[2]) self.TVOC = (buf[3] << 8) | (buf[4]) if self.error: return buf[6] else: return 0 def setEnvironmentalData(self, humidity, temperature): ''' Humidity is stored as an unsigned 16 bits in 1/512%RH. The default value is 50% = 0x64, 0x00. As an example 48.5% humidity would be 0x61, 0x00.''' ''' Temperature is stored as an unsigned 16 bits integer in 1/512 degrees there is an offset: 0 maps to -25C. The default value is 25C = 0x64, 0x00. As an example 23.5% temperature would be 0x61, 0x00. The internal algorithm uses these values (or default values if not set by the application) to compensate for changes in relative humidity and ambient temperature.''' hum_perc = humidity << 1 parts = math.fmod(temperature) fractional = parts[0] temperature = parts[1] temp_high = ((temperature + 25) << 9) temp_low = ((fractional / 0.001953125) & 0x1FF) temp_conv = (temp_high | temp_low) buf = bytearray([ CCS811_ENV_DATA, hum_perc, 0x00, ((temp_conv >> 8) & 0xFF), (temp_conv & 0xFF) ]) self.i2c_device.write(buf) #calculate temperature based on the NTC register def calculateTemperature(self): buf = bytearray(5) buf[0] = CCS811_NTC self.i2c_device.write(buf, end=1, stop=False) self.i2c_device.read_into(buf, start=1) vref = (buf[1] << 8) | buf[2] vntc = (buf[3] << 8) | buf[4] #from ams ccs811 app note rntc = float(vntc) * CCS811_REF_RESISTOR / float(vref) ntc_temp = math.log(rntc / 10000.0) ntc_temp /= 3380.0 ntc_temp += 1.0 / (25 + 273.15) ntc_temp = 1.0 / ntc_temp ntc_temp -= 273.15 return ntc_temp - self.tempOffset def setThresholds(self, low_med, med_high, hysteresis): buf = bytearray([ CCS811_THRESHOLDS, ((low_med >> 8) & 0xF), (low_med & 0xF), ((med_high >> 8) & 0xF), (med_high & 0xF), hysteresis ]) self.i2c_device.write(buf) def SWReset(self): #reset sequence from the datasheet seq = bytearray([CCS811_SW_RESET, 0x11, 0xE5, 0x72, 0x8A]) self.i2c_device.write(seq) def checkError(self): return self.error