-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensel_morph_osc.py
More file actions
1431 lines (1270 loc) · 56.4 KB
/
Copy pathsensel_morph_osc.py
File metadata and controls
1431 lines (1270 loc) · 56.4 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
#!/usr/bin/env python3
"""Broadcast live Sensel Morph data over OSC.
This tool reads frames from the Morph over the USB CDC serial register protocol,
decodes them locally, and sends OSC data. By default receivers get plain
rectangular rasters; --rle sends pressure and label raster blobs as simple
byte-RLE on dedicated OSC addresses.
"""
from __future__ import annotations
import argparse
import json
import math
import os
import pathlib
import re
import signal
import sys
import time
from dataclasses import dataclass
from typing import Iterable, Sequence
import numpy as np
from pythonosc.udp_client import SimpleUDPClient
import decode_pressure as dp
import morph_capture as mc
import sensel_morph_led as ledctl
ACTIVE_W_MM = 230.0
ACTIVE_H_MM = 130.0
DEFAULT_OSC_PORT = 1560
DEFAULT_CHUNK_SIZE = 4096
STATUS_INTERVAL_SEC = 1.0
REG_CONTACTS_MASK = 0x4B
CONTACT_MASK_ALL = 0x0F
PRESSURE_SIZES = {
"high": (185, 105),
"med": (93, 53),
"low": (47, 27),
}
SCAN_DETAILS = {
"high": 0,
"medium": 1,
}
SCAN_DETAIL_BY_PRESSURE_RES = {
"high": "high",
"med": "medium",
"low": "medium",
}
CONTENT_BITS = {
"pressure": 0x01,
"labels": 0x02,
"contacts": 0x04,
"accelerometer": 0x08,
}
CONTACT_STATE_NAMES = {
0: "invalid",
1: "start",
2: "move",
3: "end",
}
LED_TOTAL_FORCE_MODES = ("glow", "kitt", "meter", "twinkle")
LED_SPATIAL_PRESSURE_MODES = ("columns", "pulse")
LED_PRESSURE_MODES = (*LED_TOTAL_FORCE_MODES, *LED_SPATIAL_PRESSURE_MODES)
LED_MODES = (*LED_PRESSURE_MODES, "all")
@dataclass
class LiveFrame:
frame_id: int
timestamp: int
content_mask: int
pressure: dp.DecodedFrame | None = None
labels: dp.DecodedLabels | None = None
contacts: dp.DecodedContacts | None = None
accel: tuple[int, int, int] | None = None
@dataclass(frozen=True)
class RasterPeak:
x_mm: float
y_mm: float
force: float
@dataclass(frozen=True)
class RasterEllipse:
x_mm: float
y_mm: float
orientation_deg: float
major_axis_mm: float
minor_axis_mm: float
area_cells: int
@dataclass(frozen=True)
class SourcePressureCalibration:
grid_key: tuple[int, int, int, int]
dark: np.ndarray
gain: np.ndarray
support: np.ndarray
@dataclass(frozen=True)
class PressureCalibration:
path: pathlib.Path
serial_number: str
width: int
height: int
dark: np.ndarray
gain: np.ndarray
gain_key: str
coverage: np.ndarray | None = None
light: np.ndarray | None = None
target: float = 0.0
min_gain: float = 0.5
max_gain: float = 2.0
source_maps: dict[tuple[int, int, int, int], SourcePressureCalibration] | None = None
class SenselMorphOsc:
"""Live Morph-to-OSC transmitter with optional LED feedback."""
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.client = SimpleUDPClient(args.host, args.port)
self.running = True
self.last_contact_count: int | None = None
self.last_send_time = 0.0
self.profile_totals: dict[str, float] = {}
self.profile_counts: dict[str, int] = {}
self.device_serial = ""
self.status_device = ""
self.status_content_mask = 0
self.last_status_time = 0.0
self.calibration: PressureCalibration | None = None
self.led_info: ledctl.LedInfo | None = None
self.led_state: dict[str, object] = {}
def stop(self, *_args: object) -> None:
self.running = False
def run(self) -> int:
requested_content = frame_content_mask(self.args)
signal.signal(signal.SIGINT, self.stop)
signal.signal(signal.SIGTERM, self.stop)
device = self.args.device or mc.find_device()
usb_info = mc.usb_info_for_device(device)
self.device_serial = str(usb_info.get("serial_number") or "")
if self.args.calibration:
self.calibration = load_pressure_calibration(
self.args.calibration,
detected_serial=self.device_serial,
)
fd = mc.open_serial(device)
initial: dict[str, str] = {}
try:
initial = {
"scan_detail": mc.read_reg(fd, mc.REG_SCAN_DETAIL, 1).hex(),
"frame_content": mc.read_reg(fd, mc.REG_FRAME_CONTENT, 1).hex(),
"scan_enabled": mc.read_reg(fd, mc.REG_SCAN_ENABLED, 1).hex(),
"contacts_mask": mc.read_reg(fd, REG_CONTACTS_MASK, 1).hex(),
}
mc.write_reg(fd, mc.REG_SCAN_ENABLED, 0)
mc.write_reg(fd, mc.REG_SCAN_DETAIL, scan_detail_value_for_pressure_res(self.args.pressure_res))
mc.write_reg(fd, mc.REG_FRAME_CONTENT, requested_content)
if self.args.contacts:
mc.write_reg(fd, REG_CONTACTS_MASK, CONTACT_MASK_ALL)
compression_metadata = mc.read_vs(fd, mc.REG_COMPRESSION_METADATA)
grids = dp.grids_for_record({"compression_metadata": compression_metadata})
self.setup_leds(fd)
self.status_device = device
self.status_content_mask = requested_content
self.send_status(device, requested_content)
mc.write_reg(fd, mc.REG_SCAN_ENABLED, 1)
return self.loop(fd, grids)
finally:
self.restore_device(fd, initial)
def loop(self, fd: int, grids: list[dp.Grid]) -> int:
frame_count = 0
start = time.monotonic()
while self.running:
if self.args.max_frames is not None and frame_count >= self.args.max_frames:
break
frame_t0 = time.perf_counter()
if self.args.fps_limit:
wait = (1.0 / self.args.fps_limit) - (time.monotonic() - self.last_send_time)
if wait > 0:
time.sleep(wait)
try:
read_t0 = time.perf_counter()
raw_frame = mc.read_frame(fd, timeout=self.args.read_timeout)
read_t1 = time.perf_counter()
if not raw_frame.get("checksum_ok"):
continue
decode_t0 = time.perf_counter()
live = decode_live_frame(raw_frame, grids)
decode_t1 = time.perf_counter()
except Exception as exc:
if not self.args.quiet:
print(f"frame error: {exc}", file=sys.stderr)
time.sleep(0.005)
continue
self.profile_add("read_frame", read_t1 - read_t0)
self.profile_add("decode_frame", decode_t1 - decode_t0)
self.send_live_frame(live)
self.send_periodic_status()
self.last_send_time = time.monotonic()
self.update_leds(fd, live, frame_count)
self.profile_add("total_success_loop", time.perf_counter() - frame_t0)
frame_count += 1
elapsed = max(0.001, time.monotonic() - start)
if not self.args.quiet:
print(f"sent {frame_count} frames in {elapsed:.2f}s ({frame_count / elapsed:.1f} fps)", file=sys.stderr)
if self.args.profile:
self.print_profile(frame_count)
return 0
def restore_device(self, fd: int, initial: dict[str, str]) -> None:
try:
mc.write_reg(fd, mc.REG_SCAN_ENABLED, 0)
except Exception as exc:
print(f"warning: failed to stop scanning: {exc}", file=sys.stderr)
mc.clear_leds_safely(fd)
if initial:
for key, reg in (
("scan_detail", mc.REG_SCAN_DETAIL),
("frame_content", mc.REG_FRAME_CONTENT),
("contacts_mask", REG_CONTACTS_MASK),
):
try:
mc.write_reg(fd, reg, int(initial[key], 16))
except Exception as exc:
print(f"warning: failed to restore {key}: {exc}", file=sys.stderr)
os.close(fd)
def setup_leds(self, fd: int) -> None:
self.led_info = ledctl.read_led_info(fd)
if self.args.led_mode_name is None:
ledctl.write_led_values(fd, self.led_info, [0] * self.led_info.count)
return
if self.args.led_mode_name == "all":
brightness = ledctl.clamp_int(self.args.led_all_brightness or 0, 0, self.led_info.max_brightness)
ledctl.write_led_values(fd, self.led_info, [brightness] * self.led_info.count)
def update_leds(self, fd: int, frame: LiveFrame, frame_count: int) -> None:
if self.led_info is None:
return
if self.args.led_mode_name is None or self.args.led_mode_name == "all":
return
if not led_frame_due(self.args, frame_count):
return
values = self.led_values_for_frame(frame)
if values is None:
return
led_t0 = time.perf_counter()
ledctl.write_led_values(fd, self.led_info, values, timeout=self.args.led_write_timeout)
self.profile_add("led_update", time.perf_counter() - led_t0)
def led_values_for_frame(self, frame: LiveFrame) -> list[int] | None:
if self.led_info is None:
return None
led_args = led_args_for_osc(self.args)
if frame.pressure is not None:
return ledctl.mode_values(led_args, self.led_info, frame.pressure, self.led_state)
if self.args.led_mode_name in LED_TOTAL_FORCE_MODES and frame.contacts is not None:
total_force = sum(max(0.0, float(contact.force)) for contact in frame.contacts.contacts)
return ledctl.mode_values_for_total_force(led_args, self.led_info, total_force, self.led_state)
return None
def send_status(self, device: str, content_mask: int, announce: bool = True) -> None:
if announce and not self.args.quiet:
streams = ",".join(name for name, bit in CONTENT_BITS.items() if content_mask & bit)
calibration = "none"
if self.calibration is not None:
mode = "kernel-fit" if self.calibration.light is not None else "expanded"
calibration = f"{self.calibration.path} ({mode})"
print(
f"sensel_morph_osc device={device} host={self.args.host} port={self.args.port} "
f"streams={streams} pressure={self.args.pressure_res}/{self.args.pressure_type} "
f"rle={'on' if self.args.rle else 'off'} serial={self.device_serial or 'unknown'} "
f"calibration={calibration}",
file=sys.stderr,
)
self.client.send_message(
"/sensel_morph/status",
[
device,
int(content_mask),
self.args.pressure_res,
self.args.pressure_type,
int(self.args.rle),
self.device_serial,
int(self.calibration is not None),
],
)
self.last_status_time = time.monotonic()
def send_periodic_status(self) -> None:
if not self.status_device:
return
if time.monotonic() - self.last_status_time >= STATUS_INTERVAL_SEC:
self.send_status(self.status_device, self.status_content_mask, announce=False)
def send_live_frame(self, frame: LiveFrame) -> None:
send_frame_t0 = time.perf_counter()
self.client.send_message(
"/sensel_morph/frame",
[frame.frame_id, frame.timestamp, frame.content_mask],
)
self.profile_add("send_frame_msg", time.perf_counter() - send_frame_t0)
pressure_values: Sequence[int | float] | None = None
pressure_width = 0
pressure_height = 0
label_blob: bytes | None = None
label_width = 0
label_height = 0
if frame.pressure is not None and self.args.pressure:
pressure_values_t0 = time.perf_counter()
values, width, height = pressure_values_for_output(frame.pressure, self.args.pressure_res, self.calibration)
pressure_values = values
pressure_width = width
pressure_height = height
self.profile_add("pressure_values", time.perf_counter() - pressure_values_t0)
pressure_pack_t0 = time.perf_counter()
blob, max_value = pack_pressure(
values,
self.args.pressure_type,
self.args.force_scale,
normalize=self.args.pressure_normalize,
)
self.profile_add("pressure_pack", time.perf_counter() - pressure_pack_t0)
self.send_raster_blob(
"/sensel_morph/pressure",
[frame.frame_id, width, height, bit_depth(self.args.pressure_type), float(max_value)],
blob,
)
if frame.labels is not None and self.args.labels:
labels_values_t0 = time.perf_counter()
label_blob, width, height = label_values_for_output(frame.labels, self.args.label_res or self.args.pressure_res)
label_width = width
label_height = height
self.profile_add("labels_values", time.perf_counter() - labels_values_t0)
self.send_raster_blob("/sensel_morph/labels", [frame.frame_id, width, height], label_blob)
if frame.contacts is not None and self.args.contacts:
contacts_t0 = time.perf_counter()
ellipse_t0 = time.perf_counter()
raster_ellipses = (
raster_ellipses_by_label(frame.pressure, frame.labels)
if frame.pressure is not None and frame.labels is not None and self.args.pressure and self.args.labels
else {}
)
self.profile_add("contact_ellipses", time.perf_counter() - ellipse_t0)
raster_peaks = raster_peaks_by_label(
pressure_values,
pressure_width,
pressure_height,
label_blob,
label_width,
label_height,
force_scale=self.args.force_scale,
)
self.send_contacts(frame, raster_peaks, raster_ellipses)
self.profile_add("contacts_send", time.perf_counter() - contacts_t0)
if frame.accel is not None:
accel_t0 = time.perf_counter()
x, y, z = frame.accel
self.client.send_message(
"/sensel_morph/accelerometer",
[frame.frame_id, x, y, z, x / self.args.accel_counts_per_g, y / self.args.accel_counts_per_g, z / self.args.accel_counts_per_g],
)
self.profile_add("accel_send", time.perf_counter() - accel_t0)
sync_t0 = time.perf_counter()
self.client.send_message("/sensel_morph/sync", [frame.frame_id])
self.profile_add("send_sync_msg", time.perf_counter() - sync_t0)
def send_raster_blob(self, address: str, header: list[int | float], blob: bytes) -> None:
name = "pressure" if address.endswith("/pressure") else "labels"
self.profile_add(f"{name}_raw_bytes", float(len(blob)))
if self.args.rle:
rle_t0 = time.perf_counter()
encoded = rle_encode(blob)
self.profile_add(f"{name}_rle_encode", time.perf_counter() - rle_t0)
self.profile_add(f"{name}_sent_bytes", float(len(encoded)))
send_t0 = time.perf_counter()
self.send_blob(f"{address}_rle", [*header, len(blob)], encoded)
self.profile_add(f"{name}_send_blob", time.perf_counter() - send_t0)
return
self.profile_add(f"{name}_sent_bytes", float(len(blob)))
send_t0 = time.perf_counter()
self.send_blob(address, header, blob)
self.profile_add(f"{name}_send_blob", time.perf_counter() - send_t0)
def send_blob(self, address: str, header: list[int | float], blob: bytes) -> None:
if self.args.chunk_size <= 0 or len(blob) <= self.args.chunk_size:
self.client.send_message(address, [*header, blob])
return
chunk_count = math.ceil(len(blob) / self.args.chunk_size)
self.client.send_message(f"{address}/start", [*header, len(blob), chunk_count])
frame_id = int(header[0])
for chunk_index in range(chunk_count):
start = chunk_index * self.args.chunk_size
chunk = blob[start : start + self.args.chunk_size]
self.client.send_message(f"{address}/chunk", [frame_id, chunk_index, chunk_count, chunk])
def profile_add(self, name: str, value: float) -> None:
if not self.args.profile:
return
self.profile_totals[name] = self.profile_totals.get(name, 0.0) + value
self.profile_counts[name] = self.profile_counts.get(name, 0) + 1
def print_profile(self, frame_count: int) -> None:
if frame_count <= 0:
return
order = [
"read_frame",
"decode_frame",
"pressure_values",
"pressure_pack",
"pressure_rle_encode",
"pressure_send_blob",
"labels_values",
"labels_pack",
"labels_rle_encode",
"labels_send_blob",
"contact_ellipses",
"contacts_send",
"send_frame_msg",
"send_sync_msg",
"led_update",
"total_success_loop",
"pressure_raw_bytes",
"pressure_sent_bytes",
"labels_raw_bytes",
"labels_sent_bytes",
]
print("profile averages:", file=sys.stderr)
for name in order:
if name not in self.profile_totals:
continue
count = max(1, self.profile_counts.get(name, frame_count))
average = self.profile_totals[name] / count
if name.endswith("_bytes"):
print(f" {name}: {average:.1f} bytes/event", file=sys.stderr)
else:
print(f" {name}: {average * 1000.0:.3f} ms/event", file=sys.stderr)
def send_contacts(
self,
frame: LiveFrame,
raster_peaks: dict[int, RasterPeak] | None = None,
raster_ellipses: dict[int, RasterEllipse] | None = None,
) -> None:
assert frame.contacts is not None
contacts = frame.contacts.contacts
self.client.send_message("/sensel_morph/contacts", [frame.frame_id, len(contacts)])
self.send_contact_summary(frame.frame_id, contacts, raster_ellipses)
for contact in contacts:
peak = contact_peak(contact, raster_peaks)
ellipse = contact_ellipse(contact, raster_ellipses)
self.client.send_message(
"/sensel_morph/contact",
[
frame.frame_id,
contact.id,
contact.state,
ellipse.x_mm,
ellipse.y_mm,
contact.force,
int(contact.area),
ellipse.orientation_deg,
ellipse.major_axis_mm,
ellipse.minor_axis_mm,
contact.delta_x_mm or 0.0,
contact.delta_y_mm or 0.0,
contact.delta_force or 0.0,
contact.delta_area or 0.0,
contact.min_x_mm or 0.0,
contact.min_y_mm or 0.0,
contact.max_x_mm or 0.0,
contact.max_y_mm or 0.0,
peak.x_mm,
peak.y_mm,
peak.force,
],
)
compat = set(self.args.compat)
if "morphosc" in compat:
self.send_morphosc_contacts(contacts, raster_ellipses)
if "senselosc" in compat:
self.send_senselosc_contacts(frame.frame_id, contacts, raster_peaks, raster_ellipses)
def send_contact_summary(
self,
frame_id: int,
contacts: Sequence[dp.DecodedContact],
raster_ellipses: dict[int, RasterEllipse] | None = None,
) -> None:
stats = contact_stats(contacts, raster_ellipses)
self.client.send_message(
"/sensel_morph/contact_summary",
[
frame_id,
len(contacts),
stats["x"] / ACTIVE_W_MM,
stats["y"] / ACTIVE_H_MM,
stats["x_w"] / ACTIVE_W_MM,
stats["y_w"] / ACTIVE_H_MM,
stats["total_force"],
stats["avg_force"],
stats["area"],
stats["avg_dist"],
stats["avg_wdist"],
],
)
def send_morphosc_contacts(
self,
contacts: Sequence[dp.DecodedContact],
raster_ellipses: dict[int, RasterEllipse] | None = None,
) -> None:
if self.last_contact_count != len(contacts):
self.client.send_message("/num_contacts", [len(contacts)])
self.last_contact_count = len(contacts)
if not contacts:
return
self.client.send_message("/spread", [average_contact_distance(contacts, raster_ellipses)])
self.client.send_message("/total_force", [sum(contact.force for contact in contacts)])
for contact in contacts:
ellipse = contact_ellipse(contact, raster_ellipses)
self.client.send_message("/lifecycle", [contact.id, CONTACT_STATE_NAMES.get(contact.state, str(contact.state))])
self.client.send_message("/x_position", [contact.id, ellipse.x_mm])
self.client.send_message("/y_position", [contact.id, ellipse.y_mm])
self.client.send_message("/force", [contact.id, contact.force])
def send_senselosc_contacts(
self,
frame_id: int,
contacts: Sequence[dp.DecodedContact],
raster_peaks: dict[int, RasterPeak] | None = None,
raster_ellipses: dict[int, RasterEllipse] | None = None,
) -> None:
stats = contact_stats(contacts, raster_ellipses)
self.client.send_message(
"/contactAvg",
[
0,
len(contacts),
stats["x"],
stats["y"],
stats["avg_force"],
stats["avg_dist"],
int(stats["area"]),
stats["x_w"],
stats["y_w"],
stats["total_force"],
stats["avg_wdist"],
],
)
updated = [0] * 16
for index, contact in enumerate(contacts):
if 0 <= contact.id < len(updated):
updated[contact.id] = 1
ellipse = contact_ellipse(contact, raster_ellipses)
dist = distance(ellipse.x_mm, ellipse.y_mm, stats["x"], stats["y"])
wdist = distance(ellipse.x_mm, ellipse.y_mm, stats["x_w"], stats["y_w"])
self.client.send_message(
"/contact",
[
0,
contact.id,
contact.state,
ellipse.x_mm,
ellipse.y_mm,
contact.force,
int(contact.area),
dist,
wdist,
ellipse.orientation_deg,
ellipse.major_axis_mm,
ellipse.minor_axis_mm,
],
)
self.client.send_message(
"/contactDelta",
[
0,
contact.id,
contact.state,
contact.delta_x_mm or 0.0,
contact.delta_y_mm or 0.0,
contact.delta_force or 0.0,
int(contact.delta_area or 0),
],
)
self.client.send_message(
"/contactBB",
[
0,
contact.id,
contact.state,
contact.min_x_mm or 0.0,
contact.min_y_mm or 0.0,
contact.max_x_mm or 0.0,
contact.max_y_mm or 0.0,
],
)
peak = contact_peak(contact, raster_peaks)
self.client.send_message(
"/contactPeak",
[
0,
contact.id,
contact.state,
peak.x_mm,
peak.y_mm,
peak.force,
],
)
self.client.send_message("/sync", [0, *updated])
def frame_content_mask(args: argparse.Namespace) -> int:
"""Return the Morph frame-content bitmask implied by output and LED modes."""
mask = CONTENT_BITS["accelerometer"]
for name, bit in CONTENT_BITS.items():
if name == "accelerometer":
continue
if getattr(args, name):
mask |= bit
if led_mode_requires_pressure(args):
mask |= CONTENT_BITS["pressure"]
return mask
def scan_detail_value_for_pressure_res(pressure_res: str) -> int:
"""Map public pressure resolution names to the firmware scan-detail register."""
detail_name = SCAN_DETAIL_BY_PRESSURE_RES[pressure_res]
return SCAN_DETAILS[detail_name]
def led_mode_requires_pressure(args: argparse.Namespace) -> bool:
"""Return true when an LED mode needs raw pressure, not just contact force."""
mode = getattr(args, "led_mode_name", None)
if mode in LED_SPATIAL_PRESSURE_MODES:
return True
if mode in LED_TOTAL_FORCE_MODES:
return not bool(getattr(args, "contacts", False))
return False
def led_frame_due(args: argparse.Namespace, frame_count: int) -> bool:
"""Throttle LED writes by frame count so OSC throughput stays prioritized."""
interval = max(1, int(getattr(args, "led_frame_interval", 4)))
return frame_count % interval == 0
class LedModeAction(argparse.Action):
"""Parse ``--led-mode`` values, including the two-token ``all N`` mode."""
def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: str | Sequence[str],
option_string: str | None = None,
) -> None:
items = [values] if isinstance(values, str) else list(values)
if not items:
parser.error("--led-mode requires a mode")
mode = items[0].lower()
if mode not in LED_MODES:
parser.error(f"--led-mode must be one of {', '.join(LED_MODES)}")
if mode == "all":
if len(items) != 2:
parser.error("--led-mode all requires one brightness value")
try:
brightness = int(items[1], 0)
except ValueError:
parser.error("--led-mode all brightness must be an integer")
setattr(namespace, "led_mode", items)
setattr(namespace, "led_mode_name", "all")
setattr(namespace, "led_all_brightness", brightness)
return
if len(items) != 1:
parser.error(f"--led-mode {mode} does not accept extra values")
setattr(namespace, "led_mode", items)
setattr(namespace, "led_mode_name", mode)
setattr(namespace, "led_all_brightness", None)
def led_args_for_osc(args: argparse.Namespace) -> argparse.Namespace:
"""Translate OSC CLI names into the argument object used by LED helpers."""
return argparse.Namespace(
mode_name=args.led_mode_name,
pressure_ref=args.led_pressure_ref,
pressure_floor=args.led_pressure_floor,
column_threshold=args.led_column_threshold,
pulse_min_step=args.led_pulse_min_step,
pulse_max_step=args.led_pulse_max_step,
pulse_response_gamma=args.led_pulse_response_gamma,
kitt_min_step=args.led_kitt_min_step,
kitt_max_step=args.led_kitt_max_step,
seed=args.led_seed,
)
def decode_live_frame(frame: dict[str, object], grids: Iterable[dp.Grid]) -> LiveFrame:
"""Decode one raw serial frame into typed pressure/label/contact sections."""
payload_hex = frame.get("payload_hex")
if not isinstance(payload_hex, str):
raise dp.DecodeError("missing payload_hex")
payload = bytes.fromhex(payload_hex)
if len(payload) < dp.FRAME_HEADER_SIZE:
raise dp.DecodeError("truncated frame header")
content_mask = payload[0]
pos = dp.FRAME_HEADER_SIZE
contacts = None
accel = None
if content_mask & 0x04:
contacts = dp.parse_contacts(payload, pos)
pos += contacts.bytes_used
if content_mask & 0x08:
if pos + 6 > len(payload):
raise dp.DecodeError("truncated accelerometer section")
accel = (dp.i16_le(payload, pos), dp.i16_le(payload, pos + 2), dp.i16_le(payload, pos + 4))
pos += 6
pressure = None
labels = None
if content_mask & 0x03:
body = payload[pos:]
if content_mask & 0x01:
pressure, errors = dp.infer_frame(body, grids, require_all=not bool(content_mask & 0x02))
if pressure is None:
raise dp.DecodeError(f"pressure decode failed: {errors}")
if content_mask & 0x02:
labels = dp.decode_label_body(body[pressure.bytes_used :], pressure.grid)
elif content_mask & 0x02:
labels = decode_labels_without_pressure(body, grids)
return LiveFrame(
frame_id=int(frame.get("rolling_counter") or payload[1]),
timestamp=int(frame.get("timestamp_le") or int.from_bytes(payload[2:6], "little")),
content_mask=content_mask,
pressure=pressure,
labels=labels,
contacts=contacts,
accel=accel,
)
def decode_labels_without_pressure(body: bytes, grids: Iterable[dp.Grid]) -> dp.DecodedLabels:
"""Decode a labels-only frame by trying all plausible source grids."""
errors: dict[str, str] = {}
for grid in grids:
try:
return dp.decode_label_body(body, grid)
except dp.DecodeError as exc:
errors[grid.name] = str(exc)
raise dp.DecodeError(f"label decode failed: {errors}")
def pressure_values_for_output(
decoded: dp.DecodedFrame,
resolution: str,
calibration: PressureCalibration | None = None,
) -> tuple[list[float], int, int]:
"""Return pressure values at the requested public raster resolution."""
width, height = PRESSURE_SIZES[resolution]
if calibration is not None:
values, high_w, high_h = expand_calibrated_pressure(decoded, calibration)
if (high_w, high_h) != (calibration.width, calibration.height):
raise dp.DecodeError(
f"calibration size {calibration.width}x{calibration.height} does not match pressure {high_w}x{high_h}"
)
if resolution == "high":
return values, high_w, high_h
return resize_values_nearest(values, high_w, high_h, width, height), width, height
if resolution == "high":
return dp.expand_pressure(decoded)
if decoded.grid.cols < width or decoded.grid.rows < height:
values, expanded_w, expanded_h = dp.expand_pressure(decoded)
return resize_values_nearest(values, expanded_w, expanded_h, width, height), width, height
return resize_values_nearest(decoded.values, decoded.grid.cols, decoded.grid.rows, width, height), width, height
def expand_calibrated_pressure(decoded: dp.DecodedFrame, calibration: PressureCalibration) -> tuple[list[float], int, int]:
"""Apply calibration either before or after Sensel's interpolation kernel."""
source_calibration = source_calibration_for_grid(calibration, decoded.grid)
if source_calibration is None:
values, width, height = dp.expand_pressure(decoded)
return apply_pressure_calibration(values, calibration), width, height
source = np.asarray(decoded.values, dtype=np.float64).reshape((decoded.grid.rows, decoded.grid.cols))
corrected_source = np.maximum(0.0, source - source_calibration.dark) * source_calibration.gain
x_matrix, y_matrix = dp.interpolation_plan(decoded.grid.cols, decoded.grid.rows, decoded.grid.x_scale, decoded.grid.y_scale)
expanded = y_matrix @ corrected_source @ x_matrix.T
return expanded.ravel().tolist(), int(expanded.shape[1]), int(expanded.shape[0])
def apply_pressure_calibration(values: Sequence[int | float], calibration: PressureCalibration) -> list[float]:
"""Apply expanded-pixel dark subtraction and gain correction."""
raw = np.asarray(values, dtype=np.float64)
corrected = np.maximum(0.0, raw - calibration.dark) * calibration.gain
return corrected.tolist()
def source_calibration_for_grid(calibration: PressureCalibration, grid: dp.Grid) -> SourcePressureCalibration | None:
"""Return a cached source-grid calibration fitted for this compressed grid."""
if calibration.light is None:
return None
if calibration.source_maps is None:
return None
key = (grid.cols, grid.rows, grid.x_scale, grid.y_scale)
cached = calibration.source_maps.get(key)
if cached is not None:
return cached
out_cols = (grid.cols - 1) * grid.x_scale + 1
out_rows = (grid.rows - 1) * grid.y_scale + 1
if (out_cols, out_rows) != (calibration.width, calibration.height):
raise dp.DecodeError(
f"calibration size {calibration.width}x{calibration.height} does not match grid expansion {out_cols}x{out_rows}"
)
source = fit_source_calibration(calibration, grid)
calibration.source_maps[key] = source
return source
def fit_source_calibration(calibration: PressureCalibration, grid: dp.Grid) -> SourcePressureCalibration:
"""Fit high-res calibration maps back through the Sensel interpolation kernel."""
x_matrix, y_matrix = dp.interpolation_plan(grid.cols, grid.rows, grid.x_scale, grid.y_scale)
x_pinv_t = np.linalg.pinv(x_matrix).T
y_pinv = np.linalg.pinv(y_matrix)
target = calibration.target if calibration.target > 0 else calibration_target_from_gain(calibration)
light = np.asarray(calibration.light, dtype=np.float64).reshape((calibration.height, calibration.width))
coverage = coverage_mask_for_calibration(calibration).reshape((calibration.height, calibration.width))
filled_light = np.where(coverage, light, target)
source_light = np.maximum(1.0e-6, y_pinv @ filled_light @ x_pinv_t)
dark = calibration.dark.reshape((calibration.height, calibration.width))
source_dark = np.maximum(0.0, y_pinv @ dark @ x_pinv_t)
source_support = source_support_from_coverage(coverage, x_matrix, y_matrix)
source_gain = np.clip(target / source_light, calibration.min_gain, calibration.max_gain)
source_gain = 1.0 + (source_gain - 1.0) * source_support
return SourcePressureCalibration(
grid_key=(grid.cols, grid.rows, grid.x_scale, grid.y_scale),
dark=source_dark,
gain=source_gain,
support=source_support,
)
def coverage_mask_for_calibration(calibration: PressureCalibration) -> np.ndarray:
"""Return pixels covered by a brush/light calibration pass."""
if calibration.coverage is not None:
return np.asarray(calibration.coverage, dtype=np.float64) > 0
if calibration.light is not None:
return np.asarray(calibration.light, dtype=np.float64) > 0
return np.ones(calibration.width * calibration.height, dtype=bool)
def source_support_from_coverage(coverage: np.ndarray, x_matrix: np.ndarray, y_matrix: np.ndarray) -> np.ndarray:
"""Project expanded-pixel calibration coverage into compressed source cells."""
weighted = y_matrix.T @ coverage.astype(np.float64) @ x_matrix
normalizer = y_matrix.T @ np.ones_like(coverage, dtype=np.float64) @ x_matrix
support = np.divide(weighted, normalizer, out=np.zeros_like(weighted), where=normalizer > 0)
return np.clip(support, 0.0, 1.0)
def calibration_target_from_gain(calibration: PressureCalibration) -> float:
"""Choose a stable target response for gain fitting."""
if calibration.light is not None:
values = calibration.light[coverage_mask_for_calibration(calibration)]
if values.size:
return float(np.median(values))
return 1.0
def load_pressure_calibration(path_value: str, detected_serial: str) -> PressureCalibration:
"""Load a calibration JSON only when its serial matches the connected Morph."""
path = resolve_calibration_path(path_value, detected_serial)
with path.open("r", encoding="utf-8") as f:
data = json.load(f)
serial = str(data.get("device_serial") or data.get("serial_number") or "")
if not serial:
raise SystemExit(f"Refusing calibration without device_serial: {path}")
if not detected_serial:
raise SystemExit(f"Refusing calibration {path}: connected device serial could not be detected")
if serial != detected_serial:
raise SystemExit(
f"Refusing calibration {path}: calibration serial {serial!r} does not match connected device {detected_serial!r}"
)
filename_serial = serial_from_calibration_filename(path)
if filename_serial and filename_serial != detected_serial:
raise SystemExit(
f"Refusing calibration {path}: filename serial {filename_serial!r} does not match connected device {detected_serial!r}"
)
width = int(data.get("width") or 0)
height = int(data.get("height") or 0)
if (width, height) != PRESSURE_SIZES["high"]:
raise SystemExit(f"Calibration must be 185x105, got {width}x{height}: {path}")
gain_key = "gain"
cells = width * height
dark = require_numeric_array(data, "dark", cells, path)
gain = require_numeric_array(data, gain_key, cells, path)
coverage = None
if isinstance(data.get("coverage"), list):
coverage = require_numeric_array(data, "coverage", cells, path)
light = None
if isinstance(data.get("light"), list):
light = require_numeric_array(data, "light", cells, path)
target = float(data.get("target") or 0.0)
min_gain = float(data.get("min_gain") or 0.5)
max_gain = float(data.get("max_gain") or 2.0)
return PressureCalibration(
path=path,
serial_number=serial,
width=width,
height=height,
dark=dark,
gain=gain,
gain_key=gain_key,
coverage=coverage,
light=light,
target=target,
min_gain=min_gain,
max_gain=max_gain,
source_maps={},
)
def resolve_calibration_path(path_value: str, detected_serial: str = "") -> pathlib.Path:
"""Resolve a calibration file or a directory containing calibration_<serial>.json."""
path = pathlib.Path(path_value).expanduser()
candidates = [path]
if not path.is_absolute():
candidates.append(pathlib.Path.cwd() / path)
candidates.append(pathlib.Path(__file__).resolve().parent / path)
for candidate in candidates:
if candidate.is_dir():
if detected_serial:
serial_named = candidate / f"calibration_{detected_serial}.json"
if serial_named.exists():
return serial_named.resolve()
serial_named_files = sorted(candidate.glob("calibration_*.json"))
if len(serial_named_files) == 1:
return serial_named_files[0].resolve()
if len(serial_named_files) > 1:
raise SystemExit(f"Multiple calibration_*.json files in {candidate}; pass the exact file path")
raise SystemExit(f"No calibration_*.json file found in calibration directory: {candidate}")
if candidate.exists():
return candidate.resolve()
raise SystemExit(f"Calibration file not found: {path_value}")
def serial_from_calibration_filename(path: pathlib.Path) -> str:
"""Extract the serial from calibration_<serial>.json when present."""
match = re.search(r"calibration_([A-Za-z0-9]+)\.json$", path.name)
return match.group(1) if match else ""
def require_numeric_array(data: dict[str, object], key: str, count: int, path: pathlib.Path) -> np.ndarray:
"""Read a fixed-length numeric calibration array or raise a CLI error."""
values = data.get(key)