Function pointers - The unconventional use
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 */
}
Function pointers - Simple use without confusion in C Programming
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.
Finding IMEI number if mobile is lost or stolen
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.
Lost my mobile for second time
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.
Fluid width for facebook comment box
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 dont want to generate, then it is also possible to just edit the existing code and change the part.
'data-width' to 100%
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%.
How much power does my USB project/device/phone consume?
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
Note: If you want to buy one like this, you may search for USB power measurement or USB current measurement.
Voltage reading of Arduino
Pinguino on bread board
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 !).
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.
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.
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.
Following are steps to make it working.
I will soon add some posts on some of these stages since some problems might occur.