Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 当从Python中的另一个非同步方法调用时,WS2812b LED条带未在Raspberry Pi上更新_Python 3.x_Python Asyncio_Python Multithreading_Raspberry Pi4 - Fatal编程技术网

Python 3.x 当从Python中的另一个非同步方法调用时,WS2812b LED条带未在Raspberry Pi上更新

Python 3.x 当从Python中的另一个非同步方法调用时,WS2812b LED条带未在Raspberry Pi上更新,python-3.x,python-asyncio,python-multithreading,raspberry-pi4,Python 3.x,Python Asyncio,Python Multithreading,Raspberry Pi4,我正在为一个项目建造充电站,但我无法解决这个问题。 由于某些原因,当检测到RFID/NFC卡时,我的LED条(WS 2812b)不闪烁 每当读卡时,我试图使LED条闪烁,然后将LED更新为连接器的状态 尝试将数据管脚更改为库中所有受支持的管脚(PWM、SPI、PCM),但没有任何效果。还尝试调整刷新频率。我怀疑这是硬件问题,因为当从库中运行示例脚本时,它工作正常(我还制作了一个异步版本)。 我尝试过将异步循环从主非同步方法解析到rfid\u reader\u线程,并使用asyncio.run\u

我正在为一个项目建造充电站,但我无法解决这个问题。 由于某些原因,当检测到RFID/NFC卡时,我的LED条(WS 2812b)不闪烁

每当读卡时,我试图使LED条闪烁,然后将LED更新为连接器的状态

尝试将数据管脚更改为库中所有受支持的管脚(PWM、SPI、PCM),但没有任何效果。还尝试调整刷新频率。我怀疑这是硬件问题,因为当从库中运行示例脚本时,它工作正常(我还制作了一个异步版本)。 我尝试过将异步循环从主非同步方法解析到
rfid\u reader\u线程
,并使用
asyncio.run\u coroutine\u threadsafe
运行它,但结果是一样的

混合使用不同的GPIO库是否会影响这一点?(使用用于PN532阅读器的wiringPi、RPi.GPIO、adafruit库)

有人能解释为什么会发生这种情况,或者如何解决这个问题吗

import board
import busio
from adafruit_pn532.i2c import PN532_I2C
from digitalio import DigitalInOut
from unsync import unsync
import asyncio
from rpi_ws281x import Color, PixelStrip


class PN532Reader:
    def __init__(self):
        self._i2c = busio.I2C(board.SCL, board.SDA)
        self._reset_pin = DigitalInOut(board.D6)
        self._req_pin = DigitalInOut(board.D12)
        self._reader: PN532_I2C = PN532_I2C(self._i2c, debug=False, reset=self._reset_pin, req=self._req_pin)
        self._reader.SAM_configuration()
        self._reader.reset()

    def read_passive(self):
        return self._reader.read_passive_target(timeout=.1)


class LEDStrip:
    RED: Color = Color(255, 0, 0)
    GREEN: Color = Color(0, 0, 255)
    BLUE: Color = Color(0, 255, 0)
    YELLOW: Color = Color(245, 241, 29)
    ORANGE: Color = Color(255, 144, 59)
    WHITE: Color = Color(255, 255, 255)
    OFF: Color = Color(0, 0, 0)

    def __init__(self, pixels: int = 1, has_card_indicator: bool = False):
        if has_card_indicator:
            pixels += 1
        self._has_card_indicator: bool = has_card_indicator
        self._LED_COUNT: int = pixels
        self._LED_PIN: int = 12
        self._LED_FREQ_HZ: int = 800000
        self._LED_DMA: int = 10
        self._LED_BRIGHTNESS: int = 255
        self._LED_INVERT: bool = False
        self._LED_CHANNEL: int = 0
        self._strip: PixelStrip = PixelStrip(self._LED_COUNT, self._LED_PIN,
                                             self._LED_FREQ_HZ, self._LED_DMA,
                                             self._LED_INVERT, self._LED_BRIGHTNESS,
                                             self._LED_CHANNEL)
        self._strip.begin()
        self.clear()
        if self._has_card_indicator:
            self._indicator_pixel: int = self._LED_COUNT - 1
            self._strip.setPixelColor(self._LED_COUNT - 1, LEDStrip.WHITE)
            self._strip.show()

    def clear(self):
        for index in range(0, self._LED_COUNT):
            self._strip.setPixelColor(index, LEDStrip.OFF)
        self._strip.show()

    async def blink(self, index: int, color: Color, timeout: float):
        if index < 0:
            return
        self._strip.setPixelColor(index, LEDStrip.OFF)
        self._strip.show()
        await asyncio.sleep(timeout)
        self._strip.setPixelColor(index, color)
        self._strip.show()
        await asyncio.sleep(timeout)
        self._strip.setPixelColor(index, LEDStrip.OFF)
        self._strip.show()
        await asyncio.sleep(timeout * 2)
        self._strip.setPixelColor(index, color)
        self._strip.show()

    async def indicate_card_read(self):
        if not self._has_card_indicator:
            return
        await self.blink(self._indicator_pixel, LEDStrip.GREEN, .2)
        self._strip.setPixelColor(self._indicator_pixel, LEDStrip.WHITE)
        self._strip.show()

    async def indicate_card_rejected(self):
        if not self._has_card_indicator:
            return
        await self.blink(self._indicator_pixel, LEDStrip.RED, .2)
        self._strip.setPixelColor(self._indicator_pixel, LEDStrip.WHITE)
        self._strip.show()


class BindingClass:
    def __init__(self):
        # Other things, such as APScheduler and I/O classes
        self.led_strip: LEDStrip = LEDStrip(2, True)

    async def indicate_card_read(self):
        await self.led_strip.indicate_card_read()

    async def indicate_card_rejected(self):
        await self.led_strip.indicate_card_rejected()


class_that_binds_stuff_together = None


@unsync
async def communication_with_server():
    global class_that_binds_stuff_together
    class_that_binds_stuff_together = BindingClass()
    rfid_reader_thread()
    while True:
        # maintain websocket communication
        await asyncio.sleep(1)
        pass


@unsync
def rfid_reader_thread():
    reader = PN532Reader()
    while True:
        uid = reader.read_passive()
        if uid is not None:
            handle_request(uid)


@unsync
async def handle_request(uid):
    global class_that_binds_stuff_together
    # Wait for multiple tasks to be complete
    await asyncio.gather(class_that_binds_stuff_together.indicate_card_read())
    # do some other stuff


if __name__ == '__main__':
    communication_with_server().result()
导入板
进口业务
从adafruit_pn532.i2c导入pn532_i2c
从digitalio导入DigitalInOut
从非同步导入非同步
导入异步
从rpi_ws281x导入颜色,像素带
PN532类读取器:
定义初始化(自):
self._i2c=业务i2c(board.SCL,board.SDA)
自复位引脚=数字输入输出(电路板D6)
自我要求引脚=数字输入输出(电路板D12)
self.\u读卡器:PN532_I2C=PN532_I2C(self.\u I2C,debug=False,reset=self.\u reset\u引脚,req=self.\u req\u引脚)
self.\u reader.SAM\u配置()
self.\u reader.reset()
def read_被动(自):
返回self.\u reader.read\u被动式\u目标(超时=.1)
类LED带:
红色:颜色=颜色(255,0,0)
绿色:颜色=颜色(0,0,255)
蓝色:颜色=颜色(0,255,0)
黄色:颜色=颜色(245241,29)
橙色:颜色=颜色(255、144、59)
白色:颜色=颜色(255、255、255)
关闭:颜色=颜色(0,0,0)
def uuu init uuuu(self,像素:int=1,has_card_指示器:bool=False):
如果有\u卡\u指示器:
像素+=1
self.\u有\u卡\u指示器:bool=有\u卡\u指示器
自发光二极管计数:整数=像素
自发光二极管引脚:int=12
自发光二极管频率:int=800000
自发光二极管DMA:int=10
自发光二极管亮度:int=255
自发光二极管反转:布尔=假
自发光二极管通道:int=0
自发光二极管计数、自发光二极管引脚、,
自发光二极管频率,自发光二极管DMA,
自发光二极管反转,自发光二极管亮度,
自发光二极管(发光二极管通道)
self.\u strip.begin()
self.clear()
如果self.\有\卡\指示器:
自发光二极管计数-1
self.\u strip.setPixelColor(self.\u LED\u COUNT-1,LED strip.WHITE)
self._strip.show()
def清除(自):
对于范围内的索引(0,自计数):
self.\u strip.setPixelColor(索引,LED strip.OFF)
self._strip.show()
异步def闪烁(self,index:int,color:color,timeout:float):
如果指数<0:
返回
self.\u strip.setPixelColor(索引,LED strip.OFF)
self._strip.show()
等待异步IO.sleep(超时)
self.\u strip.setPixelColor(索引,颜色)
self._strip.show()
等待异步IO.sleep(超时)
self.\u strip.setPixelColor(索引,LED strip.OFF)
self._strip.show()
等待异步睡眠(超时*2)
self.\u strip.setPixelColor(索引,颜色)
self._strip.show()
异步def指示卡读取(自):
如果不是自我。有卡指示灯:
返回
等待自我闪烁(自我指示灯\像素,LED带绿色,.2)
self.\u strip.setPixelColor(self.\u indicator\u pixel,led strip.WHITE)
self._strip.show()
异步def指示卡被拒绝(自身):
如果不是自我。有卡指示灯:
返回
等待自我闪烁(自我指示灯\像素,LED条带红色,.2)
self.\u strip.setPixelColor(self.\u indicator\u pixel,led strip.WHITE)
self._strip.show()
类绑定类:
定义初始化(自):
#其他东西,如APScheduler和I/O类
self.led_strip:LEDStrip=LEDStrip(2,真)
异步def指示卡读取(自):
等待自我。led灯带。指示卡读数()
异步def指示卡被拒绝(自身):
等待自我。led灯带。指示卡被拒绝()
将内容绑定在一起的类=无
@不同步
与服务器()的异步def通信:
将内容绑定在一起的全局类
将东西绑定在一起的类=BindingClass()
rfid_阅读器_线程()
尽管如此:
#维护websocket通信
等待asyncio.sleep(1)
通过
@不同步
def rfid_读卡器_线程():
读卡器=PN532Reader()
尽管如此:
uid=读卡器。读取\u被动()
如果uid不是None:
处理请求(uid)
@不同步
异步def句柄请求(uid):
将内容绑定在一起的全局类
#等待多个任务完成
等待asyncio.gather(将内容绑定在一起的类。指示卡片读取()
#做些别的事情
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
与_服务器()的通信。结果()