PI_ZA
Posts: 5
Joined: Wed Aug 28, 2013 4:24 pm
Location: Pretoria, South Africa

GPIO button to emulate keyboard

Wed Aug 28, 2013 10:41 pm

Hello,

I am trying to setup my Pi as a music player, I want to be able to attach buttons to the GPIO pins to emulate keys being pressed (namely, P; < ; > ; * ; and / ). Is there any way to do this?

User avatar
JRV
Posts: 270
Joined: Mon Apr 02, 2012 1:39 pm
Location: Minneapolis, MN

Re: GPIO button to emulate keyboard

Thu Aug 29, 2013 6:50 pm

Yes, it's possible.
I made a test breadboard using 5 switches.
The switches are each attached to a digital pin
and the other lead to ground.

For the software I am using the wiringPi libraries and programming in C.

The documentation for wiringPi is located here:

http://wiringpi.com/

WiringPi uses different pin numbering, study this carefully:

http://wiringpi.com/pins/

You will also need to read these pages:

http://wiringpi.com/download-and-install/

http://wiringpi.com/reference/core-functions/

Code: Select all

/***************************************************
  Filename: keytest.c

Five switches are connected to pins 0 - 4, 
the other lead is connected to ground.

***************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>

void setup(void)
{
  int i;

  if (geteuid() != 0)
  {
    fprintf (stderr, "You need to be root to run this program. (sudo?)\n") ;
    exit(0) ;
  }

  if (wiringPiSetup() == -1)
  {
    printf("wiringPiSetup error\n");
    exit(1) ;
  }

  for(i=0;i<5;i++)    //Set pins 0-4 for input, with pullup.
  {
    pinMode(i, INPUT);
    pullUpDnControl (i, PUD_UP);
  }
}

void loop(void)
{
  int i,c;
  int readpin[5] = {0,1,2,3,4};
  int ascii[5] = {80,62,60,47,42};

  for(i=0;i<5;i++)
  {
    if(digitalRead(readpin[i])==LOW)
    {
      while(digitalRead(readpin[i])==LOW); // De-bounce
      ungetc(ascii[i],stdin);              // Push value into input buffer.
      c = getchar();
      printf("%c - Ascii = %d\n",c,c);     // Or whatever you want to do with it.
      i = 5;                               // Force exit of for loop.
    }
  }
}

int main(void)
{
  setup();

  for(;;)
  {
    loop();
  }
}
Compile it with the command:

Code: Select all

gcc -o keytest  keytest.c -lwiringPi
Run it with the command:

Code: Select all

sudo ./keytest

PI_ZA
Posts: 5
Joined: Wed Aug 28, 2013 4:24 pm
Location: Pretoria, South Africa

Re: GPIO button to emulate keyboard

Thu Aug 29, 2013 9:55 pm

Thank You! that works, However I think I found a better way of communicating with the program.I will use Mplayer and put it in slave mode, then create a fifo to communicate with it (as per this document http://www.mplayerhq.hu/DOCS/tech/slave.txt).

I want to be able to run
mkfifo ~/Mplay
at startup and then run Mplayer in slave mode. I will (Hopefully) use a program to
Echo "P" > MPlay
on a GPIO button press. I can successfully create the fifo from a script, but when I try
mplayer -slave -quiet FILE -input file=~/Mplay
, I get
-bash: /home/pi/fif: Permission denied
.
.

Does any body have any ideas on how to achieve this?

agash
Posts: 1
Joined: Mon Nov 25, 2013 8:20 am

Re: GPIO button to emulate keyboard

Mon Nov 25, 2013 8:29 am

JRV wrote:Yes, it's possible.
I made a test breadboard using 5 switches.
The switches are each attached to a digital pin
and the other lead to ground.

For the software I am using the wiringPi libraries and programming in C.

The documentation for wiringPi is located here:

http://wiringpi.com/

WiringPi uses different pin numbering, study this carefully:

http://wiringpi.com/pins/

You will also need to read these pages:

http://wiringpi.com/download-and-install/

http://wiringpi.com/reference/core-functions/

Code: Select all

/***************************************************
  Filename: keytest.c

Five switches are connected to pins 0 - 4, 
the other lead is connected to ground.

***************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>

void setup(void)
{
  int i;

  if (geteuid() != 0)
  {
    fprintf (stderr, "You need to be root to run this program. (sudo?)\n") ;
    exit(0) ;
  }

  if (wiringPiSetup() == -1)
  {
    printf("wiringPiSetup error\n");
    exit(1) ;
  }

  for(i=0;i<5;i++)    //Set pins 0-4 for input, with pullup.
  {
    pinMode(i, INPUT);
    pullUpDnControl (i, PUD_UP);
  }
}

void loop(void)
{
  int i,c;
  int readpin[5] = {0,1,2,3,4};
  int ascii[5] = {80,62,60,47,42};

  for(i=0;i<5;i++)
  {
    if(digitalRead(readpin[i])==LOW)
    {
      while(digitalRead(readpin[i])==LOW); // De-bounce
      ungetc(ascii[i],stdin);              // Push value into input buffer.
      c = getchar();
      printf("%c - Ascii = %d\n",c,c);     // Or whatever you want to do with it.
      i = 5;                               // Force exit of for loop.
    }
  }
}

int main(void)
{
  setup();

  for(;;)
  {
    loop();
  }
}
Compile it with the command:

Code: Select all

gcc -o keytest  keytest.c -lwiringPi
Run it with the command:

Code: Select all

sudo ./keytest
Hello JRV

I am very interested in implementing your solution, because I have a similar Problem. But sadly I have absolut no experience with C-Compile. I made all your steps, but when I want to compile, I receive a lot of errors:
keytest.c: In function ‘setup’:
keytest.c:14:1: error: stray ‘\302’ in program
keytest.c:14:1: error: stray ‘\240’ in program... etc. Do you know what I'm doing wrong or what I am missing?

Alex_raspberry
Posts: 10
Joined: Wed Dec 09, 2015 3:05 pm

Re: GPIO button to emulate keyboard

Wed Dec 09, 2015 3:24 pm

Hello,

I tried testkey, it works fine but only in one terminal which I ran: sudo ./testkey // one terminal which I ran

in other terminals and softwares it didn't work.

how can I emulate keys(such as "Enter" ,... ) in other terminals and other softwares?


thanks

Wyahm_122
Posts: 1
Joined: Sun Jan 21, 2018 12:32 pm

Re: PI_ZA

Sun Jan 21, 2018 12:43 pm

Instead of

Code: Select all

mplayer -slave -quiet FILE -input file=~/Mplay
Do:

Code: Select all

Sudo mplayer -slave -quiet FILE -input file=~/Mplay

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11088
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: GPIO button to emulate keyboard

Sun Jan 21, 2018 1:54 pm

Adafruit has written software for exactly this, initially so you can use arcade buttons to control RETROPIE , but it will work just as well for other applications. It's simply a background program that scans the GPIO's and insert keyboard button press/release codes in the keyboard queue.
https://github.com/adafruit/Adafruit-Retrogame

Return to “General discussion”