Having got my WiPy connected to my Wireless network I wrote the following script mainly as a way of testing the process of updating main.py (which is the python script that is executed automatically when the4 WiPy 2.0 finishes booting), but I felt like a bit of fun too.
Unfortunately there is a limit to what you can do with a single LED!
You don’t need to include #!/usr/bin/python at the beginning of the script but I’ve left it in as I originally wrote the script on my desktop using Python and added in the WiPy specific bits later – one of the nice things about using Micro Python is that most of the time your scripts will just work with Python.
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>
def to_hex(r, g, b):
h = int(r*255)
h = h * 256 + int(g*255)
return h * 256 + int(b*255)
def hsv_to_rgb(h, s, v): # Taken from colorsys.py on github
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]
# Note - hsv_to_rgb returns floating point fractional values (0-1.0)
# Disable heartbeat (otherwise setting the LED colour will fail).
n = 360 # Colour range # (0-360 degrees)
s = 1.0 # Saturation # (0-1.0)
v = 0.5 # Vibrance # (0-1.0)
while True: # Loop for ever.
for j in range(0, n, 1):
r, g, b = hsv_to_rgb(j*1.0/n, s, v)
pycom.rgbled(to_hex(r, g, b))
# Turn off the LED and enable heartbeat again.
If you only want to cycle through all the colours of the rainbow a limited number of times replace the while loop with a for loop.
for i in range(2): # Cycle through rainbow twice.