pwm_lightness module
Generates lightness correction tables for eyeball pleasing LED brightness.
Want a smooth fade on your pulsing LEDs? Want antialiasing on LED matrix fonts? Use this library to correct your raw linear brightness values for human eyeball persistence of vision perception sensitivity.
The adjustment is otherwise known as the CIE 1931 Lightness curve. It is also covered in many books. The research was done in the 1920s, winding up as one part of the International Commission on Illumination (CIE) 1931 standard.
This module does work on CircuitPython (and MicroPython if you build with floating point enabled). In microcontroller environments recomputing a table on device is often overkill. Precompute the table(s) you need using this library and store them as data to save precious RAM.
Smoothly fade an LED
This example assumes CircuitPython or adafruit-blinka.
import time, board, pulseio, pwm_lightness
PWM = pwm_lightness.get_pwm_table(0xffff, max_input=100)
output_pin = pulseio.PWMOut(board.D13)
while True:
for v in range(100, -1, -1):
output_pin.duty_cycle = PWM[v]
time.sleep(0.02)
for v in range(1, 100):
output_pin.duty_cycle = PWM[v]
time.sleep(0.02)
It is similarly useful with the RGB values you send to programmable LED strips.
Improve antialiased fonts or other 8-bit images
Shown here using Pillow to make an
antialiased font shine rather than appearing as a bold blur:
This example assumes a RPi connected to an I2C Adafruit charlieplexed
LED matrix.
import pwm_lightness
PWM = pwm_lightness.get_pwm_table(60) # 0..255 is our max LED PWM value.
import board, busio, adafruit_is31fl3731, PIL
i2c = busio.I2C(board.SCL, board.SDA)
led_matrix = adafruit_is31fl3731.CharlieBonnet(i2c)
font = PIL.ImageFont.truetype('fonts/RobotoCondensed-Regular.ttf', 16)
image = PIL.Image.new('L', (led_matrix.height, led_matrix.width), 0)
draw = PIL.ImageDraw.Draw(image)
# fill=255 gives us the most antialiasing detail to work with. Control
# brightness using the max_output value passed to get_pwm_table() above.
draw.text((0,-1), '?', fill=255, font=font)
image = image.point(PWM) # Corrects linear 0..255 values for PWM lightness.
image = image.transpose(PIL.Image.ROTATE_90) # Match the matrix orientation.
led_matrix.image(image) # Send pixels to our LED display.
Metadata
Release files for pwm-lightness 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pwm_lightness-1.0.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for pwm_lightness-1.0.1-py2.py3-none-any.whl |
Release files / pwm_lightness-1.0.1-py2.py3-none-any.whl
| Download URL | pwm_lightness-1.0.1-py2.py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
9e4a6656038498d1e8dc921fc4262c2ea8de2afe4fb35b4f0917fe625b90150f
|
|
BLAKE2b-256 checksum How to use checksums |
878df35379342499580891ea5bef322b7eef2de4e16eb15414d60f9a5e084bb9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.4rc1
|
Security for pwm-lightness 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.