Skip to content

Commit

Permalink
Dissolve BaseSingleControl into BaseControl and BaseNumericControl.
Browse files Browse the repository at this point in the history
  • Loading branch information
otaku42 committed Apr 5, 2023
1 parent dea5cbe commit 5640d4e
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions v4l2py/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}"

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 5640d4e

Please sign in to comment.