Saturday, July 21, 2012

Video Player Using Qt Embedded

Cross compiling Qt Embedded:

Buildroot provide the easiest way to cross compile Qt Embedded for arm platform. Buildroot can be downloaded here.

configure buildroot to cross compile the Qt for arm platform, make sure to enable this in .config file of buildroot

BR2_arm=y
BR2_generic_arm=y
BR2_ARM_TYPE="GENERIC_ARM"
BR2_ARM_EABI=y
BR2_ARCH="arm"
BR2_ENDIAN="LITTLE"
BR2_GCC_TARGET_ABI="aapcs-linux"

Dependency packages for creating video player,

  • Qt.
  • Phonon packages.
  • gstreamer packages.


Modifications to be done on .conf files of Qt package:-

Modify qws.conf  file content like,

 QT                     += core gui network phonon

qws.conf file path  "/home/hussain/buildroot-2012.05/output/build/qt-4.8.2/mkspecs/common"

Modify  linux.conf file content like,

QMAKE_INCDIR_QT       = /home/hussain/buildroot-2012.05/output/build/qt-4.8.2/include
QMAKE_LIBDIR_QT       = /home/hussain/buildroot-2012.05/output/target/usr/lib
QMAKE_MOC             = /home/hussain/buildroot-2012.05/output/build/qt-4.8.2/bin/moc
QMAKE_UIC             = /home/hussain/buildroot-2012.05/output/build/qt-4.8.2/bin/uic

linux.conf file path  "/home/hussain/buildroot-2012.05/output/build/qt-4.8.2/mkspecs/common"

Compilation steps:-

  •  /home/hussain/buildroot-2012.05/output/build/qt-4.8.2/bin/qmake -project
  •  /home/hussain/buildroot-2012.05/output/build/qt-4.8.2/bin/qmake -spec /home/hussain/buildroot-2012.05/output/build/qt-4.8.2/mkspecs/qws/linux-arm-g++ -r
  • make
  • ./media_player <path of videos folder>  -qws
source can be downloaded from here.









Thursday, March 29, 2012

Gdb remote debugging

Gdb remote debugging :  

Gdb cross compilation: 

Gdb source can be download from here 
extract the source,
   $ tar -xvf gdb-7.4.tar.gz
enter into the gdbserver folder,
   $ cd gdb-7.4/gdb/gdbserver
cross compiling the gdbserver, make sure that arm toolchain should be in binany path.
   $ CC=arm-none-linux-gnueabi-gcc ./configure --host=arm-linux
    $ make
copy the gdbserver binary to the target machine.
   

Cross compiling the application:


   $ arm-none-linux-gnueabi-gcc -g test.c -o test
transfer the binary to the target machine

Running gdb server in target:


   # gdbserver :2345 ./test
output is like,
     Process ./test created, pid = 57
     Listening on port 2345

Running gdb in host machine:

   $ arm-none-linux-gnueabi-gdb ./test
after gdt prompt appers, 
   (gdb) set sysroot /path/to/rootfs
   (gdb) set solib-search-path /path/to/arm/toolchain/lib
example toolchain path is like,
"/home/abdul/em_linux/arm-2011.09/arm-none-linux-gnueabi/libc/lib"
   (gdb) target remote 192.168.1.2:2345
replace the ipaddress with target ipaddress
   (gdb) b main
 after this we can step through and debug the application in remote machine.

Tuesday, February 14, 2012

command line mp3 player using gstreamer

Gstreamer is an opensource multimedia framework which is widely using in various multimedia application.
It comes with bunch of codes, mux , demux etc.,
Below diagram shows how to build the pipeline using variuos plugins(in our case pipeline built for mp3 audio playing)

 
Compilation steps :

gcc player.c -o player $(pkg-config --cflags --libs gstreamer-0.10)

Run command:

./player <folder containing mp3 files>

 source code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <gst/gst.h>
#include <glib.h>
#include <pthread.h>

int flag=TRUE;

void play(char *song);
static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data);

static gboolean
cb_print_position (GstElement *pipeline);

int check_mp3(char *name)
{
    char buf[4];
    int i;

    while(*name != '\0')
        name++;
    name = name - 3;
    for(i=0;i<3;i++){
        buf[i] = *name;
        name++;
    }
    buf[i]='\0';
    return strcmp(buf,"mp3");
}

int main(int argc,char *argv[])
{
    DIR *dir;
    struct dirent *dent;
    char buf[PATH_MAX];

    if(argc != 2)
    {
        printf("Usage: ./player <mp3_songs_folder>\n");
        exit(EXIT_FAILURE);
    }
   
    if(!(dir = opendir(argv[1])))
    {
        perror("opendir()");
        exit(EXIT_FAILURE);
    }

    while((dent = readdir(dir)))
    {
        if(!check_mp3(dent->d_name)){
            snprintf(buf,PATH_MAX,"%s/%s",argv[1],dent->d_name);
            g_print("Playing :- %s\n",dent->d_name);
            play(buf);
           
        }
    }
    return 0;
}

static gboolean
cb_print_position (GstElement *pipeline)
{
    GstFormat fmt = GST_FORMAT_TIME;
    gint64 pos, len;

    if (gst_element_query_position (pipeline, &fmt, &pos)
        && gst_element_query_duration (pipeline, &fmt, &len)) {
        g_print ("Time: %lld,%lld\r",
             (long long int) pos,(long long int)len);
    }

    if(flag == TRUE)
        return TRUE;
    else
    {
        flag = TRUE;
        return FALSE;
    }
}


void play(char *song)
{
    GMainLoop *loop;
    GstElement *pipeline,*filesrc,*mad,*audioconvert,*autoaudiosink;
    GstBus *bus;
   
    gst_init(NULL,NULL);
    loop = g_main_loop_new(NULL,FALSE);
   
    pipeline = gst_pipeline_new("mp3_player");
   
    filesrc = gst_element_factory_make("filesrc","filesrc");
    mad = gst_element_factory_make("mad","mad");
    audioconvert  = gst_element_factory_make("audioconvert","audioconvert");
    autoaudiosink = gst_element_factory_make("autoaudiosink","autoaudiosink");

    g_object_set(G_OBJECT(filesrc),"location",song,NULL);
   
    bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
    gst_bus_add_watch (bus, bus_call, loop);
    gst_object_unref (bus);

    gst_bin_add_many(GST_BIN(pipeline),filesrc,mad,audioconvert,autoaudiosink,NULL);
    gst_element_link_many(filesrc,mad,audioconvert,autoaudiosink,NULL);
   
    gst_element_set_state(pipeline,GST_STATE_PLAYING);
   
    g_timeout_add (200, (GSourceFunc) cb_print_position, pipeline);
    g_main_loop_run(loop);
    gst_element_set_state(pipeline,GST_STATE_NULL);
    gst_object_unref(pipeline);
}

static gboolean bus_call(GstBus *bus,GstMessage *msg,gpointer data)
{
    GMainLoop *loop = (GMainLoop *) data;

    switch (GST_MESSAGE_TYPE (msg)) {

    case GST_MESSAGE_EOS:
        g_print ("\nEnd of stream\n");
        g_main_loop_quit (loop);
        flag=FALSE;
        break;

    case GST_MESSAGE_ERROR: {
        gchar  *debug;
        GError *error;

        gst_message_parse_error (msg, &error, &debug);
        g_free (debug);

        g_printerr ("Error: %s\n", error->message);
        g_error_free (error);

        g_main_loop_quit (loop);
        break;
    }
    default:
        break;
    }

    return TRUE;
}

Tuesday, September 27, 2011

Solar system simulation using vpython

from visual import *
import time

class solor_system:
 
    def __init__(self):
        scene.width=1000
        scene.height=800
        scene.autoscale=0
        scene.range=(100,100,100)
        scene.center=(0,0,0)
       
        self.sun = sphere(pos=(0,0,0), radius=6, color=color.red,material=materials.emissive)
        lamp = local_light(pos=(0,0,0), color=color.orange)

        self.mercury = sphere(pos=(0,0,0), radius=1, color=color.green)
        self.venus = sphere(pos=(0,0,0), radius=1.5, color=color.blue)
        self.earth = sphere(pos=(0,0,0), radius=2, color=color.yellow,material=materials.earth)
        self.mars = sphere(pos=(0,0,0), radius=2.5, color=color.orange)
        self.jupiter = sphere(pos=(0,0,0), radius=3, color=color.cyan)
        self.saturn = sphere(pos=(0,0,0), radius=3.5, color=color.magenta)
        self.uranus = sphere(pos=(0,0,0), radius=4, color=color.white)
        self.neptune = sphere(pos=(0,0,0), radius=4.5, color=color.blue)
        self.pluto = sphere(pos=(0,0,0), radius=5, color=color.cyan)
       
        self.mercury_angle = 0
        self.venus_angle = 0
        self.earth_angle = 0
        self.mars_angle = 0
        self.jupiter_angle = 0
        self.saturn_angle = 0
        self.uranus_angle = 0
        self.neptune_angle = 0
        self.pluto_angle = 0
       
    def create_orbit(self):
       
        self.mercury.trail = curve(color=color.white)
        self.venus.trail = curve(color=color.white)
        self.earth.trail = curve(color=color.white)
        self.mars.trail = curve(color=color.white)
        self.jupiter.trail = curve(color=color.white)
        self.saturn.trail = curve(color=color.white)
        self.uranus.trail = curve(color=color.white)
        self.neptune.trail = curve(color=color.white)
        self.pluto.trail = curve(color=color.white)

    def draw_ellipse(self,x, y, a, b, angle, steps,ball,i):
        if steps == None:
            steps = 36
            # Angle is given by Degree Value
        beta = -angle * (math.pi / 180); #(Math.pi/180) converts Degree Value into Radians
        sinbeta = math.sin(beta);
        cosbeta = math.cos(beta)
        
        time.sleep(0.01)
        alpha = i * (math.pi / 180)
        sinalpha = math.sin(alpha)
        cosalpha = math.cos(alpha)
       
        X = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta)
        Y = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta)
        
        ball.x=(X)
        ball.y=(Y)
               
        
        ball.trail.append(pos=ball.pos)
       

    def main(self):
        self.create_orbit()
        while True:
            self.draw_ellipse(0,0,20,10,0,None,self.mercury, self.mercury_angle)
            self.mercury_angle = (self.mercury_angle + 5) % 360
            self.draw_ellipse(0,0,25,15,0,None,self.venus, self.venus_angle)
            self.venus_angle = (self.venus_angle + 4.5) % 360
            self.draw_ellipse(0,0,30,20,0,None,self.earth,self.earth_angle)
            self.earth_angle = (self.earth_angle + 4) % 360
            self.draw_ellipse(0,0,35,25,0,None,self.mars,self.mars_angle)
            self.mars_angle = (self.mars_angle + 3.5) % 360
            self.draw_ellipse(0,0,40,30,0,None,self.jupiter,self.jupiter_angle)
            self.jupiter_angle = (self.jupiter_angle + 3) % 360
            self.draw_ellipse(0,0,47,37,0,None,self.saturn,self.saturn_angle)
            self.saturn_angle = (self.saturn_angle + 2.5) % 360
            self.draw_ellipse(0,0,55,45,0,None,self.uranus,self.uranus_angle)
            self.uranus_angle = (self.uranus_angle + 2) % 360
            self.draw_ellipse(0,0,65,55,0,None,self.neptune,self.neptune_angle)
            self.neptune_angle = (self.neptune_angle + 1.5) % 360
            self.draw_ellipse(0,0,80,70,0,None,self.pluto,self.pluto_angle)
            self.pluto_angle = (self.pluto_angle + 1) % 360


if __name__=='__main__':
    ss=solor_system()
    ss.main()

Thursday, January 13, 2011

creating shared library with simple example

1.create a separate folder to try this examples.

2. create the a main c file , another two c files which contain the functions used in the main file.

3. create the relative header file.
4.create the include folder and move your header file into that.
5.create the lib folder where we are going to place the our shared library.
6.say for example our files named as
main.c
func1.c
func2.c
lib/
include/

7.create the object file for both func1.c and func2.c using the below command

    gcc -Wall -fPIC -I include/ -c func1.c
    gcc -Wall -fPIC -I include/ -c func2.c

options:

-Wall:- include warnings
-fPIC:- Compiler directive to output position independent code, a        characteristic required by shared libraries

8.create the shared library using the following command.

    gcc -shared -Wl,-soname,libfunc.so -o libfunc.so *.o

options:

-shared:-Produce a shared object which can then be linked with other objects to form an executable.
-Wl:-Pass options to linker(eg--soname,libfunc.so)

9.move that created library file to lib folder
10.create  the  final executable using command

     gcc -Wall -I include/ -L lib/ -lfunc  main.c -o  main

11.normally when executing the final executable file, linker will look into the  /lib, so we have include our library path in the default searching path.


   export LD_LIBRARY_PATH=/path/to/our_lib_folder
12.now run the final executable..

Wednesday, January 12, 2011

Music and Browsing with emacs

Play music with emacs:


  •       Download the mpg123 player using apt-get install command.
  •       Download the emacs lisp function source code from here.
  •       Untar the source, inside the directory you will find the file called 'mpg123.el'.
  •       Copy that file and paste into the folder /usr/share/emacs/site-lisp.
  •       Create the file .emacs into the home folder.
  •       Add the following text in that (autoload 'mpg123' "mpg123" "A Front end to mpg123" t)
  •       Save that file and open the emacs.
  •       Enter the key stroke M+x mpg123 and you will ask for the folder where  the music files are available.


Browsing with emacs:


 w3m is text based browser can be used in terminal .
  •      Download 'w3m-el' package using apt-get command.
  •      Open emacs and enter the key stroke M+x w3m.
  •      It will ask for the url and enter the url.
  •      Now you can browse through the text version of web page.

Thursday, November 25, 2010

TicTacToe game using Python

import sys

class TicTacToe:
    def __init__(self,usr_input=None,x=None,y=None):
        self.matrix=([0,0,0],[0,0,0],[0,0,0])
        self.usr_input=usr_input
        self.x=x
        self.y=y
# Function to display the tictactoe grid
    def show_grid(self):
        """ Display the grid layout in the standard output
        There is no arguments and no return types"""
        for i in (0,1,2):
            for j in (0,1,2):
                if self.matrix[i][j]==0:
                    sys.stdout.write("   ")
                elif self.matrix[i][j]==4:
                    sys.stdout.write(" X ")
                else:
                    sys.stdout.write(" 0 ")
                if j!=2:
                    sys.stdout.write("|")
            if i!=2:
                print " "
                print "---|---|---"
        print " "
# Function to get the input from the user
    def get_input(self):
        while True:
            try:
                self.usr_input=str(raw_input("Enter the x and y axis:"))
                if (len(self.usr_input)>3):
                    print ("invalid input!")
                    continue
                else:
                    self.x,self.y=self.usr_input.split(",")
                    self.x=int(self.x)
                    self.y=int(self.y)
                    break
            except:
                print "invalid input!"
                continue
    def user_primary_input(self,mark):
        while True:
            self.get_input()
            try:
                if ((self.matrix[self.x-1][self.y-1]==4) or (self.matrix[self.x-1][self.y-1]==3)):
                    print "invalid move!"
                    return 0
                else:
                    self.matrix[self.x-1][self.y-1]=mark
                    return 1
            except:
                print "out of range"
                continue
# Fuction to check the winning status
    def check_win(self):
        for j in (0,1,2):
            if (self.matrix[j][0] + self.matrix[j][1] + self.matrix[j][2]==12):
                return 0
            elif (self.matrix[j][0] + self.matrix[j][1] + self.matrix[j][2]==9):
                return 1
            if (self.matrix[0][j] + self.matrix[1][j] + self.matrix[2][j]==12):
                return 0
            elif (self.matrix[0][j] + self.matrix[1][j] + self.matrix[2][j]==9):
                return 1
        if (self.matrix[0][0] + self.matrix[1][1] + self.matrix[2][2]==12):
            return 0
        elif (self.matrix[0][0] + self.matrix[1][1] + self.matrix[2][2]==9):
            return 1
        if (self.matrix[2][0] + self.matrix[1][1] + self.matrix[0][2]==12):
            return 0
        elif (self.matrix[2][0] + self.matrix[1][1] + self.matrix[0][2]==9):
            return 1
# Function to check the draw status
    def check_draw(self):
        count=0
        for i in (0,1,2):
            for j in (0,1,2):
                if self.matrix[i][j]==0:
                    count=count+1
        if count==0:
            return 0
        return 1
# Initial movement of the player
    def start_game(self,mark,player,player_win):
        win=0
        while True:
            print "player " +str( player) + ":"
            valid=self.user_primary_input(mark)
            if valid==0:
                continue
            win=self.check_win()
            if win==player_win:
                self.show_grid()
                print "player " +str(player) + " won the game"
                return 1
            x=self.check_draw()
            if x==0:
                self.show_grid()
                return 2
            self.show_grid()
            break
        return 0
def main():
    t=TicTacToe()
    status=0
    first_player_mark=4
    second_player_mark=3
    player_one=1
    player_two=2
    print "your game starts:"
    t.show_grid()
    while True:
        status=t.start_game(first_player_mark,player_one,0)
        if status==1 or status==2:
            break
        status=t.start_game(second_player_mark,player_two,1)
        if status==1 or status==2:
            break
    if status!=1:
        print "Game draw!"
    return

   
if __name__=='__main__':
    main()