So a couple weeks ago, I was watching a MrCrayfish livestream. He was working on his Minecraft Vehicle Mod and added controller support but wanted to expand upon that by making an API that other mod developers could use (Controllables API). I thought about how Nintedo Switch JoyCon support would work. I looked online for an API that lets you do this. I had found a Unity C# asset package but obviously for Unity C#. I found a Java API, which is what Minecraft mods call for, but looking through the API I found that it's quite messy, hard to read, not very thread-safe (in terms of high-quality JoyCon support), nor did it offer a whole lot of functionalities that you could do that would be handled/managed by the API. There were lots of things you could do with the API, the problem was that it was very skeletal and barebones. I wanted an API that could manage your use of the JoyCons in a clean, efficient, and thread-safe way while still giving you all the flexibility. I started off with Java since that's what I know the most and it was originally for aiding in MrCrayfish's Controllables API. As time went on I began to understand how HID devices worked, how the JoyCon's worked, how multithreaded programming worked, and finally getting the analog stick calibration to work. I was reading through the reverse engineering notes for days and days trying to get things to work. After over a week of tinkering with things, I had finally gotten basic input to work in a thread-safe and clean way.
First what I had to do was create interfaces for taking input reports. Input reports are raw bytes structured in a particular pattern that told software how to handle the data. They always started with an id at report index 0, and contained the data for that report from index 1 to index 13. Index 14 had a subcommand id (for subcommand replies) and index 15 onward contained the rest of that data. This is how a standard full input report was structured. There were several subcommands that could be handled. I had to improve my interfacing to properly handle the input reports and all the different subcommands. I also had to create an output report factory for interacting with the JoyCons. This was all easy and all but what was difficult was trying to make things thread safe. I also needed to figure out how the analog sticks worked since their input is fluid and not static like the buttons. In the JoyCon's SPI flash memory is a section (or really a subsection) that contains the factory default calibration data. All the data was encoded from unsigned 16 bit integers to 8 bit bytes. All the data for the JoyCons were encoded like this. They were also in little endian so I not only had to decode them from little endian to big endian but also decode them into 16 bit unsigned integers. When I started working on the calibration I realized that I need to read from memory and then wait for the memory to come back to me. This required me having to make the current thread wait/sleep (waiting was the right thing to do since sleep didn't allow me to notify the thread that the memory had come back). In order for this to happen I had to create some delegate threads for handling input reports, this way I would have the input report thread notify the main thread that everything had come back. I had no idea how threading worked at the time so it was a learning experience.
I needed to make this thread-safe because if games are going to be using this API for proper, efficient, clean, and thread-safe JoyCon support, it needed to be thread-safe. I needed to figure out how I wanted this to work. As I was learning Java's Thread API, I was constantly struggling with understanding how it all worked. I kept running into hanging with the program but didn't understand until I looked at the threads in IntelliJ's debug panel. I had realized that I was making the main thread wait. The threads that I created were all dependent upon the main thread cause the main thread was actually a thread *group*. So making the main thread wait meant that all threads made under the main thread group also had to wait. When I came to that realization I went to make the main JoyCon class, which is a wrapper class that contained all the necessary information and methods needed to construct a usable JoyCon object, a thread. This way when I make the JoyCon thread wait for memory to come back, the entire program isn't waiting as well. So now I had a JoyCon thread, an input handling thread, and the only thing that I needed to do was have my JoyconManager class, a singleton for managing all connected JoyCons, wait for the JoyCon object to be done initializing. So I had to make a separate thread for the JoyconManager as well. Oh wait, I also need to ensure that the JoyCon object is having all of its important information initialized before it can be used. How do I do that? Not only that but I need to ensure that there are no async problems during initialization. So I had to make a thread dedicated to initialization and make the JoyCon thread wait until that init thread is finished by calling Thread#join. This made things a whole lot better cause now I can guarantee that the JoyCon class will be initialized with all the important information before it gets used. Then the JoyconManager can continue afterwards. There is a LOT of improvement to be made with this but I will do that some other time. When this all started to work I headed on over to the analog stick calibration.
The analog stick calibration data, as mentioned before, was in little endian and encoded as 8 bit bytes. The bytes were sent like this: 0x5D 0xC9 0xB2 or something like that. This is for input. It has to be decoded into two unsigned 16 bit integers for the horizontal and vertical axes. Now the calibration data itself was encoded into three groups of three bytes. When decoded it gave you the center_x, center_y, min_x, min_y, max_x, max_y variables. There was also in memory information about the deadzone. The deadzone is where the analog stick memory stops being read as input and instead gets read as 0 during the calculations. I had to decode that. I'm not gonna go into the maths involved in decoding but you can view it all here. I was struggling to get the calibration right. I had no idea why the numbers weren't coming back between 1 and -1 and 0 for the center. I was so confused. I then realized that the bitwise operators are considered mathematical operations by the JVM and return to you signed 32 bit integers every time. So I had to fight against the JVM's love for two's compliment by taking the result of every bitwise operation and doing a bitwise AND with the proper mask such as 0xF or 0xFF or 0xF0 depending on what I need. When I realized this I finally got the calibration to work. It all now reads between 1 and -1 on both the horizontal and vertical axes.
This API currently has support for reading the battery information, including the exact voltage output. I'm going to find a way to set a flag for whether the JoyCons are charging or not but since to charge the JoyCons they have to be connected to the console and on the doc, there really wouldn't be any use for this flag. This API also has memory reading capabilities, but reading memory right now isn't really a biggie but it's there if needed.
Right now I'm in the process of getting a Kotlin/Native workspace going so that I can port my API to native libraries. I'm already fluent in Kotlin so it's not gonna be as painful as having to learn an entire language on top of learning its tools and compilers and learning its libraries. Kotlin/Native will allow me to compile the API natively, and I can create bindings for different languages and platforms (such as iOS, Android Native, WASM, etc) without having to rewrite everything and have inconsistencies. I am just struggling to get the workspace to work because I have to learn how C compilers work since it uses C compilers for interop and linking native libraries together with projects.
I am looking at this project as having a lot potential honestly. I think if I do it right, there could be a lot of games out there that take JoyCon support. Not only that, but since the JoyCon's connect via bluetooth you could actually use this API for making mobiles games that have JoyCon support. With the ChromeCast or apple tv (or whatever the apple screencast is called), you could probably (no definitely) create a mobile game that you can play on a bigger screen with the JoyCons. This would obviously require the JoyCons of course but perhaps there could be a way to connect any bluetooth controller.
I'm gonna be doing occasional livestreams of my development over at my twitch channel. Once I get myself a bluetooth adapter for my desktop (I'm using my macbook for API development and it does not like to stream for whatever reason, don't really care tho (not a fan of macs tbh)) then I'll be good to go with porting my API to Kotlin/Native.