"""
Created on 15 Nov 2018

@author: Bruno Beloff (bruno.beloff@southcoastscience.com)

Firmware report:
OPC-N3 Iss1.1 FirmwareVer=1.17a...........................BS
"""

import time

from scs_dfe.particulate.alphasense_opc import AlphasenseOPC

from scs_dfe.particulate.opc_n3.opc_firmware_conf import OPCFirmwareConf
from scs_dfe.particulate.opc_n3.opc_n3_datum import OPCN3Datum
from scs_dfe.particulate.opc_n3.opc_status import OPCStatus


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

class OPCN3(AlphasenseOPC):
    """
    classdocs
    """
    MIN_SAMPLE_PERIOD =                  5.0        # seconds
    MAX_SAMPLE_PERIOD =                 10.0        # seconds
    DEFAULT_SAMPLE_PERIOD =             10.0        # seconds

    DEFAULT_BUSY_TIMEOUT =               5.0        # seconds

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

    __BOOT_TIME =                       4.0         # seconds
    __POWER_CYCLE_TIME =               10.0         # seconds

    __LASER_START_TIME =                1.0         # seconds
    __FAN_START_TIME =                  5.0         # seconds

    __FAN_STOP_TIME =                   2.0         # seconds

    __MAX_PERMITTED_ZERO_READINGS =     4

    __CMD_POWER =                       0x03
    __CMD_LASER_ON =                    0x07
    __CMD_LASER_OFF =                   0x06
    __CMD_FAN_ON =                      0x03
    __CMD_FAN_OFF =                     0x02

    __CMD_READ_HISTOGRAM =              0x30

    __CMD_GET_FIRMWARE =                0x3f
    __CMD_GET_VERSION =                 0x12
    __CMD_GET_SERIAL =                  0x10
    __CMD_GET_STATUS =                  0x13

    __CMD_GET_CONF =                    0x3c
    __CMD_SET_CONF =                    0x3a
    __CMD_SET_BIN_WEIGHTING_INDEX =     0x05

    __CMD_SAVE_CONF =                   0x43
    __CMD_SAVE_CONF_SEQUENCE =          (0x3F, 0x3c, 0x3f, 0x3c, 0x43)

    __CMD_CHECK =                       0xcf

    __CMD_RESET =                       0x06

    __RESPONSE_BUSY =                   0x31
    __RESPONSE_NOT_BUSY =               (0x00, 0xff, 0xf3)

    __SPI_CLOCK =                       326000      # Minimum speed for OPCube
    __SPI_MODE =                        1

    __DELAY_TRANSFER =                  0.020       # 0.001
    __DELAY_CMD =                       0.020       # 0.010
    __DELAY_BUSY =                      0.100

    __LOCK_TIMEOUT =                    20.0


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

    @classmethod
    def source(cls):
        return OPCN3Datum.SOURCE


    @classmethod
    def lock_timeout(cls):
        return cls.__LOCK_TIMEOUT


    @classmethod
    def boot_time(cls):
        return cls.__BOOT_TIME


    @classmethod
    def power_cycle_time(cls):
        return cls.__POWER_CYCLE_TIME


    @classmethod
    def max_permitted_zero_readings(cls):
        return cls.__MAX_PERMITTED_ZERO_READINGS


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

    def __init__(self, interface, spi_bus, spi_device):
        """
        Constructor
        """
        super().__init__(interface, spi_bus, spi_device, self.__SPI_MODE, self.__SPI_CLOCK)


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

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

            # fan...
            self.__cmd_power(self.__CMD_FAN_ON)
            time.sleep(self.__FAN_START_TIME)

            # laser...
            self.__cmd_power(self.__CMD_LASER_ON)

        finally:
            self.release_lock()


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