1

I am building a new input device for gaming and home automation purposes and would like to use python arcade (https://arcade.academy) for a demo. Arcade supports reading input from keyboard or mouse and I would like to extend this functionality for a new serial device.

How could a do that in the most simple way?

dan_0
  • 599
  • 1
  • 5
  • 17
  • 1
    normally programs get key/mouse events from system (Windows/LInux/Mac) and system has C/C++ driver to get information from keyboard and mouse. In `pygame` you have to built loop to get key/mouse events from system and you could add function which checks serial device and create `pygame.Event` to simulate key/mouse events. In `arcade` you already have this loop and I don't know if you can change it to check serial device. Eventually you could run separated thread which checks serial device but I don't know how to create events in `arcade` to simulate key/mouse events. – furas Aug 12 '20 at 13:15
  • 1
    it seems `arcade` uses `pyglet` and it has [dispatch_event()](https://pyglet.readthedocs.io/en/latest/modules/event.html#pyglet.event.EventDispatcher) to create new event so you could run in thread or using timer function which checks serial device and creates key/mouse event using `displatch_event()`. I found some example in [Creating a new input event dispatcher in Pyglet (infra red input)]https://stackoverflow.com/questions/1206628/creating-a-new-input-event-dispatcher-in-pyglet-infra-red-input) but it is not complete. – furas Aug 12 '20 at 13:33

1 Answers1

1

The main thing I could think of would be to create a controller class.

The class would have variables that are associated with the controller (the joystick, button1, button2...). It could be a data class for readability

@dataclass
class Controller:
    joystick_x:int
    joystick_y:int
    button1:bool
    button2:bool
    
    def update_controller():
        pass
        # update the controls here 

In that class, you could put a update_controller method that would check the serial input to update the variables. I think that you could put that method at the beginning of the update method of your game class so you can update in consideration of your inputs.

class MyGame(arcade.Window)
    def __init__():
        controller = Controller()
    def update():
        controller.update_controller()
        if controller.button1:
            pass # do something

You would now have to add a control variable to the game class to make it easier to use the data of the controller class.

TNTy100
  • 70
  • 8