LCD display (TM1637)

 

One more interesting component that is actually included in the contrib-drivers package is the LCD display TM1637. This peripheral is very handy when we do not have something like the alphanumeric display of the Rainbow HAT at hand, and it is also a simple and cheap display you can add to your IoT projects.

To manage this peripheral, we just need to add one line to our dependencies on gradle:

dependencies {
[...]
implementation 'com.google.android.things.contrib:driver-tm1637:+'
}

The breakout circuit has four connectors: the typical Vcc and Ground, and then two labeled as signal and clock. Both extra pins are to be connected to GPIO, but they work in a similar fashion to I2C.

This circuit uses a one-way communication protocol that has a separate clock signal to synchronize the sender and the receiver, which usually allows for faster speeds as well. The value of the signal pin will change, and when the clock is active, the value can be read. Don't worry; all that is managed by the driver.

To see how to use this component, we are going to build a digital clock:

class LCDClockActivity : Activity() {

companion object {
[...]
}

val dateFormat = SimpleDateFormat("HHmm")
val date = Date()
lateinit var display: NumericDisplay

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
display = NumericDisplay(dataGpioPinName, clockGpioPinName)
display.setBrightness(NumericDisplay.MAX_BRIGHTNESS)

Timer().schedule(timerTask {
// Blink the colon
display.setColonEnabled(!display.colonEnabled)

// Update the values
date.time = System.currentTimeMillis()

display.display(dateFormat.format(date))
}, 0, 1000)
}

override fun onDestroy() {
super.onDestroy()
display.close()
}
}

As of now, this should be exactly what we expect: a constructor that receives the name of the two pins connected, some utility methods, and then closing the device inside onDestroy.

The sample does have a timer that runs every second. This time the exact execution of a timer is very important, since we will use it to make the : blink.

Inside the lambda of the timer task, we just change the enabled status of the : and then format the time using a simple date formatter to be shown on the display.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.243.32