Skip to content

MacLin : A technologic revolution

A blog of someone addicted to technology

Archive

Category: Apple

After having created the code for my last post, I began to search a way to improve it. In the version previously posted, I used a “double” and at the 1024th loop my Macbook is showing me an “inf” instead of a numerical value… which means that I have overflowed the value of a double. I then tried different thing, my goal was to get the “infinite” as far as possible. After some reflexion I decided to use the biggest value for Integer Unit and then Floating Point Unit. Using an “unsigned long long” I realized that the value would never become “infinite” but would stay constant at “18446744073709551615″ after the 63th execution loop. Using a “long double” I’ve been able to get the “infinite” to appear only after the 16383th execution loop with a value of “6E+4931″.

This bring me the interest of knowing the limits for the different types on my Macbook Pro. I search a bit on the net and found a simple piece of C code from “Alberto Bertogli” that simply display all the informations I was interested in.

max value of char: 127
min value of char: -128
max value of int: 2147483647
min value of int: -2147483648
max value of long: 2147483647
min value of long: -2147483648
min value of long long: -9223372036854775808
max value of long long: 9223372036854775807
max value of unsigned long long: 18446744073709551615
max value of signed char: 127
min value of signed char: -128
max value of short: 32767
min value of short: -32768
max value of unsigned char: 255
max value of unsigned int: 4294967295
max value of unsigned long 4294967295
max value of unsigned short: 65535
max value of double: 1.797693e+308
min value of double: 2.225074e-308
max value of double 10 exp: 308
min value of double 10 exp: -307
max value of long double: 1.18973e+4932
min value of long double: 3.3621e-4932
max value of long double 10 exp: 4932
min value of long double 10 exp: -4931
max value of float: 3.40282e+38
min value of float: 1.17549e-38
max value of float 10 exp: 38
min value of float 10 exp: -37
——————————————————-
sizeof char: 1
sizeof short: 2
sizeof int: 4
sizeof long: 4
sizeof long long: 8
sizeof double: 8
sizeof long double: 16
sizeof float: 4
——————————————————-
sizeof off_t: 8
sizeof size_t: 4
sizeof ssize_t: 4
name max: 255
path max: 1024

By executing this code I realized that the previously obtained “18446744073709551615″ was the maximum value of an “unsigned long long”. I then though that “6E+4931″ would be the maximum value of a “long double” but I was wrong, the limits for a “long double” is “10^4932″ and it’s easy to understand why I get the “infinite” after getting “6E+4931″ because by doing a multiplication by 2 and adding one (thats what the love code do) you get over the limit of a “long double” so … you get the “infinite”.


/*
 * limits.c
 * Alberto Bertogli (albertogli@telpin.com.ar)
 * 22/Oct/2002
 *
 * Shows the limits of data types.
 *
 * Try compiling it with "gcc -std=c99 limits.c -o limits"
 */

#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <float.h>
#include <fcntl.h>
#include <sys/param.h>	/* for PATH_MAX and NAME_MAX */

int main(void) {
	printf("max value of char: %d\n", CHAR_MAX);
	printf("min value of char: %d\n", CHAR_MIN);

	printf("max value of int: %d\n", INT_MAX);
	printf("min value of int: %d\n", INT_MIN);

	printf("max value of long: %ld\n", LONG_MAX);
	printf("min value of long: %ld\n", LONG_MIN);

#ifdef LLONG_MIN
	printf("min value of long long: %lld\n", LLONG_MIN);
	printf("max value of long long: %lld\n", LLONG_MAX);
	printf("max value of unsigned long long: %llu\n", ULLONG_MAX);
#else
	printf("long long limits unavailable (compile with -std=c99)\n");
#endif

	printf("max value of signed char: %d\n", SCHAR_MAX);
	printf("min value of signed char: %d\n", SCHAR_MIN);

	printf("max value of short: %d\n", SHRT_MAX);
	printf("min value of short: %d\n", SHRT_MIN);

	printf("max value of unsigned char: %u\n", UCHAR_MAX);
	printf("max value of unsigned int: %u\n", UINT_MAX);
	printf("max value of unsigned long %lu\n", ULONG_MAX);
	printf("max value of unsigned short: %u\n", USHRT_MAX);

	printf("max value of double: %e\n", DBL_MAX);
	printf("min value of double: %e\n", DBL_MIN);

	printf("max value of double 10 exp: %i\n", DBL_MAX_10_EXP);
	printf("min value of double 10 exp: %i\n", DBL_MIN_10_EXP);

	printf("max value of long double: %Lg\n", LDBL_MAX);
	printf("min value of long double: %Lg\n", LDBL_MIN);

	printf("max value of long double 10 exp: %i\n", LDBL_MAX_10_EXP);
	printf("min value of long double 10 exp: %i\n", LDBL_MIN_10_EXP);

	printf("max value of float: %g\n", FLT_MAX);
	printf("min value of float: %g\n", FLT_MIN);

	printf("max value of float 10 exp: %i\n", FLT_MAX_10_EXP);
	printf("min value of float 10 exp: %i\n", FLT_MIN_10_EXP);

	printf("-------------------------------------------------------\n");

	printf("sizeof char: %d\n", sizeof(char));
	printf("sizeof short: %d\n", sizeof(short));
	printf("sizeof int: %d\n", sizeof(int));
	printf("sizeof long: %d\n", sizeof(long));
	printf("sizeof long long: %d\n", sizeof(long long));
	printf("sizeof double: %d\n", sizeof(double));
	printf("sizeof long double: %d\n", sizeof(long double));
	printf("sizeof float: %d\n", sizeof(float));

	printf("-------------------------------------------------------\n");

	printf("sizeof off_t: %d\n", sizeof(off_t));
	printf("sizeof size_t: %d\n", sizeof(size_t));
	printf("sizeof ssize_t: %d\n", sizeof(ssize_t));
	printf("name max: %d\n", NAME_MAX);
	printf("path max: %d\n", PATH_MAX);

	return 0;

}
#include <iostream>
#define END_OF_LIFE 74*365    // 74 years in days
							  // Accordingly to the Deathclock I'll die at 74

int main (int argc, char * const argv[]) {

	int dayPassedTogether;
	long double love = 1;

	for(dayPassedTogether = 1; dayPassedTogether <= END_OF_LIFE; dayPassedTogether++) {
		printf("Each day my love for you is incremented : %0.LE\n",love);
		love += love;
		love++;
	}

    return 0;
}

Since I’ve been placed in the “Low-Level team” at Novariant, I’ve learn a lot of stuff regarding embedded system. Being in SONIA also helped to developed this interest in embedded and robotic stuff… but the requirements in SONIA are not really the same as those I encounter at work.

At work, the timing requirements are extremely important if you want to get the best precision in the industrie. Knowing that, and planning to start my career in the embedded computing world, I decided to take the course “LOG550 – Développement de système temps réels” which means that I will learn how to program on a microcontroller. Unfortunately, this courses is not in my standard program at university (not offered to GTI student), BUT, since I already worked for a company in the embedded field and that I was in the SONIA project it was easy to get a derogation for it.

The development board used in LOG550 is an STK500 from AVR and the microcontroller is the ATMEGA32. Since I like embedded stuff, I bought the kit. Instead of only being able to touch to this hardware at school, I can now “PLAY” with it at home.

Well, having the kit is one thing but programming it is something else and for that I must have a good IDE. Unfortunately, AVR is selling their things with an IDE that is made for Windows… I though that every embedded programmers would be smart enought to not use Windows and I can’t understand why AVR Studio is Windows only… But well there is better replacement anyway and Eclipse is one of them.

So here is how I setuped everything to make it work with my Macbook Pro. (The steps can easily be adapted for any platform because Eclipse is multiplatform and AVR toolchain also)

You need:

  • The STK500 starter kit
  • The ATMEGA32 Microcontroller
  • A serial to USB adapter (only if your computer doesn’t have a serial port)

Now lets start with the software installation. The first thing you need is the AVR toolchain so you’ll be able to compile stuff for the microcontroller. I’m using a Mac so the toolchain is in the “AVR MacPack” basicly it contains a special gcc for avr. For users of linux, check your package manager for it and for the unfortunate … there is WinAVR. Look here if you want more information on the toolchains.

Ok, now that we have our toolchains installed, lets move on Eclipse installation. I suggest you to download the bundle for C/C++ “Eclipse IDE for C/C++ Developers” to get a clean IDE. I’ve download the latest, which is called Ganymede at the moment. Now you need to install the AVR plugin, like any other plugins you use the update manager to do it.

Update Site (Eclipse 3.4)

Go to the Software update dialog (Help > Software Updates…)

Then select the Available Software tab. Click on the ‘Add Site… button o the right hand side and enter the URL of the update site:

URL: http://avr-eclipse.sourceforge.net/updatesite/

Then click on OK. The AVR Eclipse update site is now shown in the list of update sites. Select the “AVR Eclipse Plugin” and press the Install… button in the top right corner.

From the plugin’s wiki

You’re done ! Eclipse is installed and setuped with the AVR Plugin. Now you have to create a C project to begin… I followed this little guide and adapted it to my needs, basicly you change the toolchain, the MCU and the frequency. I have a 16MHz crystal so I changed it to 16000000.

There is still some little settings to make everything work. We will go to the properties of your new project and create a new programmer configuration.

Properties of your project  –> AVR –> AVRDude and select the Programmer tab. Now click on New… enter a name, select your programmer hardware (mine is an Atmel STK500 version 2.x Firmware) and if you have to use a USB to serial adapter give a path to it in the “Override default port (-P)” field. For me this window look like that:

programmer_config.png

I did not use the internal crystal of the ATMEGA32 so I had to change the Fuse Bits for it. Properties of your project  –> AVR –> AVRDude and select the Fuses tab and select Direct hex values. Now, make sure your STK500 is hooked to your computer and that the ATMEGA32 is in place and also that everything is powered. Then click on the third icon (the one that look likes an MCU) on the list like below.

This will read the current fuses bit from the MCU and populate the empty fields. Now we have to modify them so we can tell the MCU to use an external crystal. For this click on the first icon on the list to start the editor. The only parameter I had to change is “SUT_CKSEL”.

You should get something looking like this.

Now, for some reasons my configuration was not generating the HEX need for the MCU and AVR plugin. To fix this, go to the properties of your project –> C/C++ Build –> Settings –> Additionals Tools in Toolchain and make sure that “Generate HEX file for Flash memory” is checked.

Also, take a look at the other configurations there. By preference I changed “AVR Compiler –> Languages Standard” and set it to “ISO C99 + GNU Extensions” it adds “-std=gnu99″ to the compilation (with this you can now use a variable declaration in your for loop header). I also point the directories to the include folder of the AVR Toolchain to make it easier to see the different “.h” files inside eclipse.

I hope I did not forget something, if I did, simply drop me a comment.

Cheers

LaMs

Yeah, I know… a strange title, but I could not find something better and it remembered me of François Pérusse. Like you surely already know, I have a Mac and its a very nice Macbook Pro. What is also very cool when you buy a Mac, its the OS and the tools that come with it. Mac OSX is very polished, I enjoy using it everyday and Leopard have bring one of my favorite feature from a Linux that I was really missing when I switched… the virtual desktop ! OSX call it space but its the same thing, I was used to organised my desktop by “type”. For example, all web related stuff goes on Desktop #1, music stuff on Desktop #2, chat windows on the #3 and so on. Anyway, on Tiger I had to use “Desktop Manager” to get a similar feature but it had some glitch.

The OS is really nice, but what make using a Mac a pleasant experience is enforced by the set of tools that comes with it. Apple give iLife with their Mac so the customer can have a very easy to use complete media platform. With iLife you can create your personnal website easily (even if I don’t use it, its easy to understand), create your own DVD with all the menu and stuff, manage your pictures and compose your own music. Yes, that’s right ! You can compose your own music using GarageBand.

I was currious to know how this software work so I played with it a little bit. Well, I’m really not a fan of techno music… but well the beat was easy to get and I still managed to create a 4 min song.

TechnoTestGB

If you are interested in knowing a bit more about iLife and to know how to use GarageBand, Apple have a lot of tutorial on their website.

EDIT: It seems that GoVelib is no more available.

Wow, I just learned that my friend François Proulx have release an apps for the iPhone. This apps is called GoVelib and use the positioning fonctionnality of your iPhone to find bike from the Velib service in Paris.

Description:
GoVelib lets users of the self-service bike rental system in the city of Paris know at any moment where the nearest Velib stations are located. The application shows a map of the stations along with realtime information about available bikes and parking spots left.

You can find more informations about the apps, and follow the development on Frank’s blog. Fortunately for us, Frank said he will port his apps for Bixi, a similar bike rental service in Montreal that should come up in spring 2009.

Again, Kudos Frank!! By searching the name of the apps on the net, only one day after it as been launched, you can already find a thousand of results.

Frank, when you get back in Montreal for visit, I’ll see you this time !

Logo GoVelib

Logo GoVelib