piloChambert / RPI-I2C-Joystick

Arduino based joystick for Raspberry Pi
MIT License
14 stars 5 forks source link

Question about I2C driver for this joystick #2

Open AlexShi23 opened 4 years ago

AlexShi23 commented 4 years ago

Hi, I wanted to assembly joystick according to your project. I'm having a problem with the driver. It compiled, but the device is still not available. Maybe I did something wrong, help me figure it out please.

piloChambert commented 4 years ago

Hi! Of course I can help :)

Did you check that the I2C is working on the raspberry? https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c : with i2cdetect you should see the arduino at address 0x18.

It's an old project, so there might be some bugs (and I never finish the power switch stuff).

AlexShi23 commented 4 years ago

OK, device in Retropie appears at 0x18. Next, I copy the folder with the driver files to the Raspberry / home folder, then the make command and then I don’t know what to do.

piloChambert commented 4 years ago

The program run i2c-joystick-driver should poll the arduino for input event, and then to a an emulated input device. So when you run it, a new joystick should appear on your system. To start it in background mode just run ./i2c-joystick-driver & from the source directory.

But I think it should be used as a daemon to start it automatically with the system.

Which distribution are you using? You can try to run jstest /dev/input/js0 while i2c-joystick-driver to see if the different buttons are working.

AlexShi23 commented 4 years ago

Hi I added ./i2c-joystick-driver & to startup and the system detected one game device. I use Retropie 4.5. But the joystick buttons are not pressed. The I2C device is discovered at address 0x18, but the device at address 0x68 is also detected. jstest /dev/input/js0 does not detect keystrokes. System shutdown often occur spontaneously. If turn off the Atmega328 or change the address, spontaneous shutdown disappears. I am not good at programming code, so all hope is for your advice.

piloChambert commented 4 years ago

Hi! When you said "but the device at address 0x68 is also detected", is it still detected when the arduino is powered off? (it might be another I2C device inside your computer at address 0x68).

And how did you wire the button on the arduino?

To prevent the system shutdown, you can try to change ligne 261 in i2c_gamepad.ino to stateFunction = runState;

and change ligne 157 to: if(0) { (instead of if(powerSwitchHasBeenReleased && powerSwitch.state == LOW && millis() - powerSwitchPressTime > 4000) {)

AlexShi23 commented 4 years ago

I changed lines 157 and 261 as You recommended and the buttons worked. I will move on. Thanks for answers!

piloChambert commented 4 years ago

Great! :) The arduino wasn't in the right mode (it was in standby mode), and that's why it wasn't working as expected. I might change that and push a fix.

I'm wondering in what kind of project you are using it? Is it for a portable pi console?

AlexShi23 commented 4 years ago

Yes. Raspberry CM3 based portable console. LCD 4.3" DPI mode.

AlexShi23 commented 4 years ago

Hi I don’t understand how the hat is connected. How to convert a hat from analog to digital buttons? I am not using volume control and these pins are free.

piloChambert commented 4 years ago

Seems like a very interesting project :) Did you make a custom PCB for the compute module? That's what I wanted to do, but I never took time to finish my prototype back then! DPI screen is a really nice feature! Will you make it a public project?

For the "analog" hat wiring, I was using a voltage divider (0, 2.5v, 5v) for hat axis. But I just push a commit which should allow you to use digital pin instead for the hat (pin 20, 21, 16, 17, look lines 58 to 61) and disable volume control (and power management too).

At the top of the ino file I added compilation configuration: #define USE_VOLUME_BUTTON 0 #define USE_ANALOG_HAT 0 #define USE_POWER_FUNCTION 0

You can leave all of them to 0 in your case.

AlexShi23 commented 4 years ago

Hi Thanks for your responsiveness. At the moment this is a prototyping board. From the interesting will be the use of the ALC5628 DAC. I've spent some time studying it. DPI mode will occupy the entire GPIO Bank0. Proto

This will be a public project, but maybe I'll be making kits for sale.

AlexShi23 commented 4 years ago

Up, down, left, right buttons are not defined by the system. I think the reason is in the driver, there they are not declared anywhere.

piloChambert commented 4 years ago

Hey! You're welcome! I wrote this I2C joystick driver a while ago, and never really finish the project... so I'm happy to know it could be useful to someone else :) A DAC for audio is a really nice feature! I wanted to use a TDA1543 (hard to find the I2S one).

And you were right, the dpad event wasn't handle in the driver code! I just fixed it, and a push a new commit. I couldn't test it, so I hope it works. In linux DPad can be handle as button events or analog axis (xbox 360 gamepad for example generate axis event), so I added a compile option in i2c-joystick-driver.c (line 12, #define USE_ANALOG_DPAD 0) to use either one mode or the other. Set it to 0 and it will generate button event (which I think should be fine for retropie)

AlexShi23 commented 4 years ago

Hi The file JoystickDevice.c also needed to be fixed. I did it myself. Everything is working! Thanks! I'll keep you up to date.

piloChambert commented 4 years ago

Oh, did I forgot to register DPADBTN*? ret |= ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_UP); ret |= ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_DOWN); ret |= ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_LEFT); ret |= ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_RIGHT);

If you want to make a merge request I'll accept it ;) (or I can fix it with a commit).

I'd love to be updated on your project! :) Thank you!

eme-ce commented 3 years ago

Hi pilochambert! Thanks for the help! I never wrote but I was trying to finish this project months ago but i had the same doubts as the other user. The RPI shutdown itself and didnt recognize actions... I'll prove what you said.

What and how is the use of the pwr mosfet gate pin? Thank you!

piloChambert commented 3 years ago

Hey! You're welcome! I should provide more information for the arduino code.

The power mosfet pin should be used to power on and off the raspberry pi. I can't remember if I tested or implemented it yet. I wanted to make the arduino able to manage raspberry pi power state, but there may be other way of doing this now?