Build a Raspberry Pi Vehicle Interior Monitor – Screen Test
In this blog series I'm documenting my maker journey as I build a Raspberry Pi-based vehicle interior monitor (PiVIM). This is the full list of posts in the series:
- Build a Raspberry Pi Vehicle Interior Monitor—Overview
- Build a Raspberry Pi Vehicle Interior Monitor—Screen Test (this post)
- Build a Raspberry Pi Vehicle Interior Monitor—Mobile Broadband
- Build a Raspberry Pi Vehicle Interior Monitor—Temperature Monitoring
- Build a Raspberry Pi Vehicle Interior Monitor—Running Code at Startup
- Build a Raspberry Pi Vehicle Interior Monitor—Putting it All Together (coming soon)
In this post I'm getting started by configuring the Raspberry Pi with a mini display which will act as a control panel. The unit I chose to put through its paces was Pimoroni's Display-O-Tron HAT (DotHAT):
Why did I choose DotHAT? Actually for no other reason than I've seen it in action and I was impressed, and it's a reasonably low cost component given the functionality on offer.
Tour Starts Here
In order to make full use of the DotHAT you need to be aware that the HAT is actually a composite of several bits of hardware. The obvious component is a 16×3 character LCD display, and this is complemented by a six-zone RGB backlight, an array of six LEDs and six capacitive touch buttons (think joystick controls). Each component can be programmed separately as required—or not as the case may be.
Physical installation—as with all HATs—is straightforward as it just sits on the GPIO pins. An initial concern was whether the DotHAT would require the BCM 4 GPIO pin that I was planning to use for the DS18B20 temperature sensor however the DotHAT pinout shows that it's not used.
In order to easily control the components of the DotHAT Pimoroni have created high-level Python libraries that wrap the lower-level libraries that interface with the hardware—a function reference is provided here. Installation of these (and supporting) libraries is straightforward with just one line of code:
1 |
curl -sS https://get.pimoroni.com/displayotron | bash |
As always it's best to make sure your OS is up-to-date first. The above command will ask if you want to install the example code and I definitely recommend this so you can see for yourself the highly creative ways you can use the DotHAT. One of the examples is a fully-functioning Internet radio with a menu system driven by the capacitive buttons—very impressive. Do take the time to run the examples and explore the code as there is oodles of functionality to play with.
PiVIM Control Panel
In my PiVIM project I'm planning to use the DotHAT as a control panel to display information about PiVIM's status such as current vehicle temperature, mobile broadband signal strength, error messages and so on. At this point I don't know exactly what items I want to display, however I do know it will need to be fairly simple so I probably won't use the menu feature for example. Instead I will most likely write information as either Left or Right-aligned and to each of the three rows of the LCD (Top, Middle, Bottom), giving six locations to write to:
In order to simplify writing to the six locations I wrote my own module with functions such as message_left_top() and message_right_middle(). You can find the code in my PiVIM-py GitHub repository as PiVIM-py/pivim/control_panel.py. There is also a PiVIM/control_panel_debug.py module which contains some code to put control_panel through its paces. The core ‘message' functions are straightforward, however there is an issue to be aware of regarding writing a new message that is shorter than the previous message because you'll find fragments of the previous message will still be displayed. I envisage updating all six positions together in a loop and will get round this problem by calling the clear_screen() function before each iteration. If you are doing something different you'll need to code accordingly.
One interesting touch (pun intended) I added was to configure the left and right capacitive buttons to turn the backlight on and off respectively. With battery life in mind I then took this a step further by implementing a function that creates a thread which calls a timer to turn the backlight off after a delay:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
BACKLIGHT_AUTO_OFF_DELAY = 10 @touch.on(touch.LEFT) def backlight_on(channel, event): """Configures the left touch button to turn the backlight on.""" display_config() backlight_auto_off() @touch.on(touch.RIGHT) def backlight_off(channel, event): """Configures the right touch button to turn the backlight off.""" backlight.off() def backlight_auto_off(): """Create a new thread from which to call a countdown timer to avoid blocking the main program. """ thread = Thread(target=backlight_countdown) thread.start() def backlight_countdown(): """Turns the backlight off after the specified period has elapsed""" time.sleep(BACKLIGHT_AUTO_OFF_DELAY) backlight.off() |
The display_config() function (not shown above) also calls backlight_auto_off() to help conserve battery life.
Future Functionality
In the interests of YAGNI that's all the functionality I'm going to write for the time being, however I do have some ideas for the future. One exciting possibility is concerned with how I represent mobile broadband signal strength. The DotHAT supports the full ASCII character set of course, but intriguingly it also supports up to eight custom glyphs. So on the one hand I could use, for example, asterisks to represent signal strength. On the other though, I could have a go at creating the sort of glyphs that are used to represent signal strength ‘bars' on mobile phones. If you are already itching to have a go at this the documentation is here. Until next time—happy coding!
Cheers -- Graham