请问提示ValueError: too many values to unpack (expected 3)怎么解决?
-
在main1.py中使用70个RGB来点亮70颗RGB灯可以正常运行,但改成其它数量时就会报错:ValueError: too many values to unpack (expected 3)
此问题是在更新OpenMV IDE之后出现的。
# -*- coding: utf-8 -*- import gc try: import pyb except ImportError: import machine as pyb class WS2812: """ Driver for WS2812 RGB LEDs. May be used for controlling single LED or chain of LEDs. Example of use: chain = WS2812(spi_bus=1, led_count=4) data = [ (255, 0, 0), # red (0, 255, 0), # green (0, 0, 255), # blue (85, 85, 85), # white ] chain.show(data) Version: 1.0 """ buf_bytes = (0x88, 0x8e, 0xe8, 0xee) def __init__(self, spi_bus=1, led_count=1, intensity=1): """ Params: * spi_bus = SPI bus ID (1 or 2) * led_count = count of LEDs * intensity = light intensity (float up to 1) """ self.led_count = led_count self.intensity = intensity # prepare SPI data buffer (4 bytes for each color) self.buf_length = self.led_count * 3 * 4 self.buf = bytearray(self.buf_length) # SPI init self.spi = pyb.SPI(2, pyb.SPI.MASTER, baudrate=3200000, polarity=0, phase=1) # turn LEDs off self.show([]) def show(self, data): """ Show RGB data on LEDs. Expected data = [(R, G, B), ...] where R, G and B are intensities of colors in range from 0 to 255. One RGB tuple for each LED. Count of tuples may be less than count of connected LEDs. """ self.fill_buf(data) self.send_buf() def send_buf(self): """ Send buffer over SPI. """ self.spi.send(self.buf) gc.collect() def update_buf(self, data, start=0): """ Fill a part of the buffer with RGB data. Order of colors in buffer is changed from RGB to GRB because WS2812 LED has GRB order of colors. Each color is represented by 4 bytes in buffer (1 byte for each 2 bits). Returns the index of the first unfilled LED Note: If you find this function ugly, it's because speed optimisations beated purity of code. """ buf = self.buf buf_bytes = self.buf_bytes intensity = self.intensity mask = 0x03 index = start * 12 for red, green, blue in data: red = int(red * intensity) green = int(green * intensity) blue = int(blue * intensity) buf[index] = buf_bytes[green >> 6 & mask] buf[index+1] = buf_bytes[green >> 4 & mask] buf[index+2] = buf_bytes[green >> 2 & mask] buf[index+3] = buf_bytes[green & mask] buf[index+4] = buf_bytes[red >> 6 & mask] buf[index+5] = buf_bytes[red >> 4 & mask] buf[index+6] = buf_bytes[red >> 2 & mask] buf[index+7] = buf_bytes[red & mask] buf[index+8] = buf_bytes[blue >> 6 & mask] buf[index+9] = buf_bytes[blue >> 4 & mask] buf[index+10] = buf_bytes[blue >> 2 & mask] buf[index+11] = buf_bytes[blue & mask] index += 12 return index // 12 def fill_buf(self, data): """ Fill buffer with RGB data. All LEDs after the data are turned off. """ end = self.update_buf(data) # turn off the rest of the LEDs buf = self.buf off = self.buf_bytes[0] for index in range(end * 12, self.buf_length): buf[index] = off index += 1
# Untitled - By: LENOVO - 周三 12月 8 2021 import sensor, image, time from ws2812 import WS2812 from pyb import SPI sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) sensor.set_auto_whitebal(False) #sensor.set_auto_exposure(False) clock = time.clock() spi = SPI(2, SPI.MASTER, baudrate=1000000, polarity=1, phase=0) chain = WS2812(spi_bus=1, led_count=10) #(spi总线,灯珠数量) #i = 1 #a = b = c = 0 while(True): clock.tick() img = sensor.snapshot() sta = img.get_statistics(roi=(0,0,320,240)) #图像数据获取范围 s_l = sta.l_mean() s_a = sta.a_mean() s_b = sta.b_mean() RGB = image.lab_to_rgb((s_l,s_a,s_b)) #lab to RGB #RGB=(a,b,c) data = [ #一行10个 RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB, ''' RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB, RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB, RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB, RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB, RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB, RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,RGB,''' #(i, 0, 0), #(0, i, 0), #(0, 0, i), #(255, 0, 0),# red #(0, 255, 0), # green #(0, 0, 255), # blue #(85, 85, 85), # white ] #a = b = c = 255 #i = i + 2 '''if i==1: a = 255 b=c=0 i=i+1 elif i==2: a=c=0 b=255 i=i+1 else: a=b=0 c=255 i=1''' chain.show(data) print("Lab",s_l,s_a,s_b) print("RGB",RGB[0],RGB[1],RGB[2])
-
代码有问题啊,注释是用警号#,你用三个单引号'''变成字符串了。