Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Receive data as Joystick

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    6

    Receive data as Joystick

    Quite a while ago I posted this thread which didn't get any response. I'm a little further in my project but my questions remain mostly the same so I'll reiterate here.

    I'm making a joystick that needs to also receive data from the host. Currently I've implemented this using my own Joystick + Serial device. The serial device is strictly receiving data, while I'm using the built-in Joystick functionality for the controls. Although it's working fine, I can't help but feel that it would be "cleaner" to simply have bidirectional communication over HID, preventing the need for the dedicated Serial device altogether.

    I'll admit that I'm not very well versed in device descriptors or HID in general, so chances are it would be too daunting a task for me. But is there any way currently to receive data through the built-in Joystick device? If not, is Joystick + Serial the next best thing, in the opinion of this forum?

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,804
    I closed the other thread and linked it here to avoid confusion.

    Bummer that thread went unanswered, glad some progress was made. No suggestions on the topic … feel free to add details on the joystick function and what is on the other end - seems there was added detail in the other post. RAWHID can do 1K msgs per sec AFAIK ...

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    4,882
    I am also probably not much help here either... I have spent most of my USB understanding time on the other end (T3.6 trying to figure out hosting devices usbhost_t36)...

    I don't see any standard way for the Joystick code to receive extra data or the like. What I would suspect many devices would do is to do something like this, is to setup new HID information, where the device, may have a few Report IDs and some of these reports would have some OUTPUT fields.

    I know several keyboards that are supported in the host code do allow the Teensy code to output information (LEDs state), likewise Joysticks like PS3/PS4 allow me to set things like Leds/Rumble... But not sure how that works internal to our USB code classes...

  4. #4
    Junior Member
    Join Date
    Sep 2018
    Posts
    6
    I get the impression that LED state or rumble information is doable through HID because it's very simple data structure and has a known size. The data in my case is more complex.

    The background is that I'm making a cockpit for Elite Dangerous. The game doesn't have a "hold" mode for a lot of actions that are logically toggled actions, which I want toggleswitches for. Of course, I could simply trigger a button press on every change of the toggleswitch's state, but that introduces margin for the switch and the game to de-sync and be opposite each other. My solution is that I can read the actual state of the toggles from the game. I'm sending them, along with some other metadata, to the Teensy as JSON over Serial. The JSON looks like this:

    Code:
    {"timestamp":"2019-06-23T20:11:34Z", "Flags":128, "StarSystem": "Sol"}
    So I'm taking the Flags value, determining the state of the game's toggles compared to the state of my switches, and firing a button press if they're out of sync (with a buffer, currently set to 2 seconds). I think due the relative complexity of the data and the fact that I'm using JSON, Serial seems like the ideal way to do this since it's pretty trivial to read any arbitrary size or shape of data as it comes in.

    I should also note that I don't need to receive this very frequently at all -- maybe a few times a second at most. If the description still doesn't make sense my sketch is here and the agent I'm using to send the data is here.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,170
    Yes, serial is probably the best way for variable size data. JSON has a lot of overhead, but should be fine if you're sending at only a moderate speed.

    I've never used the ArduinoJson library. Curious to hear how it works for you?

  6. #6
    Junior Member
    Join Date
    Sep 2018
    Posts
    6
    Quote Originally Posted by PaulStoffregen View Post
    JSON has a lot of overhead, but should be fine if you're sending at only a moderate speed.
    That was an initial concern of mine. I tried pushing it as hard, if not harder, than I'll normally be pushing it, and it handled everything I threw at it.

    Quote Originally Posted by PaulStoffregen View Post
    I've never used the ArduinoJson library. Curious to hear how it works for you?
    I've been really happy with it and would recommend it to anyone looking to use JSON. Most of my professional work is in cloud and microservice architecture so I'm comfortable with JSON in most environments, but I was worried about how it would perform on Arduino. The library is actually really easy to work with and I haven't hit any issues at all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •