Wednesday, November 30, 2011

Bit field structures in C – Microcontroller Programming


When you are working with Microcontrollers you often have to keep status flags for certain events, e-g when you are using a timer, you need to keep track of how much time in milliseconds or seconds have elapsed, but your timers only allows ticks in microseconds, so you count the number of microseconds say 1000 microseconds and “Set” the flag “one_ms” indicating one millisecond has elapsed. And more than likely, this flag “one_ms” is either a “char” type or an “int”. In either case, a char will take one whole byte and an int will take 16/32 bits depending on the architecture. But if you notice, what you needed to convey could have easily been conveyed or kept record of in one bit only. So for one status flag of one bit wide, you pay a price of unused extra bits. That is not all, you also end up having multiple status flag variables, meaning you are wasting a lot of useful resource (memory). And the code gets messy too.
However, there is a better way to keep status flags without wasting the memory or without making the code a mess. And that is by the use of “bit fields”  in C programming.

Bit fields are a special structure member type. A bit field is of type integer but its width is specified by the programmer. For example, ARM7 is has a 32-bit integer, but when I use this “int” data type for a bit field, I can specify the actual size this integer will occupy in memory by specifying the size after a “:”.

struct normal_integer
unsigned int millisecond;  //occupies 32 bits in memory
struct bitField
unsigned int millisecond : 4 ;  //occupies 4 bits in memory

Bit Fields can be of type “signed integer” , “unsigned integer” or a “bool”.
Keep in mind, the specified size cannot exceed maximum default size of that data type. And, for portability and other holy reasons always specify if you want your bit fields signed or unsigned.

Useful example of Bit Fields

Suppose you are designing a stop watch you have one hardware timer which is capable of counting ticks in microseconds. But you want time in seconds and milliseconds. So what do you do ? You wait and count.
For the stop watch example:

int tick_counter ; //global tick counter
struct lap_time
    unsigned int start : 1 ; //one bit flag indicating whether the lap time has started or not
    unsigned int millisecond : 1;  //one bit flag indicating 1 millisecond
    unsigned int ten_millisecond : 1; //one bit flag indicating 10 milliseconds
    unsigned int second : 1;  //one bit flag indicating 1 second
    unsigned int laps_elapsed : 4; //4 bit wide elapsed laps counter

} lap;
void timer_isr( )    //ticks every 10 microsecond
    //disable low / equal priority interrupts
    if (lap.start)
        tick_counter ++;
        if(tick_counter == 100)
            lap.millisecond = 1;
        if(tick_counter == 1000)
            lap.ten_millisecond = 1;
        if(tick_counter == 10000)
            lap.second = 1;

           tick_counter = 0;
    //enable low / equal priority interrupts

The isr written above is only generic code for the specified example, it does not assume any specific sort of hardware. That is why I have placed comments at the start and end of the ISR for hardware specific routines you need to do.
Link: Bit Fields on wikipedia
Technorati Tags: ,,

Friday, November 25, 2011

PlayerPro Music Player for Android - Better than cheese cake

PlayerPro for Android is "The" Music player that should have been the stock music player for Android.

Why did it land a review here at "Wired Soup" ? It is plain awesome , that is why.

I will not go into a very deep review for this Android App, but just enough to give you an idea of its goods and bads, rest you can test it yourself or take my word for that is tried and tested :)

PlayerPro for Android is a light weight Music player with support for major music formats .

Formats supported: mp3, ogg, flac, wma, wav, m4a, mp4.

As every music app, it also has its equalizer (no music app can go wrong in that section), PlayerPro has a 10 band graphic equalizer with 15 default/customizable presets and that is not something to boast here either. It has the capability to sort files by Genre , Album, Artists, Playlist, Songs , Folders. Which are really convenient for everyone even a new user who just opened up the app can figure his way out easily. The UI is very intuitive. 

It supports Dolby and SRS sound effects for HTC devices equipped with HTC sound enhancement (HTC desire HD, etc).


PlayerPro for Android has 5 Homescreen widgets (4x1, 2x2, 3x3, 4x4, 4x2) and 2 lockscreen widgets to choose from. 

PlayerPro supports swipe gestures to make the user experience more dynamic and easier to use. 

Swipe gestures

On Player Screen: swipe album art to skip songs, 
On browser screen: swipe left to play next, swipe right to play last

Shake It Support

It also supports the walkman style "shake it" feature. Don't like current track, just shake it ;)
(e.g.: shake top to bottom or bottom to top to play next/previous song).
But the most attractive feature that really caught my attention in this Android App which has been absent in (i am sorry to say) 97% of the music player apps on Android I have tested is "Auto fetch Album Art".
Auto Fetch Album Art
I never liked a "Play" icon being used as my Player screen for 100% of my downloaded songs because they never came with album art ... (for obvious reasons) and No player on Market will give me album art without me going to Google images finding the specific Album cover, saving file, editing music files tags, assigning a new Album cover and all that hassle. 

But PlayerPro did it for me, if you have your tags set, it will get the Album Art for you. If you have Artist tag set and no album tags, even then it will find the cover for you but ofcourse this time it will be based on artist and not the album. But its still cool. My playlists look so pretty now ^_^

Moreover, you can read Album and Artist reviews directly from PlayerPro too.
Well, I have to say I am sold on PlayerPro as my Music Player for Android devices. 
Let us know in Comments section below, which Music Player suits you on Android.

Thursday, November 24, 2011

Smart Keyboard - The Virtual keyboard for Android

Smart Keyboard for Android

When I first got my hands on Android Phone, the first thing I noticed was the bad default keyboard. I didn't like the layout, pressing extra key to get regularly used symbols is a pain in the you know where. No smiliy key , a small space bar and it could not handle my uber fast typing speed even though it was a multi touch phone.

So I started looking for keyboard alternatives and read a lot of reviews , downloaded and tested a lot of keyboard apps and finally settled on "Smart Keyboard" for Android

Smart keyboard offers what the default Android Keyboard should. It has skins to match your taste, you can change the size of "space bar", you can even get rid of the "comma" and "mic" key to make room for a bigger space bar key. What I really missed when I was using default keyboard and chatting with friends was the smiley key. I could not make emoticons because of the extra keystrokes required to make one. But Smart Keyboard has a smilie key by default with customizable smilie list . 

There is a good support for various languages too, you can download your required language from the market for free. You can resize the keys to your liking. You can change the tap sound, turn on /off the vibrate on key press. Smart keyboard can learn your contacts names if you wish it to, you can turn on "Smart Dictionary" which basically means, allowing the app to learn new words. There is so much you can customize.

Another very appealing feature of Smart Keyboard is various layouts which include , the classic T9 layout (like phone pads), the compact layout and the qwerty keyboard layout. The Only thing I think is missing is the swipe functionality, which is not really annoying nor is missed when you have such a great predictive keyboard.

Tuesday, November 22, 2011


Readers please ignore this post.

Applying for Technorati


RockMelt – The Social Browser

There has been a browser war going on for some years now, with mainstream competitors such as Mozilla Firefox , Opera , Chrome and Safari (I would not even dare to call Microsoft’s Internet Explorer a browser, as we say in Urdu “Apni izzat apnay hath”).

CAUTION: Use Internet explorer at your own risk.

Somewhere around November 2010, a new contestant “RockMelt” stepped in the arena. It had everything that Google Chrome has, the sleak look, customization, Fast Chromium Engine and a lot more.And It offered what no browser offered before … “social integration”. Well I may be over exegarating but the way it integrates social networks is so intuitive to use that it hardly takes fifteen minutes to get used to it. And once you get the hold of it, you just feel like “sharing” more and being more “e-social”.

RockMelt comes built on Chromium Engine, which means it can work with anything Google Chrome can. The UI may look too similar to Chrome but why should it be a problem when Chromium itself has a great UI. Rockmet is compaible with almost all Google Chrome extensions and the extra juice it pours to make your experience sweeter is “Auto Suggesting” a compatible app/extension if there is any for the current website.

You can stay updated about your social networks and other feeds without leaving your browser or the page. Plus there is an in-built “View Later” application.

You found something interesting and want to share it right away ? with RockMelt, this is not a problem. You just have to click “Share” and it will present you with a dialog box asking for which social sites you would like to share this with. Its that simple and intuitive.

Okay, so I have been playing around with this Browser, and here is my evaluation.


Chromium engine base allows RockMelt to be compatible with Google Chrome Extensions, and if you were a Chrome user then moving to RockMelt feels like a walk in the park.

User experience
The people at RockMelt have made such good and efficient use of the screen space that you never feel cluttered even with so many updates going around. It uses the side walls of the browser for Social Network Integration.

Facebook and Twitter
I have found myself sharing posts and viewing shared posts on my wall and on my twitter stream more regularly than I did before RockMelt, because I don’t have to move away from what I am doing. And It doesn't require me to do “anything” except press a button to share.

Share button
Simply, “The magic click


In-built download manager still lacks the luster.

Twitter stream does not only keep count of your mentions but also of any update on the stream, which is annoying. There should be a provision of choosing either or both, but there should be a choice.

If you are a heavy social media user, you’ll probably find the experience useful and enjoyable.  However if you only find yourself on Facebook or Twitter 1-2 times per day, I would stick with your normal browser of choice.

Sunday, November 20, 2011

10 - Hidden Tips and Tricks for MIUI Android ROM

What is MIUI ?

I first heard of MIUI a few months back, and did not pay much heed to its greatness. Quite recently, I decided to give this Chinese Origin Android ROM MIUI a go. And found out there is an English translation of this Rom (MIUI English) . So I flashed it into my HTC Desire Z (awesome awesome machine). I fell in love the moment it booted to the home screen. Though it looked similar to iOS UI but I still loved it. I might be doing a full review on the beauty of MIUI Rom later. 

To make my user experience better, I searched the long dark paths of the internets and found many interesting and useful tips to enhance MIUI Rom functionality even more. Here are all of those which I came across, feel free to add any I missed in the comments.

1. Long press on the icon in Toggles to launch the settings for that icon.
When you pull down the notification bar, you will see the Toggle settings for plenty of options (such as WIFI, volume, backlight etc). Long press on any icon in the Toggle mode will launch the setting for that icon.

2. Built-in Screen Capture function
One of the lacking feature in Android is the lack of a built-in screen capture feature (like in iPhone). Not anymore. With MIUI, you can press the “Menu” key and “Volume Down” key at the same time to capture a screenshot. The printed image is saved in the “MIUI: directory in the sd card.

3. Slide from left to right to quickly delete a message or a call log.

In the SMS or Dialer app, swipe left or right any entry will bring up the Delete mode.

4. Move home screen icon easily

Hold down an icon by one hand to enter Edit Mode, and then use the other hand to slide the screen to move the icon to the target screen.
5. Launch Music Player in lockscreen
In the lockscreen, double tap the time zone to launch the Music Player
6. Activate the torch from the lockscreen

In the lockscreen, hold down the Home button to enable the torch. The torch remains "On" as long as the Home button is pressed.
7. Access shortcut menu from contact image
In the Calls, SMS or Contacts app, tap the contact image and a shortcut menus will appear. This is true for pretty much every Android Rom.
8. Long press on the Home button to show History
Like any other Android ROM, long press on the Home button will show the recently used apps. However, in MIUI, there is also an App Killer and App Manager button. Swiping the popup left will show the search form.
9. Send SMS for rejected calls
When you cannot answer the phone, you may slide up the panel to reject the call and to send a message to the caller at the same time.
10. Quickly uninstall an app
To uninstall an app, simply press and hold the icon until the trash bin show up at the top of the screen. Move the icon to the trash bin to uninstall the app.

There is a huge list of MIUI themes available to download. You can access them from "Themes" icon on the homescreen or Press "Menu > Settings > Themes" . 

Saturday, November 19, 2011

addr2line - The forgotten ingredient for debugging

Whether you have just started programming for linux or are an old nix programmer, you must have encountered the mysterious crashes with a stack dump saying "maybe a null pointer" and a long list of hex values. Well, you know it I know it, program crashing is rarely a mystery and is almost always caused by human error. Same stands true for this scenario. And linux is being generous by giving us that stack dump and saying "you messed up, and here is where you made a mistake".

The question is, how to use this information provided in the dump? To make sense of the numbers in the dump you need some tools. Here we will only talk about addr2line which helps to find out where was your program counter when the crash occurred.

addr2line is a short for "address to line" and this tool  does exactly what its name suggests. It takes an address in hex format and tries to find the line of code in the given executable at that address. But before you use addr2line, you will need to compile your code with debug symbol information. Without debug information you cannot debug your application nor use addr2line.

How to generate debug symbols information ? Here is how:

#gcc -g myfile.c -o myexec

Now that you have debug information available, run your application and let it crash. Mine crashed with the following dump.

Defered Exception context
COMM=qsig2 PID=263
TEXT = 0x06b50000-0x06b5a2c8        DATA = 0x06b602c8-0x06b70ce0
 BSS = 0x06b70ce0-0x06a80000  USER-STACK = 0x06a9fe70

return address: [0x06b56d38]; contents of:
0x06b56d10:  b0f8  b139  b17a  bb4e  3046  bbc0  6000  bbf0 
0x06b56d20:  b999  e408  004d  3210  9152  e410  0221  bbe0 
0x06b56d30:  2006  b9e0  bbf0  b9ea [9110] bbe0  b9e0  0c00 
0x06b56d40:  13f9  a138  3008  64b1  3001  4f10  5008  4f10 

 SEQSTAT: 00000027  IPEND: 0030  SYSCFG: 0006
  EXCAUSE   : 0x27
 RETE: <0x00000000> /* Maybe null pointer? */
 RETN: <0x06ad8000> [ qsig2 + 0x0 ]
 RETX: <0x06b56d38> [ /qsig2 + 0x6d38 ]
 RETS: <0x06b52d80> [ /qsig2 + 0x2d80 ]
 PC  : <0x06b56d38> [ /qsig2 + 0x6d38 ]
DCPLB_FAULT_ADDR: <0x0000000c> /* Maybe null pointer? */
ICPLB_FAULT_ADDR: <0x06b56d38> [ /qsig2 + 0x6d38 ]

 R0 : 0000000d    R1 : 0000000e    R2 : 00000001    R3 : 00000000
 R4 : 06b407e4    R5 : 06ac5448    R6 : 00000030    R7 : 06ac5448
 P0 : 06ae3298    P1 : 06b61768    P2 : 0000000d    P3 : 06b61768
 P4 : 06ae3e24    P5 : 06ae3e24    FP : 06ae3248    SP : 06ad7f24
 LB0: 0031efcd    LT0: 0031efcc    LC0: 00000000
 LB1: 0031cf7b    LT1: 0031cf7a    LC1: 00000000
 B0 : 06ca0388    L0 : 00000000    M0 : 07f7d615    I0 : 003358ec
 B1 : 06ca0348    L1 : 00000000    M1 : 06ca0408    I1 : 00000000
 B2 : 06cffaf4    L2 : 00000000    M2 : 06ca0388    I2 : 00000000
 B3 : 02b1235c    L3 : 00000000    M3 : 06ca03c8    I3 : 00000000
A0.w: 00000000   A0.x: 00000000   A1.w: 00000000   A1.x: 00000000
USP : 06ae3208  ASTAT: 02003004

You have a harware stack, a call stack , information cache and data cache information. What you need to find out here is your program counter "PC".

PC  : <0x06b56d38> [ /qsig2 + 0x6d38 ]

The system says the application crashed when it was executing the instruction at the mentioned address (0x6d38 in this case).

Locate this address in code and you have found your culprit. But how to do this ? Here is how:

#addr2line -C -e myexec 0x6d38

-C is to get demangled C code.
-e is to specify executable file

This will return your culprit filename, function name and line number.
So here you have it, enjoy more productive coding

To get complete list of attributes for addr2line try
addr2line -h
man addr2line

Sunday, November 6, 2011

Android Development Basics - 101

Setting up Android Development Environment

I have this itch of learning development for new platforms. And since its "Android" everywhere and I own one too so I decided to scratch this itch and here I am, learning to program Android Apps.
Since, I was on it. I decided to write the process of getting a Hello World Program up and running for the noobs who have just stepped in the Android World.

NOTE: I will not go into detail of explaining every line of code. I do not assume any prev knowledge of Java but I do assume some programming experience and familiarity with eclpise IDE.

Let the journey begin...

First things first. You will need to setup a development environment to compile and test your applications. This will help you test your applications on your host system (windows or linux) without requiring you to connect your Android device.
It does so using AVD (Android Virtual Device) and ADT (Android Development Tools) an eclipse plugin for Android

So to setup your development environment, follow the given steps in order and you'll be ready before you know it.

NOTE: You will require a stable internet connection since the installation downloads the required files for installation

Step 1 : JDK
Installing JDK (download and install)

Step 2 : Android SDK
Installing SDK (for windows users I recommend using exe file instead of zip file) 
Download the exe file and install. 

Step 3 : Eclipse IDE 
Downloading eclipse (I recommend eclipse classic )
Eclipse does not have a regular setup file for install. It comes packaged in a rar/zip archive. Just extract the files and use "eclipse.exe" to launch.

Step 4 : ADT Plugin for eclipse
Installing ADT (follow step by step from the link)
I found these steps to be very straight forward and hence decided to directly link back.

Step 5 : Installing platforms and components
Installing platforms support for development.
Adding platform and component support is done via Android SDK manager.
SDK manager can be launched via :

  1. From within eclipse Window > Android SDK and AVD Manager
  2. Via Start menu > Programs> Android SDK Tools >Android SDK Manager

Once the SDK manager launches, select the components you require and press install.

NOTE: If you are not sure of what to choose and what not. I recommend using default selection. But make sure your version of Android platform is selected too. e-g SDK Platform Android 2.3.3 .

Once the downloads and install process is done. You are ready to rumble.

I'll post the "hello world" program tutorial in a couple of days :)