Monday Jul 29, 2013

Emulating I2C Devices with Java ME Embedded 3.3

In this post, I will show you how to create an emulated inter-integrated circuit (I2C) device using the Oracle Java ME SDK 3.3 Custom Device Editor and the Embedded Support API.

Oracle's Java ME SDK 3.3 is a fantastic tool to learn how to create applications for embedded devices. The focus of ME Embedded is the Information Module Profile - Next Generation (IMP-NG) headless devices - simple micro controllers with 160KB of memory (or more) designed to read sensor input or control small mechanical devices. The embedded market is growing rapidly as more devices become connected to the "internet of things."

New in the 3.3 version of the SDK is support for a host of peripheral devices, including GPIO, ADC/DAC, UART, I2C, SPI, MMIO and more. The SDK includes an emulator (for Windows) and you can choose between one of two default devices that support IMP-NG.

While the default emulator is useful to start learning Java ME Embedded, at some point you will want an emulator that resembles the target embedded device. This is where the Java ME SDK really shines, by allowing you to design your own emulator. Through the Custom Device Editor, provided with the Java ME SDK 3.3, you select the peripheral devices your physical embedded device supports, including all of relevant information to access the peripherals: hardware port number, pin number, trigger mode, etc. Designing an emulator that matches your physical embedded device can greatly shorten the development cycle of an embedded application.

Start the Custom Device Editor from the command line C:\Java_ME_platform_SDK_3.3\bin\device-editor.exe or through NetBeans and Eclipse. Using the editor, you specify GPIO pins and ports, ADC and DAC devices, and pulse counters as needed. To add serial communication devices (I2C, SPI and MMIO), the editor provides two options: add a simple loopback that echoes back bytes as they are written to the device, or add an implementation of the device using the embedded support API.

Of the three serial bus specifications, I2C is the simplest. It is a two-wire protocol, thus requiring only four lines. For more detailed information on the specification, click here.

The installation directory of the Java ME SDK 3.3, C:\Java_ME_platform_SDK_3.3, contains the documentation and a JAR for the Embedded Support API. Expand the file located under \docs\api and look at the package.

To emulate an I2C device, you create a class that implements the I2CSlaveBus interface. In NetBeans (or Eclipse) create a Java ME Embedded Application project. Add the \lib\embedded-support-api.jar to the project, then add a Java class that implements the interface:

public class TMP102Device implements I2CSlaveBus { ... }

There are just four methods to implement:

  • int read(byte[] data, int len, I2CSlaveBus.I2CSlaveIdentifier id)
  • void write(byte[] data, I2CSlaveBus.I2CSlaveIdentifier id)
  • void initialize(I2CSlaveBus.I2CSlaveIdentifier id)
  • void close(I2CSlaveBus.I2CSlaveIdentifier id)

The read method writes bytes into the byte array passed to the method as an argument and returns a count of the bytes written to the array. The write method can be used to signal the device for some action. The initialize method is called every time the device is accessed through a call - this method can be used to reset the internal state of your emulated device. Finally, the close method should release any resources the emulated device is using.

I choose to emulate a simple I2C temperature device, the Texas Instruments TMP102, a digital temperature sensor with I2C communication capability. After power-on, this device returns two bytes from an internal buffer every time it is read. The first byte contains the left-most 8 bits of a 12-bit word, and the second byte contains the 4 least significant bytes.

This 12-bit value represents a count of 0.0625 degree (Celsius) increments, using the first high order bit to indicate values below 0. Positive temperature values are converted directly to an integer and multiplied by the increment to get the temperature value. For example, if the 12-bit word is 0x320 (0011 0010 000), the temperature is calculated as 0x320 = 800 * 0.0625 = 50 degrees Celsius.

Negative temperature values have a 1 in the high-order bit, and the temperature value is calculated by the 2's complement of the count minus 1. For example, if the 12-bit word is 0xE70 (1110 0111 0000), the temperature is calculated as 0xE70 - 1 = 0xE6F (1110 0110 1111), 2's complement = 0001 1001 000 = 400 * (-0.0625) = -25 degrees C.

Rather than just create a static TMP102 device (returning the same temperature over and over), the class I wrote simulates temperature fluctuations with a thread that randomly changes the "temperature" value by a maximum of +/- .5 degree C every 5 seconds. I start this thread in the initialize method and kill it through the close method. The timing and range of temperature fluctuations is adjustable. To see the complete code, click here to download the NetBeans project for the TMP102 emulator.

 To add the TMP102 device to a custom emulator, start by creating a jar file of the project. In NetBeans, you can right-click the project and select Build. A jar file will be created in the dist folder of your NetBeans project.

Next create or modify an existing custom IMP-NG emulator. Start the Custom Device Editor from the command line or through your IDE. Select IMP-NG in the Custom Device Editor Dialog and click New to create a new IMP-NG emulator device, or select one you already have and click Edit.

In the IMP-NG Device editor window, select the I2C tab. Click Custom, then click the Browse button to navigate to the directory where the jar file is located. The implementation class name is the fully qualified name of the I2CSlaveBus class. Enter oracle.example.TMP102Device in the Implementation Class Name field. Click the Add button in the lower right to create a Slave entry.

At this point, you can choose to modify the ID, Name, Bus Number, Address Size and Address of the slave device by clicking in each field and typing. Since my design goal is to emulate a TMP102 device connected to a Raspberry Pi, Model B, I changed the bus number to 1 and the address to 48 (this is a hex number). I2C devices on the Pi use bus 1, and the default address and address size of the TMP102 is 0x48 (72) and 7-bit.

Finally, click OK, and in a few minutes you will see a message that the new/updated emulator is registered with the Device manager. Next, write some code to test the emulated I2C device. You could open the device using the String name of the emulated device like this:

I2CDevice tmp102 = (I2CDevice)"Slave0", I2CDevice.class, null);

However, to emulate opening the device the way it would be opened when attached to a Raspberry Pi, you need to create an I2CConfig object first and pass that to PeripheralManager to open the device:

I2CDeviceConfig config = new I2CDeviceConfig(1, 0x48, 7, 10000); // Bus 1, address 0x48, 7-bit addressing, 10KHz clock

I2CDevice tmp102 = (I2CDevice);

If you prefer, you can run this small NetBeans embedded application project I created to test the emulated TMP102 device.


Tuesday Jul 02, 2013

OLL-Live Java EE 7: Using WebSockets for Real-Time Communication

OLL-Live offers FREE, one-hour interactive webinars from Oracle. At an OLL Live webinar, you will experience an information packed session led by an Oracle expert showing you ways you can use Oracle products. Our speaker this time is Eduardo Moranchel, Java Curriculum Developer. Eduardo's topic is Using WebSockets for Real-Time Communication.

See how WebSocket and JSON technologies can help you build more interactive Java EE applications. You will also learn how to build an application using HTML 5 for the front end and WebSocket with JSON in the back end. The application that will be demoed is a collaborative sticker book application that was featured in the Java EE 7 embracing HTML 5 article in May/June edition of the Java Magazine.

July 10, 2013, at 8:00 AM PT

About the speaker

Eduardo Moranchel, is a Curriculum Developer at Oracle's Mexico Development Center. Eduardo has extensive experience designing and developing applications using Java. He enjoys sharing his experience and passion for the Java platform by developing courses and tutorials for the newest Java technologies. He is co-organizer of the Java User Group in Guadalajara, Mexico. He co-authored the Java EE 7 embracing HTML 5 article in the most recent Java Magazine.

Wednesday Jun 19, 2013

Java EE 7: Embracing HTML5

The latest issue of Java Magazine features an article by two of our Java Curriculum Developers: Edgar Martinez and Eduardo Moranchel. The article talks about how Java EE 7 supports and integrates HTML5 with enhancements to JSF and with two new APIs: WebSocket and JSON Processing (JSON-P).

The article uses a baseline application for kids in which they can drag and drop stickers onto a canvas at the same time as other children, collaboratively creating a "Sticker Story". You can download the application code from the Java Magazine article. You can download Java EE 7 from here.

Happy coding with Java EE 7!

Thursday Jun 13, 2013

Introducing OLL Live

OLL-Live offers FREE, one-hour interactive webinars from Oracle. At an OLL Live webinar, you will experience an information packed session led by an Oracle expert showing you ways you can use Oracle products. Our first speaker is Tom McGinn, Java Curriculum Developer. Tom's topic is Securing ADF Applications in Oracle Cloud by Using JDeveloper.

See how to use Oracle ADF Security with Oracle Cloud's built-in identity management. Using a simple ADF application, you will see how to use declarative, permission-based protection for ADF bounded task flows, top-level web pages that use ADF bindings, and ADF entity objects and their attributes.

June 26, 2013, at 8:00 AM PT

Friday Jun 07, 2013

Java is now better than ever with the release of Java EE 7.

Join Oracle and 9 Million Developers to Make the Future Java

Oracle and participants from throughout the Java community to learn how Java EE 7 can help you:

  • Deliver HTML5 dynamic, scalable applications—Reduce response times, simplify JSON parsing, create RIA using WebSocket, and deliver high-performance RESTful services
  • Increase developer productivity—Simplify architectures, reduce boilerplate code, add more annotated POJOs, and enhance portability
  • Exceed enterprise requirements—Process batch jobs more efficiently, easily define managed concurrent tasks, and simplify application integration

Choose a convenient time—two are offered—then join Oracle and 9 million developers as we make the future Java.

Register now for the keynote address, Java Specification Request (JSR) overview sessions, and live chat with Java EE 7 experts.

Monday May 06, 2013

Java Certification: Your Burning Questions Answered

Do you have questions about Java Certification? Kate Jones, of the Oracle University Java Certification team, answers your most frequently asked questions regarding Java Certification. Read about it here.

Friday May 03, 2013

Are You Ready to Start Java Embedded Programming?

Oracle has produced some excellent demonstrations and documentation that show you how to get started programming embedded devices using the Java language. The two devices highlighted are the Keil ARM evaluation board and the Raspberry Pi board.

Check out Working with Java ME Embedded and Raspberry Pi - Series and Working with Java ME Embedded on the Keil ARM Evaluation Board - Series.

You will learn how to install the Java binaries on the boards and run sample Java application on the board. Have fun.

Monday Apr 08, 2013

Java SE 8 Lambda Quick Start

Are you interested in taking a peek at the new lambda expressions feature in Java 8?  If so, this OBE is for you:

Java SE 8 Lambda Quick Start

The OBE covers the basic topics related to lambda expressions including:

  •  A review of anonymous inner classes
  • Lambda expression syntax
  • Functional interfaces
  • Using lambda expressions to replace anonymous inner classes
  • Using prebuilt functional interfaces included in java.util.function to pass behavior to your methods
  • Enhancements to the Collections API for lambda expressions

There are a number of examples for each topic and the full source code and NetBeans 8 projects are included for you. So if you are interested, check it out at the link above.

Thursday Mar 21, 2013

Oracle Cloud Development with NetBeans and Eclipse (OEPE)

Continuing the Oracle Cloud development series, I have posted 5 new tutorials that illustrate how to develop and deploy Java EE applications to Oracle Cloud using NetBeans and Oracle Enterprise Pack for Eclipse (OEPE). The tutorials introduce you to application development using of one these two fine IDEs with  Oracle Java Cloud Service and Oracle Database Cloud Service.

In the NetBeans series, you will learn how to configure NetBeans to use Oracle Cloud through a plugin, then create and deploy a JSF/JPA application and the data it uses to Oracle Cloud. Finally, there is a tutorial that illustrates using Maven projects with Oracle Cloud.

In the OEPE series, you will learn how to configure OEPE (Eclipse Juno with extensions), then modify an ADF application built using OEPE to Oracle Cloud.

Check out all of the free tutorials and other resources for Oracle Cloud on the Oracle Learning Library!

Thursday Mar 07, 2013

New OBE Series: Oracle Cloud and JDeveloper/ADF

OBE of the Month: Oracle Cloud and JDeveloper/ADF

In this Oracle By Example (OBE) series, you will learn how to develop and deploy Oracle ADF applications to Oracle Cloud by using Oracle JDeveloper. The tutorial series covers using both Oracle Java Cloud Service and Oracle Database Cloud Service, and introduces you to securing applications using Oracle Identity Management built into Oracle Cloud.

Oracle JDeveloper 11g ( is nicely integrated with Oracle Cloud, providing a single tool to manage both your database and Java services. In Part 1 of the tutorial, you will create create and populate your database tables directly from JDeveloper. In Part 2, you will deploy and run a simple HR management application that uses the database you created in part 1.

In part 3, you will secure an application in Oracle Cloud and demonstrate how to restrict access to ADF components and flows based on authentication and authorization using Oracle Identity Management features.

Sign up for a free 30-day trial of Oracle Java Cloud Service and Oracle Database Cloud Service and use these OBE's to learn more about Oracle Cloud and practice your skills with JDeveloper and ADF.

Check out all of the free tutorials and other resources for Oracle Cloud on the Oracle Learning Library, and look for additional OBE's using NetBeans and Oracle Enterprise Pack for Eclipse with Oracle Cloud coming soon!

Friday Mar 01, 2013

Oracle Secure Java Coding Guidelines Self-study

We have recently published a self-study that covers the Secure Java Coding Guidelines. The self-study is 89 minutes long and includes an overview of Java Security, a discussion of software vulnerabilities, and refers to the Secure Coding Guidelines for the Java Programming Language document. In addition, we describe some recommendations for secure JNI coding. Along the way, you can test your knowledge with word games, quizzes, and tic tac toe games. The self-study is in the Oracle Learning Library. We welcome your feedback.

Wednesday Feb 06, 2013

JavaOne 2012 Videos Posted on OracleLearning YouTube Channel

Did you miss the 201OracleLearning YouTube Channel JavaOne Playlists2 JavaOne? Well, now you can catch up on many of the sessions, including conference presentations, birds-of-a-feather events, and tutorials. We posted most of the recorded sessions on the Oracle Learning Library's YouTube channel. The videos are sorted in playlists by the session tracks, and there are a total of 7 tracks. We have over 450 JavaOne videos for you to view!

Thursday Jan 24, 2013

New OBE Series: Contexts and Dependency Injection (CDI)

OBE of the Month: CDI Series

This Oracle By Example (OBE) series covers three features of Contexts and Dependency Injection (CDI) introduced in Java EE 6: Stereotypes, Interceptors and Events. CDI was introduced with Java Platform, Enterprise Edition (Java EE) 6 as JSR-299.

The Contexts part of CDI allows beans to share and participate in the life cycle of another bean, while the Dependency Injection part of CDI allows beans to get a reference to an instance of another bean simply by injecting it. CDI power is the ability to loosely-couple classes by removing the instantiation process and simultaneously enforcing strong-type checking.

Check out all of the great free content on the Oracle Learning Library and our new cool look-and-feel!

Thursday Dec 20, 2012

New OBE Series: EJB 3.1 New Features

OBE of the Month: EJB 3.1 New Features (Series)

This Oracle By Example (OBE) series covers many of the new features of EJB 3.1. The latest release of the EJB technology, based on JSR-318: Enterprise JavaBeans 3.1, EJB 3.1 further simplifies the technology and makes many improvements. EJB 3.1 is available in the Java EE 6 platform.

There are 5 OBE's mapped in the series:

Check out all of the great free content at the Oracle Learning Library!

This article contributed by Anjana Shenoy.

Monday Nov 26, 2012

Getting started with the G1 Garbage Collector

Just before the Thanksgiving break I finished up my second Oracle by Example (OBE) course on garbage collection. This one is on the new G1 garbage available in Java 7. It provides and introduction and overview of this newly available collector. Here is the link to the course:

Getting Started with the G1 Garbage Collector

This is a follow up to this OBE on the basics of garbage collection.

Garbage Collection Basics

The OBE is based on the presentation given by Charlie Hunt and Monica Beckwith at this years Java One. Hopefully I have done justice to their most excellent session. As always, feedback and comments are welcome.


Information relating to Java training development and events.


« July 2016