[SOLVED] Weird button remapping issue

Hi,

I ran into a very weird button remapping issue that’s driving me insane.

When I use the default button mapping, everything is ok, but for a project I’m working on (a Scratch arcade) I would like to remap the buttons to keypresses S - D - F and J - K - L for buttons 1 - 2 - 3 and 4 - 5 - 6.

So I looked up the Linux keyboard codes, and added the following to /boot/config.txt :

dtoverlay=picade,button1=31,button2=32,button3=33,button4=36,button5=37,button6=38

For buttons 1 to 5, this works ok, but for some weird reason button 6 always returns the ESC keycode (ie. 1 instead of 38 that was assigned).

Then I changed it to :

dtoverlay=picade,button6=38,button1=31,button2=32,button3=33,button4=36,button5=37

And… button 6 works, but now button 5 is mapped to the ESC key.

Is this a known bug or am I doing something wrong?

BTW I currently solved it by using the legacy Python driver, by changing the KEYS mapping (in /usr/bin/picadehatd) to :

KEYS = {
ENTER: e.KEY_ENTER,
ESCAPE: e.KEY_ESC,
COIN: e.KEY_C,
START: e.KEY_S,
UP: e.KEY_UP,
DOWN: e.KEY_DOWN,
LEFT: e.KEY_LEFT,
RIGHT: e.KEY_RIGHT,
BUTTON1: e.KEY_S,
BUTTON2: e.KEY_D,
BUTTON3: e.KEY_F,
BUTTON4: e.KEY_J,
BUTTON5: e.KEY_K,
BUTTON6: e.KEY_L
}

That might have to do with whether you have an English US keyboard or English UK etc.
There is a way to see exactly what code a key produces when you press it.
I used this to get the codes from a mini wireless keyboard I have to control my rover.
https://learn.pimoroni.com/tutorial/robots/controlling-your-robot-wireless-keyboard

It’s not the keyboard layout (I picked non conflicting keys to cover that aspect :p), it’s apparently a bug in how overrides are handled in /boot/config.txt …

I decided to change the default mapping in the picade.dts file to my preferred keys :

fragment@2 {
  target-path = "/";
  __overlay__ {
    gpio_keys: gpio_keys {
      compatible = "gpio-keys";
      pinctrl-names = "default";
      pinctrl-0 = <&picade_pins>;
      status = "okay";

      up: up {
        label = "Up";
        linux,code = <103>;
        gpios = <&gpio 12 1>; 
      };

      down: down {
        label = "Down";
        linux,code = <108>;
        gpios = <&gpio 6 1>;
      };

      left: left {
        label = "Left";
        linux,code = <105>;
        gpios = <&gpio 20 1>;
      };

      right: right {
        label = "Right";
        linux,code = <106>;
        gpios = <&gpio 16 1>;
      };

      button1: button1 {
        label = "Button 1";
        linux,code = <31>;
        gpios = <&gpio 5 1>;
      };

      button2: button2 {
        label = "Button 2";
        linux,code = <32>;
        gpios = <&gpio 11 1>;
      };

      button3: button3 {
        label = "Button 3";
        linux,code = <33>;
        gpios = <&gpio 8 1>;
      };

      button4: button4 {
        label = "Button 4";
        linux,code = <36>;
        gpios = <&gpio 25 1>;
      };

      button5: button5 {
        label = "Button 5";
        linux,code = <37>;
        gpios = <&gpio 9 1>;
      };

      button6: button6 {
        label = "Button 6";
        linux,code = <38>;
        gpios = <&gpio 10 1>;
      };

      enter: enter {
        label = "Enter";
        linux,code = <28>;
        gpios = <&gpio 27 1>;
      };

      escape: escape {
        label = "Escape";
        linux,code = <1>;
        gpios = <&gpio 22 1>;
      };

      coin: coin {
        label = "Coin";
        linux,code = <23>;
        gpios = <&gpio 23 1>;
      };

      start: start {
        label = "Start";
        linux,code = <24>;
        gpios = <&gpio 24 1>;
      };
    };
  };
};

Then recompiled the overlay (removed the picade.dtbo file & ran the install script again), and then everything works fine as well.

So it looks like the overrides in /boot/config.txt have issues (makes me wonder if maybe overlay settings are limited to a specific string length & overflows are ignored?).

Ok, I guess 3.2: DT parameters on Raspberry Pi Documentation - Configuration has the answer.

Note that multiple assignments can be placed on the same line, but ensure you don’t exceed the 80-character limit.

That’s why it didn’t work. Thank god for open source so I could fix it by changing the keys in the driver definition.

Case closed :p.

What ever works lol. Glad you got it working the way “you” want it to work. =)

BTW Here’s a picture of the arcade we built for Coolest Projects Belgium, it’s running Scratch games that the kids make (Scratch 2 only for now)

.

We received the arcade cabinet as a gift (we just had to add a new panel for the buttons & joystick and mount the X-HAT & RPi inside the cabinet - you can see them just below the monitor in the picture).