|
| 1 | + |
| 2 | +## |
| 3 | +# @file mpy_rgb_blink.py |
| 4 | +# @brief This MicroPython file contains functions to control the on-board RGB LED on SparkFun MicroPython |
| 5 | +# enabled boards that have a RGB LED. |
| 6 | +# |
| 7 | +# @details |
| 8 | +# This module depends on the available `neopixel` library to control the RGB LED and the on-board |
| 9 | +# LED pin defined as "NEOPIXEL" and accessible via the machine module. |
| 10 | +# |
| 11 | +# @note This code is designed to work with the `neopixel` library and a compatible microcontroller, such as the |
| 12 | +# SparkFun IoT RedBoard - ESP32, or the SparkFun IoT RedBoard - RP2350 |
| 13 | +# |
| 14 | +# @author SparkFun Electronics |
| 15 | +# @date March 2025 |
| 16 | +# @copyright Copyright (c) 2024-2025, SparkFun Electronics Inc. |
| 17 | +# |
| 18 | +# SPDX-License-Identifier: MIT |
| 19 | +# @license MIT |
| 20 | +# |
| 21 | + |
| 22 | +import machine |
| 23 | +import neopixel |
| 24 | +import random |
| 25 | +import time |
| 26 | + |
| 27 | +BLINK_DELAY = 200 # delay in milliseconds |
| 28 | +# --------------------------------------------------------------------------------- |
| 29 | + |
| 30 | + |
| 31 | +def fade_in_out(led, color, fade_time=1000): |
| 32 | + """ |
| 33 | + @brief Fade the LED in and out to a given color. |
| 34 | +
|
| 35 | + @param led The LED object to be controlled. It is expected to be a `neopixel.NeoPixel` object. |
| 36 | + @param color The RGB color to fade to. |
| 37 | + @param fade_time The time in milliseconds for the fade effect. Default is 1000 ms. |
| 38 | +
|
| 39 | + """ |
| 40 | + |
| 41 | + # fade in |
| 42 | + for i in range(0, 256): |
| 43 | + led[0] = (int(color[0] * i / 255), int(color[1] |
| 44 | + * i / 255), int(color[2] * i / 255)) |
| 45 | + led.write() |
| 46 | + time.sleep_ms(fade_time // 256) |
| 47 | + |
| 48 | + # fade out |
| 49 | + for i in range(255, -1, -1): |
| 50 | + led[0] = (int(color[0] * i / 255), int(color[1] |
| 51 | + * i / 255), int(color[2] * i / 255)) |
| 52 | + led.write() |
| 53 | + time.sleep_ms(fade_time // 256) |
| 54 | + |
| 55 | + |
| 56 | +def rgb_fade_example(led, count=10): |
| 57 | + """ |
| 58 | + @brief Fade the LED in and out random color |
| 59 | +
|
| 60 | + @param led The LED object to be controlled. It is expected to be a `neopixel.NeoPixel` object. |
| 61 | + @param count The number of times to fade the LED. Default is 1. |
| 62 | +
|
| 63 | + """ |
| 64 | + |
| 65 | + led[0] = (0, 0, 0) # LED OFF |
| 66 | + led.write() |
| 67 | + |
| 68 | + for i in range(count): |
| 69 | + # generate random RGB values - use a lower range to avoid too bright colors |
| 70 | + R = random.randint(0, 255) |
| 71 | + G = random.randint(0, 255) |
| 72 | + B = random.randint(0, 255) |
| 73 | + |
| 74 | + fade_in_out(led, (R, G, B)) |
| 75 | + print(".", end="") |
| 76 | + |
| 77 | + |
| 78 | +def blink_the_led(led, count=30): |
| 79 | + """ |
| 80 | + @brief Blink the LED with random colors of count times. |
| 81 | +
|
| 82 | + @param led The LED object to be controlled. It is expected to be a `neopixel.NeoPixel` object. |
| 83 | + @param count The number of times to blink the LED. Default is 1. |
| 84 | +
|
| 85 | + """ |
| 86 | + |
| 87 | + led[0] = (0, 0, 0) # LED OFF |
| 88 | + led.write() |
| 89 | + |
| 90 | + for i in range(count): |
| 91 | + # generate random RGB values - use a lower range to avoid too bright colors |
| 92 | + R = random.randint(0, 180) |
| 93 | + G = random.randint(0, 180) |
| 94 | + B = random.randint(0, 180) |
| 95 | + |
| 96 | + led[0] = (R, G, B) # LED ON |
| 97 | + led.write() |
| 98 | + |
| 99 | + time.sleep_ms(BLINK_DELAY) |
| 100 | + |
| 101 | + # restore the color |
| 102 | + led[0] = [0, 0, 0] # off |
| 103 | + led.write() |
| 104 | + time.sleep_ms(BLINK_DELAY//2) |
| 105 | + print(".", end="") |
| 106 | + |
| 107 | + |
| 108 | +# --------------------------------------------------------------------------------- |
| 109 | +# rgb_blink_example |
| 110 | + |
| 111 | +def rgb_blink_example(led, count=20): |
| 112 | + """ |
| 113 | + @brief Demonstrates LED color blinking using the onboard NeoPixel. |
| 114 | +
|
| 115 | + @details |
| 116 | + - Initializes the NeoPixel LED. |
| 117 | + - Blinks the LED through a random color sequence. |
| 118 | +
|
| 119 | + """ |
| 120 | + # start at LED off |
| 121 | + led[0] = (0, 0, 0) |
| 122 | + led.write() |
| 123 | + |
| 124 | + blink_the_led(led, count) |
| 125 | + print() |
| 126 | + |
| 127 | +# --------------------------------------------------------------------------------- |
| 128 | +# Run the example when this file is loaded |
| 129 | + |
| 130 | + |
| 131 | +def run_example(): |
| 132 | + |
| 133 | + print("-----------------------------------------------------------") |
| 134 | + print("Running the SparkFun RGB blink example...") |
| 135 | + print("-----------------------------------------------------------") |
| 136 | + # the the pin object for the pin defined as "NEOPIXEL" |
| 137 | + try: |
| 138 | + pin = machine.Pin("NEOPIXEL") |
| 139 | + except ValueError: |
| 140 | + print( |
| 141 | + "Error: The NEOPIXEL pin is not defined. Please check your board configuration.") |
| 142 | + exit(0) |
| 143 | + |
| 144 | + led = neopixel.NeoPixel(pin, 1) # create a NeoPixel object with 1 LED |
| 145 | + print("Blink the LED with random colors:") |
| 146 | + rgb_blink_example(led) |
| 147 | + print("Fade in and out with random colors:") |
| 148 | + rgb_fade_example(led) |
| 149 | + print("Done!") |
| 150 | + |
| 151 | + |
| 152 | +run_example() |
0 commit comments