예제 #1
0
    def __init__(
            self,
            name: str,
            vendor_id: int,
            product_id: int,
            resolution: str,
            num_cameras: int = 1,
            simulate: bool = False,
            usb_mux_comms: Optional[Dict[str, Any]] = None,
            usb_mux_channel: Optional[int] = None,
            i2c_lock: Optional[threading.RLock] = None,
            mux_simulator: Optional[MuxSimulator] = None
    ) -> None:
        # pygame only supports Linux.  If not running Linux, then simulate.
        if PLATFORM is not None and PLATFORM != "osx-machine" and PLATFORM != "unknown":
            # Initialize pygame
            pygame.init()
            pygame.camera.init()
            self.simulate = simulate
            pygame_loaded = True
        else:
            self.simulate = True
            pygame_loaded = False

        super().__init__(name=name,
                         vendor_id=vendor_id,
                         product_id=product_id,
                         resolution=resolution,
                         num_cameras=num_cameras,
                         simulate=self.simulate,
                         usb_mux_comms=usb_mux_comms,
                         usb_mux_channel=usb_mux_channel,
                         i2c_lock=i2c_lock,
                         mux_simulator=mux_simulator)

        # USB camera specific setup
        # pygame only supports Linux.  If not running Linux, then simulate.
        if not pygame_loaded:
            self.logger.info(
                "Not running on Linux OS, so pygame is not supported.  Turning on simulation mode."
            )

        # Using usb mux, initialize driver
        if not self.simulate and self.usb_mux_enabled:
            try:
                self.dac5578 = DAC5578Driver(
                    name=name,
                    i2c_lock=i2c_lock,  # type: ignore
                    bus=self.bus,
                    mux=self.mux,
                    channel=self.channel,
                    address=self.address,
                    simulate=self.simulate,
                    mux_simulator=mux_simulator,
                )
                self.usb_mux_channel = usb_mux_channel
            except I2CError as e:
                raise exceptions.InitError(logger=self.logger) from e
def test_init() -> None:
    driver = DAC5578Driver(
        "Test",
        i2c_lock=threading.RLock(),
        bus=2,
        address=0x4C,
        mux=0x77,
        channel=4,
        simulate=True,
        mux_simulator=MuxSimulator(),
    )
def test_write_output_standard() -> None:
    driver = DAC5578Driver(
        "Test",
        i2c_lock=threading.RLock(),
        bus=2,
        address=0x4C,
        mux=0x77,
        channel=4,
        simulate=True,
        mux_simulator=MuxSimulator(),
    )
    driver.write_output(0, 0)
def test_write_output_percent_gt() -> None:
    driver = DAC5578Driver(
        "Test",
        i2c_lock=threading.RLock(),
        bus=2,
        address=0x4C,
        mux=0x77,
        channel=4,
        simulate=True,
        mux_simulator=MuxSimulator(),
    )
    with pytest.raises(WriteOutputError):
        driver.write_output(0, 101)
def test_write_outputs_standard() -> None:
    driver = DAC5578Driver(
        "Test",
        i2c_lock=threading.RLock(),
        bus=2,
        address=0x4C,
        mux=0x77,
        channel=4,
        simulate=True,
        mux_simulator=MuxSimulator(),
    )
    outputs = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}
    driver.write_outputs(outputs)
def test_write_outputs_dict_gt() -> None:
    driver = DAC5578Driver(
        "Test",
        i2c_lock=threading.RLock(),
        bus=2,
        address=0x4C,
        mux=0x77,
        channel=4,
        simulate=True,
        mux_simulator=MuxSimulator(),
    )
    with pytest.raises(WriteOutputsError):
        outputs = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0}
        driver.write_outputs(outputs)
예제 #7
0
def test_write_outputs_dict_lt() -> None:
    driver = DAC5578Driver(
        "Test",
        i2c_lock=threading.RLock(),
        bus=2,
        address=0x4c,
        mux=0x77,
        channel=4,
        simulate=True,
        mux_simulator=MuxSimulator(),
    )
    with pytest.raises(WriteOutputsError):
        outputs: Dict = {}
        driver.write_outputs(outputs)
예제 #8
0
 def initialize(self) -> None:
     """Initializes panel."""
     self.logger.debug("Initializing {}".format(self.name))
     try:
         self.driver = DAC5578Driver(
             name=self.full_name,
             i2c_lock=self.i2c_lock,
             bus=self.bus,
             address=self.address,
             mux=self.mux,
             channel=self.channel,
             simulate=self.simulate,
             mux_simulator=self.mux_simulator,
         )
         self.is_shutdown = False
     except Exception as e:
         self.logger.exception("Unable to initialize `{}`".format(self.name))
         self.is_shutdown = True
def test_read_power_register() -> None:
    driver = DAC5578Driver(
        "Test",
        i2c_lock=threading.RLock(),
        bus=2,
        address=0x4C,
        mux=0x77,
        channel=4,
        simulate=True,
        mux_simulator=MuxSimulator(),
    )
    powered_channels = driver.read_power_register()
    assert powered_channels == {
        0: True,
        1: True,
        2: True,
        3: True,
        4: True,
        5: True,
        6: True,
        7: True,
    }
예제 #10
0
    def __init__(
        self,
        name: str,
        vendor_id: int,
        product_id: int,
        resolution: str,
        num_cameras: int = 1,
        simulate: bool = False,
        usb_mux_comms: Optional[Dict[str, Any]] = None,
        usb_mux_channel: Optional[int] = None,
        i2c_lock: Optional[threading.RLock] = None,
        mux_simulator: Optional[MuxSimulator] = None,
    ) -> None:
        """Initializes USB camera camera."""

        # Initialize parameters
        self.name = name
        self.vendor_id = vendor_id
        self.product_id = product_id
        self.resolution = resolution
        self.num_cameras = num_cameras
        self.simulate = simulate

        # Initialize logger
        logname = "Driver({})".format(name)
        self.logger = logger.Logger(logname, "peripherals")

        # pygame only supports Linux.  If not running Linux, then simulate.
        if PLATFORM is not None and PLATFORM != "osx-machine" and PLATFORM != "unknown":
            # Initialize pygame
            pygame.init()
            pygame.camera.init()
        else:
            self.logger.info(
                "usb_camera module: Not running on Linux OS, so pygame is not supported.  Turning on simulation mode."
            )
            self.simulate = True

        # Check if simulating
        if self.simulate:
            self.logger.info("Simulating driver")
            self.directory = SIMULATE_IMAGE_DIR
        else:
            self.directory = IMAGE_DIR

        # Check directory exists else create it
        if not os.path.exists(self.directory):
            os.makedirs(self.directory)

        # Check if using usb mux
        if usb_mux_comms == None or usb_mux_channel == None:
            self.usb_mux_enabled = False
            return

        # Initialize usb mux properties
        self.bus = usb_mux_comms.get("bus")
        self.mux = usb_mux_comms.get("mux")
        self.channel = usb_mux_comms.get("channel")
        self.address = usb_mux_comms.get("address")

        # Check if using default bus
        if self.bus == "default":
            self.logger.debug("Using default i2c bus")
            self.bus = os.getenv("DEFAULT_I2C_BUS")

            # Convert exported value from non-pythonic none to pythonic None
            if self.bus == "none":
                self.bus = None

            if self.bus != None:
                self.bus = int(self.bus)

        # Check if using default mux
        if self.mux == "default":
            self.logger.debug("mux is default")
            self.mux = os.getenv("DEFAULT_MUX_ADDRESS")

            # Convert exported value from non-pythonic none to pythonic None
            if self.mux == "none":
                self.mux = None
            self.logger.debug("mux = {}".format(self.mux))

        # Convert i2c config params from hex to int if they exist
        if self.address != None:
            address = int(self.address, 16)
        if self.mux != None:
            self.mux = int(self.mux, 16)

        # Using usb mux, initialize driver
        try:
            self.dac5578 = DAC5578Driver(
                name=name,
                i2c_lock=i2c_lock,  # type: ignore
                bus=self.bus,
                mux=self.mux,
                channel=self.channel,
                address=self.address,
                simulate=self.simulate,
                mux_simulator=mux_simulator,
            )
            self.usb_mux_channel = usb_mux_channel
            self.usb_mux_enabled = True
        except I2CError as e:
            raise exceptions.InitError(logger=self.logger) from e
예제 #11
0
    def __init__(
        self,
        name: str,
        vendor_id: int,
        product_id: int,
        resolution: str,
        simulate: bool = False,
        usb_mux_comms: Optional[Dict[str, Any]] = None,
        usb_mux_channel: Optional[int] = None,
        i2c_lock: Optional[threading.Lock] = None,
        mux_simulator: Optional[MuxSimulator] = None,
    ) -> None:
        """Initializes USB camera camera."""

        # Initialize parameters
        self.name = name
        self.vendor_id = vendor_id
        self.product_id = product_id
        self.resolution = resolution
        self.simulate = simulate

        # Initialize logger
        self.logger = Logger(name="Driver({})".format(name),
                             dunder_name=__name__)

        # Check if simulating
        if simulate:
            self.logger.info("Simulating driver")
            self.directory = "device/peripherals/modules/usb_camera/tests/images/"
        else:
            self.directory = IMAGE_DIR

        # Check directory exists else create it
        if not os.path.exists(self.directory):
            os.makedirs(self.directory)

        # Check if using usb mux
        if usb_mux_comms == None or usb_mux_channel == None:
            self.dac5578 = None
            return

        # Get optional i2c parameters
        mux = usb_mux_comms.get("mux", None)  # type: ignore
        if mux != None:
            mux = int(mux, 16)

        # Using usb mux, initialize driver
        try:
            self.dac5578 = DAC5578Driver(
                name=name,
                i2c_lock=i2c_lock,  # type: ignore
                bus=usb_mux_comms.get("bus", None),  # type: ignore
                address=int(usb_mux_comms.get("address", None),
                            16),  # type: ignore
                mux=mux,
                channel=usb_mux_comms.get("channel", None),  # type: ignore
                simulate=simulate,
                mux_simulator=mux_simulator,
            )
            self.usb_mux_channel = usb_mux_channel
        except I2CError as e:
            raise InitError(logger=self.logger) from e
예제 #12
0
    def run(self, *args: Any, **kwargs: Any) -> None:
        """Runs driver."""

        # Run parent class
        super().run(*args, **kwargs)

        # Check if dac is used in led panel
        if "panels" in self.communication:

            # Check if panel name is specified
            if self.args.panel_name != None:

                # Check if panel name exists in list
                name = None
                for entry in self.communication["panels"]:
                    if entry["name"] == self.args.panel_name:
                        self.communication = entry
                        name = entry["name"]
                        break

                # Check if panel name was found
                if name == None:
                    print(
                        "Unable to find panel `{}`, using first entry instead".
                        format(self.args.panel_name))
            else:
                # Default to using first panel in communication list
                self.communication = self.communication["panels"][0]

        # Initialize driver optional mux parameter
        mux = self.communication.get("mux", None)
        if mux != None:
            mux = int(mux, 16)

        # Initialize driver
        self.driver = DAC5578Driver(
            name=self.args.name,
            i2c_lock=threading.RLock(),
            bus=self.communication["bus"],
            address=int(self.communication["address"], 16),
            mux=mux,
            channel=self.communication.get("channel", None),
        )

        # Check if setting a channel to a value
        if self.args.channel != None and self.args.percent != None:
            self.driver.write_output(self.args.channel, self.args.percent)

        # Check if setting all channels to a value
        elif self.args.channel == None and self.args.percent != None:
            outputs = {}
            for i in range(8):
                outputs[i] = self.args.percent
            self.driver.write_outputs(outputs)

        # Check if setting a channel or all channels high
        elif self.args.high:
            self.driver.set_high(channel=self.args.channel)

        # Check if setting a channel or all channels low
        elif self.args.low:
            self.driver.set_low(channel=self.args.channel)

        # Check if fading
        elif self.args.fade:
            self.driver.fade(cycles=10, channel=self.args.channel)

        # Check if resetting
        elif self.args.reset:
            self.driver.reset()