In our last post we looked at using the temperature sensor, but what if we want to display that data? We could display it on a attached hi resolution screen, but, what if we are using a SOM that doesn’t have display hardware built in? Also, what if we only need a small display and want to use a low cost option? You could use a tm1637 segment display if you only have a small amount of data to display, but what if you want more versatility? That is where the ssd1306 comes in.
Most ssd1306 displays come with OLED displays that are either 128×64 dots in resolution or 128×32. The displays are designed for monochromatic operation which means that each dot can either be on or off. You can adjust the brightness of the entire display, but not individual pixels. Some displays will give you a multicolor effect by having one section of the display set to one color, such as yellow, and the other one set to another, such as blue. Depending on the display and vendor you can purchase these for as little as $3.
The Android Things drivers-samples repository has good information for getting started (follow the link). While these examples can get you started they don’t give you a lot of information on how to display text. If you’ve worked with raw canvas graphics before it will be really easy to translate that knowledge. Many Android developers don’t need to do that, so for those who haven’t here is a quick rundown. First, for this display you are going to need a bitmap to paint into. Here is an example to create one for a 128×64 display
val mBitmap = Bitmap.createBitmap(128, 64, conf)
next you are going to need to create a canvas object
val canvas = Canvas(mBitmap)
now, create a paint object specifying the color(white), style(fill), and a fontsize.
val paint = TextPaint() paint.color = Color.WHITE paint.style = Paint.Style.FILL paint.textSize = fontSize
use a DynamicLayout to paint the text on the screen. We are letting it take care of the text centering etc., but it does allow for position adjustments (see the documentation).
val dynamicLayout = DynamicLayout(text, paint, 128, Layout.Alignment.ALIGN_CENTER, 1f, 0f, false) dynamicLayout.draw(canvas)
finally we will paint this on our screen
BitmapHelper.setBmpData(mScreen, 0, 0, mBitmap, false) mScreen.show()
To see a full example that prints out text and graphics head over to our example on Github here. These examples have been designed for the 128×64 displays which tend to be more common. When you run it you should see the following.
We also designed them to work with one of the two color displays. In the photo below we have a display with 48 rows of blue pixels and 16 of yellow.
The contrast feature was not initially in the driver, so we added the functionality into the driver. At the time of this writing it is still under Google code review. If you would like to use it before it is pulled into the main driver clone this repo in the directory that hosts this example (the directory should have contrib-drivers and ssd1306_example in it), build the driver and reference the .aar file (see the comments in the project). The contrast can be set anytime you have initialized the display. Valid values are between 0 (for the lowest contrast level) to 255 (for the highest).
Stay tuned for our next post as we explore more of the Android Things Platform.