Thursday, 4 August 2011

My .emacs configuration file

This post describes my .emacs configuration file.

.emacs configuration file is loaded by emacs editor when it starts.

Let's divide the .emacs configuration file into parts to describe it better:

Customized variables
Install user customizations of variable values specified in arguments.

(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(column-number-mode t)
 '(desktop-save-mode t)
 '(display-battery-mode t)
 '(display-time-24hr-format t)
 '(display-time-day-and-date nil)
 '(display-time-mode t)
 '(fringe-mode 0 nil (fringe))
 '(menu-bar-mode nil)
 '(network-speed-format-string " [%NI %AX %AB] ")
;; '(network-speed-interface-list (list "eth0" "eth1"))
 '(network-speed-interface-list (list "eth1"))
 '(network-speed-precision 1)
 '(network-speed-update-interval 2)
;; '(org-agenda-files (quote ("~/org/task_list.org")))
 '(scroll-bar-mode nil)
 '(size-indication-mode t)
 '(tool-bar-mode nil)
 '(tooltip-mode nil)
 '(w3m-session-load-last-sessions t))

Wednesday, 20 July 2011

Calculate Prime Numbers using an ARDUINO Board

Following example uses an Arduino board to calculate a prime number list, starting from one.
It shows the result using serial port communication.

BUILDING AND RUNNING THE CODE


You should copy this code into your Arduino IDE window, compile it (C-R), upload the code into the board (C-U) and to see the result open the serial monitor (C-Shift-M).

If everything goes fine your serial monitor will show a prime on each line every time it is calculated. Maximum prime able to be calculated depends on long type maximum value.

long number = 3;

void setup()
{
  Serial.begin(9600);
  Serial.println("#");
  Serial.println("1");
  Serial.println("2");
}

void loop()
{
  int is_prime = 1;
  long i = 3;
  long top = sqrt(number);
  while (i<top)
  {
    if (number%i == 0)
    {
      is_prime = 0;
      break;
    }
    i++;
  }
  if (is_prime)
  {
    Serial.println(number,DEC);
  }
  number += 2;
}

Within setup function, it shows 1 and 2 primes. It then starts calculating in the main loop using number 3.

Primality test here consists on performing divisions and checking the remainder. It is not needed to test with every divisor, when square root is reached we know it is a prime.

When a prime number is found it is sent over serial line.

As we start calculations with number three, we can skip all even numbers.


YOU MAY ALSO BE INTERESTED IN:


Install and run a program in Arduino using 64 bits Ubuntu (lucid) distro

Wednesday, 6 July 2011

Install and run a program in Arduino using 64 bits Ubuntu (lucid) distro

From Arduino home page:
Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software.

Arduino provides a Java based IDE and examples are written in C language: http://arduino.cc/en/Reference/HomePage


MY SYSTEM consists on:

Arduino Duemilanove: An ATmega328 running at 16 MHz with auto-reset. It connects to the computer using a USB cable.

Ubuntu distro:
Release: 10.04
Codename: lucid

Linux kernel
2.6.32-32-generic
x86_64 AMD 64 bits architecture.


INSTALLING ARDUINO IDE SOFTWARE


First we install needed dependencies:
  • openjdk-6-jre
  • avr-gcc (aka "gcc-avr")
  • version 4.3.2 or later 4.3.x version (4.4.x and 4.5.x seem to have various problems)
  • avr-libc

NOTE: On the contrary to what Arduino page said, I did not need avr-gcc-c++.

$ sudo aptitude install openjdk-6-jre gcc-avr avr-libc
NOTE: gcc-avr version is 4.3.4 in my system


DOWNLOADING ARDUINO IDE

Monday, 25 April 2011

Installing and Configuring ANYTHING.EL for Emacs

anything.el is an application framework written by TamasPatrovics (current maintainer is rubikitch).
It provides a better way to choose between alternatives when typing a command, switching buffers, searching for help, files, etc.
After you select a candidate option from the list anything provides, you can choose between several actions.

DOWNLOAD ANYTHING
We are going to download anything from its source code repository.
$ cd ~/.emacs.d
$ git clone git://repo.or.cz/anything-config.git

CONFIGURE .emacs FILE

;; Anything.el
(add-to-list 'load-path "~/.emacs.d/anything-config/")
(add-to-list 'load-path "~/.emacs.d/anything-config/extensions/")
(require 'anything-startup)

That it is enough for using anything:
e.g: M-x anything

Also common functions like switch-to-buffer are automatically remapped to anything ones.


However you could also customize your anything buffers, selecting the anything sources you want:

Saturday, 19 March 2011

Simple Python IRC Bot

This article shows the code of a simple python irc bot.

To run it, you should create a file, e.g: pybot.py, copy following code in it, and exec:
$ python pybot.py

other way to run it:
$ chmod a+x pybot.py
$ ./pybot.py

BOT SOURCE CODE

Thursday, 27 January 2011

Calculate prime numbers using PHP

Simply create a file named primeNumberCalculator.php. Copy following text in it.

You need a web server supporting php scripting.
Place the file where your http web server see it. e.g: /var/www

Open the file in a web browser. e.g: http://localhost/primeNumberCalculator.php

<html>
<body>

<h2>PHP Prime number calculator</h2>
<form name="input" action="primeNumberCalculator.php" method="get">
How many primes?: <input type="text" name="count" />
<input type="submit" value="Submit" />
</form>
<?php

// How many prime numbers are we going to calculate:
$count=$_GET["count"];
if ($count == "") {
   $count = 10;  // Default value
}

// We show the first prime number:
$prime_array=array("2");
echo $prime_array[0];
echo " ";

$total=1; // prime number array length.
$number = 3; // current number we are going to test for primality.

$i=1; // We start with 1 because first prime is already printed.
while ( $i<$count ) {
   $index=0;
   $is_prime="true";
   $max = floor(sqrt($number));
   $n = $prime_array[$index];
   while(($n <= $max) && ($is_prime == "true")) {
      if (($number % $n) == 0) {
         $is_prime="false";
      }
      $index++;
      $n = $prime_array[$index];
   }
   if ($is_prime=="true") {
      echo " " . $number;
      $prime_array[$total] = $number;
      $total++;
      $i++;
   }

   $number += 2;
}

?>

</body>
</html>

Wednesday, 12 January 2011

Calculate a PRIME NUMBER list using JAVA

This article shows how to write, compile, build and execute a program that calculates a list containing the first prime numbers using Java language.

We are going to create a source code file named CalculatePrimeNumbers.java
Then we copy/paste next programm in that file.

NOTE: Name is important, because in Java language file name has to match the class it contains.

import java.util.ArrayList;

/**
 * Description of CalculatePrimeNumbers class.
 */
public class CalculatePrimeNumbers {

    // By default it shows 50 first prime numbers.
    private static final int MAX_PRIMES = 50;


    /**
     * @param args the command line arguments
     * This function accepts zero or one arguments.
     * When called with no arguments it shows 50 first prime numbers.
     * If we specify a number, it shows that a prime number array with that specified length.
     */
    public static void main(String[] args) {
        int top;

 // Parse command line arguments.
        switch(args.length){
            case 0:
                top = MAX_PRIMES;
                break;
            case 1:
                top = Integer.parseInt(args[0]);
                break;
            default:
                System.err.println("ERROR: TOO MANY ARGUMENTS.");
                return;
        }
        System.out.println("We are going to calculate " + top + " first prime numbers.");


 ArrayList<long> primes = new ArrayList<long>();
        primes.add(2L); // 2 is the first prime number we get.
 long index = 3; // Calculations start for number 3.

        System.out.println("Prime number list:");
        System.out.print(2 + " ");
        while (primes.size() < top) {
            boolean isPrime = true;

     for(long n:primes) {
                if ((index % n) == 0) {
                    isPrime = false;
                    break;
                }
            } // End for.

            if (isPrime) {
                // index stores a prime number.
                primes.add(index);
                System.out.print(index + " ");
            }

            index++;
        } // End while.
       
    } // End main.

}
Java is a programming language that is compiled into bytecodes.

Wednesday, 25 August 2010

Beginning with Qt: Introduction, Installing Qt Framework, Qt Hello world

INTRODUCTION TO Qt

Qt is a complete development environment widely used to create GUI applications.

This cross-platform tool is also able to create non gui apps.

Qt is used in the KDE desktop environment, and in other projects like virtualbox.


Qt development environment provides: (version numbers when this article was written)

  • Qt libraries (version 4.6.3)
  • Qt Creator IDE (version 2.0.0)
  • Qt development tools


Qt is available under LGPL and GPL free software licenses. Nokia also provides the Qt commercial license to develop proprietary software.


Qt DEVELOPMENT TOOLS

Qt environment provides us with some development tools. e.g:

qmake
qmake tool helps us creating and managing .pro project files, and from them it creates Makefile files needed to build our projects.
qmake Manual
qmake Tutorial

Qt assistant
Qt assistant tool shows all available Qt help. It is a help browsing system.
Qt Assistant Manual

Saturday, 26 June 2010

Creating a TRIVIAL DEBIAN REPOSITORY

This howto shows the way to create a trivial debian repository. Just a simple collection of deb package files.

There are two debian repository types: automatic and trivial.

Automatic repos are more complex. We are going to deal with trivial ones.

A trivial repository is composed of a root directory and one or more subdirectories.
No database server is needed.

This trivial repository is good enough to host a few packages, e.g creating a repo with your /var/cache/apt/archives deb packages and burn a cd or store them in a removible drive.

Trivial repos do not follow the standard debian repo directory structure, so we will have to specify the exact path in sources.list file.


CREATING MY TRIVIAL REPO STRUCTURE:

Our repo will consist of MyRepo root directory with binary subdirectory.

$mkdir -p /path/to/MyRepo
$cd /path/to/MyRepo
$mkdir binary

We place there the deb packages our repo will provide.
I.e: we copy packages from the apt cache:
$rsync -vP /var/cache/apt/archives/*.deb /path/to/MyRepo/binary
$cd .. # we move to repo root directory.


CREATE PACKAGES CONFIGURATION FILE

Sunday, 2 May 2010

How to BUILD the LINUX KERNEL for the ANDROID EMULATOR (Eclair version)

DOWNLOAD THE KERNEL SOURCE CODE


First we download the kernel source code from https://android.googlesource.com

Within that page there are kernels for other platforms too. We choose to download kernel/goldfish project from there.

$ git clone https://android.googlesource.com/kernel/goldfish

A goldfish directory appears:
$ cd goldfish
This directory does not contain any source code in it.

We check which branch we have downloaded:
$ git branch
it shows * master , not the one we are searching for:

To list all remote available branches:
$ git branch -r
or
$ git remote show origin
origin/HEAD -> origin/master
  origin/android-goldfish-2.6.29
  origin/android-goldfish-3.4
  origin/linux-goldfish-3.0-wip
  origin/master


What does goldfish mean? (from android-kernel mail list)
Goldfish is the kernel hacked branch that supports the qemu based arm emulator for android, so it is the one we need.

Download GOLDFISH kernel version
Choose the version that suits you.
$ git checkout --track -b android-goldfish-2.6.29 origin/android-goldfish-2.6.29
$ git branch
* android-goldfish-2.6.29
  master


RUNNING THE EMULATOR


Within this link we will find how to get the android emulator, and launch it.
Building Android in Debian Sid

Showing the kernel version running in the emulator
$ adb shell
# cat /proc/version
Linux version 2.6.29-00261-g0097074 (digit@digit.mtv.corp.google.com) (gcc version 4.4.0 (GCC) ) #14 Tue Feb 2 15:49:02 PST 2010


OBTAINING KERNEL CONFIGURATION


We are going to obtain the kernel configuration .config file from within our running emulator.

$ cd /path/to/goldfish # we enter in the kernel source directory.
$ adb pull /proc/config.gz . # get compressed .config file from the emulator.

$ gunzip config.gz # uncompress it.
$ cp config .config # rename it into .config

Now you can edit .config file the way it suits you the most.


BUILDING AND COMPILING THE KERNEL


CROSS_COMPILE environment variable stores the path to the arm cross compiling toolchain. I use the one which comes with android source code.

$ ARCH=arm CROSS_COMPILE=/path/to/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi- make

Executing make will build the kernel.

Last lines will show:
Kernel: arch/arm/boot/Image is ready
Kernel: arch/arm/boot/zImage is ready


So we have obtained Image and zImage kernel binary files.


RUN THE EMULATOR USING THE NEW COMPILED KERNEL IMAGE


We need -kernel option:
$ emulator -kernel /path/to/goldfish/arch/arm/boot/zImage -show-kernel -verbose

We can check now the kernel version:
$ adb shell
# cat /proc/version
Linux version 2.6.29-00262-gb0d93fb (user@myPC) (gcc version 4.4.0 (GCC) ) #1 Sun May 2 14:27:31 CEST 2010


If we do not specify kernel option it usually uses the prebuilt one:
$ emulator -show-kernel -verbose
emulator: argv[01] = "-kernel"
emulator: argv[02] = "/path/to/mydroid/prebuilt/android-arm/kernel/kernel-qemu"


ACTIVATING MODULE LOADING SUPPORT IN THE KERNEL


Module loading support is previously disabled in the kernel, if we want to load modules in the kernel we have to enable it:

edit .config file and set:
CONFIG_MODULES=y

$ ARCH=arm CROSS_COMPILE=/path/to/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi- make

I am asked about some options when executing make. I ask yes for module related options.

After compiling I see several modules have been built.

MODPOST 6 modules
CC      drivers/video/fb_sys_fops.mod.o
LD [M]  drivers/video/fb_sys_fops.ko
CC      drivers/video/syscopyarea.mod.o
LD [M]  drivers/video/syscopyarea.ko
CC      drivers/video/sysfillrect.mod.o
LD [M]  drivers/video/sysfillrect.ko
CC      drivers/video/sysimgblt.mod.o
LD [M]  drivers/video/sysimgblt.ko
CC      drivers/video/vfb.mod.o
LD [M]  drivers/video/vfb.ko


We can upload the modules in the emulator and install them:
$ adb push drivers/video/fb_sys_fops.ko /data
$ adb shell
# insmod /data/fb_sys_fops.ko


REFERENCE


http://www.cianer.com/androidg1/28-building-android-kernel-images


YOU MAY ALSO BE INTERESTED IN:


Building Android in Debian Sid