首页 > 解决方案 > 对函数内的变量使用多个值

问题描述

我有以下功能(来自闪烁棒库):

bstick.set_color(channel=0, index=0, name="red")

它基本上打开index=0通过 USB 连接到我的计算机的 LED 灯条中的第一个 LED ()。index=1将打开第二个,依此类推。

我希望index参数存储从 0 一直到 31 的多个值(因为条带中有 32 个 LED)。

我知道一个简单而懒惰的解决方法是只编写函数 32 次并index手动更改,但是有没有更聪明的方法来在其中存储 0-31 值?

我试过了:

while x < 32:
    bstick.set_color(channel=0, index=x, name="white")
    x+=1

但这并不是我真正想要的,因为这对我到目前为止的项目其余部分的编写方式不太友好,如下所示:

from datetime import datetime, time
import pandas
import ctypes
from playsound import playsound
from blinkstick import blinkstick

bstick = blinkstick.find_first()

def bstick_turn_on():
    x=0
    while x < 32:
        bstick.set_color(channel=0, index=x, name="white")
        x+=1

def bstick_turn_off():
    x=0
    while x < 32:
        bstick.set_color(channel=0, index=x)
        x+=1

file_path = "myfile.xlsx" #sunrise/sunset file path
data = pandas.read_excel(file_path, header=0) #Header on line 0

#Today as day number in reference to 1st of Jan
day = datetime.now().timetuple().tm_yday

#Today's parameters
#sr and ss are column names in the Excel spreadsheet for sunrise and sunset respectively
#Minus 1 to account for 0 based indexing
sunrise = data["sr"][day - 1]
sunset = data["ss"][day - 1] 

#Time right now
now = datetime.now().time()

#Function to convert time objects into integers
def seconds_in_time(time_value: time):
    return (time_value.hour * 60 + time_value.minute) * 60 + time_value.second

#Variable for a moment in time 5 minutes before the sunset
sunset_minus_five = seconds_in_time(sunset) - 60 * 5

#Setting up the day_night variable depending on the now variable
#delta calculates the difference in seconds between now and sunset -during night- and sunrise -during day-
#A negative value for delta means that now variable is equal to any moment between midnight and the sunrise  
if now > sunrise and now < sunset:
    day_night = 'day'
    delta = (seconds_in_time(now) - seconds_in_time(sunrise))
else:
    day_night = 'night'
    delta = (seconds_in_time(now) - seconds_in_time(sunset))

#delta_notification calculates the difference in seconds between now and sunset_minus_five
delta_notification = seconds_in_time(now) - sunset_minus_five
    
#The path to the wallpapers being used
path = 'C:\\Users\\mariu\\Desktop\\Sunset\\wallpapers\\'+ day_night +'.jpg'

#Function defined to perform an action (open/close the light) depending on the time of the day
def on_off():
    if now > sunrise and now < sunset:
        return bstick_turn_on()
    else: 
        return bstick_turn_off()

#Function to change the wallpaper
def changeBG(path):
    ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 3) #SystemParametersInfoW for x64 architecture

#An alarm sound is played and a red light turns on if delta_notification is less or equal than 15 seconds AND delta_notification is greater than -30 delta_notification <= 15 and delta_notification => -30:
if delta_notification <= 15 and delta_notification >= -30:
    playsound('alarm.wav') #Plays the sound
    bstick_turn_on()

#Wallpaper changes, a three-beep sound is played, and light turns on only if delta is less than 60 seconds AND delta is greater than -1
#In order for the light to turn on, the script should be ran on a computer that is on the same network as the light bulb (wireless)
if delta <= 15 and delta >= -30:
    changeBG(path)
    playsound('sound.mp3') #Plays the sound
    on_off()

标签: python

解决方案


由于看起来所有 32 个灯都同时打开或关闭,我建议使用参数制作一个函数。

def set_light(color=None, count=32):
    args = {'channel': 0}
    if color is not None:
        args['name'] = color
    for x in range(count):       
        bstick.set_color(**args, index=x)

关掉东西,set_light()打开东西,set_light('white')


推荐阅读