def sample(self): try: self.obtain_lock() self._spi.open() # command... self.__cmd(self.__CMD_READ_HISTOGRAM) chars = self.__read_bytes(62) # time... rec = LocalizedDatetime.now() # bins... bins = [Decode.unsigned_int(chars[i:i + 2], '<') for i in range(0, 32, 2)] # bin MToFs... bin_1_mtof = chars[32] bin_3_mtof = chars[33] bin_5_mtof = chars[34] bin_7_mtof = chars[35] # period... period = Decode.float(chars[44:48], '<') # checksum... required = Decode.unsigned_int(chars[48:50], '<') actual = sum(bins) % 65536 if required != actual: raise ValueError("bad checksum: required: 0x%04x actual: 0x%04x" % (required, actual)) # PMx... try: pm1 = Decode.float(chars[50:54], '<') except TypeError: pm1 = None try: pm2p5 = Decode.float(chars[54:58], '<') except TypeError: pm2p5 = None try: pm10 = Decode.float(chars[58:62], '<') except TypeError: pm10 = None return OPCDatum(self.SOURCE, rec, pm1, pm2p5, pm10, period, bins, bin_1_mtof, bin_3_mtof, bin_5_mtof, bin_7_mtof) finally: self._spi.close() self.release_lock()
def measure_raw(self): try: self.obtain_lock() cmd = SPINDIRx1Cmd.find('mr') response = self._transact(cmd) pile_ref_value = Decode.unsigned_int(response[0:2], '<') pile_act_value = Decode.unsigned_int(response[2:4], '<') thermistor_value = Decode.unsigned_int(response[4:6], '<') return pile_ref_value, pile_act_value, thermistor_value finally: self.release_lock()
def __write_and_verify_reg(self, reg, value): read_value = None try: I2C.Utilities.start_tx(self.__ADDR) for _ in range(3): # write... I2C.Utilities.write_addr(reg, value & 0x00ff, value >> 8) time.sleep(0.001) # read... read_bytes = I2C.Utilities.read_cmd(reg, 2) time.sleep(0.001) read_value = Decode.unsigned_int(read_bytes, '<') if read_value == value: return raise RuntimeError("reg:0x%02x value:0x%04x got:0x%04x" % (reg, value, read_value)) finally: I2C.Utilities.end_tx()
def get_sample_raw(self): try: self.obtain_lock() # report... cmd = SPINDIRx1Cmd.find('sr') response = self._transact(cmd) pile_ref_amplitude = Decode.unsigned_int(response[0:2], '<') pile_act_amplitude = Decode.unsigned_int(response[2:4], '<') thermistor_average = Decode.unsigned_int(response[4:6], '<') return pile_ref_amplitude, pile_act_amplitude, thermistor_average finally: self.release_lock()
def _calib_r_unsigned_int(self, block, index): cmd = SPINDIRx1Cmd.find('cr') cmd.return_count = 2 response = self._transact(cmd, (block, index)) value = Decode.unsigned_int(response, '<') return value
def get_sample_offsets(self): try: self.obtain_lock() # report... cmd = SPINDIRx1Cmd.find('so') response = self._transact(cmd) min_ref_offset = Decode.unsigned_int(response[0:2], '<') min_act_offset = Decode.unsigned_int(response[2:4], '<') max_ref_offset = Decode.unsigned_int(response[4:6], '<') max_act_offset = Decode.unsigned_int(response[6:8], '<') return min_ref_offset, min_act_offset, max_ref_offset, max_act_offset finally: self.release_lock()
def __read_int(self): read_bytes = [] for _ in range(2): time.sleep(OPCN2.__TRANSFER_DELAY) read_bytes.extend(self.__spi.read_bytes(1)) return Decode.unsigned_int(read_bytes)
def input_raw(self): try: self.obtain_lock() cmd = SPINDIRx1Cmd.find('ir') response = self._transact(cmd) v_in_value = Decode.unsigned_int(response, '<') return v_in_value finally: self.release_lock()
def __read_reg(self, reg, signed=False): try: I2C.Utilities.start_tx(self.__ADDR) read_bytes = I2C.Utilities.read_cmd(reg, 2) time.sleep(0.001) return Decode.int(read_bytes, '<') if signed else Decode.unsigned_int( read_bytes, '<') finally: I2C.Utilities.end_tx()
def record_raw(self, deferral, interval, count): try: self.obtain_lock() # start recording... deferral_bytes = Encode.unsigned_int(deferral, '<') interval_bytes = Encode.unsigned_int(interval, '<') count_bytes = Encode.unsigned_int(count, '<') param_bytes = [] param_bytes.extend(deferral_bytes) param_bytes.extend(interval_bytes) param_bytes.extend(count_bytes) cmd = SPINDIRt1f1Cmd.find('rs') self._transact(cmd, param_bytes) # wait... lamp_period = self._calib_r_unsigned_int( 0, NDIRCalib.INDEX_LAMP_PERIOD) execution_time = (lamp_period + deferral + (interval * count)) / 1000 time.sleep(execution_time) # playback... cmd = SPINDIRt1f1Cmd.find('rp') cmd.return_count = count * 10 response = self._transact(cmd) values = [] for i in range(0, cmd.return_count, 10): timestamp = Decode.unsigned_int(response[i:i + 2], '<') pile_ref = Decode.long(response[i + 2:i + 6], '<') pile_act = Decode.long(response[i + 6:i + 10], '<') values.append((timestamp, pile_ref, pile_act)) return values finally: self.release_lock()
def record_raw(self, deferral, interval, count): try: self.obtain_lock() # start recording... deferral_bytes = Encode.unsigned_int(deferral, '<') interval_bytes = Encode.unsigned_int(interval, '<') count_bytes = Encode.unsigned_int(count, '<') param_bytes = [] param_bytes.extend(deferral_bytes) param_bytes.extend(interval_bytes) param_bytes.extend(count_bytes) cmd = SPINDIRx1Cmd.find('rs') self._transact(cmd, param_bytes) # wait... execution_time = cmd.execution_time + (( (interval * count) + deferral) / 1000) # print("execution time: %s" % execution_time, file=sys.stderr) time.sleep(execution_time) # playback... cmd = SPINDIRx1Cmd.find('rp') cmd.return_count = count * 10 response = self._transact(cmd) values = [] for i in range(0, cmd.return_count, 10): timestamp = Decode.unsigned_int(response[i:i + 2], '<') pile_ref = Decode.long(response[i + 2:i + 6], '<') pile_act = Decode.long(response[i + 6:i + 10], '<') values.append((timestamp, pile_ref, pile_act)) return values finally: self.release_lock()
def sample(self): try: self.obtain_lock() self._spi.open() # command... self.__cmd(self.__CMD_READ_HISTOGRAM) chars = self.__read_bytes(86) # checksum... required = Decode.unsigned_int(chars[84:86], '<') actual = ModbusCRC.compute(chars[:84]) if required != actual: raise ValueError( "bad checksum: required: 0x%04x actual: 0x%04x" % (required, actual)) # time... rec = LocalizedDatetime.now() # bins... bins = [ Decode.unsigned_int(chars[i:i + 2], '<') for i in range(0, 48, 2) ] # bin MToFs... bin_1_mtof = chars[48] bin_3_mtof = chars[49] bin_5_mtof = chars[50] bin_7_mtof = chars[51] # period... raw_period = Decode.unsigned_int(chars[52:54], '<') period = round(float(raw_period) / 100.0, 3) # temperature & humidity raw_temp = Decode.unsigned_int(chars[56:58], '<') raw_humid = Decode.unsigned_int(chars[58:60], '<') sht = SHTDatum(SHT31.humid(raw_humid), SHT31.temp(raw_temp)) # PMx... try: pm1 = Decode.float(chars[60:64], '<') except TypeError: pm1 = None try: pm2p5 = Decode.float(chars[64:68], '<') except TypeError: pm2p5 = None try: pm10 = Decode.float(chars[68:72], '<') except TypeError: pm10 = None return OPCDatum(self.SOURCE, rec, pm1, pm2p5, pm10, period, bins, bin_1_mtof, bin_3_mtof, bin_5_mtof, bin_7_mtof, sht) finally: self._spi.close() self.release_lock()
def sample(self): byte_values = self.__convert() code = Decode.unsigned_int(byte_values, '>') v = code * self.__CONV return round(v, 1)