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.



Wednesday, February 12, 2014

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

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.