| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 |
# Copyright (C) 2018 Takashi Sakamoto |
| 4 |
|
| 5 |
import signal |
| 6 |
|
| 7 |
from hinawa_utils.misc.cli_kit import CliKit |
| 8 |
from hinawa_utils.motu.motu_unit import MotuUnit |
| 9 |
|
| 10 |
from gi.repository import GLib |
| 11 |
|
| 12 |
|
| 13 |
def handle_opt_iface_mode(uniit, args): |
| 14 |
ops = ('set', 'get') |
| 15 |
if len(args) > 2 and args[0] in ops: |
| 16 |
op = args[0] |
| 17 |
direction = args[1] |
| 18 |
index = args[2] |
| 19 |
if op == ops[0] and len(args) == 4: |
| 20 |
mode = args[3] |
| 21 |
unit.set_opt_iface_mode(direction, index, mode) |
| 22 |
else: |
| 23 |
print(unit.get_opt_iface_mode(direction, index)) |
| 24 |
return True |
| 25 |
print('Arguments for optical input interface mode:') |
| 26 |
print(' opt-iface-mode OPERATION DIRECTION INDEX [MODE]') |
| 27 |
print(' OPERATION: [{0}]'.format('|'.join(ops))) |
| 28 |
directions = [str(d) for d in unit.get_supported_opt_iface_directions()] |
| 29 |
print(' DIRECTION: [{0}]'.format('|'.join(directions))) |
| 30 |
indexes = [str(i) for i in unit.get_opt_iface_indexes()] |
| 31 |
print(' INDEX: [{0}]'.format('|'.join(indexes))) |
| 32 |
modes = [str(m) for m in unit.get_opt_iface_modes()] |
| 33 |
print(' MODE: [{0}]'.format('|'.join(modes))) |
| 34 |
return False |
| 35 |
|
| 36 |
|
| 37 |
def handle_sampling_rate(unit, args): |
| 38 |
ops = ('set', 'get') |
| 39 |
if len(args) > 0 and args[0] in ops: |
| 40 |
op = args[0] |
| 41 |
if op == ops[0] and len(args) == 2: |
| 42 |
rate = int(args[1]) |
| 43 |
unit.set_sampling_rate(rate) |
| 44 |
else: |
| 45 |
print(unit.get_sampling_rate()) |
| 46 |
return True |
| 47 |
print('Arguments for sampling-rate command:') |
| 48 |
print(' sampling-rate OPERATION [RATE]') |
| 49 |
print(' OPERATION: [{0}]'.format('|'.join(ops))) |
| 50 |
rates = [str(r) for r in unit.get_sampling_rates()] |
| 51 |
print(' RATE: [{0}]'.format('|'.join(rates))) |
| 52 |
return False |
| 53 |
|
| 54 |
|
| 55 |
def handle_clock_source(unit, args): |
| 56 |
ops = ('set', 'get') |
| 57 |
if len(args) > 0 and args[0] in ops: |
| 58 |
op = args[0] |
| 59 |
if op == ops[0] and len(args) == 2: |
| 60 |
source = args[1] |
| 61 |
unit.set_clock_source(source) |
| 62 |
else: |
| 63 |
print(unit.get_clock_source()) |
| 64 |
return True |
| 65 |
print('Arguments for clock-source command:') |
| 66 |
print(' clock-source OPERATION [SOURCE]') |
| 67 |
print(' OPERATION: [{0}]'.format('|'.join(ops))) |
| 68 |
sources = unit.get_supported_clock_sources() |
| 69 |
print(' SOURCE: [{0}]'.format('|'.join(sources))) |
| 70 |
return False |
| 71 |
|
| 72 |
|
| 73 |
def handle_listen_message(unit, args): |
| 74 |
loop = GLib.MainLoop() |
| 75 |
|
| 76 |
def handle_unix_signal(): |
| 77 |
loop.quit() |
| 78 |
|
| 79 |
def handle_notified(unit, notification): |
| 80 |
print('{0:08x}'.format(notification)) |
| 81 |
|
| 82 |
def handle_disconnect(unit, loop): |
| 83 |
loop.quit() |
| 84 |
unit.connect('notified', handle_notified) |
| 85 |
GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGINT, handle_unix_signal) |
| 86 |
unit.connect('notify::is-disconnected', handle_disconnect, loop) |
| 87 |
loop.run() |
| 88 |
return True |
| 89 |
|
| 90 |
|
| 91 |
cmds = { |
| 92 |
'opt-iface-mode': handle_opt_iface_mode, |
| 93 |
'sampling-rate': handle_sampling_rate, |
| 94 |
'clock-source': handle_clock_source, |
| 95 |
'listen-message': handle_listen_message, |
| 96 |
} |
| 97 |
|
| 98 |
fullpath = CliKit.seek_snd_unit_path() |
| 99 |
if fullpath: |
| 100 |
with MotuUnit(fullpath) as unit: |
| 101 |
CliKit.dispatch_command(unit, cmds) |