"""Raspberry Pi Pico W LESSON 12: Understanding and Controlling an RGB LED in MicroPythonhttps://www.youtube.com/watch?v=yZkx-KWbATY&list=PLGs0VKk2DiYz8js1SJog21cDhkBqyAhC5&index=15"""fromtimeimportsleepfrommachineimport(Pin,PWM)# pylint: disable=import-errorcolors:dict={'red':(255,0,0),'green':(0,128,0),'blue':(0,0,255),'cyan':(0,255,255),'magenta':(255,0,255),'yellow':(255,255,0),'orange':(255,165,0),'white':(255,255,255)}pins:dict={'RED':13,'GREEN':14,'BLUE':15}pwms:tuple=(PWM(Pin(pins['RED'])),PWM(Pin(pins['GREEN'])),PWM(Pin(pins['BLUE'])))
[docs]defcalc_pwm(color_value)->int:""" Calculate PWM value :param color_value: :return: """returnint(color_value*(65550/255))
[docs]defpwms_off()->None:""" Turn all colors off :return: """# noinspection PyTypeCheckerforpwminpwms:pwm.duty_u16(0)
[docs]definitial_setup(val=1000)->None:""" Initial frequency setup :param val: :return: """# noinspection PyTypeCheckerforpwminpwms:pwm.freq(val)
[docs]defled_on(led_color:str)->None:""" Turn led on based on user choose :param led_color: :return: """rgb:list=[0,0,0]# noinspection PyTypeCheckerfori,cinenumerate(colors[led_color]):x:int=calc_pwm(c)pwms[i].duty_u16(x)rgb[i]=xprint(f"\nDEBUG -> R: {rgb[0]}, G: {rgb[1]}, B: {rgb[2]}\n")
[docs]defget_color()->str:""" Ask user to choose a color :return: """whileTrue:# noinspection PyTypeCheckerall_colors='\n'.join(cforcincolors)rgb_color=input("\nPlease enter color of your choice "f"from the list below:\n\n{all_colors}\n\ntype here -> ").lower()ifrgb_color=='exit':returnrgb_colorifrgb_colorincolors:returnrgb_colorprint("\nPlease choose your color only from the listed options ""or type 'exit' to stop the execution.")sleep(2)
if__name__=='__main__':initial_setup()whileTrue:# noinspection PyTypeCheckercolor:str=get_color()ifcolor=='exit':print('\nThe program will stop the execution now...')breakpwms_off()led_on(color)