From a155b435f088169158d53c2ff10599436a8a383f Mon Sep 17 00:00:00 2001 From: Hel Gibbons Date: Mon, 14 Oct 2024 14:58:23 +0100 Subject: [PATCH 1/3] add LED examples --- examples/external_leds.py | 46 +++++++++++++++++++++ examples/external_rgb_led.py | 80 ++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 examples/external_leds.py create mode 100644 examples/external_rgb_led.py diff --git a/examples/external_leds.py b/examples/external_leds.py new file mode 100644 index 0000000..6d5f42b --- /dev/null +++ b/examples/external_leds.py @@ -0,0 +1,46 @@ +""" +Connect (up to) 3 LEDs to Explorer and turn them on and off with buttons A, B and C. + +Wire the long leg of your LEDs up to GP0, GP1 and GP2. The other leg should be wired to ground. +(Note that you don't need to include a resistor in your circuit to current-limit the LEDs with Explorer, as there are resistors on the board). +""" + +from machine import Pin +import time +from explorer import display, button_a, button_b, button_c, BLACK, WHITE + +# Set up the LEDs +led_0 = Pin(0, Pin.OUT) +led_1 = Pin(1, Pin.OUT) +led_2 = Pin(2, Pin.OUT) + +# Clear all layers first +display.set_layer(0) +display.set_pen(BLACK) +display.clear() +display.set_layer(1) +display.set_pen(BLACK) +display.clear() + +# Set the layer we're going to be drawing to. +display.set_layer(0) + +# Draw some text to the screen +display.set_pen(WHITE) +display.text("Wire up some LEDs to GP0, GP1 and GP2, and then push buttons A, B and C", 0, 0, 320, 4) +display.update() + +while True: + # Start the loop with all LEDs off + led_0.off() + led_1.off() + led_2.off() + # Because we're using Pin.PULL_UP the logic is reversed - '0' is pushed and '1' is unpushed + if button_a.value() == 0: + led_0.on() + if button_b.value() == 0: + led_1.on() + if button_c.value() == 0: + led_2.on() + # Short pause to stop Thonny from falling over + time.sleep(0.01) diff --git a/examples/external_rgb_led.py b/examples/external_rgb_led.py new file mode 100644 index 0000000..7ceda8a --- /dev/null +++ b/examples/external_rgb_led.py @@ -0,0 +1,80 @@ +""" +Connect an old school four legged RGB LED up to Explorer and toggle the Red, Green and Blue channels with buttons A, B and C. +Press X for rainbows! + +Position your LED so the long leg is pin 2 - this leg should be connected to ground. +The other three legs (pins 1, 3 and 4) are R, G and B - these should be wired to GP0, GP1 and GP2. +(Note that you don't need to include resistors in your circuit with Explorer, as there are resistors on the board). +""" +import time +from explorer import display, button_a, button_b, button_c, button_x, BLACK, WHITE +from pimoroni import RGBLED + + +# From CPython Lib/colorsys.py +def hsv_to_rgb(h, s, v): + if s == 0.0: + return v, v, v + i = int(h * 6.0) + f = (h * 6.0) - i + p = v * (1.0 - s) + q = v * (1.0 - s * f) + t = v * (1.0 - s * (1.0 - f)) + i = i % 6 + if i == 0: + return v, t, p + if i == 1: + return q, v, p + if i == 2: + return p, v, t + if i == 3: + return p, q, v + if i == 4: + return t, p, v + if i == 5: + return v, p, q + + +# Set up an old school 4 pin RGB LED connected to GP0, GP1 and GP2 +led = RGBLED(0, 1, 2, invert=False) + +# Variables to keep track of rainbows +rainbow_mode = False +h = 0 + +# Clear all layers first +display.set_layer(0) +display.set_pen(BLACK) +display.clear() +display.set_layer(1) +display.set_pen(BLACK) +display.clear() + +# Set the layer we're going to be drawing to. +display.set_layer(0) + +# Draw some text to the screen +display.set_pen(WHITE) +display.text("Wire an RGB LED up to GP0, GP1 and GP2, and then press A, B, C or X", 0, 0, 320, 4) +display.update() + +while True: + # Toggle rainbow mode if button X is pushed + if button_x.value() == 0: + rainbow_mode = not rainbow_mode + if rainbow_mode is True: # then apply rainbow magic + h += 1 + r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] + led.set_rgb(r, g, b) + + # Because we're using Pin.PULL_UP the logic is reversed - '0' is pushed and '1' is unpushed + else: + led.set_rgb(0, 0, 0) + if button_a.value() == 0: # set LED to red + led.set_rgb(255, 0, 0) + if button_b.value() == 0: # set LED to blue + led.set_rgb(0, 255, 0) + if button_c.value() == 0: # set LED to green + led.set_rgb(0, 0, 255) + # Short pause to stop Thonny from falling over + time.sleep(0.01) From 3009ceacdc825a391a1b7bdf794deb8226f1d815 Mon Sep 17 00:00:00 2001 From: Hel Gibbons Date: Mon, 14 Oct 2024 15:02:58 +0100 Subject: [PATCH 2/3] add examples to readme --- examples/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/examples/README.md b/examples/README.md index e064920..245e6a2 100644 --- a/examples/README.md +++ b/examples/README.md @@ -40,6 +40,14 @@ Displays light, accelerometer and temperature data! Gotta put something on that little breadboard, right! How about more buttons. You can never have enough buttons. The ones they keep taking out of cars must go somewhere!? +# external_leds.py + +More bling for your breadboard! If you liked it then you should have put an LED on it. + +# external_rgb_led.py + +What's better than an ordinary LED? A RGB LED! (it's actually three teeny tiny ordinary LEDs smooshed together, but don't tell anyone!) + # image_gallery.py At a loss for what to do with your Explorer. Clock just not your cup of tea? How about a digital photo frame! Those are still cool, right? From e50622cb40522a58d133d6aadb1cbd77345fcb92 Mon Sep 17 00:00:00 2001 From: Hel Gibbons Date: Mon, 14 Oct 2024 15:09:12 +0100 Subject: [PATCH 3/3] fix link typo --- docs/reference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference.md b/docs/reference.md index 4c43b4b..4c39a98 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -230,7 +230,7 @@ PicoGraphics is a wrapper around a big ol' chunk of RAM, which corresponds to th ## Basic Drawing Functions -Since there are many, many things you can do with PicoGraphics that would be silly to repeat here, I'll send you over to our main MicroPython repository for [a full PicoGraphics function reference.][https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/modules/picographics/README.md#function-reference] +Since there are many, many things you can do with PicoGraphics that would be silly to repeat here, I'll send you over to our main MicroPython repository for [a full PicoGraphics function reference][https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/modules/picographics/README.md#function-reference] ## Changing The Display Mode