Wednesday, February 26, 2014

C programming exercise: copy file

The following code copy file using C language, run in Raspberry Pi.

copy file using C

#include <fcntl.h>
#include <stdio.h>

int main(int argc, char *argv[]){
    
    int srcFileDesc;    //file descriptor of source file
    int destFileDesc;   //file descriptor of output file
    ssize_t numberOfRead;
    int BUFFER_SIZE = 1024;
    char buffer[BUFFER_SIZE];
    
    char *SRC_FILE ="test";
    char *DEST_FILE ="new_test";
    
    printf("copyfile:\n");
    printf("Copy file %s to %s\n", SRC_FILE, DEST_FILE);
    
    printf("Open file: %s\n", SRC_FILE);
    srcFileDesc = open("test", O_RDONLY);
    if(srcFileDesc != -1){
        
        printf("Create output file: %s\n", DEST_FILE);
        destFileDesc = open(DEST_FILE,
            O_CREAT|O_WRONLY|O_TRUNC,
            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
        if(destFileDesc != -1){
            
            while((numberOfRead=read(srcFileDesc, buffer, BUFSIZ)) > 0){
                if(write(destFileDesc, buffer, numberOfRead) != numberOfRead){
                    printf("Error in copying...!\n");
                }
            }
            
            if(numberOfRead == -1){
                printf("Something wrong...!\n");
            }
            
            if (close(destFileDesc) != -1){
                printf("Close destination file: %s\n", DEST_FILE);
            }else{
                printf("Error in Close destination file: %s\n", DEST_FILE);
            }
            
        }else{
            printf("Error in Create output file: %s\n", DEST_FILE);
        }
        
        if (close(srcFileDesc) != -1){
            printf("Close file: %s\n", SRC_FILE);
        }else{
            printf("Error in Close file: %s\n", SRC_FILE);
        }
    }else{
        printf("Cannot open file: %s\n", SRC_FILE);
    }
}

Tuesday, February 25, 2014

Stream music to the Raspberry Pi

This video show how to stream music to the Raspberry Pi by using it as a media renderer. Prepare the Pi, install all packages required, and play sample audio which has been streamed wirelessly to the device from a smartphone app.



Saturday, February 22, 2014

ARM Cortex-A Series Programmer's Guide version 4 is available now

The new ARM Cortex-A Series Programmer’s Guide, Version 4.0, is available in a PDF version HERE (requires registration for an ARM account).

ARM Cortex-A Series Programmer’s Guide, Version 4.0

Tuesday, February 18, 2014

Oracle Learning: Using a Raspberry Pi to Deploy JavaFX Applications

Oracle Learning Library released a tutorial of "Using a Raspberry Pi to Deploy JavaFX Applications". It covers how to configure a Raspberry Pi as a development platform for the JavaFX platform.

Begin Tutorial at HERE

Using a Raspberry Pi to Deploy JavaFX Applications
Oracle Learning Library: Using a Raspberry Pi to Deploy JavaFX Applications

Thursday, February 13, 2014

MagPi February 2014 out

MagPi February 2014
The FREE Raspberry Pi magazine, MagPi Issue 20, February 2014 is here.

Wednesday, February 12, 2014

Practical OpenCV

Hands on projects for Computer Vision on the Windows, Linux, and Raspberry Pi platforms
Practical OpenCV
Practical OpenCV
Practical OpenCV is a hands-on project book that shows you how to get the best results from OpenCV, the open-source computer vision library.

Computer vision is key to technologies like object recognition, shape detection, and depth estimation. OpenCV is an open-source library with over 2500 algorithms that you can use to do all of these, as well as track moving objects, extract 3D models, and overlay augmented reality. It's used by major companies like Google (in its autonomous car), Intel, and Sony; and it is the backbone of the Robot Operating System’s computer vision capability. In short, if you're working with computer vision at all, you need to know OpenCV.

With Practical OpenCV, you'll be able to:
  • Get OpenCV up and running on Windows or Linux.
  • Use OpenCV to control the camera board and run vision algorithms on Raspberry Pi.
  • Understand what goes on behind the scenes in computer vision applications like object detection, image stitching, filtering, stereo vision, and more.
  • Code complex computer vision projects for your class/hobby/robot/job, many of which can execute in real time on off-the-shelf processors.
  • Combine different modules that you develop to create your own interactive computer vision app.

What you’ll learn

  • The ins and outs of OpenCV programming on Windows and Linux
  • Transforming and filtering images
  • Detecting corners, edges, lines, and circles in images and video
  • Detecting pre-trained objects in images and video
  • Making panoramas by stitching images together
  • Getting depth information by using stereo cameras
  • Basic machine learning techniques
  • BONUS: Learn how to run OpenCV on Raspberry Pi

Who this book is for

This book is for programmers and makers with little or no previous exposure to computer vision. Some proficiency with C++ is required.

Table of Contents

Part 1: Getting comfortable
Chapter 1: Introduction to Computer Vision and OpenCV
Chapter 2: Setting up OpenCV on your computer
Chapter 3: CV Bling – OpenCV inbuilt demos
Chapter 4: Basic operations on images and GUI windows

Part 2: Advanced computer vision problems and coding them in OpenCV
Chapter 5: Image filtering
Chapter 6: Shapes in images
Chapter 7: Image segmentation and histograms
Chapter 8: Basic machine learning and keypoint-based object detection
Chapter 9: Affine and Perspective transformations and their applications to image panoramas
Chapter 10: 3D geometry and stereo vision
Chapter 11: Embedded computer vision: Running OpenCV programs on the Raspberry Pi

Develop Java Embedded Applications Using a Raspberry Pi

In this free 5-week course, Oracle Massive Open Online Course: Develop Java Embedded Applications Using a Raspberry Pi, learn to develop Java ME Embedded 8 Applications using a Raspberry Pi as your development platform! This course will leverage your Java skills and introduce you to the world of embedded devices and the Internet of Things (IoT). Purchase a Raspberry Pi and take the training for free.

You will learn how to:
* Read input data from switches and drive LED's using the GPIO interface
* Read temperature and barometric pressure from an I2C device
* Read the device's current location using a GPS UART device
* Store and manage data collected
* Report data to a client through a variety of communication options

Sunday, February 9, 2014

Adventures In Raspberry Pi

Adventures In Raspberry Pi
Adventures In Raspberry Pi
Coding for kids is cool with Raspberry Pi and this elementary guide

Even if your kids don't have an ounce of computer geek in them, they can learn to code with Raspberry Pi and this wonderful book. Written for 11- to 15-year-olds and assuming no prior computing knowledge, this book uses the wildly successful, low-cost, credit-card-sized Raspberry Pi computer to explain fundamental computing concepts. Young people will enjoy going through the book's nine fun projects while they learn basic programming and system administration skills, starting with the very basics of how to plug in the board and turn it on.

Each project includes a lively and informative video to reinforce the lessons. It's perfect for young, eager self-learners—your kids can jump in, set up their Raspberry Pi, and go through the lessons on their own.
  • Written by Carrie Anne Philbin, a high school teacher of computing who advises the U.K. government on the revised ICT Curriculum
  • Teaches 11- to 15-year-olds programming and system administration skills using Raspberry Pi
  • Features 9 fun projects accompanied by lively and helpful videos
  • Raspberry Pi is a $35/£25 credit-card-sized computer created by the non-profit Raspberry Pi Foundation; over a million have been sold
Help your children have fun and learn computing skills at the same time with Adventures in Raspberry Pi.

January 7, 2014  1118751256  978-1118751251 1


Linux command: list and display installed fonts

The Linux command xlsfonts list the fonts for X-server, and the commaand xfd display all the characters in an X font.

example:
$ xlsfonts
$ xfd -fn micro


Get my IP Address using Java

The below Java code, myIP.java, list my IP address.

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

class myIP {
    public static void main(String[] args) {
        System.out.println(getIpAddress());
    }
    
    private static String getIpAddress(){
        String ip = "";
        try {
            Enumeration<NetworkInterface> enumNetworkInterfaces = 
                NetworkInterface.getNetworkInterfaces();
            while(enumNetworkInterfaces.hasMoreElements()){
                NetworkInterface networkInterface = 
                    enumNetworkInterfaces.nextElement();
                Enumeration<InetAddress> enumInetAddress = 
                    networkInterface.getInetAddresses();
                while(enumInetAddress.hasMoreElements()){
                    InetAddress inetAddress = enumInetAddress.nextElement();
                    
                    String ipAddress = "";
                    if(inetAddress.isLoopbackAddress()){
                        ipAddress = "LoopbackAddress: ";
                    }else if(inetAddress.isSiteLocalAddress()){
                        ipAddress = "SiteLocalAddress: ";
                    }else if(inetAddress.isLinkLocalAddress()){
                        ipAddress = "LinkLocalAddress: ";
                    }else if(inetAddress.isMulticastAddress()){
                        ipAddress = "MulticastAddress: ";
                    }
                    
                    ip += ipAddress + inetAddress.getHostAddress() + "\n"; 
                }
            }    
        } catch (SocketException e) {
            ip += "Something Wrong! " + e.toString() + "\n";
        }
        
        return ip;
    }
}

Get my IP Address using Java
Get my IP Address using Java

Check System Properties of Raspberry Pi, using Java

Once installed Java 8 release candidate on Raspberry Pi, we can create a java program, listProperties.java, to list System Properties, to verify the installation.

listProperties.java
import java.util.Properties;
import java.util.Set;

class listProperties {
    
    public static void main(String[] args) {
        
        Properties properties = System.getProperties();
        System.out.println(properties.toString());
        System.out.println("\n");
        
        Set<String> setPropertyNames = 
            properties.stringPropertyNames();
        for (String propName : setPropertyNames) {
            System.out.println(
                propName + " : " +
                System.getProperty(propName));
        }
    }
}

To compile listProperties.java
$ javac listProperties.java

Run generated listProperties.class
$ java listProperties

The result will be something like:



pi@raspberrypi ~/worksJava $ java listProperties
{java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=/opt/jdk1.8.0/jre/lib/arm, java.vm.version=25.0-b69, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=:, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, user.country=GB, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=unknown, java.vm.specification.name=Java Virtual Machine Specification, user.dir=/home/pi/worksJava, java.runtime.version=1.8.0-b128, java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment, java.endorsed.dirs=/opt/jdk1.8.0/jre/lib/endorsed, os.arch=arm, java.io.tmpdir=/tmp, line.separator=
, java.vm.specification.vendor=Oracle Corporation, os.name=Linux, sun.jnu.encoding=UTF-8, java.library.path=/usr/java/packages/lib/arm:/lib:/usr/lib, java.specification.name=Java Platform API Specification, java.class.version=52.0, sun.management.compiler=HotSpot Client Compiler, os.version=3.10.28+, user.home=/home/pi, sun.arch.abi=gnueabihf, user.timezone=Asia/Hong_Kong, java.awt.printerjob=sun.print.PSPrinterJob, file.encoding=UTF-8, java.specification.version=1.8, java.class.path=., user.name=pi, java.vm.specification.version=1.8, sun.java.command=listProperties, java.home=/opt/jdk1.8.0/jre, sun.arch.data.model=32, user.language=en, java.specification.vendor=Oracle Corporation, awt.toolkit=sun.awt.X11.XToolkit, java.vm.info=mixed mode, java.version=1.8.0, java.ext.dirs=/opt/jdk1.8.0/jre/lib/ext:/usr/java/packages/lib/ext, sun.boot.class.path=/opt/jdk1.8.0/jre/lib/resources.jar:/opt/jdk1.8.0/jre/lib/rt.jar:/opt/jdk1.8.0/jre/lib/sunrsasign.jar:/opt/jdk1.8.0/jre/lib/jsse.jar:/opt/jdk1.8.0/jre/lib/jce.jar:/opt/jdk1.8.0/jre/lib/charsets.jar:/opt/jdk1.8.0/jre/lib/jfr.jar:/opt/jdk1.8.0/jre/classes, java.vendor=Oracle Corporation, file.separator=/, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.cpu.isalist=}



java.runtime.name : Java(TM) SE Runtime Environment

sun.boot.library.path : /opt/jdk1.8.0/jre/lib/arm
java.vm.version : 25.0-b69
java.vm.vendor : Oracle Corporation
java.vendor.url : http://java.oracle.com/
path.separator : :
java.vm.name : Java HotSpot(TM) Client VM
file.encoding.pkg : sun.io
user.country : GB
sun.java.launcher : SUN_STANDARD
sun.os.patch.level : unknown
java.vm.specification.name : Java Virtual Machine Specification
user.dir : /home/pi/worksJava
java.runtime.version : 1.8.0-b128
java.awt.graphicsenv : sun.awt.X11GraphicsEnvironment
java.endorsed.dirs : /opt/jdk1.8.0/jre/lib/endorsed
os.arch : arm
java.io.tmpdir : /tmp
line.separator : 


java.vm.specification.vendor : Oracle Corporation

os.name : Linux
sun.jnu.encoding : UTF-8
java.library.path : /usr/java/packages/lib/arm:/lib:/usr/lib
java.specification.name : Java Platform API Specification
java.class.version : 52.0
sun.management.compiler : HotSpot Client Compiler
os.version : 3.10.28+
user.home : /home/pi
sun.arch.abi : gnueabihf
user.timezone : Asia/Hong_Kong
java.awt.printerjob : sun.print.PSPrinterJob
file.encoding : UTF-8
java.specification.version : 1.8
user.name : pi
java.class.path : .
java.vm.specification.version : 1.8
sun.arch.data.model : 32
java.home : /opt/jdk1.8.0/jre
sun.java.command : listProperties
java.specification.vendor : Oracle Corporation
user.language : en
awt.toolkit : sun.awt.X11.XToolkit
java.vm.info : mixed mode
java.version : 1.8.0
java.ext.dirs : /opt/jdk1.8.0/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path : /opt/jdk1.8.0/jre/lib/resources.jar:/opt/jdk1.8.0/jre/lib/rt.jar:/opt/jdk1.8.0/jre/lib/sunrsasign.jar:/opt/jdk1.8.0/jre/lib/jsse.jar:/opt/jdk1.8.0/jre/lib/jce.jar:/opt/jdk1.8.0/jre/lib/charsets.jar:/opt/jdk1.8.0/jre/lib/jfr.jar:/opt/jdk1.8.0/jre/classes
java.vendor : Oracle Corporation
file.separator : /
java.vendor.url.bug : http://bugreport.sun.com/bugreport/
sun.cpu.endian : little
sun.io.unicode.encoding : UnicodeLittle
sun.cpu.isalist : 




Java 8 release candidate is available (with steps to install on Raspberry Pi)

As announced in the post "Update on JDK 8 builds and release candidate status", the first release candidate build of JDK 8, b128 is available on https://jdk8.java.net/download.html. Including supported platform of Linux ARMv6/7 VFP, HardFP ABI, it can be run on Raspberry Pi. Here show the steps to install on Raspberry Pi.

In the steps shown here, visit https://jdk8.java.net/download.html on host PC, click to Accept License Agreement to get the actually download link of Linux ARMv6/7 VFP, HardFP ABI, it's http://www.java.net/download/jdk8/archive/b128/binaries/jdk-8-fcs-b128-linux-arm-vfp-hflt-31_jan_2014.tar.gz currently for Build b128.

JDK8 Download page
https://jdk8.java.net/download.html
Run on LXTerminal in Raspberry Pi:

  • Enter the command to downlaod the tar.gz with wget:
    wget --no-check-certificate http://www.java.net/download/jdk8/archive/b128/binaries/jdk-8-fcs-b128-linux-arm-vfp-hflt-31_jan_2014.tar.gz
  • Un-compress the downloaded tar.gz:
    sudo tar zxvf jdk-8-fcs-b128-linux-arm-vfp-hflt-31_jan_2014.tar.gz -C /opt
    The un-compressed jdk8 will be install in /opt/jdk1.8.0
  • To set the new JDK8 as default java and javac, enter the command:
    sudo update-alternatives --install /usr/bin/javac javac /opt/jdk1.8.0/bin/javac 1
    sudo update-alternatives --install /usr/bin/java java /opt/jdk1.8.0/bin/java 1

    Run the following commands to select jdk8 as default java:
    sudo update-alternatives --config javac
    sudo update-alternatives --config java
  • Now you can run the commands to verify default jdk version:
    $ java -version
    $ javac -version



After installed, you can create a test program to list System Properties to verify the installation.