diff --git a/pico/lib/dma.py b/pico/lib/dma.py index 85af8d9..86c3afd 100644 --- a/pico/lib/dma.py +++ b/pico/lib/dma.py @@ -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() diff --git a/pico/lib/ws2812.py b/pico/lib/ws2812.py index fb26524..b4f81c3 100644 --- a/pico/lib/ws2812.py +++ b/pico/lib/ws2812.py @@ -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) diff --git a/pico/src/presets.py b/pico/src/presets.py index 53d81f0..734a7f8 100644 --- a/pico/src/presets.py +++ b/pico/src/presets.py @@ -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() \ No newline at end of file + 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]) \ No newline at end of file