Don't bother like my brother

Just another WordPress weblog

 

OpenFrameworks object detection September 1, 2010

Filed under: IT, code, software — Artem @ 12:41 pm

I found out about OpenFrameworks from the project Laser Tag by Graffiti Research Lab. Got curious, and the first thing I created with OF was a bit similar to the Laser Tag :-)

I used OpenCV to detect the button on the wall, and then its coordinates to draw the line. Later I will post the source code.

 
 

OpenFrameworks ofProject tab completion

Filed under: IT, code, interesting, tips — Artem @ 11:26 am

I prefer to do many things in console and am used to tab completion in programs. It felt uncomfortable to type the plugin and project names in ofProject, especially when you have many of them. So I have created a small script that looks into appropriate directories and then autocompletes the names when you press tab.

_ofProject()
{
 local cur prev

 COMPREPLY=()
 cur="${COMP_WORDS[COMP_CWORD]}"
 prev="${COMP_WORDS[COMP_CWORD-1]}"
 opts="add"
 local ADDON_DIRS=$(ls -l /home/alius/Code/openframeworks/addons/ | grep '^d' | awk '{ print $8 }')
 local APP_DIRS=$(ls -l /home/alius/Code/openframeworks/apps/myApps/ | grep '^d' | awk '{ print $8 }')

 case "${prev}" in
    add)
        COMPREPLY=( $(compgen -W "${ADDON_DIRS}" -- ${cur}))
        return 0
        ;;
    *)
        COMPREPLY=( $(compgen -W "${APP_DIRS}" -- ${cur}))
        return 0
        ;;
 esac
}
complete -F _ofProject ofProject

What you need is just change the paths and place this script into /etc/bash_completion.d/ as ofProject

 
 

Android PopupWindow example

Filed under: IT, code, tips — Artem @ 11:04 am

I’ve struggled for some time to get the PopupWindow to work on Android. For all of those who have trouble making it to work here is a short tutorial.

First, create an XML layout, name it popup_layout.xml and in the res/layout/ folder:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_menu_root"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button android:id="@+id/popup_menu_button1"
        android:text="close"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/popup_menu_button2"
        android:text="ok2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/popup_menu_button3"
        android:text="ok3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Here I have created a linear layout and put three buttons inside. So that the popup is visible I have made the background white.

Now to the code. You probably already have a class where you need to create a popup. You can declare it wherever you need. I needed to show the popup window after user clicks a button, so I made it class-level and declared in the beginning:

private PopupWindow pw;

Next in the appropriate onClickListener I create the PopupWindow and show it:

// get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopupWindowClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate our view from the corresponding XML file
View layout = inflater.inflate(R.layout.popup_menu, (ViewGroup)findViewById(R.id.popup_menu_root));
// create a 100px width and 200px height popup window
pw = new PopupWindow(layout, 100, 200, true);
// set actions to buttons we have in our popup
Button button1 = (Button)layout.findViewById(R.id.popup_menu_button1);
button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View vv) {
        // close the popup
        pw.dismiss();
    }
});
Button button2 = (Button)layout.findViewById(R.id.popup_menu_button2);
button2.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View vv) {
        Toast.makeText(PopupWindowClass.this, "Hello", Toast.LENGTH_LONG).show();
    }
});
Button button3 = (Button)layout.findViewById(R.id.popup_menu_button3);
button3.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View vv) {
        finish();
    }
});
// finally show the popup in the center of the window
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

Hope this helps you :-)

 
 

Benchmarking your C code September 11, 2009

Filed under: code — Artem @ 7:57 pm

Ever wondered how long does it take for a piece of your code to execute? It is easy to find out the execution time using the gettimeofday function from the <sys/time.h>. The code is the following:

#include <sys/time.h>

struct timeval t1, t2;
double tdiff;

gettimeofday(&t1, NULL);

<your code here>

gettimeofday(&t2, NULL);
tdiff = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) / 1000000.0;
printf("Execution time: %2.5f\n", tdiff);
 
 

Skype API on Python = Skype4Py April 10, 2009

Filed under: code — Artem @ 1:30 pm

Recently I needed to write a small utility for Skype. Python seemed as a good start for this purpose as the language is easy and flexible and it has a Skype4Py module with all the necessary APIs. The installation is straightforward. First off, go to the download page and get the latest version. Then go to the folder you downloaded it into and unpack the archive:

tar xzvf Skype4Py-1.0.31.0.tar.gz

Go to the folder with the source and install the module:

cd Skype4Py-1.0.31.0
sudo python setup.py install

Now when everything is installed, you can proceed to the code. There are not much examples of the code in the Internet, but if you know python, then the process is very simple. The project page has extensive documentation on all the classes and methods, and is very helpful in using this module. To demonstrate how it all works, I wrote a small and not very useful program, which however serves its purpose:

import sys
import Skype4Py

# attach our program to the Skype
def OnAttach(status):
    print 'API attachment status: ' + skype.Convert.AttachmentStatusToText(status)
    if status == Skype4Py.apiAttachAvailable:
        skype.Attach()

# create Skype object
skype = Skype4Py.Skype()
skype.OnAttachmentStatus = OnAttach

if not skype.Client.IsRunning:
    print 'Starting Skype..'
    skype.Client.Start()

# get the name of Skype contact from the command line and create an object uprofile
uname = sys.argv[1]
uprofile = skype.User(Username=uname)

# now we can use all the methods from this object (see the documentations for "IUser" class for all available methods)
# print the full name of a person
print 'profile: ' + uprofile.FullName

# open chat with uname
message = 'From cmd: ' + sys.argv[2]
uchat = skype.CreateChatWith(uname)
uchat.SendMessage(message)
print 'message sent: ' + message

sys.exit

The main Skype interface is Skype4Py.Skype, from there you start the chat, get the user profiles, place calls and do all other things.

To run this program, use this command, where skypedummy.py is the name of your program, “i.am” is the skype name of the contact, and in the quotation marks is the message you are sending to him:

python skypedummy.py i.am "Greetings from my first skype app"