Pi IoT In Python Using GPIO Zero - Compound Devices
Written by Harry Fairhead & Mike James   
Monday, 07 February 2022
Article Index
Pi IoT In Python Using GPIO Zero - Compound Devices
An Example
LEDBarGraph
  • LEDBarGraph

LEDBarGraph(*pinspwm=Falseactive_high=True,
                             initial_value=0pin_factory=None)

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.

ledbar

  • TrafficLights

TrafficLights(red, amber, green, *, yellow=None,
pwm=False, initial_value=False, pin_factory=None)

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

By Harry Fairhead & Mike James

GPIOZero2E360

Buy from Amazon.

Contents

  1. Why Pi for IoT?
  2. Getting Started With Python And GPIO Zero
  3. Introduction to the GPIO
  4. Python - Class and Object
  5. Simple On/Off Devices
      Extract 1: On/Off Devices *
  6. Pins And Pin Factories
      Extract 1: Pins 
  7. Some Electronics
  8. Simple Input
      Extract 1:Getting Input ***NEW!!
  9. Complex Input Devices
      Extract 1: Complex Input *
  10. Pulse Width Modulation
      Extract 1:  PWM*
  11. Controlling Motors And Servos
      Extract 1: DC Motors *
  12. Working With Compound Devices
      Extract 1: Compound Devices*
  13. The SPI Bus
  14. Custom SPI Devices
  15. Using The Lgpio Library
  16. Appendix Visual Studio Code Remote Python
    *Extracts from first edition - will be updated.

 <ASIN:1871962870>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Microsoft Introduces Vector Abstractions Library For .NET
21/11/2024

Microsoft has announced a preview release of the Microsoft Extensions VectorData Abstractions library, which can be used to help integrate vector stores into .NET applications and libraries.



Ursina - A Game Engine Powered by Python
08/11/2024

Ursina is a new open source game engine in which you can code any type of game in Python, be it 2-D, 3-D, an application, a visualization, you name it.


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Monday, 07 February 2022 )