Sunday, 15 July 2012

Arduino tutorial

we intend to dump small codes onto arduino in linux,

We will use roboduino which is similar to arduino

Start by Downloading the IDE for Arduino boards, namely Arduino 1.0.1 from
http://arduino.cc/en/Main/Software

Open the IDE

Connect the Arduino Board using the USB cable provided with the
board


Go to Tools Serial Port to make sure that the Board has been recog-
nised. Select the appropriate port from the list. Also select the right
board from Tools Board. Roboduino uses Arduino Duemilanove. The
board given to us was Arduino Duemilanove with ATMega328 proces-
sor (If you are using any other board with different processor then all you have to do is choose the appropriate one from the list).

Go to File Examples 1.Basics Blink . This will open the blink pro-
gram from the list of sample programs provided with the IDE

Compile the program either by going to Sketch Verify/Compile or by
pressing Ctrl+R . Correct any errors that may appear. Since this is a
preloaded example program, it should compile without errors.

Go to File Upload to upload the program to the board or press
Ctrl+U.


Arduino led blink code

int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH);
// turn the LED on (HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(led, LOW);
// turn the LED off by making the voltage LOW
delay(1000);
// wait for a second
}

Code transfered to arduino board.

Monday, 2 July 2012

system crash recovery

I had mistakenly used the killer command dd on dev/sda.Which caused my grub to obliterate and the partition table as well completely disappeared ,hence i was unable to boot my system ,i had windows as well as ubuntu running on separate partition;fearing the worst i searched for ways to recover data on web.
I hence state here the complete method to recover data from your system in case you face the same problem.
To begin with you have to use a live CD or live USB ,the live indicates that the OS can be booted from them.This also ensures that you do not overwrite any data on your hard disk as you are using a bootable usb or cd.
Plugin the Live USB or insert the Live CD and restart your system ,you may have to press f12 or any equivalent key to provide booting priority.
We shall be using the Ubuntu OS .Once the OS boots up ,open a terminal and type:

$ sudo su 
 This makes you a super user.
then type:

$ sudo apt-get install testdisk

The testdisk checks for partitions on the harddisk and the detected partition can then be saved and once rebooted with the Live CD/USB the partitions are then visible and can be mounted which then can allow the user to access the data from the harddisk,itself.

upon downloading the testdisk ,start it by the command
$ testdisk

This starts the programme ,you can choose to create a new log file.
As the next step choose from the list your hard disk,observe the size of the media presented and determine your harddisk from its size.
After choosing your harddisk proceed ,you then have to select the partition table type,the software will choose the most probable partition table type,however you can be extra careful and determine the same from other sources.

Then select analyze which will start scanning your harddisk for any information on partition, After some time when it is done with scanning it provides a list of partition,you can recollect the partition of your system ,if it matches select write,which causes the partition to be saved.If however the provided partition does not match then select deeper search ,which forces the software to search for the partition information .

Once it is done reboot your system and then observe that your drives have been mounted and you can access the data!!

If the deleted data needs to be recovered then use photorec
download it by 
$ sudo apt-get install photorec
and use it by

$ photorec

You then have to provide the appropriate file type you are searching for and viola it finds all the deleted files of the indicated type for you in the chosen destination folder.

  Also there may be possibility of recovering the grub or bootloader it self,but for the provided steps please use extreme caution before using them.

If you have windows and linux partitions on the same machine,and if the bootloader or grub has been missing then you may recover by using the following steps .
use $ fdisk -l
To check the partition on your system,identify the dev/sdx which corresponds to the linux partition x may be 1,2,3, and so on.
Once you have identified ,you have to mount it onto the live cd/usb by using
$ sudo mount /dev/sdx /mnt

lets mount the boot as well

$ sudo mount /dev/sdx /mnt/boot

and then we can use bind to make a mirror image of the linux partition on the live cd/usb by using 
$ sudo mount --bind /dev /mnt/dev


now you can access all your data on the linux partition as the /mnt will replicate the root of your linux.


we will now switch root to /mnt
$ sudo chroot /mnt


we will install grub here by
$ sudo grub-install /dev/sda


Reboot the system and viola !! the purple screen greets us with our favorite OS's.

Wednesday, 16 May 2012

Latex tutorial,sample code included

Latex is a really pretty tool used for creating all sort of document viewing format mainly pdf's .Yes there exists tools for converting MS word to pdf but its not as flexible as Latex ,By latex you have total control on just everything.

Latex can be used in linux OS ,for that you may have to install kile .
Kile provides gui tools so that you can just click and the corresponding codes are incorporated into the latex.

Generally another counterpart of kile is dia diagram editor ,using which you can just do any diagrams with built in symbols to assist you ,so again it has gui help for the user.

I will not dwell into deeper waters as the web is filled with ubiquitous help for latex and dia.
Latex can also be used for making ppt slides .

A sample latex format for presentation slides are provided here

A ieee latex format is provided here

If you encounter any errors when using this format,that may be because you may not have IEEEtran.cls in your working directory , download from here

Sunday, 29 April 2012

Write your own shell in c

The main shell program which uses fork and exec to fork a process when the user provides a command :download
The shell itself can be forked ,thus multiple terminal can be created and program runs in each of the terminal :


#include<stdio.h>
int main()
{char c;
while(1)
{printf("want to login ? press enter\n");
scanf("%c",&c);
if(fork()==0)
{
system("xterm -e 'path-to-the-executable file of shell program' ");

}
}
}

save it as say main.c


Here create the executable for the shell program after downloading
> gcc filename -o executablename

then put the path to the executable in the above script then run it as

>gcc main.c -o main
>./main

The shell program involves the use of pushd,popd and dirs command which are used to change the directory "pushd directory-path " will take you to that directory and "popd " takes you back to the previous directory
dirs provides the list of directory which is in the stack ,which you have pushed

Also there exist option of altering the path variable wherin "path + path-name" will add the path to the shell and "path - path-name" will remove it from shell
and "path" gives the list of path.The provision of path allows the shell to search for executables at that path.Thus when the user provides a command at the terminal it exhaustively searches for any executable file at all the path list specified corresponding to the user terminal command.




Executing the shell: password and user name is stored in a file named passwords.txt ,note the usage of path and pushd popd options .
pushd , popd allows you to change the directory and go back to the older one similar to the implementation of a stack
The path allows you to specify the location of executable,note i have specified path as /bin which contain all the executables thus allowing ls command to work.
Also in the current directory i have stored my own c file generated executables such as ls1 which does the ls operation.

 Thus you can create your own c files and save the executables in the current or path specified directory and use the same in your shell program.

Happy shelling !!

<A tutorial on shell will be provided shortly>

Friday, 27 April 2012

How to call C functions in python ,A tutorial of swig

Here we shall use a binary search tree c program and invoke the same from python

The c function file : download here , name it as bst.c
The swig interface file : download here , name it as bst.i
The make file would be : saved as mf


all:bst.o bst_wrap.o
    ld -shared bst.o bst_wrap.o -o _bst.so    
    python p.py   

bst_wrap.c:bst.i   
    swig -python bst.i

bst.o:bst.c
    gcc -c bst.c  -o bst.o  -I/usr/include/python2.6
   

bst_wrap.o:bst_wrap.c
    gcc -c bst_wrap.c -o bst_wrap.o -I/usr/include/python2.6
   
  
kindly note that the path to python directory and the version may be different for you .



The python p.py file :

import bst
bst.bst()





Execute the make file  as > make -f mf


But with the make file:











<A tutorial and explanation for all stuff's mentioned here will be provided asap>