Hello! I'd like to ask the cunning people of the teensy forum if the following idea is feasible. I'd like to make a 12(x10bit) axis joystick/gamepad with the help of teensy 2.0. More specifically I want to "translate" a radio controller to a USB device. First I was quite happy to find the many axis joystick thread, but I soon found out that it wouldn't work with my teensy 2.0. Then after some time I found a post by Les "Pointy", which describes the making of a custom teensy++ 2.0 joystick. I assume the same could be applied to teensy 2.0 (I wanted to post a question in the comments of that post, but the button was "greyed out" even though I filled in all the fields).
I'm new to things like micro controllers and programming, and feel like I might be out of my depth, which is why I'd like to ask the forum members if this project is feasible and if I have understood the principles correctly.
I'd feel somewhat rude to just refer you to the post (but I still recommend the original source, of course), which is why I'll attempt a quick summary and hope I don't get things to mixed up. Les post describes that in order to create your custom joystick you need to edit boards.txt in ‘arduino install folder\hardware\teensy‘ by adding following lines (I suppose for teensy 2.0, one would place and name these lines accordingly ("teensy2.menu.usb...")):
teensypp2.menu.usb.gamepad.name=Gamepad Controller
teensypp2.menu.usb.gamepad.build.define0=-DUSB_GAMEPAD
teensypp2.menu.usb.gamepad.fake_serial=teensy_gateway
Thereafter one needs to add the correct inclusions in the file "core_id.h", "usb.c", "usb_api.cpp", "usb_api.h", "usb_private.h", in ‘arduino install folder\hardware\teensy\cores\teensy’ by adding following lines:
#elif defined(USB_GAMEPAD)
#include "../usb_gamepad/[name of the file you are editing at the moment]"
Then one should make a folder in the ‘arduino install folder\hardware\teensy\cores‘ directory and name the folder 'usb_gamepad'. Inside that folder one should put the files provided in the post by Les "Pointy" (usb_private.h, usb_api.cpp, usb.c, usb_api-h, core_id.h). The last step is to edit the provided sketch code, if needed, and upload it to the teensy.
Then Pointy explains that if you want to customize it further you should edit "gamepad_report_data" and "GAMEPAD_SIZE" in "usb_private.h", as well as "gamepad_report_data" in "usb_api.h" if you need more than 56 bits for the packet. In my example, with 12 10 bit axis, I assume I need then 120 bits. Thefore I change usb_private.h to "#define GAMEPAD_SIZE 13", and "gamepad_report_data[12]", and also "gamepad_report_data[12]" in usb_api.h. For clarity, the "13" in "#define GAMEPAD_SIZE 13" comes from the fact that the value is set to 8 in the original code, which is 1 more than the 7 bytes required by the gamepad in the example. Therefore I'd like to ask why it is the case, and of course to be corrected if the value of GAMEPAD_SIZE should not simply be one more than the amount of bytes.
Next step would be to edit the HID descriptor in usb.c. Since I want to add several axis, I assume I should add the Z, Zrotate, and several sliders. This would mean that the descriptor should also contain
USAGE (Z) 09 32
USAGE (Rz) 09 35
USAGE (Slider) 09 36 (this repeated 6 times)
And the REPORT_COUNT should be increased to 12.
Now I wonder if the sliders somehow need to get specific names in the descriptor (like Slider1, Slider2, etc).
Thereafter I'd need to edit the code 'usb_api.h' to handle the bites and bytes from right to left, to send the data. And I suppose, since I don't need the buttons, I should remove that part of the code.
Lastly I'd have to change the Sketch accordingly to map the analog inputs to the different axis/sliders.
Is what I want to achieve possible with the specified hardware? Have I understood the described parts of the code correctly? Where have I made the obvious mistakes and where do you see the pitfalls? I'm very thankful for any input! And if "Pointy" sees this post, I'd like to thank you for your blog post. I couldn't send a comment on the blog though.
/Dran
I'm new to things like micro controllers and programming, and feel like I might be out of my depth, which is why I'd like to ask the forum members if this project is feasible and if I have understood the principles correctly.
I'd feel somewhat rude to just refer you to the post (but I still recommend the original source, of course), which is why I'll attempt a quick summary and hope I don't get things to mixed up. Les post describes that in order to create your custom joystick you need to edit boards.txt in ‘arduino install folder\hardware\teensy‘ by adding following lines (I suppose for teensy 2.0, one would place and name these lines accordingly ("teensy2.menu.usb...")):
teensypp2.menu.usb.gamepad.name=Gamepad Controller
teensypp2.menu.usb.gamepad.build.define0=-DUSB_GAMEPAD
teensypp2.menu.usb.gamepad.fake_serial=teensy_gateway
Thereafter one needs to add the correct inclusions in the file "core_id.h", "usb.c", "usb_api.cpp", "usb_api.h", "usb_private.h", in ‘arduino install folder\hardware\teensy\cores\teensy’ by adding following lines:
#elif defined(USB_GAMEPAD)
#include "../usb_gamepad/[name of the file you are editing at the moment]"
Then one should make a folder in the ‘arduino install folder\hardware\teensy\cores‘ directory and name the folder 'usb_gamepad'. Inside that folder one should put the files provided in the post by Les "Pointy" (usb_private.h, usb_api.cpp, usb.c, usb_api-h, core_id.h). The last step is to edit the provided sketch code, if needed, and upload it to the teensy.
Then Pointy explains that if you want to customize it further you should edit "gamepad_report_data" and "GAMEPAD_SIZE" in "usb_private.h", as well as "gamepad_report_data" in "usb_api.h" if you need more than 56 bits for the packet. In my example, with 12 10 bit axis, I assume I need then 120 bits. Thefore I change usb_private.h to "#define GAMEPAD_SIZE 13", and "gamepad_report_data[12]", and also "gamepad_report_data[12]" in usb_api.h. For clarity, the "13" in "#define GAMEPAD_SIZE 13" comes from the fact that the value is set to 8 in the original code, which is 1 more than the 7 bytes required by the gamepad in the example. Therefore I'd like to ask why it is the case, and of course to be corrected if the value of GAMEPAD_SIZE should not simply be one more than the amount of bytes.
Next step would be to edit the HID descriptor in usb.c. Since I want to add several axis, I assume I should add the Z, Zrotate, and several sliders. This would mean that the descriptor should also contain
USAGE (Z) 09 32
USAGE (Rz) 09 35
USAGE (Slider) 09 36 (this repeated 6 times)
And the REPORT_COUNT should be increased to 12.
Now I wonder if the sliders somehow need to get specific names in the descriptor (like Slider1, Slider2, etc).
Thereafter I'd need to edit the code 'usb_api.h' to handle the bites and bytes from right to left, to send the data. And I suppose, since I don't need the buttons, I should remove that part of the code.
Lastly I'd have to change the Sketch accordingly to map the analog inputs to the different axis/sliders.
Is what I want to achieve possible with the specified hardware? Have I understood the described parts of the code correctly? Where have I made the obvious mistakes and where do you see the pitfalls? I'm very thankful for any input! And if "Pointy" sees this post, I'd like to thank you for your blog post. I couldn't send a comment on the blog though.
/Dran