树莓派学习障碍
import RPi.GPIO as GPIOimport time
colors =
R = 11
G = 12
B = 13
def setup(Rpin, Gpin, Bpin):
global pins
global p_R, p_G, p_B
pins = {'pin_R': Rpin, 'pin_G': Gpin, 'pin_B': Bpin}
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
for i in pins:
GPIO.setup(pins, GPIO.OUT) # Set pins' mode is output
GPIO.output(pins, GPIO.HIGH) # Set pins to high(+3.3V) to off led
p_R = GPIO.PWM(pins['pin_R'], 2000)# set Frequece to 2KHz
p_G = GPIO.PWM(pins['pin_G'], 1999)
p_B = GPIO.PWM(pins['pin_B'], 5000)
p_R.start(100) # Initial duty Cycle = 0(leds off)
p_G.start(100)
p_B.start(100)
def map(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def off():
for i in pins:
GPIO.output(pins, GPIO.HIGH) # Turn off all leds
def setColor(col): # For example : col = 0x112233
R_val = (col & 0xff0000) >> 16
G_val = (col & 0x00ff00) >> 8
B_val = (col & 0x0000ff) >> 0
R_val = map(R_val, 0, 255, 0, 100)
G_val = map(G_val, 0, 255, 0, 100)
B_val = map(B_val, 0, 255, 0, 100)
p_R.ChangeDutyCycle(100-R_val) # Change duty cycle
p_G.ChangeDutyCycle(100-G_val)
p_B.ChangeDutyCycle(100-B_val)
def loop():
while True:
for col in colors:
setColor(col)
time.sleep(1)
def destroy():
p_R.stop()
p_G.stop()
p_B.stop()
off()
GPIO.cleanup()
if __name__ == "__main__":
try:
setup(R, G, B)
loop()
except KeyboardInterrupt:
destroy()
为啥要设置color的元祖,意义有什么作用还有PWM控制的频率为啥设置这个数值 color 是数组吧,意义就是用起来方便。
频率拍脑袋定,你改一个差别大的数,看一下效果就知道了。 赚小钱 发表于 2020-7-3 17:59
color 是数组吧,意义就是用起来方便。
频率拍脑袋定,你改一个差别大的数,看一下效果就知道了。
好的,我试试,谢谢 mhxy199069 发表于 2020-7-6 09:03
好的,我试试,谢谢
调试 + 控制变量法,只要有耐心,能读懂任何代码。 赚小钱 发表于 2020-7-6 10:39
调试 + 控制变量法,只要有耐心,能读懂任何代码。
谢谢指教
页:
[1]