-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpy_rgb_ramp.py
198 lines (149 loc) · 5.55 KB
/
mpy_rgb_ramp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
##
# @file mpy_rgb_ramp.py
# @brief This MicroPython file contains functions to control the on-board RGB LED on SparkFun MicroPython
# enabled boards that have a RGB LED.
#
# @details
# This module depends on the available `neopixel` library to control the RGB LED and the on-board
# LED pin defined as "NEOPIXEL" and accessible via the machine module.
#
# @note This code is designed to work with the `neopixel` library and a compatible microcontroller, such as the
# SparkFun IoT RedBoard - ESP32, or the SparkFun IoT RedBoard - RP2350
#
# @author SparkFun Electronics
# @date March 2025
# @copyright Copyright (c) 2024-2025, SparkFun Electronics Inc.
#
# SPDX-License-Identifier: MIT
# @license MIT
#
import machine
import neopixel
import time
# ---------------------------------------------------------------------------------
# Wink the LED by turning it off and
def wink_led(led):
"""
@brief Wink the LED by turning it off and on three times.
@param led The LED object to be controlled. It is expected to be a `neopixel.NeoPixel` object.
@details
- Saves the current color of the LED.
- Turns the LED off and on three times with a delay of 100 milliseconds between each state change.
- Restores the LED to its original color after winking.
"""
# safe the current color
cur_clr = led[0]
# wink the LED ... off and on three times
for i in range(0, 3):
led[0] = [0, 0, 0] # off
led.write()
time.sleep_ms(100)
# restore the color
led[0] = cur_clr
led.write()
time.sleep_ms(100)
# ---------------------------------------------------------------------------------
# Transition the current LED value to a given RGB value.
# This function assumes pixel color/channel values are 8 bit (0-255)
#
def led_transition(led, R, G, B):
"""
@brief Transition the current LED value to a given RGB value.
This function assumes pixel color/channel values are 8-bit (0-255).
@param led The LED object to be controlled. It is expected to be a `neopixel.NeoPixel` object.
@param R The target red color value (0-255).
@param G The target green color value (0-255).
@param B The target blue color value (0-255).
@details
- Retrieves the current color of the LED
- transitions the current color to the provided color over a series of increments.
- Also outputs a dot for each increment to indicate progress.
@example
@code
led_transition(led, 255, 0, 0); // Transition to red color
@endcode
"""
# get current led value - which is a tuple
# Note - we convert to a list to support value assignment below.
clrCurrent = list(led[0])
# How many increments during the transition
inc = 51 # 255/5
# how much to change a color component value every increment
rInc = (R - clrCurrent[0]) / inc
gInc = (G - clrCurrent[1]) / inc
bInc = (B - clrCurrent[2]) / inc
# loop - adjust color during each increment.
for i in range(0, inc):
# add the desired increment to each color component value. Use round() to convert the float value to an integer
clrCurrent[0] = round(clrCurrent[0] + rInc)
clrCurrent[1] = round(clrCurrent[1] + gInc)
clrCurrent[2] = round(clrCurrent[2] + bInc)
# set the new LED color and write (enable) it
led[0] = clrCurrent
led.write()
# indicate process ... add a small delay
print(".", end='')
time.sleep_ms(20)
# ---------------------------------------------------------------------------------
# rgp_ramp_example
def rgb_ramp_example():
"""
@brief Demonstrates LED color transitions using the onboard NeoPixel.
@details
- Initializes the NeoPixel LED.
- Transitions the LED through a series of colors: Blue, Red, Green, Yellow, White, and Off.
- Winks the LED (turns it off and on three times) after each color transition.
"""
# the the pin object for the pin defined as "NEOPIXEL"
try:
pin = machine.Pin("NEOPIXEL")
except ValueError:
print(
"Error: The NEOPIXEL pin is not defined. Please check your board configuration.")
return
led = neopixel.NeoPixel(pin, 1) # create a NeoPixel object with 1 LED
# start at LED off
led[0] = (0, 0, 0)
led.write()
print()
print("RGB LED Color Transitions:")
time.sleep_ms(100)
# transition through a series of colors
print("\t<Off>\t", end='')
led_transition(led, 0, 0, 255)
print(" <Blue>")
wink_led(led)
print("\t<Blue>\t", end='')
led_transition(led, 255, 0, 0)
print(" <Red>")
wink_led(led)
print("\t<Red>\t", end='')
led_transition(led, 0, 255, 0)
print(" <Green>")
wink_led(led)
print("\t<Green>\t", end='')
led_transition(led, 255, 255, 0)
print(" <Yellow>")
wink_led(led)
print("\t<Yellow>", end='')
led_transition(led, 255, 255, 255)
print(" <White>")
wink_led(led)
print("\t<White>\t", end='')
led_transition(led, 0, 0, 0)
print(" <Off>")
# turn off the LED
led[0] = (0, 0, 0)
led.write()
def run():
"""
@brief Run the RGB ramp example.
@details
- Calls the rgb_ramp_example function to demonstrate LED color transitions.
"""
print("-----------------------------------------------------------")
print("Running the SparkFun RGB ramp example...")
print("-----------------------------------------------------------")
rgb_ramp_example()
print("Done!")
run()