CHUSBIE552

Showing posts with label configuring. Show all posts
Showing posts with label configuring. Show all posts

Monday, 15 June 2015

Simulating Keyboard Input in Python (Linux)

This is a big question that has very little good answers too it as I found out. Sorry if this is a little dry but this need to be shared.

NOTE: This is orientated towards Linux Python 2.6, the Windows Python solution is different.

How to I get Python to simulate keyboard key presses and releases?  For me this came up while trying to make a custom capacitive touch  controller with a on a Raspberry Pi for use with MAME or Minecraft Pi edition.

For this we going to use a Python Module called Uinput. This module can simulate keyboard, mouse or custom device. It can also migrate happily between programs in the desktop environment as well as the command line.

All of the following need to be completed on the command prompt or in a terminal window so if you are on the desktop go ahead and open your terminal program.

 

Prerequisites

There are a couple of extra development libraries that need  to be installed, before installing the python module otherwise Uinput will refuse to compile and install.

First of all it is always worthwhile making sure your apt repositories are up to date it is a good rule to do this before installing any new software, so to do this you need to
sudo apt-get update 
Then the development libraries that you need to install are libxtst-dev

sudo apt-get install libxtst-dev



Installing the module


Make sure that you have the python pip installer installed this will make it easier to build an install on your Linux system.
sudo apt-get install  python-pip
Then to install the module using the python pip installer.

sudo pip install python-uinput
If all the prerequisites are satisfied this will download compile and install the python module. :)

The module requires Uinput kernel module to be running this can easily be done with
sudo modprobe uinput
This will load the module this time round. I you want it to load every time you reboot your system then it will need to be added to the /etc/modules file. To edit the file from your command line.

sudo nano /etc/modules
Add an extra time to the line to the file with.
uinput
Then.
Ctrl + x
This will save and exit just hit  Y to confirm overwriting the file.

 

Using the module


So now that is all complete its only a case of writing some code.
import uinput
device =  uinput.Device((uinput.KEY_W,uinput.KEY_S))
import time
while 1:
        device.emit(uinput.KEY_W,1)
        time.sleep(0.5)
        device.emit(uinput.KEY_W,0)
        time.sleep(0.5)

This is a  little sketch to setup a two button device I'm just using this as an example but any device can be made with any number of keys so lets have a look at the code.

Lets have a look at what the code is doing.
import uinput 
This imports the required module into your python sketch.
 device =  uinput.Device((uinput.KEY_W,uinput.KEY_S))
This line creates a uinput device with two keys W and S all the letter keys emit as there lower case version unless it is modified with a shift.

import time
imports the time module for handling delays
while 1:
Repeat forever
        device.emit(uinput.KEY_W,1)
Keydown w key
        time.sleep(0.5)
wait 0.5 seconds
        device.emit(uinput.KEY_W,0)
Keyup w key
        time.sleep(0.5)
  
 This module could be used for making custom combos for games in MAME like street fighter or just allow automated control of some command line tasks. I am currently using it to make a custom game controller on with a Raspberry Pi's  GPIO connection.

Hope this helps out a few people as I found it tricky to get up and running in the first place hopefully I have covered that so people don't have the same difficulty.





Wednesday, 27 May 2015

Pi Supply Gert VGA666 Software Setup and Test


This is how to Set up the Gert VGA666 for Pi Supply I have a previous post on assembling the components if you haven't already done so please check it out here.

Installing and Configuring software 


Be aware that you are changing the where the Pi is going to output it video if you have the option I would recommend setting up over an SSH connection.

Firstly you'll need to download the required software to run the Gert VGA 666. Make sure your Pi has an internet connection.
git clone https://github.com/fenlogic/vga66.git


Then you will need to copy the driver file into /boot/ and rename it.
cd vga666/setup/
sudo cp dt-blob-dpi.bin /boot/
sudo mv /boot/dt-blob-dpi.bin /boot/dt-blob.bin

Now the final step is to add it to your raspberry pi configuration.

So to open the configuration file.
sudo nano /boot/config.txt
The you'll need to add.
 enable_dpi_lcd=1
display_default_lcd=1
This will enable the VGA output. Then you can set the output resolutions by adding
dpi_group=2
dpi_mode=82
 For 1080p @60Hz or.
dpi_group=2
dpi_mode=86
 For 1366 x 768 @60Hz





These are the two screen modes I have tried myself please now the resolution values are the same as setting the resolution for the HDMI output, a full list can be found here.

Problems you may run into 

The first time that I tried this the VGA work for about 10 seconds of boot time the the text turned yellow and then vanished dropping my screen into power saving.


I was using a Rasbian image that had some of the GPIO setup for various tasks so it was 'grabbing' some of the GPIO for other tasks this was fixed with a fresh burn of Raspbian.




In conclusion 

This is great add-on to the Raspberry Pi and the documentation makes for interesting reading as it goes into an explanation of how it generates and analogue signal from the GPIO header, and makes for great learning and soldering practice.

This is in no way the perfect solution for VGA output on the Raspberry as it will have the occasional slight shimmer on the display and it uses huge chuck of you GPIO to drive it. So if your you are looking for a VGA output from your Raspberry Pi I'd recommend a good HDMI to VGA adapter.

So I award this 3 of 5 Sparks it was fun to assemble the documentation could be a little clearer but as a learning exercise it's fantastic.