Saturday, 24 December 2011

Git Command Quick Reminder

NOTE: You can find an updated version of this article in Git Command Quick Reminder


INSTALLING GIT
$ aptitude install git-core # git basic tools.

NOTE: In Debian Unstable
$ aptitude install git


gitk graphic tool.
$ aptitude install gitk # this graphic tool allows us to browse a git repo.



INITIALIZE A REPOSITORY

$ git init # Creates an empty repository in current directory, or reinitializes an existing one.
$ git init directory_name # Creates an empty repo in directory_name directory.

$ git clone remote_url # clone a remote repo. Passing as argument a local directory this command clones a local repo.

$ git clone --depth 1 git://git.sip.router.org/sip-router kamailio # clones sip-router source code, creating a local directory named kamailio. "depth 1" option means it only takes one revision history. That reduces the amount of history to download, but it disallows cloning, fetching, pushing or pulling from this repo. Then, it is only suitable to generate patches.

$ git clone source_repo dest_repo # clones a local repository source_repo in another local repository dest_repo,
if both are directories.
$ git clone file:///path/to/source/repo file:///path/to/dest/repo # another way to specify local directories.
This way we are sure git does not use hard links when cloning source repo.
$ git clone --no-hardlinks source_local_repo dest_local_repo # clone a repository and copy object files instead of using hardlinks for local repo objects.


GIT SVN

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.