"""
Constants for laser control module.

Physical constraints, protocol parameters, and operational limits
extracted from original device_commands.py and device_conversion.py.
"""

# ---- Protocol constants

BAUDRATE = 115200
SERIAL_TIMEOUT_SEC = 1.0

GET_DATA_TOTAL_LENGTH = 30       # bytes in device DATA response
SEND_PARAMS_TOTAL_LENGTH = 30    # bytes in DECODE_ENABLE command
TASK_ENABLE_COMMAND_LENGTH = 32  # bytes in TASK_ENABLE command

WAIT_AFTER_SEND_SEC = 0.15       # delay after sending a command
GUI_POLL_INTERVAL_MS = 5         # GUI event loop timeout

# ---- Command codes (as sent to device, already flipped to LE)

CMD_DECODE_ENABLE  = 0x1111   # Set control parameters
CMD_DEFAULT_ENABLE = 0x2222   # Reset device
CMD_TRANSS_ENABLE  = 0x3333   # Request all saved data (not implemented)
CMD_TRANS_ENABLE   = 0x4444   # Request last data
CMD_REMOVE_FILE    = 0x5555   # Delete saved data
CMD_STATE          = 0x6666   # Request state
CMD_TASK_ENABLE    = 0x7777   # Start a task

# ---- Error codes from device STATE response (after flipfour)

STATE_OK                = '0000'
STATE_SD_ERR            = '0001'   # SD Card read/write error
STATE_UART_ERR          = '0002'   # Command (UART) error
STATE_UART_DECODE_ERR   = '0004'   # Wrong parameter value
STATE_TEC1_ERR          = '0008'   # Laser 1 TEC driver overheat
STATE_TEC2_ERR          = '0010'   # Laser 2 TEC driver overheat
STATE_DEFAULT_ERR       = '0020'   # System reset error
STATE_REMOVE_ERR        = '0040'   # File deletion error

STATE_DESCRIPTIONS = {
    STATE_OK:             "All ok.",
    STATE_SD_ERR:         "SD Card reading/writing error (SD_ERR).",
    STATE_UART_ERR:       "Command error (UART_ERR).",
    STATE_UART_DECODE_ERR:"Wrong parameter value error (UART_DECODE_ERR).",
    STATE_TEC1_ERR:       "Laser 1: TEC driver overheat (TEC1_ERR).",
    STATE_TEC2_ERR:       "Laser 2: TEC driver overheat (TEC2_ERR).",
    STATE_DEFAULT_ERR:    "Resetting system error (DEFAULT_ERR).",
    STATE_REMOVE_ERR:     "File deletion error (REMOVE_ERR).",
}

# ---- Physical / hardware constants  (from device_conversion.py)

VREF = 2.5          # Reference voltage, Volts

# Bridge resistors for temperature measurement
R1 = 10000          # Ohm
R2 = 2200           # Ohm
R3 = 27000          # Ohm
R4 = 30000          # Ohm
R5 = 27000          # Ohm
R6 = 56000          # Ohm

RREF = 10           # Current-setting resistor, Ohm
                    # (@1550 nm – 28.7 Ohm; @840 nm – 10 Ohm)

# External thermistor divider resistors
R7  = 22000         # Ohm
R8  = 22000         # Ohm
R9  = 5100          # Ohm
R10 = 180000        # Ohm

# Thermistor Steinhart–Hart B-coefficient (internal / external)
BETA_INTERNAL = 3900    # K
BETA_EXTERNAL = 3455    # K
T0_K = 298              # Kelvin (25 °C reference)
R0   = 10000            # Ohm (thermistor nominal at 25 °C)

# ADC resolution
ADC_BITS_16 = 65535     # 2^16 - 1
ADC_BITS_12 = 4095      # 2^12 - 1

# Voltage conversion coefficients
U3V3_COEFF = 1.221e-3   # counts → Volts for 3.3V rail
U5V_COEFF  = 1.8315e-3  # counts → Volts for 5V rails
U7V_COEFF  = 6.72e-3    # counts → Volts for 7V rail

# ---- Operational limits (validated in validators.py)

TEMP_MIN_C = 15.0   # Minimum allowed laser temperature, °C
TEMP_MAX_C = 40.0   # Maximum allowed laser temperature, °C

CURRENT_MIN_MA = 15.0   # Minimum allowed laser current, mA
CURRENT_MAX_MA = 60.0   # Maximum allowed laser current, mA

# Variation step limits
CURRENT_STEP_MIN_MA = 0.002   # Minimum current variation step, mA
CURRENT_STEP_MAX_MA = 0.5     # Maximum current variation step, mA

TEMP_STEP_MIN_C = 0.05   # Minimum temperature variation step, °C
TEMP_STEP_MAX_C = 1.0    # Maximum temperature variation step, °C

# Time parameter limits
TIME_STEP_MIN_US = 20    # Minimum time step, microseconds
TIME_STEP_MAX_US = 100   # Maximum time step, microseconds

DELAY_TIME_MIN_MS = 3    # Minimum delay between pulses, milliseconds
DELAY_TIME_MAX_MS = 10   # Maximum delay between pulses, milliseconds

# ---- Acceptable voltage tolerances for power rail health check

VOLT_3V3_MIN = 3.1
VOLT_3V3_MAX = 3.5
VOLT_5V_MIN  = 4.8
VOLT_5V_MAX  = 5.3
VOLT_7V_MIN  = 6.5
VOLT_7V_MAX  = 7.5

# ---- Data buffer limits

MAX_DATA_POINTS = 1000   # Max stored measurement points
PLOT_POINTS = 100        # Points shown in real-time plots