C: Initialize multiple variables in same declaration statement

First thing first, never initialize nor declare multiple variables on the same line. It makes things very difficult to understand. But if you have to, this post will help you.

Initializing multiple variables in same line like this:

int p, q = 10;

is ambiguous because it is hard to understand if only q is initialized to 10 or if p is also initialized to 10. Of course, in this case only q is initialized to 10. To avoid the ambiguity, each initialization can be done in its own line.

int p;
int q = 10;

Also, try to initialize automatic variables (especially pointer variables) when they are declared.

C: declaring multipe pointer variables in same declaration statement

First thing first, never declare multiple pointer variables in same declaration statement. Don’t mix declaration of pointer and non-pointer variables in same line. Following this will save you a lot of debugging time in the future.

But in case, you are forced do it or you are debugging code written by somebody else, this short post can help you.

What does this code really mean?

int* c, d;

It is clear that c is a pointer to integer. But what about d? Is it integer or pointer to integer? Ofcourse, it is integer not a pointer to integer. Using declaration as above is very ambigious. Avoid it any cost. Instead, declare them on their own statements.

int* c;
int d;

Much clear. Isn’t it?

It is also better to follow this rule when all variables are pointers. Also, I prefer to put the star next to varaible name instead of the variable type.

int *p, *q;

is more intuitive than

int* p, *q;

Waspmote v1.1: Installing IDE and development environement

In this article, we will install the waspmote IDE and necessary development environment.

Download and Install Waspmote IDE

Currently there are two versions of Waspmotes available(v1.1 and v1.2). If you don’t want to break your head trying to debug why you cannot upload the sketch to board, download the correct IDE (:p) from this link.

If you get the following error during upload, you have the wrong IDE installed.

avrdude: Send: 0 [30] [20]
avrdude: Send: 0 [30] [20]
avrdude: Send: 0 [30] [20]
avrdude: Recv: . [00]
avrdude: stk500_getsync(): not in sync: resp=0x00

avrdude done. Thank you.

Extract the downloaded archive into a suitable directory and fire up the waspmote script present inside it.

waspmote_ide_start

Install avr-gcc compiler tool chain

To make things more complicated, the latest version of gcc-avr package in the Ubuntu repositories are not backward compatible with the old v1.1 waspmote IDE. So when you try to compile or verify the sketch, you will get the following error:

avr-gcc: error: unrecognized command line option ‘-assembler-with-cpp’

The fix is to remove the latest gcc-avr package and install the old version of it manually. Execute this command to remove the latest packages:

sudo apt-get remove avr-libc gcc-avr

Download the following packages and install them manually by double clicking them.

  1. gcc-avr 4.5.3-3
  2. avr-libc 1.7.1-2

Install 32-bit version of libusb

Latest versions of Waspmote IDE uses 64-bit version of avrdude binary by default on 64-bit machines. But the old v1.1 IDE uses 32-bit version. So we have to install 32-bit version of libusb.

sudo apt-get install libusb-0.1-4:i386

Now you have the IDE and the development environment set up. Try to compile and upload the hello_world or test sketch.

Binary arithmetic using python

Convert unsinged integer to binary string

bin(10)

‘0b1010′

Convert binary string to unsigned integer

int('1010',2)

10

It also works if you try the binary string with the prefix ‘0b’. For example,

int('0b1010',2)

Convert signed integer to binary string

It is a little bit difficult to deal with negative numbers. Trying to convert it the same way we did with unsigned numbers doesn’t work as expected,

bin(-10)

‘-0b1010′

You would have expected a Two’s complement number as the output but it just prints the binary string of positive number with a ‘-‘ prefix.
This problem can be fixed by specifying the length of the bits you want as output.

bin(-10 & 0xff)

‘0b11110110′

If you want the length to be dynamic,

int("1" * 8, 2)

Convert singed binary string to signed integer

I am not sure if there is a direct way to do this in python. If you find any please let me know! I have written a small function to do it,

Simple one-line script to convert toolchain prefix(ex: arm-linux- to arm-elf-)

This simple script helps you create symbolic links of your tool chain binaries with different prefix. For example, from arm-linux- to arm-elf-, etc.

for file in `ls arm-linux-*`; do new=`echo $file | sed -e "s/arm-linux-/arm-elf-/"`; ln -s $file $new ; done;

Naturally, it only creates symbolic links. So your binaries with original prefix still exist! This simple one-line script always saves a lot of time, each time I install a new tool chain.