-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathv4l2py-ctl.py
200 lines (161 loc) · 5.76 KB
/
v4l2py-ctl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#
# This file is part of the linuxpy project
#
# Copyright (c) 2023 Tiago Coutinho
# Distributed under the GPLv3 license. See LICENSE for more info.
import argparse
from linuxpy.video.device import Device, MenuControl
def _get_ctrl(cam, control):
if control.isdigit() or control.startswith("0x"):
_ctrl = int(control, 0)
else:
_ctrl = control
try:
ctrl = cam.controls[_ctrl]
except KeyError:
return None
else:
return ctrl
def show_control_status(device: str) -> None:
with Device(device) as cam:
# Group controls by class
class_controls = {}
classes = {}
for control in cam.controls.values():
classes[control.control_class.id] = control.control_class
control_ids = class_controls.setdefault(control.control_class.id, [])
control_ids.append(control)
print("Showing current status of all controls ...\n")
print(f"*** {cam.info.card} ***")
for control_class_id, controls in class_controls.items():
control_class = classes[control_class_id]
print(f"\n{control_class.name.decode().title()}\n")
for ctrl in controls:
print("0x%08x:" % ctrl.id, ctrl)
if isinstance(ctrl, MenuControl):
for key, value in ctrl.items():
print(11 * " ", f" +-- {key}: {value}")
print("")
def get_controls(device: str, controls: list) -> None:
with Device(device) as cam:
print("Showing current value of given controls ...\n")
for control in controls:
ctrl = _get_ctrl(cam, control)
if not ctrl:
print(f"{control}: unknown control")
continue
if not ctrl.is_flagged_write_only:
print(f"{control} = {ctrl.value}")
else:
print(f"{control} is write-only, thus cannot be read")
print("")
def set_controls(device: str, controls: list, clipping: bool) -> None:
controls = ((ctrl.strip(), value.strip()) for (ctrl, value) in (c.split("=") for c in controls))
with Device(device) as cam:
print("Changing value of given controls ...\n")
cam.controls.set_clipping(clipping)
for control, value_new in controls:
ctrl = _get_ctrl(cam, control)
if not ctrl:
print(f"{control}: unknown control")
continue
if not ctrl.is_flagged_write_only:
value_old = ctrl.value
else:
value_old = "(write-only)"
try:
ctrl.value = value_new
except Exception as err:
success = False
reason = f"{err}"
else:
success = True
result = "%-5s" % ("OK" if success else "ERROR")
if success:
print(f"{result} {control}: {value_old} -> {value_new}\n")
else:
print(f"{result} {control}: {value_old} -> {value_new}\n{result} {reason}\n")
def reset_controls(device: str, controls: list) -> None:
with Device(device) as cam:
print("Resetting given controls to default ...\n")
for control in controls:
ctrl = _get_ctrl(cam, control)
if not ctrl:
print(f"{control}: unknown control")
continue
try:
ctrl.set_to_default()
except Exception as err:
success = False
reason = f"{err}"
else:
success = True
result = "%-5s" % ("OK" if success else "ERROR")
if success:
print(f"{result} {control} reset to {ctrl.default}\n")
else:
print(f"{result} {control}:\n{result} {reason}\n")
def reset_all_controls(device: str) -> None:
with Device(device) as cam:
print("Resetting all controls to default ...\n")
cam.controls.set_to_default()
def csv(string: str) -> list:
return [v.strip() for v in string.split(",")]
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--clipping",
default=False,
action="store_true",
help="when changing numeric controls, enforce the written value to be within allowed range (default: %(default)s)",
)
parser.add_argument(
"--device",
type=str,
default="0",
metavar="<dev>",
help="use device <dev> instead of /dev/video0; if <dev> starts with a digit, then /dev/video<dev> is used",
)
parser.add_argument(
"--get-ctrl",
type=csv,
default=[],
metavar="<ctrl>[,<ctrl>...]",
help="get the values of the specified controls",
)
parser.add_argument(
"--set-ctrl",
type=csv,
default=[],
metavar="<ctrl>=<val>[,<ctrl>=<val>...]",
help="set the values of the specified controls",
)
parser.add_argument(
"--reset-ctrl",
type=csv,
default=[],
metavar="<ctrl>[,<ctrl>...]",
help="reset the specified controls to their default values",
)
parser.add_argument(
"--reset-all",
default=False,
action="store_true",
help="reset all controls to their default value",
)
args = parser.parse_args()
if args.device.isdigit():
dev = f"/dev/video{args.device}"
else:
dev = args.device
if args.reset_all:
reset_all_controls(dev)
elif args.reset_ctrl:
reset_controls(dev, args.reset_ctrl)
elif args.get_ctrl:
get_controls(dev, args.get_ctrl)
elif args.set_ctrl:
set_controls(dev, args.set_ctrl, args.clipping)
else:
show_control_status(dev)
print("Done.")