"""ctypes layouts for protocol status unions used by the driver."""

from __future__ import annotations

import ctypes


class DeviceStatusV1(ctypes.LittleEndianStructure):
    """Device status bit layout for V1 family."""

    _pack_ = 1
    _fields_ = [
        ("extRefAvailable", ctypes.c_uint8, 1),
        ("extRefInUse", ctypes.c_uint8, 1),
        ("FPGA_configured", ctypes.c_uint8, 1),
        ("source_locked", ctypes.c_uint8, 1),
        ("LO1_locked", ctypes.c_uint8, 1),
        ("ADC_overload", ctypes.c_uint8, 1),
        ("unlevel", ctypes.c_uint8, 1),
        ("_unused", ctypes.c_uint8, 1),
        ("temp_source", ctypes.c_uint8),
        ("temp_LO1", ctypes.c_uint8),
        ("temp_MCU", ctypes.c_uint8),
    ]


class DeviceStatusVFF(ctypes.LittleEndianStructure):
    """Device status bit layout for VFF family."""

    _pack_ = 1
    _fields_ = [
        ("source_locked", ctypes.c_uint8, 1),
        ("LO_locked", ctypes.c_uint8, 1),
        ("ADC_overload", ctypes.c_uint8, 1),
        ("unlevel", ctypes.c_uint8, 1),
        ("_unused", ctypes.c_uint8, 4),
        ("temp_MCU", ctypes.c_uint8),
    ]


class DeviceStatusVFE(ctypes.LittleEndianStructure):
    """Device status bit layout for VFE family."""

    _pack_ = 1
    _fields_ = [
        ("source_locked", ctypes.c_uint8, 1),
        ("LO_locked", ctypes.c_uint8, 1),
        ("ADC_overload", ctypes.c_uint8, 1),
        ("unlevel", ctypes.c_uint8, 1),
        ("_unused", ctypes.c_uint8, 4),
        ("temp_MCU", ctypes.c_uint8),
        ("temp_eCal", ctypes.c_uint16),
        ("power_heater", ctypes.c_uint16),
    ]


class DeviceStatusVD0(ctypes.LittleEndianStructure):
    """Device status bit layout for VD0/VE0 families."""

    _pack_ = 1
    _fields_ = [
        ("extRefAvailable", ctypes.c_uint8, 1),
        ("extRefInUse", ctypes.c_uint8, 1),
        ("FPGA_configured", ctypes.c_uint8, 1),
        ("source_locked", ctypes.c_uint8, 1),
        ("LO_locked", ctypes.c_uint8, 1),
        ("ADC_overload", ctypes.c_uint8, 1),
        ("unlevel", ctypes.c_uint8, 1),
        ("_unused", ctypes.c_uint8, 1),
        ("temp_MCU", ctypes.c_uint8),
        ("supply_voltage", ctypes.c_uint16),
        ("supply_current", ctypes.c_uint16),
    ]


class DeviceStatusUnion(ctypes.Union):
    """6-byte device-status union shared across hardware families."""

    _pack_ = 1
    _fields_ = [
        ("V1", DeviceStatusV1),
        ("VFF", DeviceStatusVFF),
        ("VFE", DeviceStatusVFE),
        ("VD0", DeviceStatusVD0),
        ("raw", ctypes.c_uint8 * 6),
    ]


assert ctypes.sizeof(DeviceStatusUnion) == 6
