def conduct(self): if self.verbose: print("OPC...", file=sys.stderr) opc = None try: I2C.open(Host.I2C_SENSORS) # resources... opc = OPCN2(Host.opc_spi_bus(), Host.opc_spi_device()) opc.power_on() opc.operations_on() # test... self.datum = opc.firmware() if self.verbose: print(self.datum, file=sys.stderr) # test criterion... return len(self.datum) > 0 and self.datum.startswith('OPC') finally: if opc: opc.operations_off() opc.power_off() I2C.close()
def conduct(self): if self.verbose: print("EEPROM...", file=sys.stderr) # validate... if not path.isfile(Host.eep_image()): print("error: eeprom image not found", file=sys.stderr) exit(1) try: # resources... Host.enable_eeprom_access() I2C.open(Host.I2C_EEPROM) eeprom = CAT24C32() # test... file_image = EEPROMImage.construct_from_file( Host.eep_image(), CAT24C32.SIZE) eeprom.write(file_image) # test criterion... return eeprom.image == file_image finally: I2C.close()
def conduct(self): if self.verbose: print("GPS...", file=sys.stderr) gps = None try: I2C.open(Host.I2C_SENSORS) # GPS... gps = PAM7Q(Host.gps_device()) gps.power_on() gps.open() # test... self.datum = gps.report(GPRMC) if self.verbose: print(self.datum, file=sys.stderr) # criterion... return self.datum is not None finally: if gps: gps.close() gps.power_off() I2C.close()
def conduct(self): if self.verbose: print("AFE...", file=sys.stderr) try: I2C.open(Host.I2C_SENSORS) # AFE... dfe_conf = DFEConf.load(Host) afe = dfe_conf.afe(Host) # test... self.datum = afe.sample() if self.verbose: print(self.datum, file=sys.stderr) ok = True # test criterion... for gas, sensor in self.datum.sns.items(): sensor_ok = 0.9 < sensor.we_v < 1.1 and 0.9 < sensor.ae_v < 1.1 if not sensor_ok: ok = False return ok finally: I2C.close()
def conduct(self): if self.verbose: print("RTC...", file=sys.stderr) try: I2C.open(Host.I2C_SENSORS) # resources... now = LocalizedDatetime.now() DS1338.init() # test... rtc_datetime = RTCDatetime.construct_from_localized_datetime(now) DS1338.set_time(rtc_datetime) time.sleep(2) rtc_datetime = DS1338.get_time() localized_datetime = rtc_datetime.as_localized_datetime( tzlocal.get_localzone()) self.datum = localized_datetime - now if self.verbose: print(self.datum, file=sys.stderr) # test criterion... return 1 <= self.datum.seconds <= 2 finally: I2C.close()
def __read_reg(cls, addr): try: I2C.start_tx(cls.__ADDR) value = I2C.read_cmd(addr, 1) finally: I2C.end_tx() return value
def status(self): try: I2C.start_tx(self.__addr) status_msb, status_lsb, _ = I2C.read_cmd16(SHT31.__CMD_READ_STATUS, 3) return (status_msb << 8) | status_lsb finally: I2C.end_tx()
def __read_image(cls, memory_addr, count): try: I2C.start_tx(Host.DFE_EEPROM_ADDR) content = I2C.read_cmd(memory_addr, count) return EEPROMImage(content) finally: I2C.end_tx()
def __read_raw(self): try: I2C.start_tx(self.__I2C_ADDR) values = I2C.read_cmd(self.__reg_addr, 2) return values[0] << 8 | values[1] finally: I2C.end_tx()
def read(self): try: I2C.start_tx(self.__addr) byte = I2C.read(1) finally: I2C.end_tx() return byte
def __read_config(self): try: I2C.start_tx(self.__addr) msb, lsb = I2C.read_cmd(ADS1115.__REG_CONFIG, 2) finally: I2C.end_tx() config = (msb << 8) | lsb return config
def sample(self): try: I2C.start_tx(self.__addr) temp_msb, temp_lsb, _, humid_msb, humid_lsb, _ = I2C.read_cmd16( SHT31.__CMD_READ_SINGLE_HIGH, 6) raw_humid = (humid_msb << 8) | humid_lsb raw_temp = (temp_msb << 8) | temp_lsb return SHTDatum(SHT31.humid(raw_humid), SHT31.temp(raw_temp)) finally: I2C.end_tx()
def __read_temp(cls): try: I2C.start_tx(cls.__ADDR) msb, lsb = I2C.read_cmd(cls.__REG_TEMP, 2) finally: I2C.end_tx() # render voltage... unsigned_c = float(msb & 0x1f) * 16 + float(lsb) / 16 sign = msb & 0x10 temp = 256 - unsigned_c if sign else unsigned_c return temp
def sample(self): if self.__addr == 0: return None try: I2C.start_tx(self.__addr) temp_msb, temp_lsb, _, humid_msb, humid_lsb, _ = I2C.read_cmd16(SHT31.__CMD_READ_SINGLE_HIGH, 6, wait=self.MEASUREMENT_DURATION) raw_humid = (humid_msb << 8) | humid_lsb raw_temp = (temp_msb << 8) | temp_lsb return SHTDatum(SHT31.humid(raw_humid), SHT31.temp(raw_temp)) finally: I2C.end_tx()
def read_conversion(self): """ read most recent conversion returned value is voltage """ try: I2C.start_tx(self.addr) v, config = self.__read() finally: I2C.end_tx() self.release_lock() if config & MCP342X.__START: raise ValueError(self.__class__.__name__ + ":read_conversion: conversion not ready.") return v
def __read(self, command, wait, count=0): try: self.obtain_lock() try: I2C.start_tx(self.__i2c_addr) encoded = I2C.read_cmd16(command, count) values = self.__decode(encoded) finally: I2C.end_tx() time.sleep(wait) return values finally: self.release_lock()
def __read_conv(self): try: I2C.start_tx(self.__addr) msb, lsb = I2C.read_cmd(ADS1115.__REG_CONV, 2) finally: I2C.end_tx() # render voltage... unsigned = (msb << 8) | lsb # print("unsigned: 0x%04x" % unsigned) signed = struct.unpack('h', struct.pack('H', unsigned)) v = (signed[0] / 32767.5) * ADS1115.__FULL_SCALE[self.__gain] return v
def reset(self): try: I2C.start_tx(self.__addr) I2C.write16(SHT31.__CMD_RESET) time.sleep(0.001) I2C.write16(SHT31.__CMD_CLEAR) time.sleep(0.001) finally: I2C.end_tx()
def __read_array(cls, device_addr, memory_addr, count): try: I2C.start_tx(Host.DFE_UID_ADDR) # I2C.read(1) finally: I2C.end_tx() try: I2C.start_tx(Host.DFE_UID_ADDR) # I2C.write(0x80) return I2C.read_cmd(0x80, count) # memory_addr, finally: I2C.end_tx()
def __read(self): # get data... msb, lsb, config = I2C.read(3) unsigned = (msb << 8) | lsb # render voltage... signed = struct.unpack('h', struct.pack('H', unsigned)) v = (signed[0] / 32767.5) * 2.048 / MCP342X.__GAIN[self.__gain] return v, config
def conduct(self, ): if self.verbose: print("%s (0x%02x)..." % (self.__name, self.__sht.addr), file=sys.stderr) try: I2C.open(Host.I2C_SENSORS) # test... self.__sht.reset() self.datum = self.__sht.sample() if self.verbose: print(self.datum, file=sys.stderr) # criterion... return 10 < self.datum.humid < 90 and 10 < self.datum.temp < 50 finally: I2C.close()
def __write_config(self, config): try: I2C.start_tx(self.__addr) I2C.write(ADS1115.__REG_CONFIG, config >> 8, config & 0xff) finally: I2C.end_tx()
def __write(self, config): try: I2C.start_tx(self.addr) I2C.write(config) finally: I2C.end_tx()
def convert(cls): try: I2C.start_tx(MPL115A2Reg.__I2C_ADDR) I2C.write(cls.__CMD_START_CONV, 0x00) finally: I2C.end_tx()
def write(self, byte): try: I2C.start_tx(self.__addr) I2C.write(byte) finally: I2C.end_tx()
def conduct(self): if self.verbose: print("Pt1000...", file=sys.stderr) try: I2C.open(Host.I2C_SENSORS) # AFE... afe_conf = AFEConf.load(Host) afe = afe_conf.afe(Host) # test... self.datum = afe.sample_temp() if self.verbose: print(self.datum, file=sys.stderr) # test criterion... return 0.4 < self.datum.v < 0.6 finally: I2C.close()
def __write_image(cls, memory_addr, values): # max 32 values try: I2C.start_tx(Host.DFE_EEPROM_ADDR) I2C.write_addr(memory_addr, *values) time.sleep(cls.__TWR) finally: I2C.end_tx()
def conduct(self): if self.verbose: print("Board temp...", file=sys.stderr) try: I2C.open(Host.I2C_SENSORS) # resources... sensor = MCP9808(True) # test... self.datum = sensor.sample() if self.verbose: print(self.datum, file=sys.stderr) temp = self.datum.temp # test criterion... return 10 < temp < 50 finally: I2C.close()
def convert(self): """ start single-shot conversion, wait for ready, then read warning: creates high level of I2C traffic returned value is voltage """ self.start_conversion() try: I2C.start_tx(self.addr) while True: v, config = self.__read() if not (config & MCP342X.__START): break time.sleep(MCP342X.RATE_240) finally: I2C.end_tx() self.release_lock() return v
def start_sampling(self, frequency): cmd = CmdSampler() # test what this does on a terminal with I2C.open(Host.I2C_SENSORS) as f: system_id = SystemID.load(Host) tag = None if system_id is None else system_id.message_tag() interface_conf = InterfaceConf.load(Host) interface = interface_conf.interface() gas_sensors = interface.gas_sensors(Host) ndir_monitor = None # we don't use alphasenses for that. sht_conf = SHTConf.load(Host) sht = None if sht_conf is None else sht_conf.int_sht() runner = TimedRunner(cmd.interval, cmd.samples) if cmd.semaphore is None \ else ScheduleRunner(cmd.semaphore, False) sampler = GasesSampler(runner, tag, ndir_monitor, sht, gas_sensors) sampler.start() for sample in sampler.samples(): return JSONify.dumps(sample) sampler.stop()