This is an app to drive a JHD1313M1 LCD display connected via the I2C interface, as found, for example, in the Grove* - Starter Kit Plus. The sample code can be found in the following location: https://github.com/gomobile/iotapp-template-lcd-driver

The LCD features

The JHD1313M1 is a two-line LCD that supports user-defined characters and requires only two pins for data exchange and backlight control. For additional information and specifications, see http://www.seeedstudio.com/wiki/Grove_-_LCD_RGB_Backlight.

Using the app with libupm

The app can be run in two ways. One is to use the upm library support for the device. This is the way you would normally use the device in a “real-world” application. In this mode, the application is quite trivial.

Using the app with direct libmraa calls

The second method is by driving the I2C interface directly. In effect, the file “lcd.js” is a device driver written in Javascript.
While we recommend using libupm in most situations, the direct approach is interesting for several reasons:

  • To learn about the I2C programming in Javascript using libmraa.
  • To test the I2C interfaces in libmraa.
  • As a starting point for driving non-supported devices.

The lcd.js implementation

The programming model for Javascript* is somewhat different from what you might see in C, because there is no equivalent of “sleep” in Javascript. Instead, the Javascript approach is to set a timer and call a function when it expires.
You could, of course, put your code into a very tight loop waiting for time to pass. For example:

function sleep(time) {
  var millSeconds  = time / 1000;
  var endTime = Date().getTime() + milliSeconds;
  while (Date().getTime() < endTime) {
     // We can’t leave, we’re waiting
  }
}

But this is a bad idea. As Javascript is single-threaded, any events that might want to occur during this interval will be blocked.

At the same time, having to split up your code across functions that are attached to timers would be cumbersome. So instead, lcd.js implements a small engine that handles the timing for you.

Instead of performing actions immediately, we put them on a queue to be possibly executed at some time in the future. The actual execution is then event-driven.

This leaves the question of how you can tell when something has actually happened. Most of the time this won’t be important and you can just treat the calls as if they were synchronous. This is what we do in the app.

However, a good way to handle this is with the q module. You can find documentation for the module here: https://github.com/kriskowal/q/wiki/API-Reference.

The lcd.js code contains the routine “waitForQuiescent” which will return any error or success when the queued operations are completed, while “clearError” will clear any errors so that you can continue using the LCD. See the code in main.js for an example of their use.

Para obter informações mais completas sobre otimizações do compilador, consulte nosso aviso de otimização.