This treats the LEDs connected to the specified pins as a bar graph. Setting a value between -1 and 1 lights up the corresponding proportion of the LEDs with negative values, working from the top of the list of pins. So:
graph.value=0.5
sets the first half of the LEDs on and:
graph.value=-0.5
sets the last half of the LEDs on.
It is worth knowing that you can get packaged LED Bars with 10 or 20 segments. However, you need to keep in mind that allocating even as few as 10 GPIO lines to drive such a device might leave you short for other a pplications.
This controls a simple traffic light device by specifying the pins that the red, amber and green LEDs are connected to. You can control the lights using the red, amber, green attributes. The attribute yellow can be used as an alternative name for amber. You have to provide the code that changes the lights, e.g. green, red/amber, red and so on. You can also use PWM to vary the brightness, but it isn’t clear why you might want to do this.
There are also some classes that work with off-the-shelf boards made by a number of different companies.
In Chapter but not in this extract
PiHutXmasTree
LedBorg
PiLiter
PiLiterBarGraph
SnowPi
PiTraffic
PiStop
StatusZero
PumpkinPi
General I/O Composites
The Robots
Refactoring the Stepper Motor Class
Summary
Inheritance isn’t the only way classes can be reused. Composition, or containment, placing instances of other classes in a class, is also very useful.
The CompositeDevice class makes creating compound devices very easy and more organized than an ad hoc approach.
The CompositeOutputDevice class adds some methods that are appropriate for a compound device that contains nothing but output devices.
Driving multiple LEDs is so common a task that there is a special class, LEDCollection, designed to do just this.
There are a large number of off-the-shelf compound devices based on LEDCollection. Even though they are proprietary devices, it is easy to make your own from basic components.
There are also some more general, off-the-shelf, compound devices that use LED, Buttons and sound devices.
Finally, there are a set of robot classes which essentially consist of motor objects and methods to allow control in terms of direction and speed.
The stepper motor class created in earlier chapters is easy to refactor using CompositeDevice and you can even add a background task to make it rotate without the involvement of the main program.
Raspberry Pi IoT In Python Using GPIO Zero Second Edition
At last ISO C23 has been published, but at $250 you probably aren't going to read it. Can we really tolerate this sort of profiteering on the work of others? This is worse than academic publishing!
IBM has released new Granite models that it says provide state-of-the-art performance relative to model size. The Granite 3.0 collection includes a new, instruction-tuned, dense decoder-only LLM.