The next piece of hardware we will set up is the six switches that will be used for the four direction keys, back, and enter. Although they can be easily remapped to whatever set of controls you like.
Once this is done, we can move on to the software setup for the buttons. To interface with the GPIO hardware, we will use the sysfs bindings that are available by reading and writing to files under the /sys/class/gpio/
directory. This method is used because OpenELEC does not include the library to access GPIO through Python, and because the operating system runs mostly from a read-only filesystem, it is difficult to install this library.
button_watcher.py
:BUTTONS['enter'] = (4, 'Input.Select') BUTTONS['back'] = (9, 'Input.Back') BUTTONS['up'] = (27, 'Input.Up') BUTTONS['down'] = (22, 'Input.Down') BUTTONS['left'] = (10, 'Input.Left') BUTTONS['right'] = (11, 'Input.Right')
Here, the first entry in the tuple for each button is the GPIO number (not the pin number) the button is connected to and the second entry is the command that is sent to the XBMC API when the button is pressed.
Changing the values for these entries allows you to remap the buttons to any functions supported by the API. Full documentation of the API is available on the XBMC wiki (http://kodi.wiki/view/JSON-RPC_API/v4). Here is a list of some common remote control functions that can be used:
Input.Up
: This navigates up in the GUIInput.Down
: This navigates down in the GUIInput.Left
: This navigates left in the GUIInput.Right
: This navigates right in the GUIInput.Select
: This selects the current item in the GUIInput.Back
: This navigates back one level in the GUIInput.Home
: This returns to the home menu in the GUIPlayer.PlayPause
: This pauses or resumes playbackPlayer.Stop
: This stops playbackPlayer.GoPrevious
: This takes you to the previous item in the playlistPlayer.GoNext
: This takes you to the next item in the playlistbutton_watcher.py
script to the /storage/.config/
directory on the SD card. This is easily done by using SFTP.python /storage/.config/button_watcher.py
If all is well, you should see an output similar to the following on the console and XBMC should react according to what the button has been configured to do in button_watcher.py
. To exit the script, press Ctrl + C.
If you see an error stating that the device is busy or unavailable, check the mapping in button_watcher.py
and ensure that all of the GPIO port numbers are correct and that you have not mapped a button over another button or the LCD.
touch /storage/.config/autostart.sh chmod +x /storage/.config/autostart.sh nano /storage/.config/autostart.sh
#!/bin/sh python /storage/.config/button_watcher.py &
&
trailing). Now, simply exit nano using Ctrl + X and reboot the Pi using the following command:reboot
When the Pi has booted back into OpenELEC and XBMC is running, you should now be able to use the buttons right away.
18.223.170.63