Neopix发光二极管库

2024-06-08 01:18:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为Neopixel Led条创建我自己的库,因为没有我需要的函数库,我想知道Led是如何工作的。在{a1}的LED中,我发现我必须以方波的形式发送24位GRB颜色值,并在24位之间暂停50us

Bithttps://i.stack.imgur.com/SG30f.png

Period Times Of The Wavesperiod times of the square wave

现在我想知道如何在覆盆子圆周率的帮助下将这些24位GRB颜色转换成方波

例01

import time
import RPi.GPIO as GPIO



us = 0
GPIO.setmode(GPIO.BOARD)

GPIO.setup(18, GPIO.OUT)

def microdelay(us):
    time.sleep(us/1e6)




def zero():
    GPIO.output(18, GPIO.HIGH)
    microdelay(0.4)
    GPIO.output(18, GPIO.LOW)
    microdelay(0.85)

def one():
    GPIO.output(18, GPIO.HIGH)
    microdelay(0.8)
    GPIO.output(18, GPIO.LOW)
    microdelay(0.45)


# Color Red : 0000 0000 1111 1111 0000 0000

if __name__ == "__main__":
    zero()
    zero()
    zero()
    zero()

    zero()
    zero()
    zero()
    zero()

    one()
    one()
    one()
    one()

    one()
    one()
    one()
    one()

    zero()
    zero()
    zero()
    zero()

    zero()
    zero()
    zero()
    zero()
    print("strip is red")

我在控制台上获得以下输出:

pi@raspberrypi:~/Libary testing$sudo python3 LED_testing.py

LED_testing.py:9:RuntimeWarning:此通道已在使用中,仍在继续。使用GPIO.setwarnings(False)禁用警告。 GPIO.setup(18,GPIO.OUT)

条纹是红色的

pi@raspberrypi:~/图书馆测试$

例02

import time


import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BOARD)

GPIO.setup(18, GPIO.OUT)

def usleep(us: int):
  time.sleep(us/1e6)


def one():
    GPIO.output(18, GPIO.HIGH)
    usleep(0.8)
    GPIO.output(18, GPIO.LOW)
    usleep(0.45)



def zero():
    GPIO.output(18, GPIO.HIGH)
    usleep(0.4)
    GPIO.output(18, GPIO.LOW)
    usleep(0.85)




if __name__ == '__main__':

    for i in range(0, 862):
        # Green
        one()
        one()
        one()
        one()

        one()
        one()
        one()
        one()

        # Red
        one()
        one()
        one()
        one()

        one()
        one()
        one()
        one()

        # Blue
        one()
        one()
        one()
        one()

        one()
        one()
        one()
        one()

        usleep(50)
    print("strip is white")

控制台输出: pi@raspberrypi:~/Libary testing$sudo python3 LED_testing.py

LED_testing.py:7:运行时警告:此通道已在使用中,仍在继续。使用GPIO.setwarnings(False)禁用警告。 GPIO.setup(18,GPIO.OUT) 条纹是白色的

pi@raspberrypi:~/图书馆测试$


Tags: importoutputledgpiotimedefsetupout
1条回答
网友
1楼 · 发布于 2024-06-08 01:18:28

方波只是数字信号的图形表示。 下表很重要:

enter image description here

然后查看序列图时:

enter image description here

您可以看到以下内容:

  • 0由0.4us高和0.85us低表示
  • A 1代表0.8us高,其后为0.45us低
  • 静止是50 us或以上的低信号

提示:要睡眠0.4毫秒,您可以执行以下操作:

import time

# sleep for 0.4us
time.sleep(0.4/1000000)

假设您希望发送GBR值(255、16、12)。 这将转化为: 1111111100000000001100

在python中,这可以通过位操作完成:

def get_bits_from_grb(g: int, r: int, b: int) -> list:
  return list(bin(g<<16|r<<8|b).replace("0b", "")) 

文档状态:按照GRB的顺序发送数据,并首先发送高位

“高位先发送”表示从左到右发送

您的代码可能是以下几行代码之一:

import time

def usleep(us: int):
  time.sleep(us/1e6)

def send_bit(signal_bit: bool):
  if signal_bit:
    GPIO.output(pin_number, GPIO.HIGH)
    usleep(0.8)
    GPIO.output(pin_number, GPIO.LOW)
    usleep(0.45)
  else:
    GPIO.output(pin_number, GPIO.HIGH)
    usleep(0.4)
    GPIO.output(pin_number, GPIO.LOW)
    usleep(0.85)

def send_rest():
  GPIO.output(pin_number, GPIO.HIGH)
  usleep(0.51) # should be more than 50us
  GPIO.output(pin_number, GPIO.LOW)

def send_bits(bit_list: list):
  for b in bit_list:
    send_bit(b)

bit_list = get_bits_from_grb(176, 106, 76)
send_bits(bit_list)

相关问题 更多 >

    热门问题