TV remote control, Ruby and Turtle Graphics

Friday, February 22nd, 2008

In my previous post I had written about doing Turtle graphics with Ruby. Now i want to control the movement of the turtle on the screen, using a TV remote control, wirelessly from a distance. Let me breakdown the whole process into simple steps :

  1. Transmitting Infrared (IR) signals from a distance.
  2. Receiving these IR signals.
  3. Interface to the computer.
  4. Software Interface (API) for reading the data from the I/O port.
  5. Application level program for controlling the movement of Turtle based on this data.

Transmitting IR signals

An circuit comprising of IR LED, power supply and a few switches would have been required, but we can simply use a TV remote control, instead :)

Receiving IR signals

A simple IR receiver circuit and instructions for building it can be found here. Components costs less than Rs.20 and can be bought from any electronics shop.

Here is the one which i made :

ir_receiver.jpg

Interface to the computer

Our IR receiver circuit has a serial interface. So we can connect it directly to the serial port of the computer.

Software Interface for reading and interpreting signals on the serial port

The Linux Infrared Remote Control project (LIRC) provides all necessary support for us. LIRC is a package that allows us to decode infra-red signals of the most commonly used remote controls. Even if we have a remote control that is not directly supported by LIRC, we can train LIRC to use our remote comtrol.

Instructions for installing and configuring LIRC is available here.

Alas, the remote controller which I had with me - it was the one which came with my TV tuner card, and was faulty. So I couldn’t configure it properly with LIRC. I was able to receive signals from my remote, and see it using ‘xmode2′ program that comes with LIRC, but LIRC wasn’t able to configure the keys of my remote control. I dint have the time to go and get a new remote control, so i devised a temporary work around - Make the turtle move in any one direction by default at a predefined speed, and whenever a key pressed on the remote (irrespective of which key is pressed), turn the turtle by 45 degrees.

Program for controlling Turtle movements upon keypress on remote control

Even though LIRC couldn’t help me directly with my remote, i found a device file /dev/lirc0 (that appears once the circuit is connected, and ‘lircd’ daemon started). I tried reading from this file, and on each keypress on the remote i got a series of bytes. So i decided to use this file, for detecting keypress on the remote.

Now the only thing left was modifying my previous turtle program, to make the turtle change direction upon every keypress on the remote control. I used two threads :

  • first one for moving the turtle forward in a direction
  • second one for detecting keypress (by reading /dev/lirc0) and changing the direction whenever key is pressed

A shared global variable key_pressed is used for communication between these threads. Access to key_pressed is synchronized with a mutex.

Here is my complete program for controlling Turtle on screen with a TV remote.

turtle_remote.rb

Root previleges are required for accessing /dev/lirc0, so run it as

sudo ruby turtle_remote.rb

Turtles movement on screen can be seen here

Keyboard lights & Ruby

Wednesday, February 20th, 2008

I happened to see a ruby library called Blinkenlights, which helps to control the three LEDs (Caps lock, Scroll lock & Num lock) on keyboards. I have written few simple programs to play with the keyboard LEDs - basically simulation of three experiments I did in my Digital Electronics lab - Binary Counter, Ring Counter & Johnson Counter.

sudo gem install blinkenlights

Binary Counter

A counter that counts from 0 to 7 in binary, using the three LEDs as three bits. 0(000) - All LEDs will be turned off, and 7(111) all of them turned ON.
counter.rb

super user privilege is required to access the keyboard, so run it as

sudo ruby counter.rb

Ring Counter

Starting with 4 (100), we right shift to get 2 (010), again right shift to get 1(001), and then start over again with 4. This causes the keyboard LEDs to turn ON and then OFF one by one.

ringcounter.rb

Johnson Counter

This is the Johson counter sequence : 000,001,011,111,110,100,000,001,…

johnsoncounter.rb

Anvil: GUI development Framework in Ruby

Monday, January 14th, 2008

Recently i have done a small experimentation on Anvil - A ruby framework to create GUI based applications.The back bone of Anvil is WxRuby. At a glance i thought it will be very easy, so decided to to give a try :)

sudo apt-get install anvil

This installs anvil and all the other required gems. Then running the command

anvil my_editor

Creates a project my_editor. Anvil follows MVC architecture. Browsing through the new project’s folder structure, we can see folders and files for inserting our Models, Views and controllers.

cd my_editor
anvil

A window pops up..! Now we just need to add GUI components as we require, in the respective views and controllers.

I was very excited to see the ease with which i could build an GUI application with Anvil but i couldn’t finish by the end of the day as i had wished. I was getting some errors while using “button” method. It was saying NoMethod error :(. But couldn’t find any error that time & tried a lot then i came to know that the WxRuby that got installed was the latest version and in documentation of Anvil was with an older version. Then i tried with “evt_button” of the new version and it worked.

I found Anvil and WxRuby very useful in building GUI applications.