Hopefully we now have a working circuit, so we need to put some code behind it all to make the magic happen. Before you roll your metaphorical/physical sleeves up and get your hands dirty in the code make sure you have I2C enabled on your Pi and can see the three backpacks. If you’re not sure how there are loads of guides on the interwebs; here’s Adafruits.
Now that public service announcement is done; to the code. I ended up with 3 python code files (2 of them classes) which are briefly detailed below.
This is a reworked version of the Adafruit_SevenSegment.py class and has the core display functions. The most significant changes from the Adafruit verison were described earlier.
The main functions are:
writeDigit – sets the digit specified to represent the value required
writeDigitRaw – as above, but the raw hex representation of the segments to display is passed
setSegState – toggles a segment for a specified digit on or off
charToHex – convert a character to it hex representation from the lookup table
This class is a grouping of my helper functions and imports SevenSegment_Anode.py.
The main functions are:
showTime – takes a datetime and displays is across all twelve 7-segments
showColon – toggle the colon on the time display
textToHex – converts a string into an array of hex representation of the segments
writeHexArray – takes the result of textToHex and displays is on the 7-segments
This is the main program/module. Its main loop is really straight forward
Update current time If we've looped 20 times then change top and bottom displays Sleep 1/4 second
There is no exit condition as it is intended to just keep going unattended. The code to change the top/bottom displays for the most part walks an array of times, however to add a little flair when the cycle completed I included some special cases to write out some words. In this case:
the name of the event
and the fancy dress theme in two stages
– – and – –
Just for completeness the main functions are:
writeTPC / writeBackAnd /writeForInTime – write words (or close approximations) to the digits
initFilmTimes – populates an array with times used in the three films
You can download the source in this ZIP file. It comes with no promises or guarantees and you may find some of the techniques clunky….. but it works, and that was the primary aim. (Python is not my day job programming language!)