from enum import IntEnum

from msl.equipment.connection_msl import ConnectionSerial


class IntegrationTime(IntEnum):
    """The amount of time to integrate the photodiode signal."""
    TIME_50u = 5
    TIME_100u = 6
    TIME_1m = 7
    TIME_10m = 8
    TIME_20m = 9
    TIME_100m = 10
    TIME_200m = 11
    TIME_500m = 12
    TIME_1 = 13
    TIME_2 = 14


class SIA3(ConnectionSerial):

    GAIN = IntegrationTime  #: The gain (i.e., the integration time)

    def __init__(self, record):
        """
        Establishes a connection to the Switched Integrator Amplifier
        (SIA3 board) that is designed by the Czech Metrology Institute.

        Parameters
        ----------
        record : :class:`~msl.equipment.record_types.EquipmentRecord`
            An equipment record from an **Equipment-Register**
            :class:`~msl.equipment.database.Database`.
        """
        ConnectionSerial.__init__(self, record)

    def set_integration_time(self, t):
        """Set the integration time (i.e., the gain).

        Parameters
        ----------
        t : :class:`.IntegrationTime`
            The integration time as a :class:`.IntegrationTime` enum value or member name.
        """
        self._send_byte(7)
        self._send_byte(self.convert_to_enum(t, IntegrationTime, prefix='TIME_'))

    def set_ps(self, ps):
        """Set the PS.

        Parameters
        ----------
        ps : int
            The PS to use. Value must be in the range [0, 7].

        Raises
        ------
        ValueError:
            If the value of `ps` is invalid.
        """
        _ps = int(ps)
        if _ps < 0 or _ps > 7:
            raise ValueError('Invalid ps of {}. Must be 0 <= ps <= 7.'.format(ps))
        self._send_byte(1)
        self._send_byte(4)
        self._send_byte(_ps)
        self._send_byte(_ps)

    def _send_byte(self, byte):
        self.write(chr(byte))


@@ -1,29 +1,266 @@
-from enum import IntEnum%0A
+%22%22%22%0AEstablishes a connection to the Switched Integrator Amplifier%0A(SIA3 board) that is designed by the %60Czech Metrology Institute%60_.%0A%0A.. _Czech Metrology Institute: https://www.cmi.cz/?language=en%0A%22%22%22%0Afrom enum import IntEnum%0A%0Afrom msl.equipment import constants
 %0Afro
@@ -396,16 +396,17 @@
 he photo
+-
 diode si
@@ -850,16 +850,17 @@
  by the 
+%60
 Czech Me
@@ -876,19 +876,228 @@
 nstitute
+%60_
 .%0A%0A
+        Do not instantiate this class directly. Use the :meth:%60~.EquipmentRecord.connect%60%0A        method to connect to the equipment.%0A%0A        .. _Czech Metrology Institute: https://www.cmi.cz/?language=en%0A%0A
         
@@ -1152,29 +1152,26 @@
 class:%60~
-msl.e
+.E
 quipment
 .record_
@@ -1166,156 +1166,570 @@
 ment
-.record_types.EquipmentRecord%60%0A            An equipment record from an **Equipment-Register**%0A            :class:%60~msl.equipment.database.Database%60.
+Record%60%0A            A record from an :ref:%60equipment_database%60.%0A        %22%22%22%0A        if len(record.connection.properties) == 0:%0A            # then use the default connection properties%0A            record.connection.properties = %7B%0A                'baud_rate': 14400,%0A                'data_bits': constants.DataBits.EIGHT,%0A                'parity': constants.Parity.NONE,%0A                'stop_bits': constants.StopBits.ONE,%0A                'write_timeout': 10.0,%0A                'xon_xoff': False,%0A                'rts_cts': False,%0A                'dsr_dtr': False
 %0A   
@@ -1725,35 +1725,37 @@
 : False%0A        
-%22%22%22
+    %7D
 %0A        Connect
@@ -1824,16 +1824,19 @@
 (self, t
+ime
 ):%0A     
@@ -1932,16 +1932,19 @@
        t
+ime
  : :clas
@@ -2140,16 +2140,19 @@
 o_enum(t
+ime
 , Integr
@@ -2312,17 +2312,21 @@
 to use. 
-V
+The v
 alue mus
@@ -2550,18 +2550,24 @@
 Invalid 
-ps
+PS value
  of %7B%7D. 
@@ -2579,18 +2579,18 @@
 be 0 %3C= 
-ps
+PS
  %3C= 7.'.
@@ -2776,9 +2776,8 @@
 (byte))%0A
-%0A

define default values for ConnectionRecord.properties