Thursday, August 25, 2011

GUI Programming with C Language on Linux Platform

When you say you gonna write a GUI application in C for Linux platform, some people may laugh at you saying it is really really hard to write GUI applications in C and you will have to write thousands of lines of codes to get even simple GUI components working. However it is not true. You can write very nice GUI applications with C and you don't have to put that much big effort for it.
   
There are different libraries available for writing GUI applications in Linux. Among all these, GTK+ is an interesting library which I have been explored recently. GTK stands for GIMP Tool Kit and it was originally developed for GIMP(GNU Image Manipulation Program). Currently GTK+ has been used for so many applications and specially it has been used for developing GNOME desktop environment.

Before developing applications with GTK+, you have to install it. You can install it by typing the following on the terminal.

sudo apt-get install build-essential
sudo apt-get install libgtk2.0-doc devhelp


Now it's time write some codes and explore. Open your favourite text editor and add the following code into it. Save this file as "first.c".
1:  #include <gtk/gtk.h>  
2:  int main( int  argc,  
3:       char *argv[] )  
4:  {  
5:    GtkWidget *window;  
6:    gtk_init (&argc, &argv);  
7:    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);  
8:    gtk_widget_show (window);  
9:    gtk_main ();  
10:    return 0;  
11:  }  

You can compile this program in the terminal by issuing the following command. You should note that the single quote used here is this (`) one, not this (') one.

gcc first.c -o first `pkg-config --cflags --libs gtk+-2.0`

Run the executable program by typing ./first on the terminal. You will see a window like the one showed in the Figure:1. But when you close the window you will notice that the program doesn't exit from the terminal even the window disappears. You have to put a CTRL+C to exit. This is because our simple program doesn't handle events such as mouse click in different places.

Figure-1















Lets explore much improved program. Copy the following code to a new file and save it as "second.c".

1:  #include<gtk/gtk.h>  
2:  void hello(GtkWidget* widget, gpointer data){  
3:       g_print("Hello World\n");  
4:  }  
5:  gint delete_event_handler(GtkWidget* widget, GdkEvent* event, gpointer data){  
6:       g_print("delete event occured\n");  
7:       return FALSE;  
8:  }  
9:  void destroy(GtkWidget* widget, gpointer data){  
10:       gtk_main_quit();  
11:  }  
12:  int main(int argc, char *argv[]){  
13:       GtkWidget *window;  
14:       GtkWidget *button;  
15:       gtk_init(&argc, &argv);  
16:       window = gtk_window_new(GTK_WINDOW_TOPLEVEL);  
17:       g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event_handler), NULL);  
18:       g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);  
19:       gtk_container_set_border_width(GTK_CONTAINER(window), 10);  
20:       button = gtk_button_new_with_label("Hello World");  
21:       g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(hello), NULL);  
22:       gtk_container_add(GTK_CONTAINER(window), button);  
23:       gtk_widget_show(button);  
24:       gtk_widget_show(window);  
25:       gtk_main();  
26:       return 0;  
27:  }  

Compile and run this program by issuing following commands in the terminal.

gcc second.c -o second `pkg-config --cflags --libs gtk+-2.0`
./second


This time you have a window with a button. Click on the "Hello World" button and seen how the mouse click event is handled by the hello() function that we have declared and it prints "Hello World" string in the terminal. Click the close button for issuing a delete event and see how the program get exited by calling gtk_main_quit() function inside the destroy() event handler function.

Figure-2 







Having a good understanding about how to register event handlers with the widgets and how to pack the widgets inside the window is necessary for learning to use GTK+. There's a good GTK+ tutorial here that can be viewed online or even off-line by downloading. Different example codes are also included in it and is very helpful. Go though it and explore what are the things you can do with GTK+.

Enjoy GUI programming in C!


Tuesday, August 23, 2011

Running Contiki on MSPSIM simulator

Running with lack of hardware is a big issue for people who are working in wireless sensor network related areas. Before burning a program into real hardware it is necessary to test the programs we write whether everything is OK. This is where hardware simulators are useful.

MSPSIM is a tool which can emulate Texas Instruments MSP430 micro-controller based sensor motes from the instruction level. It can be used independently or with the Contiki operating system. When writing programs for Contiki operating system and hopes to run these programs in MSP430 based sensor motes like MSB430, Tmote Sky, ESB, etc we can use MSPSIM simulator.

Running your Contiki programs on MSPSIM is really easy. Goto the examples directory of the Contiki operating system from the terminal and issue the following commands. (If you don't have Contiki source yet, download it first from here)


cd contiki-2.x/examples/hello-world/

make TARGET=sky hello-world.mspsim

By the second command, the hello-world program will be compiled into Tmote Sky motes platform and will run that executable code on MSPSIM simulator. These images shows what you will see in newly opened windows.
 














In the USART Port Output window you will see the hello-world programs output. It prints Hello, world! To exit the program type
exit 
on the command line prompt.

You can even compile the program into ESB motes platform. For that, you have to issue the command as,

make TARGET=esb hello-world.mspsim


This is the ESB motes window that you will see.
















There are lot of things that you can do with the MSPSIM simulator. You can learn more things in the web. This is a good resource to learn about MSPSIM.

Monday, August 8, 2011

Cooja Sensor Network Simulator

Cooja is a simuator that comes with the Contiki operating system. It is very useful for testing sensor network applications written for Contiki platform without having to put them in real sensor network hardware. If we use real hardware motes for testing our Contiki programs, we will have to burn the flash memory of the motes sevaral times until we get a nicely tested program without any known bug. But is not easy to program motes which is a time consuming task. Even if we are ready for that, it may be not good for a mote to reburn its flash memory so many times since the number of read write cycles possible in flash memory is limited.

    Because of these reasons whenever we write a Contiki program, we better try it running on the Cooja simulator first and later on we can go for real hardware. Cooja is written in Java language and has a very modular structure which enables it to be extended with custom made plugins for it. When we compile a Contiki program for running on Cooja, it actually gets compiled into an executable of the native platform which is the operating system where our Cooja simulator is running on. Cooja runs these executables using Java Native Interface(JNI) from a higher level.

    Before using Cooja simulator for testing our programs we have to setup the Environment for it to work properly. First of all download the Contiki source code from here. Let's say you have downloaded it. Inside the contiki source code you can find the Cooja simulators source code in the path /contiki-2.4/tools/cooja. Go through the following steps to prepare the environment. You may skip some steps if your platform already have such configurations. Here I assume you are using Ubuntu Linux as your operating system since it is where I have tested all these stuff.

1) Installing Java
    Since Cooja is written in java, you have to have java installed in your platform. Do the following to install Java. If you have already installed Java, you can skip this step. Open a terminal and issue the following command with an active internet connection.

    sudo apt-get install java-6-openjdk

2) Installing Apache ant
    We use ant as the build tool to compile and build the Cooja simulator from its source. Therefore to install it, issue the following command in a termianl.

    sudo apt-get install ant

3) Setting JAVA_HOME and PATH variables
    Cooja simulator requires the environmental variables JAVA_HOME and PATH to work properly. Therefore follow this link to set those variables.

Now you have the proper environment for running Cooja simuator. It's time to deal directly with Cooja. Open a terminal and goto the downloaded Contiki source code. From there, goto Cooja source code directory which should be in the path /contiki-2.4/tools/cooja. From there issue the following commands.

    ant jar
    ant run

Figure-1





















    If everything went fine, you'll get a window like the one showed in the figure-1. It contains only a menu bar. You can test whether all the environment setup has done correctly by using a simple testing wizard provided by Cooja. Open this testing wizard by going through this way.

 menubar >> Settings >> Compiler configuration wizard

Figure-2





















You will see the wizard showed in the figure-2 now. Click on the "Start tests" button to begin the test. The first test will be "Compile and link Contiki library" as showed in fugure-3 and click on "Run test" button. If it prints "Test OK", you have done with first test. Click on "Close" button and then "Next test" button. In this way you should go through all the four tests. The final test window will be like the one showed in figure-4. After passing though the final test, you can "Close Wizard".

Figure-3





















Figure-4





















If one of these test is failed, that means you have missed something when setting up the environment as mentioned above. After passing the tests you can start using Cooja simulator for testing your Contiki programs.

To run the hello-world example which resides inside the examples directory of Contiki source code, do the following steps. Goto "File" menu and clieck on "New simulation" menu item. It will bring a window to create a new simulation. Enter a name for the simulation and click "Create". Now you'll get something like the figure-5. From there goto the menu "Mote Types" and from it click on "Contiki Mote Type". Now from that window, browse for the hello-world.c file in Contiki examples directory. Then click "Compile". After compilation completed, click on "Create" button.

Figure-5





















A new window will ask you to add the number of motes for simulation. For now lets type 1 on the text field and then click "Create and Add". In the simulation visualizer window you can see a mote now like a circle. Now in the Control Panel window, set the Delay value to 1ms and click "Start" button. Now you can see the output of the virtual motes that run your program in the Log listener window. You can stop the simulation by clicking on "Pause" button in the Control Panel window.

Figure-6





    Alternatively you have another easy way to quick start your Contiki program with Cooja. To try it, close the Cooja simulator window first. From the terminal goto the hello-world directory in the examples directory of Contiki. From there type the following on the terminal.

       make TARGET=cooja hello-world

This will start the hello-world program with Cooja simulator. There are so many things to explore in Cooja simulator. With the current understanding find about different things you can do with Cooja simulator.