Function pointers - The unconventional use

No comments :

In my previous post about function pointers, I have written about the definition of function pointers. Which explains how to avoid confusions while defining. In this post, I will list some uses of the function pointer.

The unconventional use

Usually function pointer are used in callbacks or some special cases. But, we can use it also to simplify code. Consider the below example
void print_zero(void)
{
    printf("Zero\n");
}
void print_one(void)
{
    printf("One\n");
}
void print_two(void)
{
    printf("Two\n");
}
void print_three(void)
{
    printf("Three\n");
}
void print_four(void)
{
    printf("Four\n");
}
void example_switch(void)
{
    unsigned int number = 0;

    switch(number)
    {
        case 0:
            print_zero();
            break;
        case 1:
            print_one();
            break;
        case 2:
            print_two();
            break;
        case 3:
            print_three();
            break;
        case 4:
            print_four();
            break;
        default:
            break;
    }
    
}
The snippet above is nothing but calling few functions in a switch case. Using function pointers, it can be simplified as below.
void example_fPtr(void)
{
    unsigned int number = 0;
    void (*pFun[5])(void) = { /* Define and initialize function pointer */
        print_zero, print_one, print_two, print_three, print_four
    };
    
    number = 4;
    if(number < 5)
        pFun[number](); /* Call function corresponding to number */
}

Read More

Function pointers - Simple use without confusion in C Programming

No comments :

Pointer is one of the most used feature of C - Programming language. It gives us a lot of power to access and control. Function pointers are nothing but a pointer to a function. While learning the concept, it may seem a redundant feature to assign address of a function to a pointer and call the same function using the pointer. However, in simple programs, it is of no use. But, it make sense and it is the only way to achieve certain functionality using C-Programming language.

Good way to define function pointer

In an embedded project with different programs, pointer is of great use. For example, in an embedded project, one may want to do a soft reset. Which means nothing but jumping back to the starting address(most of the cases, the reset vector = address 0). In C program, this can be achieved by using a pointer, assign it to value 0 and call.
void (*pSoftReset)(void); /* Define a function pointer */
pSoftReset = (void (*)(void))0; /* Assign address 0 to pointer */
pSoftReset(); /* Function call using pointer */
In the above snippet, in line 1, a function pointer is defined. In line 2, pointer is initialized with address. Note the typecasting. 0 integer is typecast-ed to a function pointer with address value 0. Then, the function is called. It will never return as the initialization routines will initialize all the variables, stack etc. The same code can also be written in single line as
((void (*)(void))0)(); /* Typecast address 0 to pointer and call */
Here integer is typecast-ed and called at the same time. This function pointer definition, typecasting etc might be difficult to read for many. Because of this code may be less readable. To avoid this we may use the typedef functionality to define new types as below.
typedef void (*typFPtr)(void); /* Define new type typFPtr as pointer to a function */
typFPtr pSoftReset = (typFPtr)0; /* Assign address 0 */
pSoftReset(); /* Call using pointer */
In order to avoid confusion and improve readability, it is better to define new types and use it instead of directly using the function prototype. However, please note that this may also cause some confusion as the '*' is missing in declaration of variable and typecast. Alternately, it can also be done as below.
typedef void (typFN)(void); /* Define new type typFN as pointer to a function */
typFN *pSoftReset = (typFN *)0; /* Define pointer to function and Assign address 0 */
pSoftReset(); /* Call using pointer */

To conclude, definition and use of complex function pointers can be achieved by using typedef. Some examples are mentioned below.
 /* Below definiton is a function pointer fp which
  * will accept a pointer to a function which takes
  * pointer to int as argument, returns void and 
  * another int as second parameter and returns a
  * function pointer which takes int as argument
  * and returns pointer to int */
 int *(*((*fp)(void (*)(int*), int *)))(int);
 
 /* Uning typedef it can be simplified as below */
 typedef int *typFun_pint_int(int);
 typedef void typFun_void_pint_int(int*);
 
 typFun_pint_int * fp_simplified(typFun_void_pint_int *, int *);
 /* Here it is clear what is the return type
    and what are the arguments */
In my next post, I will list some examples of using function pointers. Initially, I thought of adding it here. But, it is too late for me now. So thought of writing it as another post.

Read More

Finding IMEI number if mobile is lost or stolen

No comments :

Normally, the IMEI number of a mobile is found by dialing *#06# on mobile. What if mobile is lost? and we haven't noted the IMEI number. We do not have mobile to dial and check. In this case, there are few places you could check for finding out the IMEI number.
  1. Mobile phone package: If you still have the mobile phone package/carton box in which the phone was shipped, It will have the IMEI number.
  2. Mobile phone purchase bill/Invoice: Mobile phone purchase bill will have the IMEI number noted. If your bill is missing, then still you may get the duplicate bill/invoice from the retailer. If mobile is purchased from online retailer like Flipkart, EBay, Amazon, Snapdeal, etc. It can be obtained from purchase history in the respective website.
  3. In the warranty card or the user manual of the mobile.
So if you have any of the above options available, it is possible to obtain the IMEI number for reporting to the police. Below you can see my Xiaomi Redmi 1S's box with IMEI labels. I have masked it for privacy.

Read More

Lost my mobile for second time

No comments :

This is the second time my mobile is lost. Well this time it is clearly a theft. I kept my Xiaomi Redmi 1S for charging on a table nearby a window. Somebody somehow opened the window and picked the mobile during night when we were sleeping. Though we woke up hearing some sound, it was too late; the one who picked my mobile has left by then.
Without wasting much time, I called the customer care center and requested for temporally blocking my mobile number which he accepted after verifying my personal details.
Now, the next thing is to inform police. However, from my previous experience, I was sure that they will not help much. Maximum they will do is nothing but issue acknowledge for lost mobile reporting. They says, the IMEI number is added to the tracking system and if someone use the mobile it can be tracked. But, still there is no information about my Sony xperia U which is in tracking system since two years. I have found the news here about arrest of person who used to modify IMEI. So what I think is, the people who steals the mobile would approach such people, change the IMEI and then either use it or resell. So, the tracking systems will not be able to find the mobile phone. Below line is from the news source.
Accused, who is a software engineer, was arrested and charged under the IT Act for using technology to change the unique IMEI number of mobile phones
Now, for getting a duplicate Sim-card also, acknowledge from police station is required. This is because, I have ported my number from Vodafone postpaid to BSNL prepaid. And for getting new sim for prepaid, acknowledge is must. Also I think it is necessary to report the loss to avoid any misuse/abuse of the device.
While searching the Bangalore police website, I found this website of Bangalore police. In the page, there is an option 'report lost'. A screenshot is pasted below.
For reporting, we will have to first search in their partner site Lost Click Found. If our mobile is not in the list, then it will give us option to reach this page(Police report of lost item) finally report to Bangalore City Police site. Here there are lot of categories. After submitting we will get option to download the acknowledgement. I have pasted screenshot of my acknowledge below.
Acknowledgement downloaded

However, in the form, there is no option to enter IMEI number. So, I mentioned in the comments column. Since, the IMEI number is not specifically asked, I have also personally went to the nearby police station and informed so that they will add the IMEI number for tracking.

Read More

Fluid width for facebook comment box

No comments :

A page layout, which stretches or shrinks to fill the browser window according to its size, is called, fluid layout. Advantage the view will adjust according to the size of the screen, ie page will adjust itself to browser used in different devices like mobile, tablet, desktop etc.
Usually, blog templates has fixed widths for most of the fields. But those who are using fluid layout may want the facebook comment box to be fluid too. Or those who does not know the width to be set can also try fluid width as it will fill the container. In blogger, filling the container will be nothing but taking the width of the side bar or body where the widget is placed.

Facebook comment box

If you are generating the comment box, just enter 100% in the Width field.

If you dont want to generate, then it is also possible to just edit the existing code and change the part.
'data-width' to 100%

data-width="100%"
data-numposts="5" data-colorscheme="light"> This is documented in facebook developer page from where we generate the code for comment box.
The width of the plugin. Either a pixel value or the literal 100% for fluid width. The mobile version of the Comments plugin ignores the width parameter, and instead has a fluid width of 100%.

Read More

How much power does my USB project/device/phone consume?

No comments :

Ever wondered how much power, a USB device consumes? Sometimes when mobile phone battery is deeply drained, mobile will not indicate whether it is charging or not. Is there a way to check current is really flowing and charging the battery? May be you are doing a USB powered electronics project and want to check how much current it draws.
USB - Current reading for arduino
Current reading of Arduino

Well I found this nice little device(seen in the image above) from Deals Extreme which is also very cheap. It shows the current and voltage of the USB device attached. All we have to do is add this device in between.

Some uses for this are
  1. Find if a USB cable is complaint(Current will remain 0A).
  2. Find if USB charger adapter is complaint.
  3. Check if the USB device is drawing more current.
  4. Check amount of current required used by arduio or raspberry pi or other embedded project boards which you are using.
  5. Show off this cool gadget in-front of friends, etc.
Note: If you want to buy one like this, you may search for USB power measurement or USB current measurement.
USB - Voltage reading for arduino
Voltage reading of Arduino

Read More

Pinguino on bread board

No comments :

I am a fan of PIC microcontroller. Reason being the first one to use during my college/university time. Pic16f84 was one of the first microcontroller which I used as I started exploring the new world of micro-controllers.  Those days programmers were very costly and there were no shops near the place I lived where I could take a microcontroller to program. But, the JDM programmer which can be constructed with some basic components made my life easy.
Now a days, there are lot of cheap options, if we want to do some hobby projects using micro-controllers. Arduino being one of the most popular which uses the self reprogramming feature to flash the new code, can be used even by people who does not know much about software or hardware programming.
Pinguino is an arduino like hardware and IDE for PIC microcontroller. Its software is Free and Open Source. And Pinguino boards are Open Hardware. It is very much similar to arduino with additional USB features supported by PIC microcontrollers. So it does not need a USB to RS232 converter chip in the hardware which must make the hardware cheaper. Below is the description of Pinguino as published in Pinguino website.
Pinguino is an Arduino-like electronics prototyping platform. It supports different 8- and 32-bit ©Microchip microcontrollers, all with built-in USB module (no FTDI chip !).
Pinguino comes with a USB Bootloader. This small program running inside the microcontroller is responsible for transferring your application from your PC to the microcontroller memory and handing over the control to this program afterwards.
No programmer is needed(*), the microcontroller can be reprogrammed over USB with a PC.
  • 8-bit : PIC18Fx550, PIC18Fx5K50, PIC18Fx6J50 and PIC18Fx7J53 family
  • 32-bit : PIC32MX Mips family
Pinguino is an Integrated Development Environement (IDE) which gives everyone the ability to write, compile and upload programs on a Pinguino board.
Pinguino's Language is an Arduino-like or Arduino-influenced rather than Arduino-compatible Language. Users can use the same keywords but can not include Arduino's libraries in their code. Adapted libraries are listed here.
It makes you write easily your application without spending hours learning pragma, configuration bits or command line compiler.
However, due to lack of popularity the Pinguino boards are difficult to find and costlier than that of the Arduino boards. Hence I decided to try it using a PIC18f2550 on bread board. It is very easy to construct as there is not much components. Firmware which is necessary to make the controller work with the Pinguino IDE is available at the Pinguino website. An image of final constructed circuit with an 8x8 led matrix display is below.
Following are steps to make it working.
  1. Download software(Pinguino IDE) and Firmware/bootloader for PIC18f2550.
  2. Flash firmware to the controller.
  3. Construct the Pinguino hardware on bread board as per the schematic.
  4. Install drivers for pinguino if using windows.
  5. Connect hardware to PC using USB.
  6. Open IDE and select the correct com port number for the pinguino.
  7. Make sketches and enjoy.
 I will soon add some posts on some of these stages since some problems might occur.

Read More