|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import threading |
| 4 | +from dataclasses import dataclass |
| 5 | +from enum import Enum |
| 6 | + |
| 7 | +import yaml |
| 8 | + |
| 9 | + |
| 10 | +class AxisMode(Enum): |
| 11 | + HOLD = "hold" |
| 12 | + STICKY = "sticky" |
| 13 | + |
| 14 | + |
| 15 | +@dataclass(frozen=True) |
| 16 | +class AxisBinding: |
| 17 | + axis: int |
| 18 | + val: float |
| 19 | + mode: AxisMode |
| 20 | + |
| 21 | + |
| 22 | +@dataclass(frozen=True) |
| 23 | +class JoyState: |
| 24 | + axes: list[float] |
| 25 | + buttons: list[int] |
| 26 | + frame_id: str = "keyboard" |
| 27 | + |
| 28 | + |
| 29 | +class KeyboardJoyCore: |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + axis_mappings: dict[str, AxisBinding], |
| 33 | + button_mappings: dict[str, int], |
| 34 | + *, |
| 35 | + axis_update_period: float = 0.02, |
| 36 | + publish_period: float = 0.05, |
| 37 | + axis_increment_step: float = 0.05, |
| 38 | + frame_id: str = "keyboard", |
| 39 | + ): |
| 40 | + self.axis_mappings = axis_mappings |
| 41 | + self.button_mappings = button_mappings |
| 42 | + |
| 43 | + self.axis_update_period = float(axis_update_period) |
| 44 | + self.publish_period = float(publish_period) |
| 45 | + self.axis_increment_step = float(axis_increment_step) |
| 46 | + |
| 47 | + self._frame_id = frame_id |
| 48 | + |
| 49 | + self._active_axes: dict[int, float] = {} |
| 50 | + self._sticky_axes: dict[int, float] = {} |
| 51 | + |
| 52 | + max_axis_index = max((b.axis for b in self.axis_mappings.values()), default=-1) |
| 53 | + max_button_index = max(self.button_mappings.values(), default=-1) |
| 54 | + |
| 55 | + self._axes = [0.0] * (max_axis_index + 1) |
| 56 | + self._buttons = [0] * (max_button_index + 1) |
| 57 | + |
| 58 | + self._lock = threading.Lock() |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def from_yaml_file(cls, config_file: str) -> KeyboardJoyCore: |
| 62 | + with open(config_file, encoding="utf-8") as f: |
| 63 | + keymap = yaml.safe_load(f) or {} |
| 64 | + |
| 65 | + raw_axes = keymap.get("axes", {}) or {} |
| 66 | + axis_mappings: dict[str, AxisBinding] = {} |
| 67 | + for key, spec in raw_axes.items(): |
| 68 | + # YAML format: [axis_index, value, "sticky"/"hold"] |
| 69 | + axis, val, mode = spec |
| 70 | + axis_mappings[key] = AxisBinding( |
| 71 | + axis=int(axis), |
| 72 | + val=float(val), |
| 73 | + mode=AxisMode(str(mode)), |
| 74 | + ) |
| 75 | + |
| 76 | + button_mappings = keymap.get("buttons", {}) or {} |
| 77 | + |
| 78 | + params = keymap.get("parameters", {}) or {} |
| 79 | + axis_update_period = float(params.get("axis_update_period", 0.02)) |
| 80 | + publish_period = float(params.get("publish_period", 0.05)) |
| 81 | + axis_increment_step = float(params.get("axis_increment_step", 0.05)) |
| 82 | + |
| 83 | + return cls( |
| 84 | + axis_mappings=axis_mappings, |
| 85 | + button_mappings=button_mappings, |
| 86 | + axis_update_period=axis_update_period, |
| 87 | + publish_period=publish_period, |
| 88 | + axis_increment_step=axis_increment_step, |
| 89 | + ) |
| 90 | + |
| 91 | + def press(self, key_str: str) -> None: |
| 92 | + if not key_str: |
| 93 | + return |
| 94 | + |
| 95 | + with self._lock: |
| 96 | + if key_str in self.axis_mappings: |
| 97 | + binding = self.axis_mappings[key_str] |
| 98 | + |
| 99 | + if binding.mode == AxisMode.STICKY: |
| 100 | + axis = binding.axis |
| 101 | + new_val = ( |
| 102 | + self._sticky_axes.get(axis, 0.0) |
| 103 | + + binding.val * self.axis_increment_step |
| 104 | + ) |
| 105 | + new_val = max(min(new_val, 1.0), -1.0) |
| 106 | + self._sticky_axes[axis] = new_val |
| 107 | + self._axes[axis] = round(new_val, 3) |
| 108 | + else: |
| 109 | + # HOLD mode: ramp toward binding.val while key is held |
| 110 | + self._active_axes[binding.axis] = binding.val |
| 111 | + |
| 112 | + elif key_str in self.button_mappings: |
| 113 | + idx = int(self.button_mappings[key_str]) |
| 114 | + if idx >= len(self._buttons): |
| 115 | + self._buttons.extend([0] * (idx + 1 - len(self._buttons))) |
| 116 | + self._buttons[idx] = 1 |
| 117 | + |
| 118 | + def release(self, key_str: str) -> None: |
| 119 | + if not key_str: |
| 120 | + return |
| 121 | + |
| 122 | + with self._lock: |
| 123 | + if key_str in self.axis_mappings: |
| 124 | + binding = self.axis_mappings[key_str] |
| 125 | + self._active_axes.pop(binding.axis, None) |
| 126 | + if binding.mode != AxisMode.STICKY: |
| 127 | + self._axes[binding.axis] = 0.0 |
| 128 | + |
| 129 | + elif key_str in self.button_mappings: |
| 130 | + idx = int(self.button_mappings[key_str]) |
| 131 | + if idx < len(self._buttons): |
| 132 | + self._buttons[idx] = 0 |
| 133 | + |
| 134 | + def update_active_axes(self) -> None: |
| 135 | + """For HOLD axes: move axis value toward target at axis_increment_step per call.""" |
| 136 | + with self._lock: |
| 137 | + for axis, target in list(self._active_axes.items()): |
| 138 | + if axis >= len(self._axes): |
| 139 | + self._axes.extend([0.0] * (axis + 1 - len(self._axes))) |
| 140 | + |
| 141 | + current = self._axes[axis] |
| 142 | + delta = ( |
| 143 | + self.axis_increment_step |
| 144 | + if target > 0 |
| 145 | + else -self.axis_increment_step |
| 146 | + ) |
| 147 | + next_val = current + delta |
| 148 | + |
| 149 | + if (delta > 0 and next_val > target) or ( |
| 150 | + delta < 0 and next_val < target |
| 151 | + ): |
| 152 | + next_val = target |
| 153 | + |
| 154 | + self._axes[axis] = round(next_val, 3) |
| 155 | + |
| 156 | + def get_state(self) -> JoyState: |
| 157 | + with self._lock: |
| 158 | + return JoyState( |
| 159 | + axes=list(self._axes), |
| 160 | + buttons=list(self._buttons), |
| 161 | + frame_id=self._frame_id, |
| 162 | + ) |
| 163 | + |
| 164 | + # Helper for tests/debug |
| 165 | + def set_axis(self, axis: int, value: float) -> None: |
| 166 | + with self._lock: |
| 167 | + if axis >= len(self._axes): |
| 168 | + self._axes.extend([0.0] * (axis + 1 - len(self._axes))) |
| 169 | + self._axes[axis] = round(max(min(float(value), 1.0), -1.0), 3) |
0 commit comments