From: whitequark Date: Wed, 9 Oct 2019 22:38:45 +0000 (+0000) Subject: test.blinky: invert LED status when button/switch is active. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=b785b7c18097584988e3664de0f974d846436b63;p=nmigen-boards.git test.blinky: invert LED status when button/switch is active. Allows testing buttons/switches as well. --- diff --git a/nmigen_boards/test/blinky.py b/nmigen_boards/test/blinky.py index 5e20191..4eb76db 100644 --- a/nmigen_boards/test/blinky.py +++ b/nmigen_boards/test/blinky.py @@ -11,20 +11,34 @@ class Blinky(Elaboratable): def elaborate(self, platform): m = Module() - leds = [] - for n in itertools.count(): - try: - leds.append(platform.request("led", n)) - except ResourceError: - break - leds = Cat(led.o for led in leds) + def get_all_resources(name): + resources = [] + for number in itertools.count(): + try: + resources.append(platform.request(name, number)) + except ResourceError: + break + return resources + + leds = [res.o for res in get_all_resources("led")] + buttons = [res.i for res in get_all_resources("button")] + switches = [res.i for res in get_all_resources("switch")] + + inverts = [0 for _ in leds] + for index, button in zip(itertools.cycle(range(len(inverts))), buttons): + inverts[index] ^= button + for index, switch in zip(itertools.cycle(range(len(inverts))), switches): + inverts[index] ^= switch clk_freq = platform.default_clk_frequency - ctr = Signal(max=int(clk_freq//2), reset=int(clk_freq//2) - 1) - with m.If(ctr == 0): - m.d.sync += ctr.eq(ctr.reset) - m.d.sync += leds.eq(~leds) + timer = Signal(max=int(clk_freq//2), reset=int(clk_freq//2) - 1) + flops = Signal(len(leds)) + + m.d.comb += Cat(leds).eq(flops ^ Cat(inverts)) + with m.If(timer == 0): + m.d.sync += timer.eq(timer.reset) + m.d.sync += flops.eq(~flops) with m.Else(): - m.d.sync += ctr.eq(ctr - 1) + m.d.sync += timer.eq(timer - 1) return m