Code

You are currently browsing the archive for the Code category.

To compile a C++ application using gtkmm in Code::Blocks, do the following steps:

  1. Open Project > Build Options…
  2. Select your project.
  3. On the Compiler settings tab, select Other options and add this to the field:
    `pkg-config gtkmm-3.0 --cflags`
  4. Select the Linker settings tab and type the following line in the Other linker options text area:
    `pkg-config gtkmm-3.0 --libs`

Tags: , , , , , ,

There is a pretty cool command-line based application that can execute keystrokes, mouse movement and mouse clicks, called Xautomation. It’s kinda useless if you are sitting at the computer with the terminal in the foreground. But it’s really awesome if you are coding an application that runs in the background and responds to signals from a remote control or similar. I have coded a little command-line based Python 3 program that does just that. It reads USB signals from my Arduino board which is connected to a IR receiver, and executes hotkeys that fits in Totem Media Player.

As an Arch Linux user it was a piece of cake to get Xautomation by downloading it from the AUR:

To use it, open a terminal and print:

xte --help

Then you will get a list of stuff that you can do with this piece of software, like:

xte "key a"
xte "str aAa"
xte "keydown Shift_L" "key a" "keyup Shift_L"
xte "mousermove 100 100"

To execute these commands from a Python 3 application, use this code:

import subprocess
subprocess.call(["xte", "key a"])

Tags: , , , , , , ,

In the Arduino kit that I bought not long ago were an IR remote control and an IR receiver included. And I have found a really good library for Arduino that makes decoding the IR signals from the remote control really easy. So I have used this library and coded a short program to my Arduino card that reads the IR signals and then sends more logical signals to the computer through the USB cable.

Here is a link to the page where I found the library: http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html

Here is code that I have written for my Arduino board:

#include <IRremote.h>

int RECV_PIN = 11;
String in;
String cur = "";
String cur2 = "";

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {

    in = String(int(results.value), HEX);
    irrecv.resume(); // Receive the next value
    cur2 = "";

    if (in != "ffff") {
      if (in == "a25d") {      // ON/OFF
        cur = "11";
      }
      else if (in == "629d") { // Mode
        cur = "12";
      }
      else if (in == "e21d") { // Sound ON/OFF
        cur = "13";
      }
      else if (in == "22dd") { // Play/Pause
        cur = "21";
      }
      else if (in == "2fd") {  // Back
        cur = "22";
      }
      else if (in == "c23d") { // Forward
        cur = "23";
      }
      else if (in == "e01f") { // EQ
        cur = "31";
      }
      else if (in == "a857") { // VOL-
        cur = "32";
      }
      else if (in == "906f") { // VOL+
        cur = "33";
      }
      else if (in == "6897") { // 0
        cur = "41";
      }
      else if (in == "9867") { // RPT
        cur = "42";
      }
      else if (in == "b04f") { // U/SD
        cur = "43";
      }
      else if (in == "30cf") { // 1
        cur = "51";
      }
      else if (in == "18e7") { // 2
        cur = "52";
      }
      else if (in == "7a85") { // 3
        cur = "53";
      }
      else if (in == "10ef") { // 4
        cur = "61";
      }
      else if (in == "38c7") { // 5
        cur = "62";
      }
      else if (in == "5aa5") { // 6
        cur = "63";
      }
      else if (in == "42bd") { // 7
        cur = "71";
      }
      else if (in == "4ab5") { // 8
        cur = "72";
      }
      else if (in == "52ad") { // 9
        cur = "73";
      }
    }

    if (in == "ffff") { // Presses down a button
      Serial.println(cur + "d");
    } else if (cur.length() == 2) {
      Serial.println(cur + "0");
    }

  }

  delay(100);
}

I’m not sure if this program is so easy to understand, but I hope that it’s at least little help if you are setting up your own program for reading IR signals (and later controlling your computer).

Basically will the loop decode a signal, convert it to HEX and then check what button was pressed down. If I press a button on my remote control it will send a normal, unique signal (which in hex is something like a25d or 629d). But if I keep it pressed down it will keep on sending ffff signals, regardless of what button is kept pressed down. Therefore I have to save what button that was initially pressed down, which I do in cur.

The normal signals will result in numbers like 11, 12, 33 and 72 are saved as cur, and behind that is a really simple system. The first digit is the button row of the remote control, and the second digit is the the column. So as you may understand, I have 3 x 7 buttons on my remote control.

Before sending the number 22, 33, or something similar, to the computer, is 0 added to the end if the button was just pressed down or d if it already was pressed down in the previous loop.

So if press the button on my remote control that says 8 (row 7, column 2) the program will in the first run through send the message 720 to the computer. If I keep it pressed down will the program keep on sending 72d to the computer. If I bring up the Serial Monitor in the Arduino software will the output be:

720
72d
72d
72d
72d
72d
72d
72d

I apologize for my crappy English. ^.^

Tags: , , ,