|
| 1 | +import math |
| 2 | +import struct |
| 3 | +import os |
| 4 | +os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" # Disable pygame welcome message |
| 5 | +import pygame |
| 6 | +import time |
| 7 | + |
| 8 | +class Button: |
| 9 | + def __init__(self) -> None: |
| 10 | + self.pressed = False |
| 11 | + self.on_pressed = False |
| 12 | + self.on_released = False |
| 13 | + self.data = 0 |
| 14 | + self.click_count = 0 # 记录连续点击次数 |
| 15 | + self.last_pressed_time = 0 # 上次按下时间 |
| 16 | + |
| 17 | + def __call__(self, data) -> None: |
| 18 | + current_time = time.perf_counter() |
| 19 | + # print('before',self.data) |
| 20 | + |
| 21 | + self.pressed = (data != 0) |
| 22 | + self.on_pressed = self.pressed and self.data == 0 |
| 23 | + self.on_released = not self.pressed and self.data != 0 |
| 24 | + |
| 25 | + # print('after',self.data) |
| 26 | + # 处理连续点击 |
| 27 | + if self.on_pressed: |
| 28 | + # print('on_pressed') |
| 29 | + # print('on_pressed current_time',current_time) |
| 30 | + # print('on_pressed last_pressed_time',self.last_pressed_time) |
| 31 | + # print('on_pressed diff',current_time-self.last_pressed_time) |
| 32 | + |
| 33 | + if current_time - self.last_pressed_time <= 0.3: # 0.1 秒以内的连续点击 |
| 34 | + self.click_count += 1 |
| 35 | + # print(self.click_count) |
| 36 | + else: |
| 37 | + self.click_count = 0 # 超过时间间隔,重置计数器 |
| 38 | + self.last_pressed_time = current_time |
| 39 | + self.data = data |
| 40 | + |
| 41 | + def reset_click_count(self): |
| 42 | + """手动重置连续点击计数器""" |
| 43 | + self.click_count = 0 |
| 44 | + |
| 45 | +class Axis: |
| 46 | + def __init__(self) -> None: |
| 47 | + self.data = 0.0 |
| 48 | + self.pressed = False |
| 49 | + self.on_pressed = False |
| 50 | + self.on_released = False |
| 51 | + |
| 52 | + self.smooth = 0.03 |
| 53 | + self.deadzone = 0.01 |
| 54 | + self.threshold = 0.5 |
| 55 | + |
| 56 | + def __call__(self, data) -> None: |
| 57 | + data_deadzone = 0.0 if math.fabs(data) < self.deadzone else data |
| 58 | + new_data = self.data * (1 - self.smooth) + data_deadzone * self.smooth |
| 59 | + self.pressed = math.fabs(new_data) > self.threshold |
| 60 | + self.on_pressed = self.pressed and math.fabs(self.data) < self.threshold |
| 61 | + self.on_released = not self.pressed and math.fabs(self.data) > self.threshold |
| 62 | + self.data = new_data |
| 63 | + |
| 64 | + |
| 65 | +class Joystick: |
| 66 | + def __init__(self) -> None: |
| 67 | + # Buttons |
| 68 | + self.back = Button() |
| 69 | + self.start = Button() |
| 70 | + # self.LS = Button() |
| 71 | + # self.RS = Button() |
| 72 | + self.LB = Button() |
| 73 | + self.RB = Button() |
| 74 | + self.LT = Button() |
| 75 | + self.RT = Button() |
| 76 | + self.A = Button() |
| 77 | + self.B = Button() |
| 78 | + self.X = Button() |
| 79 | + self.Y = Button() |
| 80 | + self.up = Button() |
| 81 | + self.down = Button() |
| 82 | + self.left = Button() |
| 83 | + self.right = Button() |
| 84 | + self.F1 = Button() |
| 85 | + self.F2 = Button() |
| 86 | + |
| 87 | + # Axes |
| 88 | + # self.LT = Axis() |
| 89 | + # self.RT = Axis() |
| 90 | + self.lx = Axis() |
| 91 | + self.ly = Axis() |
| 92 | + self.rx = Axis() |
| 93 | + self.ry = Axis() |
| 94 | + |
| 95 | + self.last_active_time = time.perf_counter() # 最后一次活动时间 |
| 96 | + self.inactive_timeout = 0.5 # 超时时间(单位:秒) |
| 97 | + def update(self): |
| 98 | + """ |
| 99 | + Update the current handle key based on the original data |
| 100 | + Used to update flag bits such as on_pressed |
| 101 | +
|
| 102 | + Examples: |
| 103 | + >>> new_A_data = 1 |
| 104 | + >>> self.A( new_A_data ) |
| 105 | + """ |
| 106 | + pass |
| 107 | + |
| 108 | + def extract(self, wireless_remote): |
| 109 | + """ |
| 110 | + Extract data from unitree_joystick |
| 111 | + wireless_remote: uint8_t[40] |
| 112 | + """ |
| 113 | + # Buttons |
| 114 | + button1 = [int(data) for data in f'{wireless_remote[2]:08b}'] |
| 115 | + button2 = [int(data) for data in f'{wireless_remote[3]:08b}'] |
| 116 | + self.LT(button1[2]) |
| 117 | + self.RT(button1[3]) |
| 118 | + self.back(button1[4]) |
| 119 | + self.start(button1[5]) |
| 120 | + self.LB(button1[6]) |
| 121 | + self.RB(button1[7]) |
| 122 | + self.left(button2[0]) |
| 123 | + self.down(button2[1]) |
| 124 | + self.right(button2[2]) |
| 125 | + self.up(button2[3]) |
| 126 | + self.Y(button2[4]) |
| 127 | + self.X(button2[5]) |
| 128 | + self.B(button2[6]) |
| 129 | + self.A(button2[7]) |
| 130 | + # Axes |
| 131 | + self.lx( struct.unpack('f', bytes(wireless_remote[4:8]))[0] ) |
| 132 | + self.rx( struct.unpack('f', bytes(wireless_remote[8:12]))[0] ) |
| 133 | + self.ry( struct.unpack('f', bytes(wireless_remote[12:16]))[0] ) |
| 134 | + self.ly( struct.unpack('f', bytes(wireless_remote[20:24]))[0] ) |
| 135 | + |
| 136 | + |
| 137 | + # 检查是否有按键按下 |
| 138 | + if any([ |
| 139 | + self.LT.pressed, self.RT.pressed, self.back.pressed, self.start.pressed, |
| 140 | + self.LB.pressed, self.RB.pressed, self.left.pressed, self.down.pressed, |
| 141 | + self.right.pressed, self.up.pressed, self.Y.pressed, self.X.pressed, |
| 142 | + self.B.pressed, self.A.pressed |
| 143 | + ]): |
| 144 | + self.last_active_time = time.perf_counter() # 更新最后一次活动时间 |
| 145 | + elif time.perf_counter() - self.last_active_time > self.inactive_timeout: |
| 146 | + # 超过设定的超时时间未按下任何键,重置所有按键的点击计数 |
| 147 | + self.reset_all_click_counts() |
| 148 | + self.last_active_time = time.perf_counter() # 重置最后活动时间 |
| 149 | + |
| 150 | + def reset_all_click_counts(self): |
| 151 | + """重置所有按键的连续点击计数器""" |
| 152 | + for button in [ |
| 153 | + self.LT, self.RT, self.back, self.start, self.LB, self.RB, |
| 154 | + self.left, self.down, self.right, self.up, self.Y, self.X, self.B, self.A |
| 155 | + ]: |
| 156 | + button.reset_click_count() |
| 157 | + |
| 158 | + def combine(self): |
| 159 | + """ |
| 160 | + Merge data from Joystick to wireless_remote |
| 161 | + """ |
| 162 | + # prepare an empty list |
| 163 | + wireless_remote = [0 for _ in range(40)] |
| 164 | + |
| 165 | + # Buttons |
| 166 | + wireless_remote[2] = int(''.join([f'{key}' for key in [ |
| 167 | + 0, 0, round(self.LT.data), round(self.RT.data), |
| 168 | + self.back.data, self.start.data, self.LB.data, self.RB.data, |
| 169 | + ]]), 2) |
| 170 | + wireless_remote[3] = int(''.join([f'{key}' for key in [ |
| 171 | + self.left.data, self.down.data, self.right.data, |
| 172 | + self.up.data, self.Y.data, self.X.data, self.B.data, self.A.data, |
| 173 | + ]]), 2) |
| 174 | + |
| 175 | + # Axes |
| 176 | + sticks = [self.lx.data, self.rx.data, self.ry.data, self.ly.data] |
| 177 | + packs = list(map(lambda x: struct.pack('f', x), sticks)) |
| 178 | + wireless_remote[4:8] = packs[0] |
| 179 | + wireless_remote[8:12] = packs[1] |
| 180 | + wireless_remote[12:16] = packs[2] |
| 181 | + wireless_remote[20:24] = packs[3] |
| 182 | + return wireless_remote |
| 183 | + |
| 184 | +class PyGameJoystick(Joystick): |
| 185 | + def __init__(self) -> None: |
| 186 | + super().__init__() |
| 187 | + |
| 188 | + pygame.init() |
| 189 | + pygame.joystick.init() |
| 190 | + if pygame.joystick.get_count() <= 0: |
| 191 | + raise Exception("No joystick found!") |
| 192 | + |
| 193 | + self._joystick = pygame.joystick.Joystick(0) |
| 194 | + self._joystick.init() |
| 195 | + |
| 196 | + def print(self): |
| 197 | + print("\naxes: ") |
| 198 | + for i in range(self._joystick.get_numaxes()): |
| 199 | + print(self._joystick.get_axis(i), end=" ") |
| 200 | + print("\nbuttons: ") |
| 201 | + for i in range(self._joystick.get_numbuttons()): |
| 202 | + print(self._joystick.get_button(i), end=" ") |
| 203 | + print("\nhats: ") |
| 204 | + for i in range(self._joystick.get_numhats()): |
| 205 | + print(self._joystick.get_hat(i), end=" ") |
| 206 | + print("\nballs: ") |
| 207 | + for i in range(self._joystick.get_numballs()): |
| 208 | + print(self._joystick.get_ball(i), end=" ") |
| 209 | + print("\n") |
| 210 | + |
| 211 | +class LogicJoystick(PyGameJoystick): |
| 212 | + """ Logic F710 """ |
| 213 | + def __init__(self) -> None: |
| 214 | + super().__init__() |
| 215 | + |
| 216 | + def update(self): |
| 217 | + pygame.event.pump() |
| 218 | + |
| 219 | + self.back(self._joystick.get_button(6)) |
| 220 | + self.start(self._joystick.get_button(7)) |
| 221 | + self.LS(self._joystick.get_button(9)) |
| 222 | + self.RS(self._joystick.get_button(10)) |
| 223 | + self.LB(self._joystick.get_button(4)) |
| 224 | + self.RB(self._joystick.get_button(5)) |
| 225 | + self.A(self._joystick.get_button(0)) |
| 226 | + self.B(self._joystick.get_button(1)) |
| 227 | + self.X(self._joystick.get_button(2)) |
| 228 | + self.Y(self._joystick.get_button(3)) |
| 229 | + |
| 230 | + self.LT((self._joystick.get_axis(2) + 1)/2) |
| 231 | + self.RT((self._joystick.get_axis(5) + 1)/2) |
| 232 | + self.rx(self._joystick.get_axis(3)) |
| 233 | + self.ry(-self._joystick.get_axis(4)) |
| 234 | + |
| 235 | + |
| 236 | + # Logitech controller has 2 modes |
| 237 | + # mode 1: light down |
| 238 | + self.up(1 if self._joystick.get_hat(0)[1] > 0.5 else 0) |
| 239 | + self.down(1 if self._joystick.get_hat(0)[1] < -0.5 else 0) |
| 240 | + self.left(1 if self._joystick.get_hat(0)[0] < -0.5 else 0) |
| 241 | + self.right(1 if self._joystick.get_hat(0)[0] > 0.5 else 0) |
| 242 | + self.lx(self._joystick.get_axis(0)) |
| 243 | + self.ly(-self._joystick.get_axis(1)) |
| 244 | + # mode 2: light up |
| 245 | + # self.up(1 if self._joystick.get_axis(1) < -0.5 else 0) |
| 246 | + # self.down(1 if self._joystick.get_axis(0) > 0.5 else 0) |
| 247 | + # self.left(1 if self._joystick.get_axis(0) < -0.5 else 0) |
| 248 | + # self.right(1 if self._joystick.get_axis(0) > 0.5 else 0) |
| 249 | + # self.lx(self._joystick.get_hat(0)[1]) |
| 250 | + # self.ly(self._joystick.get_hat(0)[1]) |
| 251 | + |
0 commit comments