我对编码和东西很陌生。我正在使用带有 HX711 分线板的数字称重秤,并通过 4 位 7 段显示器输出值。
weighing()
循环读取值的速度比我的显示多路复用时间慢,因此代码在读取值之前不会继续,导致显示像地狱一样闪烁。所以我尝试通过同时运行weighing()
循环和displaying()
循环concurrent.futures
。但是代码只会执行weighing()
一次然后卡在display()
循环中,所以它们没有同时运行?
我的代码一定有问题,请帮助我澄清并留下任何其他方法的建议。
import time
import RPi.GPIO as GPIO
import concurrent.futures
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
segments = [2, 3, 4, 5, 6, 7, 8, 9]
digits = [12, 13, 18, 19]
GPIO.setup(segments, GPIO.OUT)
GPIO.output(segments, GPIO.HIGH)
GPIO.setup(digits, GPIO.OUT)
GPIO.output(digits, GPIO.LOW)
nums ={
0:(1,1,1,1,1,1,0),
1:(0,1,1,0,0,0,0),
2:(1,1,0,1,1,0,1),
3:(1,1,1,1,0,0,1),
4:(0,1,1,0,0,1,1),
5:(1,0,1,1,0,1,1),
6:(1,0,1,1,1,1,1),
7:(1,1,1,0,0,0,0),
8:(1,1,1,1,1,1,1),
9:(1,1,1,1,0,1,1)}
switchpolarity = {1: 0,
0:1}
def display(value):
while 1:
s = [int(d) for d in str(value)]
for digit in range(0,len(s)):
for segment in range(0,7):
GPIO.output(segments[segment], switchpolarity[nums[s[digit]][segment]])
GPIO.output(digits[digit], 1)
time.sleep(0.01)
GPIO.output(digits[digit], 0)
EMULATE_HX711=False
if not EMULATE_HX711:
from hx711 import HX711
else:
from emulated_hx711 import HX711
def weighing():
while 1:
val = round(abs(hx.get_weight(1)))
print(val)
hx.power_down()
hx.power_up()
return(val)
hx = HX711(9, 10)
hx.set_reading_format("MSB", "MSB")
hx.set_reference_unit(754)
hx.reset()
hx.tare()
print("Tare done! Add weight now...")
try:
with concurrent.futures.ProcessPoolExecutor() as executor:
weighing = executor.submit(weighing)
displaying = executor.submit(display, (t1.result()))
except(KeyboardInterrupt):
GPIO.cleanup()
对于我发布的代码中的拼写错误,我感到非常抱歉,我在没有测试的情况下更改了进程名称。这是我的新代码,我可以说没有愚蠢的错误:
import time
import RPi.GPIO as GPIO
import concurrent.futures
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
segments = [2, 3, 4, 5, 6, 7, 8, 9]
digits = [12, 13, 18, 19]
GPIO.setup(segments, GPIO.OUT)
GPIO.output(segments, GPIO.HIGH)
GPIO.setup(digits, GPIO.OUT)
GPIO.output(digits, GPIO.LOW)
nums ={
0:(1,1,1,1,1,1,0),
1:(0,1,1,0,0,0,0),
2:(1,1,0,1,1,0,1),
3:(1,1,1,1,0,0,1),
4:(0,1,1,0,0,1,1),
5:(1,0,1,1,0,1,1),
6:(1,0,1,1,1,1,1),
7:(1,1,1,0,0,0,0),
8:(1,1,1,1,1,1,1),
9:(1,1,1,1,0,1,1)}
switchpolarity = {1: 0,
0:1}
def display(value):
while 1:
s = [int(d) for d in str(value)]
for digit in range(0,len(s)):
for segment in range(0,7):
GPIO.output(segments[segment], switchpolarity[nums[s[digit]][segment]])
GPIO.output(digits[digit], 1)
time.sleep(0.01)
GPIO.output(digits[digit], 0)
EMULATE_HX711=False
if not EMULATE_HX711:
from hx711 import HX711
else:
from emulated_hx711 import HX711
def weighing():
while 1:
val = round(abs(hx.get_weight(1)))
print(val)
hx.power_down()
hx.power_up()
return(val)
hx = HX711(9, 10)
hx.set_reading_format("MSB", "MSB")
hx.set_reference_unit(754)
hx.reset()
hx.tare()
print("Tare done! Add weight now...")
try:
with concurrent.futures.ProcessPoolExecutor() as executor:
weighing1 = executor.submit(weighing)
displaying1 = executor.submit(display, (weighing1.result()))
except(KeyboardInterrupt):
GPIO.cleanup()