from machine import Pin, PWM, Timer
from sys import stdin
from neopixel import NeoPixel
import time
import select
frequency = 10000
duty_cycle = 65535
duty_step = -100
led_blue = PWM(Pin(25))
led_blue.freq(frequency)
led_green = PWM(Pin(16))
led_green.freq(frequency)
led_red = PWM(Pin(17))
led_red.freq(frequency)
power = Pin(11, Pin.OUT)
power.high()
np = NeoPixel(Pin(12), 1)
np[0] = (0, 0, 0)
np.write()
rainbow_color = [(127, 0, 0),
(127, 30, 0),
(127, 127, 0),
(0, 127, 0),
(0, 0, 127),
(30, 0, 127),
(127, 0, 127)]
poll_object = select.poll()
poll_object.register(stdin,1)
while True:
led_blue.duty_u16(duty_cycle)
led_green.duty_u16(duty_cycle)
led_red.duty_u16(duty_cycle)
if (duty_cycle<=55535):
duty_step = 100
elif (duty_cycle>=65535):
duty_step = -100
duty_cycle = duty_cycle + duty_step
if poll_object.poll(0):
msg = stdin.readline().rstrip()
else:
time.sleep(0.05)
continue
n = len(msg)
m = msg.find(' ')
if (m != -1):
color = msg[0:m]
num = msg[m+1:n]
print(color+" light will blink "+num+" times")
num = int(num)
else:
color = msg[0:n]
print(color+" light will shine")
num = 0
if (color == "red"):
np_color = rainbow_color[0]
elif (color == "orange"):
np_color = rainbow_color[1]
elif (color == "yellow"):
np_color = rainbow_color[2]
elif (color == "green"):
np_color = rainbow_color[3]
elif (color == "blue"):
np_color = rainbow_color[4]
elif (color == "indigo"):
np_color = rainbow_color[5]
elif (color == "violet"):
np_color = rainbow_color[6]
elif (color == "rainbow"):
num = num + (num == 0)
for i in range(num):
for c in rainbow_color:
np[0] = c
np.write()
time.sleep(0.5)
np[0] = (0, 0, 0)
np.write()
continue
if (num == 0):
np[0] = np_color
np.write()
else:
for i in range(num):
np[0] = np_color
np.write()
time.sleep(0.5)
np[0] = (0, 0, 0)
np.write()
time.sleep(0.5)