Monday, November 26, 2012

Printing call stack of a function using code in C/C++

Many times when you deal with memory related issues like resolving core-dumps, you are mis-led by debuggers in call stack of the issue because of various reasons.
In those cases, you can actually write a piece of code that can print the call stack from any place. The good thing is, its fully in code, so you can identify your culprit call stack with this way by adding this code in your suspect functions.

Use this unix code snippet to print call stack:
(Dont forget to include execinfo.h header file)

void* buff[100];
int cnt=backtrace(buff,100);
char** str=backtrace_symbols(buff,cnt);
for(int i=0;i<cnt;i++)
{
printf("\n%s\n",str[i]);
}

Search more on the functions used in example to leverage more. If you have any troubles in the call stack, use -rdynamic option while compilation.

Thursday, October 11, 2012

Best way to master C/C++

Use the "-S" option when you compile the code like "bcc32.exe -S Hello.c". This option informs the compiler to generate the assembly code of the given C code which is linked to produce the executable. Now open the asm code and understand how C works at the most minute level.
This helps a lot in understanding the stack and how datatypes are dealt with. "-S" option works with almost every compiler, if it doesnt work, check the compiler manual for generating asm code.

Thursday, June 14, 2012

Webcam real time background remove using Flash/Actionscript/Flex

Today, I was trying a small prototype for my new project on actionscript for Hobby Coders, for that I had to make a good background remove script in actionscript. I wanted to choose HTML5 for this. If it were a desktop app, its wise to go with openCV with C for their rich libraries and speed. But for certain modules, flash was chosen.

Demo (Fixed threshold): http://hobbycoders.com/demos/backgroundremove/fixed_threshold
Code (Fixed threshold - Actionscript): http://hobbycoders.com/demos/backgroundremove/fixed_threshold/code.txt

Demo (Variable threshold): http://hobbycoders.com/demos/backgroundremove/variable_threshold
Code (Variable threshold - Flex 3): http://hobbycoders.com/demos/backgroundremove/variable_threshold/code.txt

Here is the algorithm Im using:
1. Save a bitmap (we will call it BMP_BG) of the background without the person
2. From now the current frame is compared with BMP_BG, pixel by pixel
3. For comparison, I calculate the luminance value (one used in Grey scale) for each pixel for both the images and compare this value, so that their difference lie inside a threshold value
4. If they both are same, I put the pixel of the rendered image as white otherwise, I copy the corresponding pixel from the current frame 

Please take a look at them guys, if someone wants similar codes, ofcourse feel free to use mine but please give your feedbacks here! If anyone can suggest me a better method, please post here too.

Still im continuously optimizing the algorithm that Im using to get a perfect result. If I get a better result, I will definitely share the idea with you all.

Tuesday, June 12, 2012

Simple HTML Counter - count on tap - for Touchphones Android/iOS etc

I have a practice of chanting daily. For that I used to use a mechanical counter. Many times I bought that and it broke. I have a Android Smartphone. This time I made a small HTML page that will count on finger tap anywhere on mobile screen. The good thing in this is, when I walk, I can tap anywhere in the screen without seeing the screen and the count occurs unlike many other apps that contains a specific button to be pressed for counting. I made this thing for my personal use, but thought of sharing with people of similar requirements

Screenshot:  (In device: Tap anywhere on screen, count increases) Tested and working in Android and iOS (its HTML, device independent)
Steps:
from your mobile browser. If you want the code to be hosted online and follow from step 5 below

OR If you want to have it offline without Internet, follow the steps

1. Create a new file called "simplecounter.html"
2. Open it with any text editor (notepad, etc)
3. Paste this code in it
<!DOCTYPE html>
<html>
<head>
<title>Simple Counter by Hobby Coders</title>
</head>
<body>
<center>
<br/><br/><br/>
<h1>
<div id="count">0</div></h1>
<br/><br/><br/>
<font size="1">&copy;2012-13 HobbyCoders.com</font>
</center>
<script type="text/javascript">
var count=0;
var countPtr=document.getElementById('count');
document.addEventListener('touchstart', function(e){e.preventDefault(); count++; countPtr.innerHTML=count;}, false);
</script>
</body>
</html>
4. Copy this html file to your mobile device
5. Open this file in Android/iOS browser. Tap anywhere on the screen to count up
If in android it doesnt work in default browser, do this
RECOMMENDED FOR ANDROID: Install this file manager app from Google Play (https://play.google.com/store/apps/details?id=com.rhmsoft.fm) and open the html file with the "HTML Viewer" of this app. It works neat and clean
6. People who dont require hi-fi app for counting, people who just need such an app for chanting or similar purposes can use this app
7. And I didnt complicate the app with save last count, reset etc because, you go back from app and open it again in browser, count resets, and press Home key, the browser goes to background and you can continue later from the same count (multitasking)

Tuesday, February 14, 2012

Simulating moving targets in Google Maps

Recently i made a project in which I had to simulate real targets/GPS Clients moving gently in Google Maps. I want to share the logic for people who want similar codes.

DEMO: http://hobbycoders.com/demos/gpssimulation

Google API provides a method of directly rendering the entire path between a source and destination. But if you want the same thing to be done but simulating a moving target in the same path, a bit of code has to be written. So here is the logic:

1. Google API can give the path coordinates (Latitude, Longitude) in JSON or XML. An example of JSON direction lookup from Srirangam to Trichy (They both are places in India), google direct URI which gives direct JSON output will look like:
http://maps.googleapis.com/maps/api/directions/json?origin=srirangam&destination=trichy&sensor=false

2. Now from AJAX get the output of file and make a Javascript object from it easily (thats why JSON used). I was facing problem of cross domain ajax referencing with this, so i created a Java code in my domain which can proxy the web page for me in same domain

3. Once done, for each step of direction (like first turn left, then turn left etc.,), you get a variable called points in converted Javascript object from JSON. To extract it use:
jsonObject.routes[index].legs[index].steps[index].polyline.points

4. If you notice, the points data will be encoded. Decode it, google has already given the code to decode it

5. And now for each step in direction, you will get many coordinates of latitude-longitude pair

6. Create a Polyline (Google Map object) for each pair of lat-long (if you need the entire trail of path) or a Marker (only current position)

7. Call some delay function in javascript like setInterval will be a good choice, iterate through the coordinates slowly and render polylines. This will give a feeling that some target is actually moving on map

8. Use a loop and do the same for many targets, so that many GPS targets will be on your Google Map (Multithreading experience)

Code Demo: http://hobbycoders.com/demos/gpssimulation   (view the source code of the demo)

If you face any problem with the logic/code i shared or if you need any code helps with this please email me or leave a comment here. If you have an alternate method also, please share.

Wednesday, January 25, 2012

[PHPBB MOD] Facebook like button in each topic / thread

This mod adds a facebook like button to each topic.
Tested with phpBB 3 (But there is no version dependent code, so it will work on all versions, if someone tests in other versions, please post the errors/success messages here)

Screenshot:


Steps:
1. OPEN FORUM_ROOT/includes/functions.php

2. FIND THE BLOCK:
--------------------
// Send a proper content-language to the output
    $user_lang = $user->lang['USER_LANG'];
    if (strpos($user_lang, '-x-') !== false)
    {
        $user_lang = substr($user_lang, 0, strpos($user_lang, '-x-'));
    }
--------------------

3. ADD BELOW:
--------------------
//Facebook like hack by Sriram.A.S. (sriramdasty7@gmail.com)
$pieces=explode("?",$user->page['page']);
if($pieces[0]=="viewtopic.php")
{
$curr_url=generate_board_url() .'/'. $pieces[0]."?";
$pieces=explode("&",$pieces[1]);
$flag_tmp=0;
foreach($pieces as $temp_x)
{
if(substr($temp_x,0,2)=="f=" && $flag_tmp==0)
{
$curr_url=$curr_url.$temp_x;
$flag_tmp=1;
break;
}
}
foreach($pieces as $temp_x)
{
if(substr($temp_x,0,2)=="t=" && $flag_tmp==1)
{
$curr_url=$curr_url."&".$temp_x;
break;
}
}
}
--------------------

4. In the same file, FIND
--------------------
'SITE_LOGO_IMG'            => $user->img('site_logo'),
--------------------

5. ADD BELOW
--------------------
'CURRENT_URL'      => $curr_url,
--------------------

6. SAVE file with changes

7. OPEN FORUM_ROOT/styles/{YOUR_TEMPLATE}/template/viewtopic_body.html

8. FIND FIRST OCCURANCE OF THE BLOCK
--------------------
<div class="buttons">
    <!-- IF not S_IS_BOT and S_DISPLAY_REPLY_INFO -->
--------------------

8. ADD BEFORE
--------------------
<div align="right">
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="{CURRENT_URL}" show_faces="true" width="450"></fb:like></div>
--------------------

9. If you want the like button also in bottom, fine the second occurance of the block and also add accordingly

10. SAVE file with changes

11. Now, Login to Administration Control Panel from frontend and in the main page,
Resynchronise or reset statistics-->Purge the cache-->Run now

If you face any problems, post here.

Tuesday, January 24, 2012

Web Programming using C / C++

Here is a small API in form of header file in C / C++ which will allow simple programs to be converted into web programs and can be run through a browser.

The guide is written in such a way that each and every step is explained carefully for people who are mainly from non computer origin. This will help you to create simple web pages and do some web server scripting.
The guide explains from downloading required softwares, configuring them, writing simple C web apps. It will be a good thing for non IT people who work with simple C. Now do anything in C code, and atlast send the outputs to browser, make it a web app!!

Please post any bugs/suggestions/feedbacks here. Thank you.

Download Link:
http://www.4shared.com/zip/IVyKtfue/web_scripting_using_c_c.html  (COMPLETE FILE, API, Example, Tutorial Guide)
OR
http://www.mediafire.com/?fjhyzmr73nncuj1

IMPORTANT: If you have the older release, the file home.c in examples directory, will fail to compile because of a line disableCache(); in it, please remove the line and compile the file. Initially i kept a function for cache management in the API and finally i thought of removing it, but forgot to update that file. Thankyou.

Thursday, January 5, 2012

Rise of planet of "Hobby Coders"

Passionate about coding? Want to work on something interesting (probably :P), and chase some unseen problem?? Well, welcome to the world of Hobby Coders.

Wondering what it is? Honestly, i myself dont know the answer fully as of now! :) But it will be a NON PROFIT team (not a company, not even an organization, just a team) of DEDICATED coders who are thinking to do something useful in coding in their leisure time (just a part of your leisure time, not even full leisure time)
So, if you can give us, say, 15 or 30 minutes a day, and is willing to join me, please let me know.
And BTW, Im not so rich to give you salary, its not a company or something that works for profit. Its about passion, dedication and learning. Anytime you can join me and if you want to leave sure, anytime!!

Who are you anyways? Im Sriram, nothing much to say about me. I try to learn work on stuffs like Computer Vision, Artificial Intelligence, Virtual Reality, Neural Networks, Distributed Systems. Im not an expert in anything, but i keep on learning and think of some new projects, products to be more accurate!

I know more than you! Why should i work under you? Well, i still didnt say, im going to lead the team. IT is an area where each and everyone will have a unique talent and unique approach for a problem. This team will not worry about who is going to lead, who is going to get name and fame, but we will work on what we are going to do. All products will have the list of people who have worked for it. Thats it. Its more a friendly team, not something that has a hierarchy of positions

Do you think this idea will be a success? Im not going to boast about this attempt, may be we will make some good products, or may be the attempt will loose focus with time, atleast you will take knowledge and learning with you

So what we will do? We will take some small and challenging problems and we will give code solutions for that, also we will release few projects in some areas for free
The activities are not yet planned, i want to see how many people are interested, if i get enough strength, then we will proceed, otherwise, this will be shutdown!
But to give an example of projects that we will work, may be we will make a web based script for hotel management, may be a small and efficient memory management algorithm, may be a data over voice system for data communication (currently planning), may be a javascript library for better image processing and lot more

I also want the help of experts in many domains like neural networks, image processing, computer vision, programming languages and so on. You dont even have to spend more time, atleast please guide us for our future ideas

If anyone interested, please email me your details: sriramdasty7@gmail.com
Based on the total participation and the level of expertise we get, further activities will be planned

Thanks and Regards
Sriram.A.S.
B.Tech[IT]
NIT Raipur 2011 Passout
sriramdasty7@gmail.com