Repository navigation
Expand file tree
/
Copy pathemwolf_page.py
More file actions
1314 lines (1237 loc) · 55.7 KB
/
Copy pathemwolf_page.py
File metadata and controls
1314 lines (1237 loc) · 55.7 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import copy
import json
import sys
import time
from datetime import datetime
from pathlib import Path
from wolfeye.qt import (
QAbstractButton,
QAbstractItemView,
QAbstractSpinBox,
QCheckBox,
QComboBox,
QFileDialog,
QGridLayout,
QGroupBox,
QHBoxLayout,
QHeaderView,
QLabel,
QLineEdit,
QMessageBox,
QPlainTextEdit,
QPushButton,
QSpinBox,
QTableWidget,
QTableWidgetItem,
QTabWidget,
QVBoxLayout,
QWidget,
QObject,
QSize,
Qt,
Signal,
QColor,
QDoubleValidator,
QPainter,
QPen,
)
from wolfeye.models import DeviceInfo
from wolfeye.services.emwolf_protocol import (
BOARD_SAMPLE_TIME_US,
CHANNEL_NAMES,
CURRENT_CHANNEL_NAMES,
DEFAULT_HOST,
DEFAULT_PORT,
END_TYPES,
MAX_CURRENT_AMPLITUDE_A,
MAX_SCENARIOS,
AnalogOutput,
DeviceMetadata,
EmWolfConnection,
Project,
Scenario,
Setup,
emwolf_default_scenario,
load_project,
save_project,
)
from wolfeye.services.emwolf_run import STATE_NAMES, TERMINAL
from wolfeye.services.emwolf_process import EmWolfProcess
from wolfeye.services.emwolf_report import EmWolfRunReport, generate_emwolf_pdf
from wolfeye.services.fc22_input_adapters import emwolf_scenario_values
class _ConnectionBridge(QObject):
log = Signal(str)
state = Signal(bool)
result = Signal(int, bool, int)
trip = Signal(int, bool, int)
run_state = Signal(object)
run_event = Signal(object)
class _DeviceModelSwitch(QAbstractButton):
"""Compact two-state model selector for offline EmWolf operation."""
model_changed = Signal(str)
def __init__(self) -> None:
super().__init__()
self.setCheckable(True)
self.setCursor(Qt.CursorShape.PointingHandCursor)
self.setToolTip("Select the device model while LAN identity is unavailable")
self.setAccessibleName("Device model")
self.setFixedSize(self.sizeHint())
self.toggled.connect(self._emit_model)
def sizeHint(self) -> QSize:
return QSize(138, 26)
def model(self) -> str:
return "FC22S" if self.isChecked() else "FC22AR"
def set_model(self, model: str) -> None:
if model not in {"FC22AR", "FC22S"}:
return
blocked = self.blockSignals(True)
self.setChecked(model == "FC22S")
self.blockSignals(blocked)
self.update()
def _emit_model(self) -> None:
self.model_changed.emit(self.model())
def paintEvent(self, _event) -> None:
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
outer = self.rect().adjusted(1, 1, -1, -1)
selected = QColor("#087ad1" if self.isEnabled() else "#8295a3")
painter.setPen(QPen(QColor("#6f818e"), 1))
painter.setBrush(QColor("#f4f7f9"))
painter.drawRoundedRect(outer, 12, 12)
half = outer.width() // 2
selected_rect = (
outer.adjusted(half, 0, 0, 0)
if self.isChecked()
else outer.adjusted(0, 0, -half, 0)
)
painter.setPen(Qt.PenStyle.NoPen)
painter.setBrush(selected)
painter.drawRoundedRect(selected_rect, 12, 12)
font = painter.font()
font.setBold(True)
font.setPointSize(8)
painter.setFont(font)
left = self.rect().adjusted(0, 0, -self.width() // 2, 0)
right = self.rect().adjusted(self.width() // 2, 0, 0, 0)
painter.setPen(QColor("#ffffff") if not self.isChecked() else QColor("#445661"))
painter.drawText(left, Qt.AlignmentFlag.AlignCenter, "FC22AR")
painter.setPen(QColor("#ffffff") if self.isChecked() else QColor("#445661"))
painter.drawText(right, Qt.AlignmentFlag.AlignCenter, "FC22S")
class EmWolfPage(QWidget):
"""Qt port of the supplied EmWolf Scenario Tool, preserving its wire format."""
log_line = Signal(str)
log_cleared = Signal()
log_window_requested = Signal()
test_run_started = Signal(str)
test_result = Signal(str, object)
board_state_changed = Signal(bool, str, int)
def __init__(self) -> None:
super().__init__()
self.setObjectName("emwolfPage")
self.project = Project()
self._live_device_info: DeviceInfo | None = None
self._device_model_is_manual = False
self.execution_report: EmWolfRunReport | None = None
self.current_index: int | None = None
self.loading = False
self._run_info: dict = {}
self._run_status_ready = False
self._logged_run_id: int | None = None
self._logged_event_sequence = 0
self.bridge = _ConnectionBridge()
self.connection = EmWolfConnection(
self.bridge.log.emit,
self.bridge.result.emit,
self.bridge.state.emit,
self.bridge.trip.emit,
autonomous_enabled=True,
on_run_state=self.bridge.run_state.emit,
on_run_event=self.bridge.run_event.emit,
)
self._apply_visual_style()
self._build_ui()
self.emwolf_process = EmWolfProcess(self)
self.emwolf_process.log.connect(self._log)
self.emwolf_process.state_changed.connect(self._set_emwolf_process_state)
self.start_emwolf_button.clicked.connect(
lambda: self.emwolf_process.start(self.host.text())
)
self.stop_emwolf_button.clicked.connect(self.emwolf_process.stop)
self.bridge.log.connect(self._connection_log)
self.bridge.state.connect(self._set_connection_state)
self.bridge.result.connect(self._show_result)
self.bridge.trip.connect(self._record_trip)
self.bridge.run_state.connect(self._handle_run_state)
self.bridge.run_event.connect(self._handle_run_event)
self._load_setup()
self._refresh_scenarios(0)
self._refresh_report_preview()
self.host.textChanged.connect(lambda: self.publish_board_state())
self.port.valueChanged.connect(lambda: self.publish_board_state())
def _apply_visual_style(self) -> None:
root = Path(getattr(sys, "_MEIPASS", Path(__file__).resolve().parents[2]))
checked = (root / "assets" / "checkbox-checked.svg").as_posix()
unchecked = (root / "assets" / "checkbox-unchecked.svg").as_posix()
arrow = (root / "assets" / "combo-arrow.svg").as_posix()
self.setStyleSheet(
f"""
QWidget#emwolfPage QLineEdit,
QWidget#emwolfPage QSpinBox {{
background: #ffffff;
border: 1px solid #929292;
border-radius: 2px;
min-height: 18px;
}}
QWidget#emwolfPage QLineEdit {{ padding: 2px 8px; }}
QWidget#emwolfPage QSpinBox {{ padding: 2px 28px 2px 8px; }}
QWidget#emwolfPage QLineEdit:focus,
QWidget#emwolfPage QSpinBox:focus {{
border: 2px solid #087ad1;
}}
QWidget#emwolfPage QLineEdit:focus {{ padding: 1px 7px; }}
QWidget#emwolfPage QLineEdit#diNameEdit {{
background: transparent;
border: 1px solid transparent;
border-radius: 2px;
padding: 2px 4px;
font-weight: 600;
}}
QWidget#emwolfPage QLineEdit#diNameEdit:hover {{
background: #f7fafc;
border-color: #cad6de;
}}
QWidget#emwolfPage QLineEdit#diNameEdit:focus {{
background: #ffffff;
border: 1px solid #087ad1;
padding: 2px 4px;
}}
QWidget#emwolfPage QSpinBox:focus {{ padding: 1px 27px 1px 7px; }}
QWidget#emwolfPage QSpinBox#portSpin {{ padding: 2px 8px; }}
QWidget#emwolfPage QSpinBox#portSpin:focus {{ padding: 1px 7px; }}
QWidget#emwolfPage QComboBox {{
background: #ffffff;
border: 1px solid #737373;
border-radius: 2px;
padding: 2px 23px 2px 8px;
min-height: 18px;
}}
QWidget#emwolfPage QComboBox:hover {{
border-color: #287fbd;
}}
QWidget#emwolfPage QComboBox:focus {{
border: 2px solid #087ad1;
padding: 1px 22px 1px 7px;
}}
QWidget#emwolfPage QComboBox:disabled {{
color: #737373;
background: #e9e9e9;
border-color: #adadad;
}}
QWidget#emwolfPage QComboBox::drop-down {{
subcontrol-origin: border;
subcontrol-position: top right;
width: 22px;
background: #e9e9e9;
border-left: 1px solid #858585;
}}
QWidget#emwolfPage QComboBox::down-arrow {{
image: url("{arrow}");
width: 9px;
height: 6px;
}}
QWidget#emwolfPage QCheckBox {{
spacing: 6px;
}}
QWidget#emwolfPage QCheckBox::indicator {{
width: 16px;
height: 16px;
}}
QWidget#emwolfPage QCheckBox::indicator:unchecked {{
image: url("{unchecked}");
}}
QWidget#emwolfPage QCheckBox::indicator:checked {{
image: url("{checked}");
}}
QWidget#emwolfPage QTableWidget#scenarioTable QHeaderView::section {{
padding: 5px 8px;
}}
QWidget#emwolfPage QTableWidget#scenarioTable::item {{
padding: 4px 8px;
}}
"""
)
def _build_ui(self) -> None:
root = QVBoxLayout(self)
root.setContentsMargins(8, 5, 8, 5)
root.setSpacing(5)
self.root_layout = root
connection_box = QGroupBox("Connection")
connection_layout = QVBoxLayout(connection_box)
connection_layout.setContentsMargins(8, 10, 8, 6)
connection_layout.setSpacing(4)
connection_row = QHBoxLayout()
connection_row.setSpacing(7)
connection_layout.addLayout(connection_row)
self.connection_box = connection_box
self.connection_row = connection_layout
self.host = QLineEdit(DEFAULT_HOST)
self.host.setMinimumWidth(125)
self.host.setMaximumWidth(180)
self.port = QSpinBox()
self.port.setObjectName("portSpin")
self.port.setRange(1, 65535)
self.port.setValue(DEFAULT_PORT)
self.port.setButtonSymbols(QAbstractSpinBox.ButtonSymbols.NoButtons)
self.port.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.port.setFixedWidth(78)
self.connect_button = QPushButton("Connect")
self.start_emwolf_button = QPushButton("Start EmWolf")
self.start_emwolf_button.setToolTip("Start /home/FC22/EmWolf as root over SSH on Host")
self.stop_emwolf_button = QPushButton("Stop EmWolf")
self.stop_emwolf_button.setToolTip("Send Ctrl+C to EmWolf in the SSH session started here")
self.stop_emwolf_button.setEnabled(False)
self.send_button = QPushButton("Upload and run once")
self.send_button.setEnabled(False)
self.send_button.setToolTip("Upload all stages once; Ethernet loss does not stop the accepted run")
self.stop_button = QPushButton("Stop test")
self.stop_button.setEnabled(False)
self.connect_button.setToolTip("Reconnect to receive stored results. Disconnect leaves an autonomous run executing.")
self.state_label = QLabel("Disconnected")
self.state_label.setWordWrap(True)
self.state_label.setMaximumWidth(260)
self.save_button = QPushButton("Save project")
self.save_button.setToolTip("Save scenario settings as JSON; measured results are kept separately")
self.load_button = QPushButton("Load project")
self.load_button.setToolTip("Load scenario settings from JSON; the report remains tied to its original run")
self.pdf_button = QPushButton("Export PDF")
self.pdf_button.setToolTip(
"Export the run shown in Report preview: original settings and board-confirmed results."
)
connection_row.addWidget(QLabel("Host"))
connection_row.addWidget(self.host)
connection_row.addWidget(QLabel("Port"))
connection_row.addWidget(self.port)
connection_row.addWidget(self.start_emwolf_button)
connection_row.addWidget(self.stop_emwolf_button)
connection_row.addWidget(self.connect_button)
connection_row.addWidget(self.send_button)
connection_row.addWidget(self.stop_button)
connection_row.addStretch()
file_row = QHBoxLayout()
file_row.setSpacing(7)
file_row.addWidget(self.state_label)
file_row.addStretch()
file_row.addWidget(self.pdf_button)
file_row.addWidget(self.save_button)
file_row.addWidget(self.load_button)
connection_layout.addLayout(file_row)
root.addWidget(connection_box)
device_box = QGroupBox("Device Under Test")
device_row = QHBoxLayout(device_box)
device_row.setContentsMargins(10, 8, 10, 7)
device_row.setSpacing(10)
self.device_box = device_box
self.device_model_value = QLabel("Not available")
self.device_model_value.setStyleSheet("font-weight:700;color:#16324a")
self.device_details_value = QLabel("FW - | HW - | Build -")
self.device_ip_value = QLabel("IP -")
self.device_transport_value = QLabel("LAN status not read")
self.device_status_value = QLabel("OFFLINE")
self.device_status_value.setStyleSheet("color:#667784;font-weight:700")
self.device_model_switch = _DeviceModelSwitch()
self.device_model_switch.model_changed.connect(self._set_offline_device_model)
device_row.addWidget(QLabel("MODEL"))
device_row.addWidget(self.device_model_value)
device_row.addSpacing(12)
device_row.addWidget(self.device_details_value)
device_row.addSpacing(12)
device_row.addWidget(self.device_ip_value)
device_row.addSpacing(12)
device_row.addWidget(self.device_transport_value)
device_row.addStretch()
device_row.addWidget(self.device_model_switch)
device_row.addSpacing(4)
device_row.addWidget(self.device_status_value)
root.addWidget(device_box)
self._set_offline_device_model(self.device_model_switch.model())
setup_box = QGroupBox("Setup")
setup_box.setMaximumHeight(128)
setup_grid = QGridLayout(setup_box)
setup_grid.setContentsMargins(8, 7, 8, 4)
setup_grid.setHorizontalSpacing(8)
setup_grid.setVerticalSpacing(2)
setup_grid.setColumnStretch(10, 1)
self.setup_box = setup_box
self.setup_grid = setup_grid
self.offsets: dict[str, QSpinBox] = {}
for index, name in enumerate(CHANNEL_NAMES):
label = QLabel(f"{name} offset")
label.setFixedWidth(104)
value = QSpinBox()
value.setRange(0, 65535)
value.setValue(2048)
value.setFixedWidth(116)
value.setAlignment(
Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter
)
row, pair = divmod(index, 4)
setup_grid.addWidget(label, row, pair * 2)
setup_grid.addWidget(value, row, pair * 2 + 1)
self.offsets[name] = value
self.sample_time = QLineEdit(str(BOARD_SAMPLE_TIME_US))
self.sample_time.setReadOnly(True)
self.sample_time.setToolTip("The current EmWolf firmware uses a fixed 320 us sample period.")
self.sample_time.setFixedWidth(90)
sample_time_label = QLabel("Sample (us)")
sample_time_label.setFixedWidth(104)
setup_grid.addWidget(sample_time_label, 2, 0)
setup_grid.addWidget(self.sample_time, 2, 1)
self.di_name_edits: dict[int, QLineEdit] = {}
self.di_labels: dict[int, QLabel] = {}
for input_id in range(1, 5):
name = QLineEdit(f"DI{input_id}")
name.setObjectName("diNameEdit")
name.setMaxLength(32)
name.setFixedWidth(104)
name.setAlignment(
Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter
)
name.setToolTip(
"Click to edit the digital input name used in End type, logs, and reports"
)
name.editingFinished.connect(
lambda input_id=input_id: self._di_name_changed(input_id)
)
setup_grid.addWidget(name, 2, input_id * 2)
state = QLabel("?")
state.setAlignment(Qt.AlignmentFlag.AlignCenter)
state.setFixedWidth(46)
setup_grid.addWidget(state, 2, input_id * 2 + 1)
self.di_name_edits[input_id] = name
self.di_labels[input_id] = state
root.addWidget(setup_box)
body = QWidget()
body_layout = QHBoxLayout(body)
body_layout.setContentsMargins(0, 0, 0, 0)
body_layout.setSpacing(6)
left = QTabWidget()
self.scenario_tabs = left
scenario_list = QWidget()
left_layout = QVBoxLayout(scenario_list)
left_layout.setContentsMargins(0, 0, 0, 0)
left_layout.setSpacing(7)
self.scenario_table = QTableWidget(0, 3)
self.scenario_table.setObjectName("scenarioTable")
self.scenario_table.setHorizontalHeaderLabels(["Scenario", "End", "DO"])
self.scenario_table.setSelectionBehavior(
QAbstractItemView.SelectionBehavior.SelectRows
)
self.scenario_table.setSelectionMode(
QAbstractItemView.SelectionMode.SingleSelection
)
self.scenario_table.setEditTriggers(
QAbstractItemView.EditTrigger.NoEditTriggers
)
self.scenario_table.verticalHeader().setVisible(False)
self.scenario_table.verticalHeader().setDefaultSectionSize(30)
header = self.scenario_table.horizontalHeader()
header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch)
header.setSectionResizeMode(1, QHeaderView.ResizeMode.Fixed)
header.setSectionResizeMode(2, QHeaderView.ResizeMode.Fixed)
self.scenario_table.setColumnWidth(1, 110)
self.scenario_table.setColumnWidth(2, 60)
header.setDefaultAlignment(Qt.AlignmentFlag.AlignCenter)
left_layout.addWidget(self.scenario_table)
button_grid = QGridLayout()
button_grid.setContentsMargins(0, 0, 0, 0)
button_grid.setHorizontalSpacing(7)
button_grid.setVerticalSpacing(6)
actions = (
("Add", self._add),
("Add default", self._add_default),
("Duplicate", self._duplicate),
("Remove", self._remove),
("Up", lambda: self._move(-1)),
("Down", lambda: self._move(1)),
)
for index, (text, callback) in enumerate(actions):
button = QPushButton(text)
button.clicked.connect(callback)
button_grid.addWidget(button, index // 3, index % 3)
left_layout.addLayout(button_grid)
preview = QWidget()
preview_layout = QVBoxLayout(preview)
preview_layout.setContentsMargins(6, 6, 6, 6)
self.report_table = QTableWidget(0, 4)
self.report_table.setHorizontalHeaderLabels(["Stage", "Result / status", "Input", "Time (ms)"])
self.report_table.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
self.report_table.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
self.report_table.verticalHeader().setVisible(False)
report_header = self.report_table.horizontalHeader()
report_header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch)
for column in range(1, 4):
report_header.setSectionResizeMode(column, QHeaderView.ResizeMode.ResizeToContents)
preview_layout.addWidget(self.report_table, 1)
left.addTab(scenario_list, "Scenarios")
left.addTab(preview, "Report preview")
editor = QGroupBox("Selected scenario")
editor_grid = QGridLayout(editor)
editor_grid.setContentsMargins(10, 14, 10, 8)
editor_grid.setHorizontalSpacing(8)
editor_grid.setVerticalSpacing(2)
self.editor_box = editor
self.editor_grid = editor_grid
self.name = QLineEdit()
self.name.setFixedWidth(240)
editor_grid.addWidget(QLabel("Name"), 0, 0)
editor_grid.addWidget(self.name, 0, 1, 1, 2)
headers = (
"Channel",
"On",
"Amplitude",
"Frequency",
"Phase",
"H1",
"H1 amp",
"H2",
"H2 amp",
)
for column, title in enumerate(headers):
editor_grid.addWidget(QLabel(title), 1, column)
self.analog_widgets: dict[str, dict[str, QWidget]] = {}
for row, channel in enumerate(CHANNEL_NAMES, 2):
enabled = QCheckBox()
amplitude = self._number_edit(92)
frequency = self._number_edit(92)
phase = self._number_edit(76)
h1_order = QComboBox()
h1_order.addItems([str(v) for v in range(2, 16)])
h1_order.setFixedWidth(68)
h1_amplitude = self._number_edit(88)
h1_amplitude.setValidator(QDoubleValidator(h1_amplitude))
h2_order = QComboBox()
h2_order.addItems([str(v) for v in range(2, 16)])
h2_order.setFixedWidth(68)
h2_amplitude = self._number_edit(88)
h2_amplitude.setValidator(QDoubleValidator(h2_amplitude))
if channel in CURRENT_CHANNEL_NAMES:
for current_edit in (amplitude, h1_amplitude, h2_amplitude):
current_edit.setValidator(
QDoubleValidator(
0.0,
MAX_CURRENT_AMPLITUDE_A,
6,
current_edit,
)
)
current_edit.setToolTip(
f"Current limit: fundamental + harmonics <= "
f"{MAX_CURRENT_AMPLITUDE_A:g} A"
)
widgets = (
QLabel(channel),
enabled,
amplitude,
frequency,
phase,
h1_order,
h1_amplitude,
h2_order,
h2_amplitude,
)
for column, widget in enumerate(widgets):
editor_grid.addWidget(widget, row, column)
self.analog_widgets[channel] = {
"enabled": enabled,
"amplitude": amplitude,
"frequency": frequency,
"phase": phase,
"h1_order": h1_order,
"h1_amplitude": h1_amplitude,
"h2_order": h2_order,
"h2_amplitude": h2_amplitude,
}
separator = QWidget()
separator.setFixedHeight(1)
separator.setStyleSheet("background:#aaa")
editor_grid.addWidget(separator, 10, 0, 1, 9)
editor_grid.setRowMinimumHeight(10, 5)
editor_grid.addWidget(QLabel("Digital outputs"), 11, 0)
digital_row = QHBoxLayout()
digital_row.setContentsMargins(0, 2, 0, 2)
digital_row.setSpacing(24)
self.digital_outputs: list[QCheckBox] = []
digital_output_names = ("DO1 (CLOSE)", "DO2 (TRIP / OPEN)", "DO3", "DO4")
digital_output_colors = ("#b42318", "#087a20", "", "")
for name, color in zip(digital_output_names, digital_output_colors):
checkbox = QCheckBox(name)
if color:
checkbox.setStyleSheet(f"color:{color};font-weight:700")
self.digital_outputs.append(checkbox)
digital_row.addWidget(checkbox)
digital_row.addStretch()
editor_grid.addLayout(digital_row, 11, 1, 1, 8)
self.end_type = QComboBox()
self.end_type.addItems(END_TYPES)
self.end_type.setToolTip(
"DI_TOGGLE completes on the first valid HIGH or LOW transition."
)
self.end_type.setFixedWidth(140)
self.digital_input = QComboBox()
self.digital_input.setFixedWidth(130)
self.digital_input.view().setMinimumWidth(130)
self.end_value = self._number_edit(110)
end_row = QHBoxLayout()
end_row.setContentsMargins(0, 5, 0, 2)
end_row.setSpacing(12)
end_row.addWidget(QLabel("End type"))
end_row.addWidget(self.end_type)
end_row.addSpacing(28)
self.digital_input_label = QLabel("Digital input")
end_row.addWidget(self.digital_input_label)
end_row.addWidget(self.digital_input)
end_row.addSpacing(28)
self.end_value_label = QLabel("Cycles")
end_row.addWidget(self.end_value_label)
end_row.addWidget(self.end_value)
end_row.addStretch()
editor_grid.addLayout(end_row, 12, 0, 1, 9)
editor_grid.setRowStretch(13, 1)
left.setMinimumWidth(240)
editor.setMinimumWidth(0)
body_layout.addWidget(left, 3)
body_layout.addWidget(editor, 7)
root.addWidget(body, 1)
log_box = QGroupBox("Run log")
log_layout = QVBoxLayout(log_box)
log_layout.setContentsMargins(6, 11, 6, 6)
log_layout.setSpacing(4)
self.log_box = log_box
self.log_layout = log_layout
log_tools = QHBoxLayout()
log_tools.addStretch()
self.open_log_button = QPushButton("Open log window")
self.open_log_button.clicked.connect(self.log_window_requested)
log_tools.addWidget(self.open_log_button)
self.clear_log_button = QPushButton("Clear log")
self.clear_log_button.setToolTip("Clear both log views; recorded results and PDF data are kept")
log_tools.addWidget(self.clear_log_button)
log_layout.addLayout(log_tools)
self.log_view = QPlainTextEdit()
self.log_view.setReadOnly(True)
self.clear_log_button.clicked.connect(self.clear_log)
self.log_view.setMaximumBlockCount(1000)
self.log_view.setLineWrapMode(QPlainTextEdit.LineWrapMode.NoWrap)
self.log_view.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
self.log_view.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
self.log_view.setMinimumHeight(72)
self.log_view.setMaximumHeight(100)
log_layout.addWidget(self.log_view)
log_box.setMaximumHeight(158)
root.addWidget(log_box)
self.connect_button.clicked.connect(self._toggle_connection)
self.send_button.clicked.connect(self._send)
self.stop_button.clicked.connect(self._stop_test)
self.save_button.clicked.connect(self._save)
self.load_button.clicked.connect(self._load)
self.pdf_button.clicked.connect(self._export_pdf)
self.scenario_table.currentCellChanged.connect(
lambda row, _column, _old_row, _old_column: (
self._select(row) if row >= 0 else None
)
)
self.end_type.currentTextChanged.connect(self._end_type_changed)
self._update_end_controls(self.end_type.currentText())
@staticmethod
def _number_edit(width: int) -> QLineEdit:
widget = QLineEdit()
widget.setFixedWidth(width)
return widget
def _toggle_connection(self) -> None:
if self.connection.connected:
self.connection.disconnect()
else:
self.connection.connect(self.host.text().strip(), self.port.value())
def _set_emwolf_process_state(self, state: str) -> None:
self.start_emwolf_button.setEnabled(state == "stopped")
self.stop_emwolf_button.setEnabled(state == "running")
self.start_emwolf_button.setText("Starting..." if state == "starting" else "Start EmWolf")
self.stop_emwolf_button.setText("Stopping..." if state == "stopping" else "Stop EmWolf")
def _refresh_report_preview(self, scenario_index: int | None = None) -> None:
report = self.execution_report
if report is None:
self.report_table.setRowCount(0)
return
self.report_table.setRowCount(len(report.executions))
rows = range(len(report.executions)) if scenario_index is None else (scenario_index,)
for row in rows:
scenario = report.project.scenarios[row]
execution = report.executions[row]
result_input = "-"
if execution.trip_input is not None:
result_input = (
f"{report.project.setup.digital_input_name(execution.trip_input)} "
f"(DI{execution.trip_input}) / {'HIGH' if execution.trip_value else 'LOW'}"
)
values = (
f"{row + 1}. {scenario.name}",
execution.operation or execution.state,
"-" if execution.trip_input is None else f"DI{execution.trip_input} {'HIGH' if execution.trip_value else 'LOW'}",
"-" if execution.trip_time_us is None else f"{execution.trip_time_us / 1000:.3f}",
)
for column, value in enumerate(values):
item = QTableWidgetItem(value)
item.setToolTip(result_input if column == 2 else value)
self.report_table.setItem(row, column, item)
def _set_connection_state(self, connected: bool) -> None:
if connected: self._run_status_ready=False
self.state_label.setText("Connected; checking run" if connected else "Disconnected; reconnect to receive results")
self.send_button.setEnabled(False)
self.stop_button.setEnabled(connected)
self.state_label.setStyleSheet("color:#087a20" if connected else "")
self.connect_button.setText("Disconnect" if connected else "Connect")
self.host.setEnabled(not connected)
self.port.setEnabled(not connected)
self.board_state_changed.emit(connected, self.host.text().strip(), self.port.value())
self._refresh_report_preview()
def _stop_test(self) -> None:
try:
self.connection.stop_outputs()
except (OSError,ValueError) as exc:
QMessageBox.critical(self,"Stop failed",str(exc))
def _handle_run_state(self, info: dict) -> None:
if "restore" in info:
meta=info["restore"]
run_id=int(meta["id"])
if run_id!=self._logged_run_id:
self._logged_run_id=run_id
self._logged_event_sequence=0
self._log(f"Report source: run {run_id:016x}; restoring original settings and board results")
if self._run_info.get("id") != run_id:
self._run_info = {}
project=Project.from_dict(meta["project"])
self.execution_report=EmWolfRunReport.begin(project,started_at=datetime.fromisoformat(meta["started_at"]))
self.execution_report.board_run_id=f"{int(meta['id']):016x}"
self.execution_report.board_state=STATE_NAMES.get(meta.get("state",1),"UNKNOWN")
self.execution_report.results_received=bool(meta.get("collected"))
self.test_run_started.emit(project.device.model)
self._refresh_report_preview()
return
previous_status = (
self._run_info.get("id"), self._run_info.get("state"), self._run_info.get("collected")
)
self._run_info=info.copy()
self._run_status_ready=True
state=info.get("state",0)
connected=self.connection.connected
description=STATE_NAMES.get(state,"UNKNOWN")
received=bool(info.get("collected")) and info.get("pc_count",0)>=info.get("count",0)
receipt=(" | results received" if received else f" | receiving {info.get('pc_count',0)}/{info.get('count',0)}") if state in TERMINAL else ""
self.state_label.setText(f"{'Connected' if connected else 'Disconnected'} | {description}{receipt}")
self.send_button.setEnabled(connected and (state==0 or received))
self.stop_button.setEnabled(connected and state in (0,1,2))
run_id=int(info.get("id",0))
report=self.execution_report
if report and info.get("id")==int(report.board_run_id or "0",16):
report.board_state=description
report.results_received=received
if state in TERMINAL:
if report.completed_at is None: report.completed_at=datetime.now().astimezone()
if state!=4:
for execution in report.executions:
if execution.state in ("RUNNING","ARMED","CONFIGURED"): execution.state=description
else:
report.completed_at=None
if run_id and previous_status != (run_id, state, info.get("collected")):
self._log(f"Run {run_id:016x}: {description}; results {'fully received' if received else 'receiving automatically'}")
self._refresh_report_preview()
def _handle_run_event(self, event: dict) -> None:
report=self.execution_report
if report is None: return
try:
index=report.record_board_event(event)
except ValueError as exc:
self._log(str(exc)); return
if event["kind"] in (3,4) and 1<=event["input_id"]<=4:
self.di_labels[event["input_id"]].setText("HIGH" if event["value"] else "LOW")
if index is not None:
execution=report.executions[index]
self.test_result.emit(report.project.device.model,emwolf_scenario_values(index,execution))
sequence=int(event["sequence"])
scenario_index = int(event["scenario"])
if sequence>self._logged_event_sequence:
self._logged_event_sequence=sequence
name=report.project.scenarios[scenario_index].name
if event["kind"]==2:
self._log_scenario_started(scenario_index)
elif event["kind"]==5:
self._log(f"Scenario {scenario_index + 1} ({name}) ended after {event['time_us']/1000:.3f} ms")
elif event["kind"]==3 and 1<=event["input_id"]<=4:
input_name = report.project.setup.digital_input_name(event["input_id"])
self._log(
f"Scenario {scenario_index + 1} ({name}): {input_name} (DI{event['input_id']})="
f"{'HIGH' if event['value'] else 'LOW'} at {event['time_us']/1000:.3f} ms "
"after this stage's output; input edge only"
)
elif event["kind"]==4 and index is not None:
self._log_trip_result(index)
if event["kind"] != 3:
self._refresh_report_preview(scenario_index)
def _log_scenario_started(self, index: int) -> None:
report = self.execution_report
if report is None:
return
scenario = report.project.scenarios[index]
self._log(f"Scenario {index + 1}/{len(report.executions)} started: {scenario.name} - output applied by board")
for label, names, unit in (("Voltage", CHANNEL_NAMES[:4], "V"), ("Current", CHANNEL_NAMES[4:], "A")):
values = []
for name in names:
analog = scenario.analogs[name]
value = f"{name}=OFF"
if analog.enabled:
value = f"{name}={analog.amplitude:g} {unit} @ {analog.frequency:g} Hz, {analog.phase} deg"
for order, amplitude in (
(analog.harmonic1_order, analog.harmonic1_amplitude),
(analog.harmonic2_order, analog.harmonic2_amplitude),
):
if amplitude:
value += f", H{order}={amplitude:g} {unit}"
values.append(value)
self._log(f" {label}: " + " | ".join(values))
outputs = ", ".join(
label for label, enabled in zip(("DO1 CLOSE", "DO2 TRIP/OPEN", "DO3", "DO4"), scenario.digital_outputs)
if enabled
) or "all LOW"
if scenario.end_type == "DI_TOGGLE":
input_name = report.project.setup.digital_input_name(scenario.digital_input)
condition = f"first edge on {input_name} (DI{scenario.digital_input}); time starts at this output LOAD"
elif scenario.end_type == "TIME":
condition = f"{scenario.end_value:g} s after output LOAD"
elif scenario.end_type == "CYCLES":
condition = f"{scenario.end_value:g} cycles"
else:
condition = "infinite; use Stop test to finish"
self._log(f" Digital outputs: {outputs} | End: {condition}")
def publish_board_state(self) -> None:
self.board_state_changed.emit(self.connection.connected, self.host.text().strip(), self.port.value())
def toggle_board_connection(self, host: str, port: int) -> None:
"""Share the existing board connection with FC22 Tester controls."""
if not self.connection.connected:
self.host.setText(host)
self.port.setValue(port)
self._toggle_connection()
def run_project(self) -> None:
"""Use the same commit, validation and EWS2 send path as the EmWolf tab."""
self._send()
def set_device_info(self, info: DeviceInfo) -> None:
"""Use identity read by WolfEye LAN status without coupling wire protocols."""
self._live_device_info = copy.copy(info)
self._refresh_device_info_banner()
def _set_offline_device_model(self, model: str) -> None:
info = self._live_device_info
if info is not None and info.connected:
return
self._device_model_is_manual = True
self.project.device = DeviceMetadata(model=model)
self._refresh_device_info_banner()
def _refresh_device_info_banner(self) -> None:
info = self._live_device_info
lan_connected = bool(info and info.connected)
live_identity = bool(
lan_connected
and info
and info.model.strip()
and info.model not in {"—", "WolfEye"}
)
if live_identity and info is not None:
self.project.device = DeviceMetadata(
model=info.model,
ip=info.ip,
firmware=info.firmware,
hardware=info.hardware,
build=info.build,
transport=info.transport,
)
device = self.project.device
if not lan_connected and device.model not in {"FC22AR", "FC22S"}:
device = DeviceMetadata(model=self.device_model_switch.model())
self.project.device = device
self._device_model_is_manual = True
if lan_connected:
self._device_model_is_manual = False
self.device_model_switch.setEnabled(False)
self.device_model_switch.setCursor(Qt.CursorShape.ArrowCursor)
if device.model in {"FC22AR", "FC22S"}:
self.device_model_switch.set_model(device.model)
else:
self.device_model_switch.setEnabled(True)
self.device_model_switch.setCursor(Qt.CursorShape.PointingHandCursor)
if device.model in {"FC22AR", "FC22S"}:
self.device_model_switch.set_model(device.model)
self.device_model_value.setText(device.model or "Not available")
self.device_details_value.setText(
f"FW {device.firmware or '-'} | HW {device.hardware or '-'} | "
f"Build {device.build or '-'}"
)
self.device_ip_value.setText(f"IP {device.ip or '-'}")
self.device_transport_value.setText(device.transport or "LAN status not read")
if live_identity:
self.device_status_value.setText("LIVE LAN")
self.device_status_value.setStyleSheet("color:#087a20;font-weight:700")
elif self._device_model_is_manual:
self.device_status_value.setText("MANUAL")
self.device_status_value.setStyleSheet("color:#a55b00;font-weight:700")
elif device.available:
self.device_status_value.setText("LOADED JSON")
self.device_status_value.setStyleSheet("color:#087ad1;font-weight:700")
else:
self.device_status_value.setText("OFFLINE")
self.device_status_value.setStyleSheet("color:#667784;font-weight:700")
def _show_result(self, input_id: int, value: bool, elapsed_us: int) -> None:
if self.execution_report is not None:
if self.execution_report.record_transition(input_id, value, elapsed_us) is None:
return
self.di_labels[input_id].setText("HIGH" if value else "LOW")
input_name = self.project.setup.digital_input_name(input_id)
self._log(
f"{input_name} (DI{input_id})={'HIGH' if value else 'LOW'}, "
f"time={elapsed_us / 1000:.3f} ms"
)
def _record_trip(self, input_id: int, value: bool, elapsed_us: int) -> None:
if self.execution_report is None:
return
scenario_index = self.execution_report.record_trip(
input_id, value, elapsed_us
)
if scenario_index is None:
return
execution = self.execution_report.executions[scenario_index]
self.test_result.emit(
self.execution_report.project.device.model,
emwolf_scenario_values(scenario_index, execution),
)
self._log_trip_result(scenario_index)
self._refresh_report_preview(scenario_index)
def _log_trip_result(self, scenario_index: int) -> None:
if self.execution_report is None:
return
scenario = self.execution_report.project.scenarios[scenario_index]
execution = self.execution_report.executions[scenario_index]
trip_time_us = execution.trip_time_us
trip_input = execution.trip_input
trip_value = execution.trip_value
if trip_time_us is None or trip_input is None or trip_value is None:
return
input_name = self.execution_report.project.setup.digital_input_name(trip_input)
operation = execution.operation or "TRIP"
self._log(
f"Scenario {scenario_index + 1} ({scenario.name}): confirmed {operation} on {input_name} (DI{trip_input}) "
f"({'HIGH' if trip_value else 'LOW'}), "
f"{operation.casefold()} time={trip_time_us / 1000:.3f} ms - included in PDF"
)
def clear_log(self) -> None:
self.log_view.clear()
self.log_cleared.emit()
def _connection_log(self, message: str) -> None:
# Keep user actions and errors, while hiding routine network traffic.
if not message.startswith(("Received frame type ", "Receiving stored EmWolf results;")):
self._log(message)
def _log(self, message: str) -> None:
line = f"{time.strftime('%H:%M:%S')} {message}"
self.log_view.appendPlainText(line)
self.log_line.emit(line)
def _read_setup(self) -> Setup:
return Setup(
{name: widget.value() for name, widget in self.offsets.items()},
BOARD_SAMPLE_TIME_US,