Here I'll be posting some videos about the Raspberry Pi, when they become avaliable.
Presentation of one of the first final boards:
First video of the Gertboard connected to the Raspberry Pi's GPIO:
Mostrando postagens com marcador Programming. Mostrar todas as postagens
Mostrando postagens com marcador Programming. Mostrar todas as postagens
quinta-feira, dezembro 29, 2011
quarta-feira, dezembro 28, 2011
The abusive price of electronics in Brazil
Last month, I've heard about the Raspberry Pi project. It's about a US$25~35 board, with an 700 MHz ARM processor (the Broadcom BCM 2835), 128/256 MB of RAM, USB, HDMI and Ethernet (depending of the model) connectors. Those boards are suposed to run some Linux distributions (initially Fedora and Debian) and programming languages such as Python, Java, C (and others), and it's aimed to educational proposes (but can you imagine the things that could be possible to do with such hardware?)
I've been very excited with the news that the Raspberry Pi final boards would be finished in December/2011 or January/2012, and they would be sold first in United Kingdom and maybe in the United States. Although, at the same time, I got worried about the possibility of that board never come to Brazil (the country I live) or to become a very expensive product that only few people could get access to. Since it's not being sold yet (until the publication of this post), I had a hope that all my worries could not become true.
Today, I knew that there is another similar project, but with a board (the BeagleBoard) that is more sophisticated than the Raspberry Pi, and is being sold for the recommended price of US$149 in the rest of the world, but in Brazil, due to a restriction of Texas Instruments that prohibits brazilians to buy that board directly from the United States through the Internet, it costs about R$850~975 !!!

Just for comparison, today US$1,00 is about R$1,90. So, we can conclude is that the BeagleBoard is being sold for, at least, 3 times it's recommended price.

I really hope that the Raspberry Pi can become a reality in brazilian education (from schools to universities), and I can't wait to get my own board. But if it costs much more than US$25~35, I'm sure that the dream is really, really over!
I've been very excited with the news that the Raspberry Pi final boards would be finished in December/2011 or January/2012, and they would be sold first in United Kingdom and maybe in the United States. Although, at the same time, I got worried about the possibility of that board never come to Brazil (the country I live) or to become a very expensive product that only few people could get access to. Since it's not being sold yet (until the publication of this post), I had a hope that all my worries could not become true.
Today, I knew that there is another similar project, but with a board (the BeagleBoard) that is more sophisticated than the Raspberry Pi, and is being sold for the recommended price of US$149 in the rest of the world, but in Brazil, due to a restriction of Texas Instruments that prohibits brazilians to buy that board directly from the United States through the Internet, it costs about R$850~975 !!!

Just for comparison, today US$1,00 is about R$1,90. So, we can conclude is that the BeagleBoard is being sold for, at least, 3 times it's recommended price.

I really hope that the Raspberry Pi can become a reality in brazilian education (from schools to universities), and I can't wait to get my own board. But if it costs much more than US$25~35, I'm sure that the dream is really, really over!
domingo, setembro 18, 2011
The Unix Time
Don't you know what is Unix Time?
According to Wikipedia: "Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds."
It's a linear representation of time, because we're just working with the amount of seconds that have passed from an event in the past (January, 1, 1970 00:00 - UTC time). Hence, it is widely used in computer systems because it's easy to store (only one "integer" variable is needed), and it makes some time calculations easier. Basically, you will work with dates the same way you work with integer numbers.
Bellow, we list some examples about how to get the Unix Time in different programming languages:
C#:
(DateTime.UtcNow - new DateTime(1970,1,1,0,0,0)).TotalSeconds;
Java:
System.currentTimeMillis()/1000;
JavaScript:
Math.round(new Date().getTime()/1000.0);
MySQL:
SELECT unix_timestamp(now())
Oracle PL/SQL:
SELECT (SYSDATE - TO_DATE('01/01/1970 00:00:00', 'MM-DD-YYYY HH24:MI:SS')) * 24 * 60 * 60 FROM DUAL
Perl:
time
PHP:
time()
PostgreSQL:
SELECT extract(epoch FROM now())
Python:
time.time()
R:
as.numeric(Sys.time())
Ruby:
Time.now
SQL Server:
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
Unix or Linux shell:
date +%s
According to Wikipedia: "Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds."
It's a linear representation of time, because we're just working with the amount of seconds that have passed from an event in the past (January, 1, 1970 00:00 - UTC time). Hence, it is widely used in computer systems because it's easy to store (only one "integer" variable is needed), and it makes some time calculations easier. Basically, you will work with dates the same way you work with integer numbers.
Bellow, we list some examples about how to get the Unix Time in different programming languages:
C#:
(DateTime.UtcNow - new DateTime(1970,1,1,0,0,0)).TotalSeconds;
Java:
System.currentTimeMillis()/1000;
JavaScript:
Math.round(new Date().getTime()/1000.0);
MySQL:
SELECT unix_timestamp(now())
Oracle PL/SQL:
SELECT (SYSDATE - TO_DATE('01/01/1970 00:00:00', 'MM-DD-YYYY HH24:MI:SS')) * 24 * 60 * 60 FROM DUAL
Perl:
time
PHP:
time()
PostgreSQL:
SELECT extract(epoch FROM now())
Python:
time.time()
R:
as.numeric(Sys.time())
Ruby:
Time.now
SQL Server:
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
Unix or Linux shell:
date +%s
sexta-feira, abril 22, 2011
String format for DateTime in C#
In C# language, there are the following custom format specifiers for date and time:
y - year
M - month
d - day
h - hour 12
H - hour 24
m - minute
s - second
f - second fraction
F - second fraction, trailing zeroes are trimmed
t - P.M or A.M
z - time zone
Some examples on how to use them (the output is commented at the end of each line):
y - year
M - month
d - day
h - hour 12
H - hour 24
m - minute
s - second
f - second fraction
F - second fraction, trailing zeroes are trimmed
t - P.M or A.M
z - time zone
Some examples on how to use them (the output is commented at the end of each line):
/*
In this example, we create an DateTime object (whose name is "dt") that representes
the date and time: 2008-03-09 16:05:07.123
*/
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
segunda-feira, abril 18, 2011
Some important Taylor/Maclaurin series
Have you ever asked yourself about how your scientific calculator or your computer "know" the sine or the cosine of a number?
Maybe you thought that those devices had predefined tables inside their memories to calculate those functions. But in fact those tables don't exist! So, how those devices are able to give you the correct answer with great precision?
The truth is that they use algorithms that calculate the results of Maclaurin functions. According to Wikipedia: "A Taylor series is a representation of a function as an infinite sum of terms calculated from the values of its derivatives at a single point. Taylor series were formally introduced by the English mathematician Brook Taylor in 1715. If the series is centered at zero, the series is also called a Maclaurin series, named after the Scottish mathematician Colin Maclaurin who made extensive use of this special case of Taylor series in the 18th century."
This is an important subject of Calculus, but the objective of this post is to show some of those functions, so we won't make demonstrations here. If you really want to study it further, I recommend you to check the book: "Calculus - Volume 2, by James Stewart". Bellow you can appreciate that interesting way to calculate some well known functions:
The exponential function:
Sine and Cosine (and hyperbolic ones):



We can tell you those are ways to approximate the functions values, and precision depends only of the value of n. Note that those sommations go from zero to the infinity, then the value of those sommations converge to the real values of those functions, but computers can't calculate them to infinity. Thus, we calculate each sommation to a finite (and relatively "small") value of n, in a manner that satisfies the desired precision that we need. One example is given bellow:
Here we calculate the exponential function for x = 2 (that is: e^2)
For n = 1 , the sommation is: 1.0
For n = 2 , the sommation is: 3.0
For n = 3 , the sommation is: 5.0
For n = 4 , the sommation is: 6.33333333333
For n = 5 , the sommation is: 7.0
For n = 6 , the sommation is: 7.26666666667
For n = 7 , the sommation is: 7.35555555556
For n = 8 , the sommation is: 7.38095238095
For n = 9 , the sommation is: 7.3873015873
For n = 10 , the sommation is: 7.38871252205
For n = 11 , the sommation is: 7.38899470899
For n = 12 , the sommation is: 7.38904601571
For n = 13 , the sommation is: 7.38905456683
For n = 14 , the sommation is: 7.38905588239
For n = 15 , the sommation is: 7.38905607033
For n = 16 , the sommation is: 7.38905609538
For n = 17 , the sommation is: 7.38905609852
For n = 18 , the sommation is: 7.38905609888
For n = 19 , the sommation is: 7.38905609893
For n = 20 , the sommation is: 7.38905609893
According to the built-in function of Python's math library, e^2 is: 7.38905609893
In this example, we conclude that n equal or greater than 19 gives a good precision (if compared with Python's built-in function) for x = 2. But for greater values of x, we should increase the number of iterations. Let's check another example, for x = 5:
For n = 1 , the sommation is: 1.0
For n = 2 , the sommation is: 6.0
For n = 3 , the sommation is: 18.5
For n = 4 , the sommation is: 39.3333333333
For n = 5 , the sommation is: 65.375
For n = 6 , the sommation is: 91.4166666667
For n = 7 , the sommation is: 113.118055556
For n = 8 , the sommation is: 128.619047619
For n = 9 , the sommation is: 138.307167659
For n = 10 , the sommation is: 143.68945657
For n = 11 , the sommation is: 146.380601025
For n = 12 , the sommation is: 147.603848505
For n = 13 , the sommation is: 148.113534955
For n = 14 , the sommation is: 148.309568205
For n = 15 , the sommation is: 148.37958008
For n = 16 , the sommation is: 148.402917371
For n = 17 , the sommation is: 148.410210275
For n = 18 , the sommation is: 148.412355247
For n = 19 , the sommation is: 148.412951072
For n = 20 , the sommation is: 148.413107868
For n = 21 , the sommation is: 148.413147067
For n = 22 , the sommation is: 148.4131564
For n = 23 , the sommation is: 148.413158522
For n = 24 , the sommation is: 148.413158983
For n = 25 , the sommation is: 148.413159079
For n = 26 , the sommation is: 148.413159098
For n = 27 , the sommation is: 148.413159102
For n = 28 , the sommation is: 148.413159102
For n = 29 , the sommation is: 148.413159103
For n = 30 , the sommation is: 148.413159103
According to the built-in function of Python's math library, e^5 is:148.413159103
Now note that n = 19 isn't enough to give the same precision as before. And the precision for n = 19 will get worse for greater values of x. Thus, the greater is x, the greater is the number of necessary iterations to get a good approximation of the real value of the function.
Maybe you thought that those devices had predefined tables inside their memories to calculate those functions. But in fact those tables don't exist! So, how those devices are able to give you the correct answer with great precision?
The truth is that they use algorithms that calculate the results of Maclaurin functions. According to Wikipedia: "A Taylor series is a representation of a function as an infinite sum of terms calculated from the values of its derivatives at a single point. Taylor series were formally introduced by the English mathematician Brook Taylor in 1715. If the series is centered at zero, the series is also called a Maclaurin series, named after the Scottish mathematician Colin Maclaurin who made extensive use of this special case of Taylor series in the 18th century."
This is an important subject of Calculus, but the objective of this post is to show some of those functions, so we won't make demonstrations here. If you really want to study it further, I recommend you to check the book: "Calculus - Volume 2, by James Stewart". Bellow you can appreciate that interesting way to calculate some well known functions:
The exponential function:
Sine and Cosine (and hyperbolic ones):



We can tell you those are ways to approximate the functions values, and precision depends only of the value of n. Note that those sommations go from zero to the infinity, then the value of those sommations converge to the real values of those functions, but computers can't calculate them to infinity. Thus, we calculate each sommation to a finite (and relatively "small") value of n, in a manner that satisfies the desired precision that we need. One example is given bellow:
Here we calculate the exponential function for x = 2 (that is: e^2)
For n = 1 , the sommation is: 1.0
For n = 2 , the sommation is: 3.0
For n = 3 , the sommation is: 5.0
For n = 4 , the sommation is: 6.33333333333
For n = 5 , the sommation is: 7.0
For n = 6 , the sommation is: 7.26666666667
For n = 7 , the sommation is: 7.35555555556
For n = 8 , the sommation is: 7.38095238095
For n = 9 , the sommation is: 7.3873015873
For n = 10 , the sommation is: 7.38871252205
For n = 11 , the sommation is: 7.38899470899
For n = 12 , the sommation is: 7.38904601571
For n = 13 , the sommation is: 7.38905456683
For n = 14 , the sommation is: 7.38905588239
For n = 15 , the sommation is: 7.38905607033
For n = 16 , the sommation is: 7.38905609538
For n = 17 , the sommation is: 7.38905609852
For n = 18 , the sommation is: 7.38905609888
For n = 19 , the sommation is: 7.38905609893
For n = 20 , the sommation is: 7.38905609893
According to the built-in function of Python's math library, e^2 is: 7.38905609893
In this example, we conclude that n equal or greater than 19 gives a good precision (if compared with Python's built-in function) for x = 2. But for greater values of x, we should increase the number of iterations. Let's check another example, for x = 5:
For n = 1 , the sommation is: 1.0
For n = 2 , the sommation is: 6.0
For n = 3 , the sommation is: 18.5
For n = 4 , the sommation is: 39.3333333333
For n = 5 , the sommation is: 65.375
For n = 6 , the sommation is: 91.4166666667
For n = 7 , the sommation is: 113.118055556
For n = 8 , the sommation is: 128.619047619
For n = 9 , the sommation is: 138.307167659
For n = 10 , the sommation is: 143.68945657
For n = 11 , the sommation is: 146.380601025
For n = 12 , the sommation is: 147.603848505
For n = 13 , the sommation is: 148.113534955
For n = 14 , the sommation is: 148.309568205
For n = 15 , the sommation is: 148.37958008
For n = 16 , the sommation is: 148.402917371
For n = 17 , the sommation is: 148.410210275
For n = 18 , the sommation is: 148.412355247
For n = 19 , the sommation is: 148.412951072
For n = 20 , the sommation is: 148.413107868
For n = 21 , the sommation is: 148.413147067
For n = 22 , the sommation is: 148.4131564
For n = 23 , the sommation is: 148.413158522
For n = 24 , the sommation is: 148.413158983
For n = 25 , the sommation is: 148.413159079
For n = 26 , the sommation is: 148.413159098
For n = 27 , the sommation is: 148.413159102
For n = 28 , the sommation is: 148.413159102
For n = 29 , the sommation is: 148.413159103
For n = 30 , the sommation is: 148.413159103
According to the built-in function of Python's math library, e^5 is:148.413159103
Now note that n = 19 isn't enough to give the same precision as before. And the precision for n = 19 will get worse for greater values of x. Thus, the greater is x, the greater is the number of necessary iterations to get a good approximation of the real value of the function.
segunda-feira, janeiro 12, 2009
How to build JNI applications using Dev-C++ and Netbeans
JNI is an acronym of Java Native Interface. It is a programming framework that allows your Java code to call and to be called by native applications. So, you can write libraries in other languages, such as Assembly, C/C++, or even C#.
Here, we are going to talk about how to write a DLL in C/C++, using Dev-C++ 4.9.9.2 (avaliable at: http://www.bloodshed.net/devcpp.html). Dev-C++ is not upgraded for a long time, so feel free to use CodeBlocks (the process of building a DLL is almost the same. You can download Codeblocks at: http://www.codeblocks.org). I used Dev-C++ because it seemed nice to me.
Before we start, let's talk about some advantages and disadvantages of JNI:
Using JNI you can...:
* Call Windows API functions.
* Increase execution speed.
* Call API functions of some product which is in C or C++, from Java Client.
Some disadvantages:
* Forget "write once run anywhere" (your code is not portable between different platforms).
* It is difficult to debug runtime error in native code.
* There is a potencial security risk.
* Only applications and signed Applets can invoke the JNI.
* There is no garbage collection at the JNI side (This means that you must dealloc the used memory your code previously alloced).
Now that you know the advantages and risks, let's show an example about how to use JNI with Dev-C++ and Netbeans.
First, configure Dev-C++. Go to Tools -> Compiler Options and create a new compiler set.
Here, we are going to talk about how to write a DLL in C/C++, using Dev-C++ 4.9.9.2 (avaliable at: http://www.bloodshed.net/devcpp.html). Dev-C++ is not upgraded for a long time, so feel free to use CodeBlocks (the process of building a DLL is almost the same. You can download Codeblocks at: http://www.codeblocks.org). I used Dev-C++ because it seemed nice to me.
Before we start, let's talk about some advantages and disadvantages of JNI:
Using JNI you can...:
* Call Windows API functions.
* Increase execution speed.
* Call API functions of some product which is in C or C++, from Java Client.
Some disadvantages:
* Forget "write once run anywhere" (your code is not portable between different platforms).
* It is difficult to debug runtime error in native code.
* There is a potencial security risk.
* Only applications and signed Applets can invoke the JNI.
* There is no garbage collection at the JNI side (This means that you must dealloc the used memory your code previously alloced).
Now that you know the advantages and risks, let's show an example about how to use JNI with Dev-C++ and Netbeans.
First, configure Dev-C++. Go to Tools -> Compiler Options and create a new compiler set.

Now, go to the Directories tab (as shown bellow) and add the include paths of JDK to C and C++ (be careful here! As I said, you should add to C and then to C++ !!!).
You should add the following directories (you can find them in the JDK folder):C:\Program Files\Java\jdk1.6.0_07\include
C:\Program Files\Java\jdk1.6.0_07\include\win32
Those are the folders of my computer! They can change according to your JDK version, OS languaage/partition. Be careful!
If you are curious, inside those folders you can find the jni.h file, that you are going to need ahead.
Now that you configured Dev-C++, go to File -> New -> Project (at the top menu), and create a DLL project.

Then, delete the files that came with the project (in the example: dllmain.cpp and dll.h).

Now, create new files in the project (Example.h and Example.cpp).


Here you can see the JNI side code. The header files and functions should be written as shown. The declaration of a function is composed by the following: Java_PackageName_ClassName_methodName (look again to the images above).
To create a DLL, simply Compile your DLL project (in the top Menu, go to: Execute -> Compile).
Finally, create a Java Application at NetBeans (avaliable at: http://www.netbeans.org). At the java project, you should create a package equals to PackageName and a class equals to ClassName.
Copy the DLL into your java project folder.
To create a DLL, simply Compile your DLL project (in the top Menu, go to: Execute -> Compile).
Finally, create a Java Application at NetBeans (avaliable at: http://www.netbeans.org). At the java project, you should create a package equals to PackageName and a class equals to ClassName.
Copy the DLL into your java project folder.

Now, make a reference to your DLL using System.loadLibrary method. To call a native method, you should create a "signature" method on Java side, using the native keyword. See the code bellow:

Now, compile you class and run it! :-)
Assinar:
Postagens (Atom)




