|
| 1 | + |
| 2 | +## |
| 3 | +# @file mpy_rgb_ramp.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 accessable 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 time |
| 25 | + |
| 26 | +# --------------------------------------------------------------------------------- |
| 27 | +# Wink the LED by turning it off and |
| 28 | + |
| 29 | + |
| 30 | +def wink_led(led): |
| 31 | + """ |
| 32 | + @brief Wink the LED by turning it off and on three times. |
| 33 | +
|
| 34 | + @param led The LED object to be controlled. It is expected to be a `neopixel.NeoPixel` object. |
| 35 | +
|
| 36 | + @details |
| 37 | + - Saves the current color of the LED. |
| 38 | + - Turns the LED off and on three times with a delay of 100 milliseconds between each state change. |
| 39 | + - Restores the LED to its original color after winking. |
| 40 | +
|
| 41 | + """ |
| 42 | + |
| 43 | + # safe the current color |
| 44 | + cur_clr = led[0] |
| 45 | + |
| 46 | + # wink the LED ... off and on three times |
| 47 | + for i in range(0, 3): |
| 48 | + led[0] = [0, 0, 0] # off |
| 49 | + led.write() |
| 50 | + |
| 51 | + time.sleep_ms(100) |
| 52 | + |
| 53 | + # restore the color |
| 54 | + led[0] = cur_clr |
| 55 | + led.write() |
| 56 | + time.sleep_ms(100) |
| 57 | + |
| 58 | +# --------------------------------------------------------------------------------- |
| 59 | +# Transition the current LED value to a given RGB value. |
| 60 | +# This function assumes pixel color/channel values are 8 bit (0-255) |
| 61 | +# |
| 62 | + |
| 63 | + |
| 64 | +def led_transition(led, R, G, B): |
| 65 | + """ |
| 66 | + @brief Transition the current LED value to a given RGB value. |
| 67 | +
|
| 68 | + This function assumes pixel color/channel values are 8-bit (0-255). |
| 69 | +
|
| 70 | + @param led The LED object to be controlled. It is expected to be a `neopixel.NeoPixel` object. |
| 71 | + @param R The target red color value (0-255). |
| 72 | + @param G The target green color value (0-255). |
| 73 | + @param B The target blue color value (0-255). |
| 74 | +
|
| 75 | + @details |
| 76 | + - Retrieves the current color of the LED |
| 77 | + - transitions the current color to the provided color over a series of increments. |
| 78 | + - Also outputs a dot for each increment to indicate progress. |
| 79 | +
|
| 80 | + @example |
| 81 | + @code |
| 82 | + led_transition(led, 255, 0, 0); // Transition to red color |
| 83 | + @endcode |
| 84 | + """ |
| 85 | + |
| 86 | + # get current led value - which is a tuple |
| 87 | + # Note - we convert to a list to support value assignment below. |
| 88 | + clrCurrent = list(led[0]) |
| 89 | + |
| 90 | + # How many increments during the transition |
| 91 | + inc = 51 # 255/5 |
| 92 | + |
| 93 | + # how much to change a color component value every increment |
| 94 | + rInc = (R - clrCurrent[0]) / inc |
| 95 | + gInc = (G - clrCurrent[1]) / inc |
| 96 | + bInc = (B - clrCurrent[2]) / inc |
| 97 | + |
| 98 | + # loop - adjust color during each increment. |
| 99 | + for i in range(0, inc): |
| 100 | + |
| 101 | + # add the desired increment to each color component value. Use round() to convert the float value to an integer |
| 102 | + clrCurrent[0] = round(clrCurrent[0] + rInc) |
| 103 | + clrCurrent[1] = round(clrCurrent[1] + gInc) |
| 104 | + clrCurrent[2] = round(clrCurrent[2] + bInc) |
| 105 | + |
| 106 | + # set the new LED color and write (enable) it |
| 107 | + led[0] = clrCurrent |
| 108 | + led.write() |
| 109 | + |
| 110 | + # indicate process ... add a small delay |
| 111 | + print(".", end='') |
| 112 | + time.sleep_ms(20) |
| 113 | + |
| 114 | + |
| 115 | +# --------------------------------------------------------------------------------- |
| 116 | +# rgp_ramp_example |
| 117 | + |
| 118 | +def rgb_ramp_example(): |
| 119 | + """ |
| 120 | + @brief Demonstrates LED color transitions using the onboard NeoPixel. |
| 121 | +
|
| 122 | + @details |
| 123 | + - Initializes the NeoPixel LED. |
| 124 | + - Transitions the LED through a series of colors: Blue, Red, Green, Yellow, White, and Off. |
| 125 | + - Winks the LED (turns it off and on three times) after each color transition. |
| 126 | +
|
| 127 | + """ |
| 128 | + |
| 129 | + # the the pin object for the pin defined as "NEOPIXEL" |
| 130 | + try: |
| 131 | + pin = machine.Pin("NEOPIXEL") |
| 132 | + except ValueError: |
| 133 | + print( |
| 134 | + "Error: The NEOPIXEL pin is not defined. Please check your board configuration.") |
| 135 | + return |
| 136 | + |
| 137 | + led = neopixel.NeoPixel(pin, 1) # create a NeoPixel object with 1 LED |
| 138 | + |
| 139 | + # start at LED off |
| 140 | + led[0] = (0, 0, 0) |
| 141 | + led.write() |
| 142 | + |
| 143 | + print() |
| 144 | + print("RGB LED Color Transitions:") |
| 145 | + |
| 146 | + time.sleep_ms(100) |
| 147 | + |
| 148 | + # transition through a series of colors |
| 149 | + print("\t<Off>\t", end='') |
| 150 | + led_transition(led, 0, 0, 255) |
| 151 | + print(" <Blue>") |
| 152 | + wink_led(led) |
| 153 | + |
| 154 | + print("\t<Blue>\t", end='') |
| 155 | + led_transition(led, 255, 0, 0) |
| 156 | + print(" <Red>") |
| 157 | + wink_led(led) |
| 158 | + |
| 159 | + print("\t<Red>\t", end='') |
| 160 | + led_transition(led, 0, 255, 0) |
| 161 | + print(" <Green>") |
| 162 | + wink_led(led) |
| 163 | + |
| 164 | + print("\t<Green>\t", end='') |
| 165 | + led_transition(led, 255, 255, 0) |
| 166 | + print(" <Yellow>") |
| 167 | + wink_led(led) |
| 168 | + |
| 169 | + print("\t<Yellow>", end='') |
| 170 | + led_transition(led, 255, 255, 255) |
| 171 | + print(" <White>") |
| 172 | + wink_led(led) |
| 173 | + |
| 174 | + print("\t<White>\t", end='') |
| 175 | + led_transition(led, 0, 0, 0) |
| 176 | + print(" <Off>") |
| 177 | + |
| 178 | + # turn off the LED |
| 179 | + led[0] = (0, 0, 0) |
| 180 | + led.write() |
| 181 | + |
| 182 | + |
| 183 | +# --------------------------------------------------------------------------------- |
| 184 | +# Run the example when this file is loaded |
| 185 | +print("-----------------------------------------------------------") |
| 186 | +print("Running the SparkFun RGB ramp example...") |
| 187 | +print("-----------------------------------------------------------") |
| 188 | +rgb_ramp_example() |
| 189 | +print("Done!") |
0 commit comments