项目作者: soldag

项目描述 :
Control LEDs connected to a micro controller using pulse-width modulation.
高级语言: Python
项目地址: git://github.com/soldag/python-pwmled.git
创建时间: 2017-01-20T14:55:50Z
项目社区:https://github.com/soldag/python-pwmled

开源协议:MIT License

下载


python-pwmled PyPI version

python-pwmled controls LEDs connected to a micro controller using pulse-width modulation. It supports one-color, RGB and RGBW leds driven by GPIOs of an Raspberry Pi or a PCA9685 controller.

Installation

python-pwmled requires Python 3. It can be installed using pip:

  1. pip install pwmled

When directly controlling the GPIOs of a Raspberry Pi using the GpioDriver(see below), the pigpio C library is required. It can be installed with the following commands:

  1. wget abyz.co.uk/rpi/pigpio/pigpio.zip
  2. unzip pigpio.zip
  3. cd PIGPIO
  4. make
  5. sudo make install

Besides the library, the pigpiod utility is installed, which starts pigpio as daemon. The daemon must be running when using the GpioDriver.

  1. sudo pigpiod

Usage

Configuration

python-pwmled supports several possibilities for connecting LEDs to your micro controller:

  • GPIO: LEDs can be connected directly to the GPIOs of a Raspberry Pi.
  • PCA9885: A PCA9685 can be used as I2C-bus PWM controller.
  1. from pwmled.driver.gpio import GpioDriver
  2. from pwmled.driver.pca9685 import Pca9685Driver
  3. # GPIO driver, which controls pins 17, 22, 23
  4. driver = GpioDriver([17, 22, 23])
  5. driver = GpioDriver([17, 22, 23], freq=200)
  6. # To control the pigpio on a other machine use the host and port parameter
  7. driver = GpioDriver([17, 22, 23], host='other.host', port=8889)
  8. # PCA9685 driver which controls pins 1, 2, 3
  9. driver = Pca9685Driver([1, 2, 3])
  10. driver = Pca9685Driver([1, 2, 3], freq=200, address=0x40)

Control

Each LED needs a separated driver, which controls the corresponding pins. The number and order of pins depends on the led type:

  • One-color: 1 pin
  • RGB: 3 pins ([R, G, B])
  • RGBW: 4 pins ([R, G, B, W])

The supported operations are shown in the following example:

  1. from pwmled import Color
  2. from pwmled.led import SimpleLed
  3. from pwmled.led.rgb import RgbLed
  4. from pwmled.led.rgbw import RgbwLed
  5. from pwmled.driver.gpio import GpioDriver
  6. # One-color led
  7. driver = GpioDriver([C])
  8. led = SimpleLed(driver)
  9. led.on()
  10. led.brightness = 0.5
  11. led.transition(5, brightness=0)
  12. led.off()
  13. # RGB led
  14. driver = GpioDriver([R, G, B])
  15. led = RgbLed(driver)
  16. led.on()
  17. led.color = Color(255, 0, 0)
  18. led.set(color=Color(0, 255, 0), brightness=0.5) # set two properties simultaneously
  19. led.transition(5, color=Color(0, 0, 255), brightness=1)
  20. led.off()
  21. # RGBW led
  22. driver = GpioDriver([R, G, B, W])
  23. led = RgbwLed(driver)
  24. # RgbwLed has same interface as RgbLed

Contributions

Pull-requests are welcome, especially for adding new drivers or led types.

License

This library is provided under MIT license.