Raspberry pi GPIO hardware buttons
#16
(2013-08-05, 13:39)el_Paraguayo Wrote:
(2013-08-05, 11:37)troyk Wrote: I've been reading about similar solutions but i'm not your experienced coder so i've got a lot of reading / learning to do if i want to pull this one off.
I thought there should be an easier way to get this done but i guess there isn't.

Now i'm considering hacking up some old USB gamepad to add directional pad + buttons. It's going to be a lot easier probaby.
Easier - probably. But will it be as fun?

Learning to code as a result of having a desired outcome but not knowing how to get there is all part of the fun of Raspberry Pis and XBMC.

Don't be disheartened if something seems difficult. Both the Raspberry Pi and XBMC forums are incredibly supportive and will help with any queries.

As for your original question, one issue I can see is that you'd need a lot of buttons wired to the Raspberry Pi to get this to work (and even then you'd need to edit a keymap quite heavily) - unless you want the button to have one specific function.

Why wouldn't you just get a USB IR remote? A lot of these seem to work with XBMC.

If you've got any other questions, just ask!

You're absolutely right but if you don't know where to start it can all get very blurry and endless.
Why do you think i need a lot of buttons wired? I think i need only 8 ? I believe there are 8 GPIO outputs on the Pi.
up / down / left / right / okay / back / info / (on/off).

I don't want to use an USB IR remote because I want to build an XBMC tablet , so hardware controls are built into the framework. I also don't want to use a touch-screen.

What i got now is:
- Pi Model B
- 17" 1440 x 900 laptop screen on 12 volts (from left-over HP pavilion)
- 15 volt lap-top battery - hacked-up for better placement
- Voltage regulator for pi to go to 5v
- VGA adapter from Ebay connected through HDMI to 17" screen
- Prototype board to do the circuitry
- Buttons / cables / leds etc etc.

So i wanted to built al those elements into one case and have a portable xbmc station. Why ? Because it looks like a lot of fun and it's something i can take into my toilet or use while cooking .

As for the casing: I have strong connections with a company that does aluminium casing based on DXF drawings. I can design my case but first I need my build to work .



-
Reply
#17
Right - no idea what happened to my last answer. Looks like it disappeared (or I posted it to some random thread...) Let's see if I can remember what I wrote.


That's what I like to see. A clear idea of where you want to end up. Now it's a case of breaking down the journey into manageable chunks.

A lot of it is going to be assembly. Sounds like you're ok there.

As for the script for the buttons, I'd gotten it into my mind that you were only going to have 4 buttons which would have been difficult! 8 sounds like it should be workable, although i suspect you'll still need to edit a keymap e.g. you may want your "on/off" button to stop music/videos from playing.

In terms of the event client script, this should be pretty straightforward. As I said, my Pi doesn't have hardware buttons so I can't mimic that part of the script. However, I can use something like pygame to capture key presses and then use that to trigger the event client code (I confess, I already tried this over the weekend to see if it's possible to control XBMC this way - good news, it is!). I haven't tried triggering that code with an xbmc service addon, but it shouldn't be difficult.

So... you've got your idea, time to make it happen! I'll help with python code as much as I can (although I'm really just a beginner too).

One question for you. How are you going to run audio on your "tablet"? HDMI to VGA sounds like you'll lose audio output that way. Are you wiring in a speaker to the 3.5mm socket?
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#18
(2013-08-05, 15:28)el_Paraguayo Wrote: Right - no idea what happened to my last answer. Looks like it disappeared (or I posted it to some random thread...) Let's see if I can remember what I wrote.


That's what I like to see. A clear idea of where you want to end up. Now it's a case of breaking down the journey into manageable chunks.

A lot of it is going to be assembly. Sounds like you're ok there.

As for the script for the buttons, I'd gotten it into my mind that you were only going to have 4 buttons which would have been difficult! 8 sounds like it should be workable, although i suspect you'll still need to edit a keymap e.g. you may want your "on/off" button to stop music/videos from playing.

In terms of the event client script, this should be pretty straightforward. As I said, my Pi doesn't have hardware buttons so I can't mimic that part of the script. However, I can use something like pygame to capture key presses and then use that to trigger the event client code (I confess, I already tried this over the weekend to see if it's possible to control XBMC this way - good news, it is!). I haven't tried triggering that code with an xbmc service addon, but it shouldn't be difficult.

So... you've got your idea, time to make it happen! I'll help with python code as much as I can (although I'm really just a beginner too).

One question for you. How are you going to run audio on your "tablet"? HDMI to VGA sounds like you'll lose audio output that way. Are you wiring in a speaker to the 3.5mm socket?

Thank you for your supportive answer, keeps the spirit up !

As for the keymap , i'm not too worried, i should get that part down within reasonable time and/or with some help.

I don't have a lot of knowledge how XBMC handles request from 'foreign' or 'external' apps/scripts but i think it's possible through JSON to code a python script which connects over JSON and then gives XBMC the info that the button is pressed from GPIO. I don't have any clue how to code this but i will read some info on JSON and how to connect, there must be some tutorials online i guess?
So that's the first chunk that needs to be solved.

I will remove the audiojack from the pi board and solder cheap (Thailand - USB iPod thing) speakers directly on to the board. I will also remove the ethernet connector since i won't be needing it (wireless) and i need all the room i can spare for the other devices / circuitry.
Speakers have a total height of 24 mm . This will also (hopefully) be the max thickness of the tablet (excluding material thickness of casing).

So yeah, loads of work to do, not so much time but willing to invest Wink.

Thanks for the help so far.
Reply
#19
I will share some code that may get you started in terms of controlling xbmc from an external script.

I'm also happy to help with any other scripting issues if I can.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#20
Here's something to get you started:

Code:
from xbmcclient import *
from socket import *


def main():
    host = "localhost"
    port = 9777
    addr = (host, port)

    sock = socket(AF_INET,SOCK_DGRAM)

    # First packet must be HELO and can contain an icon
    packet = PacketHELO("Raspberry Pi Control", ICON_NONE)
    packet.send(sock, addr)


    while True:
            #CODE TO CATCH BUTTON PRESS HERE
                packet = PacketBUTTON(map_name="KB", button_name="left", repeat=0)
                packet.send(sock, addr)    


if __name__=="__main__":
    main()

You'll need to download xbmclient.py from here and save in the same folder as the script.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#21
(2013-08-05, 20:38)el_Paraguayo Wrote: Here's something to get you started:

Code:
from xbmcclient import *
from socket import *


def main():
    host = "localhost"
    port = 9777
    addr = (host, port)

    sock = socket(AF_INET,SOCK_DGRAM)

    # First packet must be HELO and can contain an icon
    packet = PacketHELO("Raspberry Pi Control", ICON_NONE)
    packet.send(sock, addr)


    while True:
            #CODE TO CATCH BUTTON PRESS HERE
                packet = PacketBUTTON(map_name="KB", button_name="left", repeat=0)
                packet.send(sock, addr)    


if __name__=="__main__":
    main()

You'll need to download xbmclient.py from here and save in the same folder as the script.

Thanks ! I'm going to work on my project this evening again, will update you if it works.
Reply
#22
(2013-08-05, 20:38)el_Paraguayo Wrote: Here's something to get you started:

Code:
from xbmcclient import *
from socket import *


def main():
    host = "localhost"
    port = 9777
    addr = (host, port)

    sock = socket(AF_INET,SOCK_DGRAM)

    # First packet must be HELO and can contain an icon
    packet = PacketHELO("Raspberry Pi Control", ICON_NONE)
    packet.send(sock, addr)


    while True:
            #CODE TO CATCH BUTTON PRESS HERE
                packet = PacketBUTTON(map_name="KB", button_name="left", repeat=0)
                packet.send(sock, addr)    


if __name__=="__main__":
    main()

You'll need to download xbmclient.py from here and save in the same folder as the script.

So i've edited the script, downloaded the python file and ran my script (gpio.py). It says nothing so the code works out. Now the gpio code that i used works!

The problem is however : XBMC continues to go left without stopping. So now it's racing through all the menu options. But when i press the hardware button , it comes to a halt.
When i press the button i drop the voltage of the circuit and then the constant 'going left' stops.

Good thing is: It seems to work
Bad thing: How do i edit the code that it just runs 'left button' once Smile.

Here's a video :
http://www.youtube.com/watch?v=AlPc-GgyZFA

Thanks!
Reply
#23
That bit of code should send the left keyboard key. At least it does in mine.

When you start the script do you get a notification in xbmc? It should say "New device detected - raspberry pi control". If not, you may need to enable remote control in your network settings in xbmc.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#24
(2013-08-07, 22:21)el_Paraguayo Wrote: That bit of code should send the left keyboard key. At least it does in mine.

When you start the script do you get a notification in xbmc? It should say "New device detected - raspberry pi control". If not, you may need to enable remote control in your network settings in xbmc.

It does give me the pop up that xbmc is controlled by Raspberry Pi control but it still keeps scrolling to much to the left.

This is the code that i use:

Code:
while True:
        input_value = gpio.input(17)
        if input_value == False:
............
            # Here code when button touched

            packet = PacketBUTTON(map_name="KB", button_name="left", repeat=0)
            packet.send(sock, addr)....

Now this happens:
http://www.youtube.com/watch?v=Ah-8blgQI40

I think this has something to do with the time the button is 5v or 0v. If it detects 5v it will do 'key-left' so if this takes too long it might do a couple of more 'key-lefts' .
You think this is possible? Smile
Reply
#25
Sorry, misread your earlier post. Good to see that it's sending commands to xbmc.

You could try something like a simple time.sleep(n) after the button press and try to find a suitable value.

Unfortunately coding it so that there's a long delay to start with and then a shorter one if you continue to hold its probably beyond my skills...
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#26
Well at least I am somewhere now Wink.

Guess i'll just need to continue my search and hopefully other people will help me out.

Thanks for the help anyway!
Reply
#27
Troyk, I'd say you are doing brilliantly. Try the time.sleep idea, as it should work as a temporary fix.

In terms of the next steps, you might want to try the raspberry pi forums for coding advice regarding button presses and repeats.

Also have a look at the code here about "bouncing" http://www.cl.cam.ac.uk/projects/raspber..._switches/
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#28
(2013-08-07, 23:21)el_Paraguayo Wrote: Troyk, I'd say you are doing brilliantly. Try the time.sleep idea, as it should work as a temporary fix.

In terms of the next steps, you might want to try the raspberry pi forums for coding advice regarding button presses and repeats.

Also have a look at the code here about "bouncing" http://www.cl.cam.ac.uk/projects/raspber..._switches/

Yes, will try that tonight. Thanks for the ideas and help.

I'm also reading something about Callbacks .

It says this about working without callbacks (in case you want to print text when pushing a button - similar to my situation i guess):
As you press and release button you may see some instances where PUSHED! is printed more than once during a single press, or possible printed while you are releasing the button. This is because our simple circuit is very "noisy" in the moments during a press or release. This kind of multiple-triggering is referred to as "bounce" and the process of removing it is "debouncing" the input. Several strategies exist to debounce inputs ranging from simple software solution to complex hardware augmentations. For the purposes of the rest of this guide we will ignore this artifact, however as you build more complex things you will most likely want to look into debouncing solutions.

source: http://raspberry.io/projects/view/readin...om-python/

edit : Now reading your signature article about 'bouncing' Wink
Reply
#29
hello, I would like Kodi to enter and start the photos I have on the google drive at startup.
i tried with the following
code:
import xbmc
xbmc.executebuiltin ("RunAddon (plugin.image.flickr)")

it goes no further and I don't know what the right syntax is.
can you help me?
Reply

Logout Mark Read Team Forum Stats Members Help
Raspberry pi GPIO hardware buttons0