Add parallel DMA start for driving all strips at once.
Split DMA start into prepare/trigger so all eight strips can be armed and then kicked off together, optionally from a flash address. Also drop a stray self.dma.start_transfer call in the WS2812B constructor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bf0429c505
commit
47f4a6663b
3 files changed
+48
-5
No files matched your search
+20
-3
@@ -93,8 +93,22 @@ class PIO_DMA_Transfer():
|
||||
self.dma_chan.CTRL_TRIG.INCR_WRITE = 0
|
||||
self.dma_chan.CTRL_TRIG.INCR_READ = 1
|
||||
|
||||
def _set_read_addr(self, buffer_or_address, offset=0):
|
||||
if isinstance(buffer_or_address, int):
|
||||
self.dma_chan.READ_ADDR_REG = buffer_or_address + offset
|
||||
else:
|
||||
self.dma_chan.READ_ADDR_REG = uctypes.addressof(buffer_or_address) + offset
|
||||
|
||||
def prepare_transfer(self, buffer_or_address, offset=0):
|
||||
"""Set read address only; do not start. Use with trigger() for parallel start."""
|
||||
self._set_read_addr(buffer_or_address, offset)
|
||||
|
||||
def start_transfer(self, buffer, offset=0):
|
||||
self.dma_chan.READ_ADDR_REG = uctypes.addressof(buffer) + offset
|
||||
self._set_read_addr(buffer, offset)
|
||||
self.dma_chan.CTRL_TRIG.EN = 1
|
||||
|
||||
def trigger(self):
|
||||
"""Start this channel (use after prepare_transfer for parallel start)."""
|
||||
self.dma_chan.CTRL_TRIG.EN = 1
|
||||
|
||||
def transfer_count(self):
|
||||
@@ -105,9 +119,12 @@ class PIO_DMA_Transfer():
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
|
||||
|
||||
def trigger_channels(dma_transfers):
|
||||
"""Start multiple DMA channels in quick succession (parallel drive)."""
|
||||
for t in dma_transfers:
|
||||
t.trigger()
|
||||
|
||||
|
||||
|
||||
|
||||
+10
-1
@@ -28,13 +28,22 @@ class WS2812B:
|
||||
self.brightness = brightness
|
||||
self.invert = invert
|
||||
self.pio_dma = dma.PIO_DMA_Transfer(state_machine+4, state_machine, 8, num_leds*3)
|
||||
self.dma.start_transfer(self.ar)
|
||||
|
||||
|
||||
def show(self, array=None, offset=0):
|
||||
if array is None:
|
||||
array = self.ar
|
||||
self.pio_dma.start_transfer(array, offset)
|
||||
|
||||
def prepare_show(self, buffer_or_address, offset=0):
|
||||
"""Set DMA read address only (buffer or int flash address). Use with trigger() for parallel start."""
|
||||
if buffer_or_address is None:
|
||||
buffer_or_address = self.ar
|
||||
self.pio_dma.prepare_transfer(buffer_or_address, offset)
|
||||
|
||||
def trigger(self):
|
||||
"""Start DMA for this strip (use after prepare_show on all strips for parallel drive)."""
|
||||
self.pio_dma.trigger()
|
||||
|
||||
def set(self, i, color):
|
||||
self.ar[i*3] = int(color[1]*self.brightness)
|
||||
|
||||
+18
-1
@@ -1,5 +1,6 @@
|
||||
from machine import Pin
|
||||
from ws2812 import WS2812B
|
||||
import dma
|
||||
from preset import Preset
|
||||
from patterns import (
|
||||
Blink, Rainbow, Pulse, Transition, Chase, Circle, Roll, Calibration, Test,
|
||||
@@ -187,4 +188,20 @@ class Presets:
|
||||
|
||||
def show_all(self):
|
||||
for strip in self.strips:
|
||||
strip.show()
|
||||
strip.show()
|
||||
|
||||
def show_all_parallel(self):
|
||||
"""Drive all 8 strips at once: PIO+DMA, trigger all channels in quick succession."""
|
||||
for strip in self.strips:
|
||||
strip.prepare_show(None, 0) # use strip's own buffer
|
||||
dma.trigger_channels([s.pio_dma for s in self.strips])
|
||||
|
||||
def show_all_parallel_from_flash(self, flash_base_address):
|
||||
"""Drive all 8 strips from flash at once. flash_base_address = start of contiguous pixel data.
|
||||
Layout in flash: strip0 pixels (n0*3 bytes), strip1 (n1*3), ... in order.
|
||||
In MicroPython you can use uctypes.addressof(bytes_obj) if the bytes live in flash (e.g. const)."""
|
||||
offset = 0
|
||||
for strip in self.strips:
|
||||
strip.prepare_show(flash_base_address, offset)
|
||||
offset += strip.num_leds * 3
|
||||
dma.trigger_channels([s.pio_dma for s in self.strips])
|
||||
Reference in new issue
Block a user