Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://www.amazon.de/gp/product/B07Q812QK8
- # https://www.fischl.de/usbtin/
- # https://python-can.readthedocs.io/en/master/
- from subprocess import run
- from can.interfaces.socketcan import SocketcanBus
- def set_baudrate(interface, bitrate):
- run(["sudo", "ip", "link", "set", interface, "type", "can", "bitrate", str(bitrate)])
- def _set(interface, up_down):
- run(["sudo", "ip", "link", "set", up_down, "dev", "can0"])
- def set_up(interface):
- _set(interface, "up")
- def set_down(interface):
- _set(interface, "down")
- def can_joystick(data):
- D1, D2, D3 = data
- if 0 < D1 <= 0x64:
- direction_x = "right"
- x = D1
- elif 0x9c <= D1 <= 0xFF:
- direction_x = "left"
- x = 0x100 - D1
- elif D1 == 0:
- direction_x = "middle"
- x = 0
- if D2 == 0:
- y = 0
- direction_y = "middle"
- else:
- y = 0x100 - D2
- direction_y = "down"
- return f"{x:03d}: {direction_x} | {y:03d}: {direction_y} | Button: {bool(D3 & 0x01)}"
- if __name__ == "__main__":
- interface = "can0"
- bitrate = 125_000
- node_id = 0x1a4
- set_down(interface)
- set_baudrate(interface, bitrate)
- set_up(interface)
- bus = SocketcanBus(interface)
- while True:
- if (msg := bus.recv()).arbitration_id == node_id:
- print(" "*80, end="")
- print("\r", end="")
- print(can_joystick(msg.data), end="", flush=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement