Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
ppnrespeakerdemo.apa102.APA102 Class Reference

Public Member Functions

def __init__ (self, num_led, global_brightness=MAX_BRIGHTNESS, order='rgb', bus=0, device=1, max_speed_hz=8000000)
 
def cleanup (self)
 
def clear_strip (self)
 
def clock_end_frame (self)
 
def clock_start_frame (self)
 
def dump_array (self)
 
def rotate (self, positions=1)
 
def set_pixel (self, led_num, red, green, blue, bright_percent=100)
 
def set_pixel_rgb (self, led_num, rgb_color, bright_percent=100)
 
def show (self)
 
def wheel (self, wheel_pos)
 

Static Public Member Functions

def combine_color (red, green, blue)
 

Public Attributes

 global_brightness
 
 leds
 
 num_led
 
 rgb
 
 spi
 

Static Public Attributes

int LED_START = 0b11100000
 
int MAX_BRIGHTNESS = 31
 

Detailed Description

Driver for APA102 LEDS (aka "DotStar").

(c) Martin Erzberger 2016-2017

My very first Python code, so I am sure there is a lot to be optimized ;)

Public methods are:
 - set_pixel
 - set_pixel_rgb
 - show
 - clear_strip
 - cleanup

Helper methods for color manipulation are:
 - combine_color
 - wheel

The rest of the methods are used internally and should not be used by the
user of the library.

Very brief overview of APA102: An APA102 LED is addressed with SPI. The bits
are shifted in one by one, starting with the least significant bit.

An LED usually just forwards everything that is sent to its data-in to
data-out. While doing this, it remembers its own color and keeps glowing
with that color as long as there is power.

An LED can be switched to not forward the data, but instead use the data
to change it's own color. This is done by sending (at least) 32 bits of
zeroes to data-in. The LED then accepts the next correct 32 bit LED
frame (with color information) as its new color setting.

After having received the 32 bit color frame, the LED changes color,
and then resumes to just copying data-in to data-out.

The really clever bit is this: While receiving the 32 bit LED frame,
the LED sends zeroes on its data-out line. Because a color frame is
32 bits, the LED sends 32 bits of zeroes to the next LED.
As we have seen above, this means that the next LED is now ready
to accept a color frame and update its color.

So that's really the entire protocol:
- Start by sending 32 bits of zeroes. This prepares LED 1 to update
  its color.
- Send color information one by one, starting with the color for LED 1,
  then LED 2 etc.
- Finish off by cycling the clock line a few times to get all data
  to the very last LED on the strip

The last step is necessary, because each LED delays forwarding the data
a bit. Imagine ten people in a row. When you yell the last color
information, i.e. the one for person ten, to the first person in
the line, then you are not finished yet. Person one has to turn around
and yell it to person 2, and so on. So it takes ten additional "dummy"
cycles until person ten knows the color. When you look closer,
you will see that not even person 9 knows its own color yet. This
information is still with person 2. Essentially the driver sends additional
zeroes to LED 1 as long as it takes for the last color frame to make it
down the line to the last LED.

Definition at line 11 of file apa102.py.

Constructor & Destructor Documentation

◆ __init__()

def ppnrespeakerdemo.apa102.APA102.__init__ (   self,
  num_led,
  global_brightness = MAX_BRIGHTNESS,
  order = 'rgb',
  bus = 0,
  device = 1,
  max_speed_hz = 8000000 
)

Definition at line 77 of file apa102.py.

Member Function Documentation

◆ cleanup()

def ppnrespeakerdemo.apa102.APA102.cleanup (   self)
Release the SPI device; Call this method at the end

Definition at line 208 of file apa102.py.

◆ clear_strip()

def ppnrespeakerdemo.apa102.APA102.clear_strip (   self)
Turns off the strip and shows the result right away.

Definition at line 136 of file apa102.py.

◆ clock_end_frame()

def ppnrespeakerdemo.apa102.APA102.clock_end_frame (   self)
Sends an end frame to the LED strip.

As explained above, dummy data must be sent after the last real colour
information so that all of the data can reach its destination down the line.
The delay is not as bad as with the human example above.
It is only 1/2 bit per LED. This is because the SPI clock line
needs to be inverted.

Say a bit is ready on the SPI data line. The sender communicates
this by toggling the clock line. The bit is read by the LED
and immediately forwarded to the output data line. When the clock goes
down again on the input side, the LED will toggle the clock up
on the output to tell the next LED that the bit is ready.

After one LED the clock is inverted, and after two LEDs it is in sync
again, but one cycle behind. Therefore, for every two LEDs, one bit
of delay gets accumulated. For 300 LEDs, 150 additional bits must be fed to
the input of LED one so that the data can reach the last LED.

Ultimately, we need to send additional numLEDs/2 arbitrary data bits,
in order to trigger numLEDs/2 additional clock changes. This driver
sends zeroes, which has the benefit of getting LED one partially or
fully ready for the next update to the strip. An optimized version
of the driver could omit the "clockStartFrame" method if enough zeroes have
been sent as part of "clockEndFrame".

Definition at line 104 of file apa102.py.

◆ clock_start_frame()

def ppnrespeakerdemo.apa102.APA102.clock_start_frame (   self)
Sends a start frame to the LED strip.

This method clocks out a start frame, telling the receiving LED
that it must update its own color now.

Definition at line 95 of file apa102.py.

◆ combine_color()

def ppnrespeakerdemo.apa102.APA102.combine_color (   red,
  green,
  blue 
)
static
Make one 3*8 byte color value.

Definition at line 214 of file apa102.py.

◆ dump_array()

def ppnrespeakerdemo.apa102.APA102.dump_array (   self)
For debug purposes: Dump the LED array onto the console.

Definition at line 235 of file apa102.py.

◆ rotate()

def ppnrespeakerdemo.apa102.APA102.rotate (   self,
  positions = 1 
)
Rotate the LEDs by the specified number of positions.

Treating the internal LED array as a circular buffer, rotate it by
the specified number of positions. The number could be negative,
which means rotating in the opposite direction.

Definition at line 185 of file apa102.py.

◆ set_pixel()

def ppnrespeakerdemo.apa102.APA102.set_pixel (   self,
  led_num,
  red,
  green,
  blue,
  bright_percent = 100 
)
Sets the color of one pixel in the LED stripe.

The changed pixel is not shown yet on the Stripe, it is only
written to the pixel buffer. Colors are passed individually.
If brightness is not set the global brightness setting is used.

Definition at line 144 of file apa102.py.

◆ set_pixel_rgb()

def ppnrespeakerdemo.apa102.APA102.set_pixel_rgb (   self,
  led_num,
  rgb_color,
  bright_percent = 100 
)
Sets the color of one pixel in the LED stripe.

The changed pixel is not shown yet on the Stripe, it is only
written to the pixel buffer.
Colors are passed combined (3 bytes concatenated)
If brightness is not set the global brightness setting is used.

Definition at line 172 of file apa102.py.

◆ show()

def ppnrespeakerdemo.apa102.APA102.show (   self)
Sends the content of the pixel buffer to the strip.

Todo: More than 1024 LEDs requires more than one xfer operation.

Definition at line 196 of file apa102.py.

◆ wheel()

def ppnrespeakerdemo.apa102.APA102.wheel (   self,
  wheel_pos 
)
Get a color from a color wheel; Green -> Red -> Blue -> Green

Definition at line 220 of file apa102.py.

Member Data Documentation

◆ global_brightness

ppnrespeakerdemo.apa102.APA102.global_brightness

Definition at line 83 of file apa102.py.

◆ LED_START

int ppnrespeakerdemo.apa102.APA102.LED_START = 0b11100000
static

Definition at line 75 of file apa102.py.

◆ leds

ppnrespeakerdemo.apa102.APA102.leds

Definition at line 87 of file apa102.py.

◆ MAX_BRIGHTNESS

int ppnrespeakerdemo.apa102.APA102.MAX_BRIGHTNESS = 31
static

Definition at line 74 of file apa102.py.

◆ num_led

ppnrespeakerdemo.apa102.APA102.num_led

Definition at line 78 of file apa102.py.

◆ rgb

ppnrespeakerdemo.apa102.APA102.rgb

Definition at line 80 of file apa102.py.

◆ spi

ppnrespeakerdemo.apa102.APA102.spi

Definition at line 88 of file apa102.py.


The documentation for this class was generated from the following file:


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:15:20