Saturday, February 27, 2010

Why can you write arr[n] == n[arr] in C ?

Try the following program in C :

#include <stdio.h>

int main () {
  int arr[] = {42};
  printf("%d", arr[0] == 0[arr]);
  return 0;
}

The above program prints out 1...ie, true.

But why...?

Well the reason the above works is because of how arrays are represented in C. In C, when referencing an array, you're just referencing a pointer to the first element :

int arr[] = {42, 39};
int x = arr[0];
int y = *(arr + 0);
// x == y

In the *(arr + 0) example, we are accessing the first element of the array by dereferencing the pointer to the first element arr plus the 0 offset.

int arr[] = {42, 39};
int x = arr[1];
int y = *(arr + 1);
// x == y

In the above example, we are accessing the second element with index 1.

Therefore, if *(arr + 1) evaluates to arr[1], then surely *(1 + arr) evaluates to 1[arr] according to elementary math...because addition is commutative, which means that the order of the operands does not matter, as it produces the same result.

The reason for this is because since C was designed back in the 70s, computers did not have much memory and thus the C compiler didn't do much syntax checking. Therefore, something like arr[i] was blindly translated to *(arr + i)

Wednesday, February 10, 2010

Finding the logarithm of any base in Java

Unfortunately, the only methods in Java that compute the logarithm of a number are java.lang.Math.log and java.lang.Math.log10. The former returns the natural logarithm (base e) of the number and the latter returns the base 10 logarithm of the number.

To calculate the logarithm of any base in Java, we thus have to use the following method:

public double logOfBase(int base, int num) {
    return Math.log(num) / Math.log(base);
}

Why does it work?


Let's say we want to find the base 2 logarithm of 32 (method call would be logOfBase(2, 32)), which is 5.


Now if we divide the base e logarithm (ln) of our number (32) by the base e logarithm of our base (2), we get the answer we wanted:



Wednesday, February 3, 2010

Scrolling page title with JavaScript and jQuery

The following is a jQuery function I wrote that allows you to make a scrolling page title.

I admit...it's a pretty useless "feature" but I just made it to kill some time =D

Usage

The simplest usage is as follows:

$.marqueeTitle();

The above snippet will use your existing title text to scroll.

You can also pass in an object with options to alter the script's behavior. The options are the following:
  • text - Use this parameter to set custom text if you don't want the scrolling text to be taken from the title
  • dir - "left" or "right"; by default, it's set to "left"
  • speed - The time it takes, in ms, for one character rotation

Here's another example, now demonstrating the parameters:

$.marqueeTitle({
  text: "This my custom text",
  dir: "right",
  speed: 500
});

Source

(function ($) {
    var shift = {
        "left": function (a) {
            a.push(a.shift());
        },
        "right": function (a) {
            a.unshift(a.pop());
        }
    };
    $.marqueeTitle = function (options) {
        var opts = $.extend({},
        {
            text: "",
            dir: "left",
            speed: 200
        }, options),
            t = (opts.text || document.title).split("");
        if (!t) {
            return;
        }
        t.push(" ");
        setInterval(function () {
            var f = shift[opts.dir];
            if (f) {
                f(t);
                document.title = t.join("");
            }
        }, opts.speed);
    };
}(jQuery));

Thursday, January 28, 2010

Connecting a PS2 Keyboard to an Arduino

In this post, I will talk about how I connected and interfaced with a PS2 Keyboard using an Arduino. First we'll start with the basics; Soldering the PS2 port onto a strip board.

Connecting the PS2 port to the Strip Board

I soldered the PS2 port in front of a row of 6 female pin headers:



This setup will allow me push in wires into the headers to access the PS2 Port.

The following is a view from underneath, where the solder joints are:




As you can see, I had to cut two tracks. This is because of the way the setup of the pins of the PS2 port is:



The tracks need to be cut where there are the red lines on the above picture.

Connecting it to the Arduino


Taking a look at the pins again:



We will be using the above four (colored) pins for our project (the pin on the right of the VCC pin and the one below the VCC pin will not be used).

Next push in wires to the 6 female headers as follows (Black, Yellow, White and Red) :




Next we need to connect the wires to the Arduino as follows:

Wire
Arduino Pin
Red
5V
Black
GND
Yellow
Pin 3 (Digital)
White
Pin 4 (Digital)

When we push wires into the female headers and to the Arduino, we will have something like this:



Now all that's left is to plug in the Keyboard to the PS2 Port.

Interfacing with the Arduino

To read the keystrokes with the Arduino, I used the following: PS2KeyboardExt2, which is basically an extension of this library: PS2Keyboard.

So first download PS2Keyboard and extract the library to your /hardware/libraries/ folder and then download PS2KeyboardExt2 and overwrite the PS2Keyboard.h and PS2Keyboard.cpp files with the latter version.

The following is the code that you can use to capture the key strokes:

#include <PS2Keyboard.h>

// Simple test program for new PS2Keyboard library
// Connect a PS2 keyboard to pins 3 & 4 (CLK and DATA respectively) and supply 5V to the keyboard
// For examples, see here: http://www.arduino.cc/playground/ComponentLib/Ps2mouse
// or                here: http://www.beyondlogic.org/keyboard/keybrd.htm
// That second article is a great place to start if you want to understand whats going on
//
// When you've compiled the code and uploaded it to the board, start a serial monitor at
// 9600bd.  Then press keys on your PS2 keyboard (the one connected to Arduino, not the one
// connected to your computer!) Try using <shift>, <ctrl> and <alt> keys
// and check that the caps_lock key sets the caps_lock light.
// Pressing <esc> key should reset the keyboard and you should see all 3 lights go on briefly.

#define KBD_CLK_PIN  3
#define KBD_DATA_PIN 4

PS2Keyboard keyboard;

void setup() {
  keyboard.begin(KBD_DATA_PIN);

  Serial.begin(9600);
  delay(1000);
}

#define is_printable(c) (!(c&0x80))   // don't print if top bit is set

void loop() {

  if(keyboard.available()) {

    // reading the "extra" bits is optional
    byte   extra = keyboard.read_extra(); // must read extra before reading the character byte
    byte       c = keyboard.read();

    boolean ctrl = extra & 1;  // <ctrl> is bit 0
    boolean  alt = extra & 2;  //  <alt> is bit 1

    if (ctrl) Serial.print('^');
    if (alt)  Serial.print('_');

    if      (c==PS2_KC_UP)      Serial.print("up\n");
    else if (c==PS2_KC_DOWN)    Serial.print("down\n");
    else if (c==PS2_KC_BKSP)    Serial.print("backspace\n");
    else if (c==PS2_KC_ESC)   { Serial.print("escape and reset\n"); keyboard.reset(); }
    else if ( is_printable(c) ) Serial.print(c);   // don't print any untrapped special characters
  } 
}

Notice that directives KBD_CLK_PIN (clock pin) and KBD_DATA_PIN (data pin) are set to 3 and 4 respectively, and this corresponds to the way we hooked the PS2 port to the Arduino before.

Now after you compile and upload, open up the Serial Monitor and start typing. You should get something like the following:




Sunday, January 24, 2010

Unit Testing for Exceptions with Visual Studio 2008

The following are the two different ways I've unit tested for Exceptions with Visual Studio 2008.

Using Try / Catch

There first way of testing for Exceptions is by using the try-catch construct:

[TestMethod]
public void TestCard()
{
 var c = new CreditCard();
 try
 {
  c.CardNo = "123"; //Should throw an exception
  Assert.Fail("InvalidCardNoException wasn't thrown");
 }
 catch (InvalidCardNoException)
 {
  Assert.IsTrue(true, "InvalidCardNoException was properly thrown");
 }
}

As you can see from the above example, if the exception isn't thrown, Assert.Fail is called which fails the test; and if it is thrown, we succeed the test with Assert.IsTrue.

But Visual Studio 2008 offers a better way on how to deal with exceptions, by using the ExpectedException Attribute.

Using the ExpectedExceptionAttribute Class

Here is the same example, but this time using the ExpectedException Attribute:

[TestMethod, ExpectedException(typeof(InvalidCardNoException))]
public void TestCard()
{
 var c = new CreditCard();
 c.CardNo = "123"; //Should throw an exception
}


Friday, January 22, 2010

Wrapping column text in a table in LaTeX

Let's say you have the following table in LaTeX:

\begin{tabular}{|l|l|}
  \hline
  This is a very very long cell name.  Very long indeed 
  & This is a very, very, very long line that keeps on going even outside of the page!!!!! \\
  \hline
\end{tabular}

The output generated from the above tabular environment is as follows:



As you can see from the above output, the text does not wrap but just keeps going off out of the page.

Introducing the TabularX package


To fix this problem, we will use the tabularx package and use the tabularx environment:

\usepackage{tabularx}

\begin{tabularx}{\linewidth}{|X|X|}
  \hline
  This is a very very long cell name.  Very long indeed 
  & This is a very, very, very long line that keeps on going even outside of the page!!!!! \\
  \hline
\end{tabularx}

The generic template of the tabularx environment is as follows:

\begin{tabularx}{w}{\X\X . . .\}

where:
  • w : the desired width of the table
  • X : marks each column for which the width is to be adjusted

This is now the output with the tabularx environment:





Saturday, January 16, 2010

Indenting an item in an enumerate environment in LaTeX

To indent an item in a list in LaTeX, we will use the following two commands:

\setlength\itemindent{size}

And we will use it like such:

\begin{enumerate}
\item This item is not indented
{\setlength\itemindent{25pt} \item This item is indented}
\item This item is not indented
{\setlength\itemindent{25pt} \item This item is indented}
{\setlength\itemindent{25pt} \item This item is indented}
\item This item is not indented
\end{enumerate}

This will generate the following:





We can improve the above code by making use of the \newcommand to encapsulate \setlength and \itemindent into a single command :

\newcommand{\indentitem}{\setlength\itemindent{25pt}}

\begin{enumerate}
\item This item is not indented
{\indentitem \item This item is indented}
\item This item is not indented
{\indentitem \item This item is indented}
{\indentitem \item This item is indented}
\item This item is not indented
\end{enumerate}