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?
Re: GPIO button to emulate keyboard
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/
Compile it with the command:
Run it with the command:
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();
}
}
Code: Select all
gcc -o keytest keytest.c -lwiringPi
Code: Select all
sudo ./keytest
Re: GPIO button to emulate keyboard
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
.
Does any body have any ideas on how to achieve this?
I want to be able to run
at startup and then run Mplayer in slave mode. I will (Hopefully) use a program tomkfifo ~/Mplay
on a GPIO button press. I can successfully create the fifo from a script, but when I tryEcho "P" > MPlay
, I getmplayer -slave -quiet FILE -input file=~/Mplay
.-bash: /home/pi/fif: Permission denied
.
Does any body have any ideas on how to achieve this?
Re: GPIO button to emulate keyboard
Hello JRVJRV 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/Compile it with the command: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(); } }
Run it with the command:Code: Select all
gcc -o keytest keytest.c -lwiringPi
Code: Select all
sudo ./keytest
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?
-
- Posts: 10
- Joined: Wed Dec 09, 2015 3:05 pm
Re: GPIO button to emulate keyboard
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
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
Re: PI_ZA
Instead of
Do:
Code: Select all
mplayer -slave -quiet FILE -input file=~/Mplay
Code: Select all
Sudo mplayer -slave -quiet FILE -input file=~/Mplay
- mahjongg
- Forum Moderator
- Posts: 14482
- Joined: Sun Mar 11, 2012 12:19 am
- Location: South Holland, The Netherlands
Re: GPIO button to emulate keyboard
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
https://github.com/adafruit/Adafruit-Retrogame
-
- Posts: 4
- Joined: Fri Apr 03, 2015 6:18 pm
Re: GPIO button to emulate keyboard
did you ever get this to work? I am looking into doing the same thing.PI_ZA wrote: ↑Thu Aug 29, 2013 9:55 pmThank 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 runat startup and then run Mplayer in slave mode. I will (Hopefully) use a program tomkfifo ~/Mplayon a GPIO button press. I can successfully create the fifo from a script, but when I tryEcho "P" > MPlay, I getmplayer -slave -quiet FILE -input file=~/Mplay.-bash: /home/pi/fif: Permission denied
.
Does any body have any ideas on how to achieve this?