| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
from pathlib import Path |
| 4 |
from sys import argv, exit |
| 5 |
from signal import SIGINT |
| 6 |
from struct import unpack |
| 7 |
import common |
| 8 |
|
| 9 |
import gi |
| 10 |
gi.require_versions({'GLib': '2.0', 'Hinawa': '4.0', 'Gtk': '4.0'}) |
| 11 |
from gi.repository import GLib, Hinawa, Gtk |
| 12 |
|
| 13 |
|
| 14 |
def main() -> int: |
| 15 |
if len(argv) < 2: |
| 16 |
msg = ('One argument is required for path to special file of Linux FireWire character ' |
| 17 |
'device') |
| 18 |
common.print_help_with_msg(Path(__file__).name, msg) |
| 19 |
return 1 |
| 20 |
cmd, literal = argv[:2] |
| 21 |
|
| 22 |
try: |
| 23 |
path = common.detect_fw_cdev(literal) |
| 24 |
except Exception as e: |
| 25 |
common.print_help_with_msg(cmd, str(e)) |
| 26 |
return 1 |
| 27 |
|
| 28 |
try: |
| 29 |
node = Hinawa.FwNode.new() |
| 30 |
_ = node.open(str(path), 0) |
| 31 |
common.print_fw_node_information(node) |
| 32 |
except Exception as e: |
| 33 |
msg = str(e) |
| 34 |
common.print_help_with_msg(cmd, msg) |
| 35 |
return 1 |
| 36 |
|
| 37 |
with common.listen_node_event(node, path): |
| 38 |
main_dispatcher = GLib.MainLoop.new(None, False) |
| 39 |
node.connect('disconnected', lambda n, d: d.quit(), main_dispatcher) |
| 40 |
|
| 41 |
win = Sample(main_dispatcher, node) |
| 42 |
win.present() |
| 43 |
|
| 44 |
main_dispatcher.run() |
| 45 |
|
| 46 |
return 0 |
| 47 |
|
| 48 |
|
| 49 |
class Sample(Gtk.Window): |
| 50 |
def __init__(self, dispatcher, node): |
| 51 |
Gtk.Window.__init__(self, title="Hinawa-4.0 gir sample") |
| 52 |
|
| 53 |
self.node = node |
| 54 |
self.req = Hinawa.FwReq.new() |
| 55 |
|
| 56 |
grid = Gtk.Grid(row_spacing=10, row_homogeneous=True, |
| 57 |
column_spacing=10, column_homogeneous=True, |
| 58 |
margin_start=20, margin_end=20, margin_top=20, |
| 59 |
margin_bottom=20) |
| 60 |
self.set_child(grid) |
| 61 |
|
| 62 |
button = Gtk.Button(label="Read") |
| 63 |
button.connect("clicked", self.run_transaction) |
| 64 |
grid.attach(button, 0, 0, 1, 1) |
| 65 |
|
| 66 |
button = Gtk.Button(label="_Close", use_underline=True) |
| 67 |
button.connect("clicked", lambda s, d: d.quit(), dispatcher) |
| 68 |
grid.attach(button, 1, 0, 1, 1) |
| 69 |
|
| 70 |
self.entry = Gtk.Entry() |
| 71 |
self.entry.set_text("0xfffff0000980") |
| 72 |
grid.attach(self.entry, 0, 1, 1, 1) |
| 73 |
|
| 74 |
self.label = Gtk.Label(label="result") |
| 75 |
self.label.set_text("0x00000000") |
| 76 |
grid.attach(self.label, 1, 1, 1, 1) |
| 77 |
|
| 78 |
# handle unix signal |
| 79 |
GLib.unix_signal_add(GLib.PRIORITY_HIGH, SIGINT, lambda d: d.quit(), dispatcher) |
| 80 |
|
| 81 |
def run_transaction(self, button): |
| 82 |
addr = int(self.entry.get_text(), 16) |
| 83 |
frame = [0] * 4 |
| 84 |
|
| 85 |
try: |
| 86 |
quadlet = common.read_quadlet(self.node, self.req, addr) |
| 87 |
except Exception as e: |
| 88 |
print(e) |
| 89 |
|
| 90 |
label = '0x{:08x}'.format(quadlet) |
| 91 |
self.label.set_text(label) |
| 92 |
|
| 93 |
|
| 94 |
if __name__ == '__main__': |
| 95 |
exit(main()) |