gpt4 book ai didi

python - """TypeError: only length-1 arrays can be converted to Python scalars"""尝试在具有 7 段显示的 Pi3 上运行计数器时

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:40 24 4
gpt4 key购买 nike

我目前正在为带有七段显示器的 RPi-3 开发一个简单的计数程序。我有两种从一数到十的方法,一种是手动的,总是点亮我需要的单个段,另一种是基于 numpy 矩阵的方法。

手动方式工作正常,但我在使用矩阵方式时遇到问题。下面是带有一些解释的代码以供理解:

import RPi.GPIO as GPIO
import time
import numpy

GPIO.setmode(GPIO.BOARD)

GPIO.setup(31, GPIO.OUT) #up right
GPIO.setup(32, GPIO.OUT) #up
GPIO.setup(33, GPIO.OUT) #up left
GPIO.setup(35, GPIO.OUT) #middle
GPIO.setup(36, GPIO.OUT) #down right
GPIO.setup(37, GPIO.OUT) #down left
GPIO.setup(38, GPIO.OUT) #decimal point(in the bottom right corner)
GPIO.setup(40, GPIO.OUT) #down

def all_off(): #turns all segments off, works as well!
GPIO.output(31, False)
GPIO.output(32, False)
GPIO.output(33, False)
GPIO.output(35, False)
GPIO.output(36, False)
GPIO.output(37, False)
GPIO.output(38, False)
GPIO.output(40, False)

matrix = numpy.matrix([[1,0,0,0,1,0,0,0], #those are the combinations
[1,1,0,1,0,1,0,1], #from one to nine and finishing
[1,1,0,1,1,0,0,1], #with zero
[1,0,1,1,1,0,0,0], #first number of a line is value for Pin31 then 32,
[0,1,1,1,1,0,0,1], #33, 35, 36, 37, 38, 40
[0,1,1,1,1,1,0,1],
[1,1,0,0,1,0,0,0], #combinations are correct, btw!
[1,1,1,1,1,1,0,1],
[1,1,1,1,1,0,0,1]])

def set_to(byte):
all_off()
GPIO.output(31, int(byte[0])) #<-- this is the line where the error
GPIO.output(32, int(byte[1])) #occurs
GPIO.output(33, int(byte[2]))
GPIO.output(35, int(byte[3]))
GPIO.output(36, int(byte[4]))
GPIO.output(37, int(byte[5]))
GPIO.output(38, int(byte[6]))
GPIO.output(40, int(byte[7]))

for line in matrix: #the actual counting program
set_to(matrix[line])
time.sleep(1)

GPIO.cleanup()

问题显然是数组无法读取?我不明白问题是什么或者如何解决这个问题。它似乎与矩阵的“线”有关,它只是另一个矩阵(只有一条线)。

我真的不知道该怎么办,请帮忙!!

最佳答案

我改变了我的策略,现在使用 numpy 数组。现在我有了一个工作代码:

import RPi.GPIO as GPIO
import time
import numpy

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(31, GPIO.OUT)
GPIO.setup(32, GPIO.OUT)
GPIO.setup(33, GPIO.OUT)
GPIO.setup(35, GPIO.OUT)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
GPIO.setup(40, GPIO.OUT)

pinlist = [(31, 0), (32, 1) ,(33, 2) ,(35, 3) ,(36, 4) ,(37, 5) ,(38, 6), (40, 7)]

def all_off():
GPIO.output(31, False)
GPIO.output(32, False)
GPIO.output(33, False)
GPIO.output(35, False)
GPIO.output(36, False)
GPIO.output(37, False)
GPIO.output(38, False)
GPIO.output(40, False)

matrix = numpy.array([[1,0,0,0,1,0,0,0],
[1,1,0,1,0,1,0,1],
[1,1,0,1,1,0,0,1],
[1,0,1,1,1,0,0,0],
[0,1,1,1,1,0,0,1],
[0,1,1,1,1,1,0,1],
[1,1,0,0,1,0,0,0],
[1,1,1,1,1,1,0,1],
[1,1,1,1,1,0,0,1]])

for i in range(0, 9):
all_off()
for (pin, j) in pinlist:
GPIO.output(pin, int(matrix[i][j]))
time.sleep(0.25)

GPIO.cleanup()

其工作原理如下:

我创建了所有 Pin 图及其编号的列表(打包在一个元组中)。这些元组由大 for 循环内部的 for 循环读取。现在我引用数组内单个点的“坐标”。这意味着,它有点慢(你可以看到单个片段的弹出),但幸运的是我不必关心!

尽管如此,还是非常感谢您的支持!

关于python - """TypeError: only length-1 arrays can be converted to Python scalars"""尝试在具有 7 段显示的 Pi3 上运行计数器时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39145284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com