<commit_before>"""
Created on 4 Jul 2016

@author: <NAME> (<EMAIL>)

Firmware report:
OPC-N2 FirmwareVer=OPC-018.1..............................BD
"""

import time

from scs_core.data.localized_datetime import LocalizedDatetime
from scs_core.data.datum import Datum

from scs_core.particulate.opc_datum import OPCDatum

from scs_dfe.board.io import IO

from scs_host.bus.spi import SPI
from scs_host.lock.lock import Lock


# --------------------------------------------------------------------------------------------------------------------

class OPCN2(object):
    """
    classdocs
    """
    SOURCE =                            'N2'

    MIN_SAMPLE_PERIOD =                  5.0       # seconds
    MAX_SAMPLE_PERIOD =                 10.0       # seconds
    DEFAULT_SAMPLE_PERIOD =             10.0       # seconds

    POWER_CYCLE_TIME =                  10.0       # seconds

    # ----------------------------------------------------------------------------------------------------------------

    __BOOT_TIME =                        4.0       # seconds
    __START_TIME =                       5.0       # seconds
    __STOP_TIME =                        2.0       # seconds

    __FLOW_RATE_VERSION =               16

    __FAN_UP_TIME =                     10
    __FAN_DOWN_TIME =                   2

    __PERIOD_CONVERSION =               45360       # should be 12000 (1/12MHz * 1000), but found by experiment

    __CMD_POWER =                       0x03
    __CMD_POWER_ON =                    0x00        # 0x03, 0x00
    __CMD_POWER_OFF =                   0x01        # 0x03, 0x01

    __CMD_CHECK_STATUS =                0xcf
    __CMD_READ_HISTOGRAM =              0x30
    __CMD_GET_FIRMWARE_VERSION =        0x3f

    __SPI_CLOCK =                       488000
    __SPI_MODE =                        1

    __CMD_DELAY =                       0.01
    __TRANSFER_DELAY =                  0.00002

    __LOCK_TIMEOUT =                    6.0


    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    def obtain_lock(cls):
        Lock.acquire(cls.__name__, cls.__LOCK_TIMEOUT)


    @classmethod
    def release_lock(cls):
        Lock.release(cls.__name__)


    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, spi_bus, spi_device):
        """
        Constructor
        """
        self.__io = IO()
        self.__spi = SPI(spi_bus, spi_device, self.__SPI_MODE, self.__SPI_CLOCK)


    # ----------------------------------------------------------------------------------------------------------------

    def power_on(self):
        initial_power_state = self.__io.opc_power

        self.__io.opc_power = IO.LOW

        if initial_power_state == IO.HIGH:      # initial_power is None if there is no power control facility
            time.sleep(self.__BOOT_TIME)


    def power_off(self):
        self.__io.opc_power = IO.HIGH


    # ----------------------------------------------------------------------------------------------------------------

    def operations_on(self):
        try:
            self.obtain_lock()
            self.__spi.open()

            # start...
            self.__spi.xfer([self.__CMD_POWER, self.__CMD_POWER_ON])
            time.sleep(self.__START_TIME)

            # clear histogram...
            self.__spi.xfer([self.__CMD_READ_HISTOGRAM])
            time.sleep(self.__CMD_DELAY)

            for _ in range(62):
                self.__read_byte()

        finally:
            self.__spi.close()
            self.release_lock()


    def operations_off(self):
        try:
            self.obtain_lock()
            self.__spi.open()

            self.__spi.xfer([self.__CMD_POWER, self.__CMD_POWER_OFF])

        finally:
            time.sleep(self.__CMD_DELAY)

            self.__spi.close()
            self.release_lock()

        time.sleep(self.__STOP_TIME)


    # ----------------------------------------------------------------------------------------------------------------

    def sample(self):
        try:
            self.obtain_lock()
            self.__spi.open()

            self.__spi.xfer([self.__CMD_READ_HISTOGRAM])
            time.sleep(self.__CMD_DELAY)

            # bins...
            bins = [None] * 16

            for i in range(16):
                bins[i] = self.__read_int()

            # bin MToFs...
            bin_1_mtof = self.__read_byte()
            bin_3_mtof = self.__read_byte()
            bin_5_mtof = self.__read_byte()
            bin_7_mtof = self.__read_byte()

            # flow rate...
            self.__spi.read_bytes(4)

            # temperature
            self.__spi.read_bytes(4)

            # period...
            period = self.__read_float()

            # checksum...
            self.__read_int()

            # PMx...
            try:
                pm = self.__read_float()<commit_msg>Developed OPCN3 class.<commit_after>"""
Created on 4 Jul 2016

@author: <NAME> (<EMAIL>)

Firmware report:
OPC-N2 FirmwareVer=OPC-018.1..............................BD
"""

import time

from scs_core.data.localized_datetime import LocalizedDatetime
from scs_core.data.datum import Datum

from scs_core.particulate.opc_datum import OPCDatum

from scs_dfe.board.io import IO

from scs_host.bus.spi import SPI
from scs_host.lock.lock import Lock


# --------------------------------------------------------------------------------------------------------------------

class OPCN2(object):
    """
    classdocs
    """
    SOURCE =                            'N2'

    MIN_SAMPLE_PERIOD =                  5.0       # seconds
    MAX_SAMPLE_PERIOD =                 10.0       # seconds
    DEFAULT_SAMPLE_PERIOD =             10.0       # seconds

    POWER_CYCLE_TIME =                  10.0       # seconds

    # ----------------------------------------------------------------------------------------------------------------

    __BOOT_TIME =                        4.0       # seconds
    __START_TIME =                       5.0       # seconds
    __STOP_TIME =                        2.0       # seconds

    __FLOW_RATE_VERSION =               16

    __FAN_UP_TIME =                     10
    __FAN_DOWN_TIME =                   2

    __PERIOD_CONVERSION =               45360       # should be 12000 (1/12MHz * 1000), but found by experiment

    __CMD_POWER =                       0x03
    __CMD_POWER_ON =                    0x00        # 0x03, 0x00
    __CMD_POWER_OFF =                   0x01        # 0x03, 0x01

    __CMD_CHECK_STATUS =                0xcf
    __CMD_READ_HISTOGRAM =              0x30
    __CMD_GET_FIRMWARE_VERSION =        0x3f

    __SPI_CLOCK =                       488000
    __SPI_MODE =                        1

    __CMD_DELAY =                       0.01
    __TRANSFER_DELAY =                  0.00002

    __LOCK_TIMEOUT =                    6.0


    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    def obtain_lock(cls):
        Lock.acquire(cls.__name__, cls.__LOCK_TIMEOUT)


    @classmethod
    def release_lock(cls):
        Lock.release(cls.__name__)


    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, spi_bus, spi_device):
        """
        Constructor
        """
        self.__io = IO()
        self.__spi = SPI(spi_bus, spi_device, self.__SPI_MODE, self.__SPI_CLOCK)


    # ----------------------------------------------------------------------------------------------------------------

    def power_on(self):
        initial_power_state = self.__io.opc_power

        self.__io.opc_power = IO.LOW

        if initial_power_state == IO.HIGH:      # initial_power is None if there is no power control facility
            time.sleep(self.__BOOT_TIME)


    def power_off(self):
        self.__io.opc_power = IO.HIGH


    # ----------------------------------------------------------------------------------------------------------------

    def operations_on(self):
        try:
            self.obtain_lock()
            self.__spi.open()

            # start...
            self.__spi.xfer([self.__CMD_POWER, self.__CMD_POWER_ON])
            time.sleep(self.__START_TIME)

            # clear histogram...
            self.__spi.xfer([self.__CMD_READ_HISTOGRAM])
            time.sleep(self.__CMD_DELAY)

            for _ in range(62):
                self.__read_byte()

        finally:
            self.__spi.close()
            self.release_lock()


    def operations_off(self):
        try:
            self.obtain_lock()
            self.__spi.open()

            self.__spi.xfer([self.__CMD_POWER, self.__CMD_POWER_OFF])

        finally:
            time.sleep(self.__CMD_DELAY)

            self.__spi.close()
            self.release_lock()

        time.sleep(self.__STOP_TIME)


    # ----------------------------------------------------------------------------------------------------------------

    def sample(self):
        try:
            self.obtain_lock()
            self.__spi.open()

            self.__spi.xfer([self.__CMD_READ_HISTOGRAM])
            time.sleep(self.__CMD_DELAY)

            # bins...
            bins = [self.__read_int() for _ in range(16)]

            # bin MToFs...
            bin_1_mtof = self.__read_byte()
            bin_3_mtof = self.__read_byte()
            bin_5_mtof = self.__read_byte()
            bin_7_mtof = self.__read_byte()

            # flow rate...
            self.__spi.read_bytes(4)

            # temperature
            self.__spi.read_bytes(4)

            # period...
            period = self.__read_float()

            # checksum...
            self.__read_int()

            # PMx...
            try:
                pm = self.__read_float()
{'fraction_non_alphanumeric': 0.23516461523066146, 'fraction_numerical': 0.021515060542379665, 'mean_word_length': 0.9145593869731801, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3387129', 'n_tokens_mistral': 2796, 'n_tokens_neox': 2555, 'n_words': 449}