| 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'}) |
| 11 |
from gi.repository import GLib, Hinawa |
| 12 |
|
| 13 |
# Qt5 python binding |
| 14 |
from PyQt5.QtWidgets import ( |
| 15 |
QApplication, |
| 16 |
QWidget, |
| 17 |
QHBoxLayout, |
| 18 |
QVBoxLayout, |
| 19 |
QToolButton, |
| 20 |
QGroupBox, |
| 21 |
QLineEdit, |
| 22 |
QLabel, |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
def main() -> int: |
| 27 |
if len(argv) < 2: |
| 28 |
msg = ('One argument is required for path to special file of Linux FireWire character ' |
| 29 |
'device') |
| 30 |
common.print_help_with_msg(Path(__file__).name, msg) |
| 31 |
return 1 |
| 32 |
cmd, literal = argv[:2] |
| 33 |
|
| 34 |
try: |
| 35 |
path = common.detect_fw_cdev(literal) |
| 36 |
except Exception as e: |
| 37 |
common.print_help_with_msg(cmd, str(e)) |
| 38 |
return 1 |
| 39 |
|
| 40 |
try: |
| 41 |
node = Hinawa.FwNode.new() |
| 42 |
_ = node.open(str(path), 0) |
| 43 |
common.print_fw_node_information(node) |
| 44 |
except Exception as e: |
| 45 |
msg = str(e) |
| 46 |
common.print_help_with_msg(cmd, msg) |
| 47 |
return 1 |
| 48 |
|
| 49 |
with common.listen_node_event(node, path): |
| 50 |
app = QApplication(argv) |
| 51 |
sample = Sample(app, node) |
| 52 |
node.connect('disconnected', lambda n, a: a.quit(), app) |
| 53 |
|
| 54 |
sample.show() |
| 55 |
app.exec() |
| 56 |
|
| 57 |
return 0 |
| 58 |
|
| 59 |
|
| 60 |
class Sample(QWidget): |
| 61 |
def __init__(self, app, node): |
| 62 |
super(Sample, self).__init__() |
| 63 |
|
| 64 |
self.node = node |
| 65 |
self.req = Hinawa.FwReq.new() |
| 66 |
|
| 67 |
self.setWindowTitle("Hinawa-4.0 gir sample") |
| 68 |
|
| 69 |
layout = QVBoxLayout() |
| 70 |
self.setLayout(layout) |
| 71 |
|
| 72 |
top_grp = QGroupBox(self) |
| 73 |
top_layout = QHBoxLayout() |
| 74 |
top_grp.setLayout(top_layout) |
| 75 |
layout.addWidget(top_grp) |
| 76 |
|
| 77 |
buttom_grp = QGroupBox(self) |
| 78 |
buttom_layout = QHBoxLayout() |
| 79 |
buttom_grp.setLayout(buttom_layout) |
| 80 |
layout.addWidget(buttom_grp) |
| 81 |
|
| 82 |
button = QToolButton(top_grp) |
| 83 |
button.setText('Read') |
| 84 |
top_layout.addWidget(button) |
| 85 |
button.clicked.connect(self.run_transaction) |
| 86 |
|
| 87 |
close = QToolButton(top_grp) |
| 88 |
close.setText('Close') |
| 89 |
top_layout.addWidget(close) |
| 90 |
close.clicked.connect(app.quit) |
| 91 |
|
| 92 |
self.addr = QLineEdit(buttom_grp) |
| 93 |
self.addr.setText('0xfffff0000980') |
| 94 |
buttom_layout.addWidget(self.addr) |
| 95 |
|
| 96 |
self.value = QLabel(buttom_grp) |
| 97 |
self.value.setText('00000000') |
| 98 |
buttom_layout.addWidget(self.value) |
| 99 |
|
| 100 |
# handle unix signal |
| 101 |
GLib.unix_signal_add(GLib.PRIORITY_HIGH, SIGINT, lambda a: a.quit(), app) |
| 102 |
|
| 103 |
def run_transaction(self, val): |
| 104 |
addr = int(self.addr.text(), 16) |
| 105 |
frames = [0] * 4 |
| 106 |
|
| 107 |
try: |
| 108 |
quadlet = common.read_quadlet(self.node, self.req, addr) |
| 109 |
except Exception as e: |
| 110 |
print(e) |
| 111 |
|
| 112 |
label = '0x{:08x}'.format(quadlet) |
| 113 |
self.value.setText(label) |
| 114 |
|
| 115 |
if __name__ == '__main__': |
| 116 |
exit(main()) |