Thursday, 16 April 2015

What is Error *error, Error **error and &error?

Let us consider below code, here we are passing a NSString pointer to pointer object. Which is same as call by reference in C language. But in Objective C we are taking every Object as reference type so taking pointer of an pointer object we need to write it as NSString **, not like NSString *.

In simple word if you understand the below code as Call by reference and call by value concept in Objective C. You can easily find out that why most of the methods are defined as Error **error instead of Error *error. We all know that In C and Objective C method can return only single value as return type. So put value in Error we need a pointer of Error.
Therefore in Objective C we passing Error ** as &error. It is all syntactical thing related to Objective C. Concept is the same as C language calling by reference and value.
Voila for reading this

-(void)changePointerOfString:(NSString **)strVal {
        *strVal = @"New Value";
}

-(void)changeOfString:(NSString *)strVal {
        strVal = @"New Value";
}


-(void)caller {
        NSString *myStr = @"Old Value";
        // myStr has value 'Old Value'
        NSLog(@"%@", myStr);

       [self changeOfString:myStr];
       // myStr has value 'Old Value'
       NSLog(@"%@", myStr);
       
       [self changePointerOfString:&myStr];
       // myStr has value 'New Value'
       NSLog(@"%@", myStr);
}

Sunday, 12 April 2015

Ultra sonic distance test on raspberry pi

Our Motive to test ultra sonic waves, when object detect blink a led to show. Print the distance of object.  I have added a LED to show up the power is ON, one LED is check for pulse checking, and last LED will blink when object will detect. Object detection distance is set to 75 cm (.75m).

Monday, 30 March 2015

Py: Temperature info update speaker out after some time automatic in mac os x

Basically the concept is simple, we are going to write few lines of code in python to get temperature from some URL (openweathermap.org). Then make a string which should speak out by speaker.
Then finally make this python script into cron job into your Mac OS X.

Step 1: Write simple python script code

import urllib # Web service fetch
import json   # JSON parse 
import os     # text to speech convert

## get data from server
url = 'http://api.openweathermap.org/data/2.5/weather?q=Jaipur,In&units=metric'

u = urllib.urlopen(url)

## json parse 
data = json.load(u)


## make final string which should speak out
finalMessage = 'In '+data['name']+ ' temprature is '+ str(data['main']['temp']) + ' celcius. Forcasting says it is expacting '+data['weather'][0]['main']


### finally text to speak out
os.system("say "+ finalMessage)
#os.system("echo "+ finalMessage)

Step 2 : Check the program is working
python weather.py

Amazon EC2 Cloud setup (php+mysql)

Friday, 27 March 2015

how can I check if a Gyroscope is present on iOS device?

- (BOOL) isGyroscopeAvailable
{
#ifdef __IPHONE_4_0
    CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    BOOL gyroAvailable = motionManager.gyroAvailable;
    [motionManager release];
    return gyroAvailable;
#else
    return NO;
#endif

}

Get IP address of iOS Device though program

#include <ifaddrs.h>
#include <arpa/inet.h>

- (NSString *)getIPAddress {

    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            if(temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];

                }

            }

            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    return address;

} 

Sunday, 22 March 2015

Parallax effect in iOS

Parallax effect demo

what is Parallax effect?
read from here : http://en.wikipedia.org/wiki/Parallax But in term of programming, we can simply say varing speed/size of two different visible objects shows parallax effect. Now a days in web sites and mobile application it is most common attractive point.
So How to achieve this?
One point is that just varing speed or size of two visible object, shows parallax effect. So lets assume a situation where scrollview has a image at top and when scroll down it must zoom up to center, and when scroll up image must be vary with different speed (should be slow).