def sample(self):
        r = self.__read(self.__CMD_READ_MEASURED_VALUES, 0, 60)

        # density...
        pm1 = Decode.float(r[0:4], '>')
        pm2p5 = Decode.float(r[4:8], '>')
        pm4 = Decode.float(r[8:12], '>')
        pm10 = Decode.float(r[12:16], '>')

        # count...
        pm0p5_count = Decode.float(r[16:20], '>')
        pm1_count = Decode.float(r[20:24], '>')
        pm2p5_count = Decode.float(r[24:28], '>')
        pm4_count = Decode.float(r[28:32], '>')
        pm10_count = Decode.float(r[32:36], '>')

        # typical size...
        tps = Decode.float(r[36:40], '>')

        # time...
        rec = LocalizedDatetime.now()

        # report...
        counts = SPSDatumCounts(pm0p5_count, pm1_count, pm2p5_count, pm4_count,
                                pm10_count)

        return SPSDatum(self.SOURCE, rec, pm1, pm2p5, pm4, pm10, counts, tps)
    def status(self):
        try:
            self.obtain_lock()

            # restart status...
            cmd = SPINDIRx1Cmd.find('ws')
            response = self._transact(cmd)
            watchdog_reset = bool(response)

            # input voltage...
            cmd = SPINDIRx1Cmd.find('iv')
            response = self._transact(cmd)
            pwr_in = Decode.float(response, '<')

            # uptime...
            cmd = SPINDIRx1Cmd.find('up')
            response = self._transact(cmd)
            seconds = Decode.unsigned_long(response, '<')

            status = NDIRStatus(watchdog_reset, pwr_in, NDIRUptime(seconds))

            return status

        finally:
            self.release_lock()
Example #3
0
    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 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 measure_voltage(self):
        try:
            self.obtain_lock()

            cmd = SPINDIRx1Cmd.find('mv')
            response = self._transact(cmd)

            pile_ref_voltage = Decode.float(response[0:4], '<')
            pile_act_voltage = Decode.float(response[4:8], '<')
            thermistor_voltage = Decode.float(response[8:12], '<')

            return pile_ref_voltage, pile_act_voltage, thermistor_voltage

        finally:
            self.release_lock()
    def sample(self):
        try:
            self.obtain_lock()

            cmd = SPINDIRx1Cmd.find('sg')
            response = self._transact(cmd)

            cnc = Decode.float(response[0:4], '<')
            cnc_igl = Decode.float(response[4:8], '<')
            temp = Decode.float(response[8:12], '<')

            return NDIRDatum(temp, cnc, cnc_igl)

        finally:
            self.release_lock()
Example #7
0
    def get_sample_voltage(self):
        try:
            self.obtain_lock()

            # report...
            cmd = SPINDIRt1f1Cmd.find('sv')
            response = self._transact(cmd)

            pile_ref_amplitude = Decode.float(response[0:4], '<')
            pile_act_amplitude = Decode.float(response[4:8], '<')
            thermistor_average = Decode.float(response[8:12], '<')

            return pile_ref_amplitude, pile_act_amplitude, thermistor_average

        finally:
            self.release_lock()
Example #8
0
    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()
Example #10
0
    def _calib_r_unsigned_long(self, block, index):
        cmd = SPINDIRt1f1Cmd.find('cr')
        cmd.return_count = 4

        response = self._transact(cmd, (block, index))
        value = Decode.unsigned_long(response, '<')

        return value
    def __read_float(self):
        read_bytes = []

        for _ in range(4):
            time.sleep(OPCN2.__TRANSFER_DELAY)
            read_bytes.extend(self.__spi.read_bytes(1))

        return Decode.float(read_bytes)
    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 _calib_r_float(self, block, index):
        cmd = SPINDIRx1Cmd.find('cr')
        cmd.return_count = 4

        response = self._transact(cmd, (block, index))
        value = Decode.float(response, '<')

        return value
Example #14
0
    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 input_voltage(self):
        try:
            self.obtain_lock()

            cmd = SPINDIRx1Cmd.find('iv')
            response = self._transact(cmd)
            v_in_voltage = Decode.float(response, '<')

            return v_in_voltage

        finally:
            self.release_lock()
    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 pressure(self):
        try:
            self.obtain_lock()

            cmd = SPINDIRx1Cmd.find('sp')
            response = self._transact(cmd)

            p_a = Decode.float(response[0:4], '<')

            return round(p_a, 1)

        finally:
            self.release_lock()
Example #19
0
    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()
Example #20
0
    def cleaning_interval(self):
        r = self.__read(self.__CMD_AUTO_CLEANING_INTERVAL, 0, 6)
        interval = Decode.unsigned_long(r[0:4], '>')

        return interval
Example #21
0
    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()
Example #22
0
    def sample(self):
        byte_values = self.__convert()
        code = Decode.unsigned_int(byte_values, '>')
        v = code * self.__CONV

        return round(v, 1)