diff --git a/v4l2py/device.py b/v4l2py/device.py index 872e806..15a0b95 100644 --- a/v4l2py/device.py +++ b/v4l2py/device.py @@ -827,7 +827,7 @@ def with_class(self, control_class): def set_to_default(self): for v in self.values(): - if not isinstance(v, BaseSingleControl): + if not isinstance(v, BaseControl): continue try: @@ -855,11 +855,14 @@ def __init__(self, device, info): self._config_name = None self.control_class = ControlClass(raw.V4L2_CTRL_ID2CLASS(self.id)) self.type = ControlType(self._info.type) + try: self.standard = ControlID(self.id) except ValueError: self.standard = None + self.default = self._info.default_value + def __repr__(self): repr = f"{self.config_name}" @@ -891,6 +894,34 @@ def config_name(self) -> str: self._config_name = res return self._config_name + @property + def value(self): + if not self.is_writeonly: + return get_control(self.device, self.id) + else: + return None + + @value.setter + def value(self, value): + if not self.is_writeable: + reasons = [] + if self.is_readonly: + reasons.append("read-only") + if self.is_inactive: + reasons.append("inactive") + if self.is_disabled: + reasons.append("disabled") + if self.is_grabbed: + reasons.append("grabbed") + raise AttributeError(f"Control {self.config_name} is not writeable: {', '.join(reasons)}") + if value < self.minimum: + v = self.minimum + elif value > self.maximum: + v = self.maximum + else: + v = value + set_control(self.device, self.id, v) + @property def is_writeonly(self) -> bool: return (self._info.flags & ControlFlag.WRITE_ONLY) == ControlFlag.WRITE_ONLY @@ -916,50 +947,27 @@ def is_writeable(self) -> bool: return not (self.is_readonly or self.is_inactive or self.is_disabled or self.is_grabbed) + def set_to_default(self): + self.value = self.default + -class BaseSingleControl(BaseControl): +class BaseNumericControl(BaseControl): def __init__(self, device, info): super().__init__(device, info) self.minimum = self._info.minimum self.maximum = self._info.maximum self.step = self._info.step - self.default = self._info.default_value def _get_repr(self) -> str: repr = f" min={self.minimum} max={self.maximum}" repr += f" step={self.step} default={self.default}" return repr - @property - def value(self): - if not self.is_writeonly: - return get_control(self.device, self.id) - else: - return None - - @value.setter - def value(self, value): - if not self.is_writeable: - reasons = [] - if self.is_readonly: - reasons.append("read-only") - if self.is_inactive: - reasons.append("inactive") - if self.is_disabled: - reasons.append("disabled") - if self.is_grabbed: - reasons.append("grabbed") - raise AttributeError(f"Control {self.config_name} is not writeable: {', '.join(reasons)}") - if value < self.minimum: - v = self.minimum - elif value > self.maximum: - v = self.maximum - else: - v = value - set_control(self.device, self.id, v) + def increase(self, steps: int = 1): + self.value += (steps * self.step) - def set_to_default(self): - self.value = self.default + def decrease(self, steps: int = 1): + self.value -= (steps * self.step) def set_to_minimum(self): self.value = self.minimum @@ -973,7 +981,7 @@ def __init__(self, device, info): raise NotImplementedError() -class LegacyControl(BaseSingleControl): +class LegacyControl(BaseNumericControl): def __init__(self, device, info): super().__init__(device, info)