danrubel / rpi_gpio.dart

Dart library for accessing the Raspberry Pi GPIO pins
Other
47 stars 5 forks source link

Some input pins not being initialized #24

Closed khanhnwin closed 8 months ago

khanhnwin commented 1 year ago

Hey Dan,

I'm using rpi_gpio in conjunction with the Waveshare Game HAT on a Raspberry Pi 4B. I'm puzzled why my code is successfully reading input from some of the pins, but not others. I printed out the initial stream values below [3] and noticed that the working pins have an initial value of true, but the non-working pins have an initial value of false. In the code [2], I use Pull.up to initialize the input so I expected all of the pins to be initialized to a true state.

I suspected maybe my pins were defective, so I tried running a python script written with the RPi.GPIO library and that worked. Beyond that, if I run the Python script and then my Dart script, my Dart code works and the pins are initialized correctly and I get input from all the buttons.

Have you encountered this issue before? Do you have any idea what I might be overlooking here?

Thanks for your help!

Pins & State

[1] GPIO Enum

enum GameHatGPIO {
  SELECT_BUTTON(7, GameControlEvent.SELECT),
  TL_BUTTON(12, GameControlEvent.LEFT_SHOULDER),
  TR_BUTTON(16, GameControlEvent.RIGHT_SHOULDER),
  DPAD_UP_BUTTON(29, GameControlEvent.UP),
  DPAD_DOWN_BUTTON(31, GameControlEvent.DOWN),
  DPAD_LEFT_BUTTON(33, GameControlEvent.LEFT),
  DPAD_RIGHT_BUTTON(35, GameControlEvent.RIGHT),
  B_BUTTON(32, GameControlEvent.B),
  X_BUTTON(36, GameControlEvent.X),
  A_BUTTON(37, GameControlEvent.A),
  Y_BUTTON(38, GameControlEvent.Y),
  START_BUTTON(40, GameControlEvent.START);

  final int pin;
  final GameControlEvent event;
  const GameHatGPIO(this.pin, this.event);
}

[2] Initializing code

Future initialize() async {
    final gpio = await initialize_RpiGpio();

    GpioInput buttonController;

    for (GameHatGPIO button in GameHatGPIO.values) {
      buttonController = gpio.input(button.pin, Pull.up);
      buttonController.values.listen((bool buttonValue) {
        if (buttonValue == false) {
          print('${button.event} was pressed');
          // button was pressed
          events.add(GameInput(button.event, EventState.PRESSED));
        } else if (buttonValue == true) {
          print('${button.event} was released');
          // button was released
          events.add(GameInput(button.event, EventState.RELEASED));
        }
      });
    }
  }

[3] Initial stream values output:

flutter: GameControlEvent.SELECT was released (TRUE)
flutter: GameControlEvent.LEFT_SHOULDER was pressed (FALSE)
flutter: GameControlEvent.RIGHT_SHOULDER was pressed (FALSE)
flutter: GameControlEvent.UP was released (TRUE)
flutter: GameControlEvent.DOWN was released (TRUE)
flutter: GameControlEvent.LEFT was released (TRUE)
flutter: GameControlEvent.RIGHT was released (TRUE)
flutter: GameControlEvent.B was pressed (FALSE)
flutter: GameControlEvent.X was pressed (FALSE)
flutter: GameControlEvent.A was pressed (FALSE)
flutter: GameControlEvent.Y was pressed (FALSE)
flutter: GameControlEvent.START was pressed (FALSE)

[4] python script:

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library

def button_callback(channel):
    print("Button was pushed!")

PINS = [7, 12, 16, 29, 31, 33, 35, 32, 36, 37, 38, 40]

GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

for pin in PINS:
    GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 10 to be an input pin and set initial value to be pulled low (off)

GPIO.cleanup() # Clean up
danrubel commented 1 year ago

Good question.

I could not find any hardware specs for the Waveshare Game HAT to determine how the pins were wired. Specifically, for each pin, is that pin expected to be default high/+3V/pull-up or default low/gnd/pull-down.

But since the Python script is working and can initialize the pin state, then I assume that the problem lies in the Dart code.

No, I have not encountered this before, but I've not worked with this controller before either.

danrubel commented 1 year ago

I need to compare the Python code that initializes the pin's pull up/down with the code in this package that does the same thing to see if I can spot any problem.

Not sure when I'll be able to get to this, but something to investigate.

khanhnwin commented 1 year ago

Thanks Dan! Yeah, unfortunately I had trouble finding those specs as well. I'll try taking a look around the Dart code and report back if I spot anything that stands out.

Appreciate your help!

khanhnwin commented 1 year ago

Hey Dan,

I did some digging, perhaps these resources will be useful to you. It seems like the controls GPIO pull up/down/enable have been changed on the Pi 4s. That explains why the Python code that initializes the pin's pull up/down which you linked to checks whether it's running on a a Pi 4 or not. That might be it, but even then, it's not clear to me why my current Dart code initializes some pins and not others. I would have expected none of the pins to be initialized. This GPIO code is a little outside of my wheelhouse, would love your input!

Thanks!

Khanh

Here's a few more resources that I dug found:

danrubel commented 1 year ago

This is very helpful. Thank you!

I can't get to this for a couple weeks, but the information you provided should enable me to add support for the Pi 4. I'll keep you posted as I can make progress.

khanhnwin commented 11 months ago

Hi @danrubel! Just wanted to check in and see if you've been able to take a look at this? If there's anything else I can do to assist, please let me know! Excited to land a sample for the Raspberry Pi GameHAT :) https://www.youtube.com/watch?v=0CCVB31feO0&t=117s

danrubel commented 10 months ago

Wow! Thanks for the link. Very cool and nice work!

I have plenty of older RPi models, but finally purchased a RPi 4 so that I can test and fix this bug. Now I need to allocate time to setup the new pi, install everything, and fix. Will keep you posted here.

danrubel commented 8 months ago

I've fixed the gpio input handling on Raspberry Pi 4B and published version 0.9.1 with this fix.

Thank you again for sharing the native code used by the equivalent python module. I heavily leaned on that code to fix gpio input handling.

khanhnwin commented 8 months ago

Thank you, Dan! Thanks so much for your help on this! I really appreciate it.

Once the sample code us published, you'll be the first to know! :)