我的以下代码工作得很好,但是它 WAY 太慢了-大约是3/4秒的延迟。它在Raspbian Stretch下的Raspberry Pi 3+上运行,铬浏览器作为客户端,而apache2作为服务器,两者都在同一Pi上。有一个5英寸显示器,但没有连接键盘或鼠标。该设备有四个连接到GPIO引脚的瞬时开关,每个按钮控制一个光标。两个按钮上下移动光标图像,另外两个按钮控制光标图像上方的文本。每次按下按钮后,我都会使用pynput.keyboard向浏览器发出一个按键(在下面的代码中为F5键)。我确信这太慢的原因是我更新/ run / thermostat中的文件中的控制值后刷新整个屏幕,更好的解决方案是模拟UpArrow和DnArrow以移动突出显示图像或更新文本(视情况而定)。发送适当的控制代码很容易,但是如何在不刷新整个页面的情况下移动图像或更改文本呢(请注意,这还允许我使用外部浏览器的键盘来执行相同的操作。)
监视按钮状态的外部Python脚本的片段:
import RPi.GPIO as GPIO
import time, os
from pins import pinsarrow
from pynput.keyboard import Key, Controller
keyboard = Controller()
...
def my_callback4(channel):
Exists = os.path.exists('/run/thermostat/Cursor') # Check to see Cursor position is set
if Exists:
Fsize = os.path.getsize('/run/thermostat/Cursor') # Check for a valid file
if Fsize == 0:
Write_Cursor(0)
Cursor = Read_Cursor()
if Cursor > 0:
Cursor = Cursor - 1
Write_Cursor(Cursor) # Write the new Cursor position
##################################################
keyboard.press(Key.f5) #
keyboard.release(Key.f5) #
##################################################
else:
Write_Cursor(0) # File does not exist. Write the Cursor file.
GPIO.add_event_detect(pinsarrow[0], GPIO.RISING, callback=my_callback1, bouncetime=100)
GPIO.add_event_detect(pinsarrow[1], GPIO.RISING, callback=my_callback2, bouncetime=100)
GPIO.add_event_detect(pinsarrow[2], GPIO.RISING, callback=my_callback3, bouncetime=100)
GPIO.add_event_detect(pinsarrow[3], GPIO.RISING, callback=my_callback4, bouncetime=100)
创建网页的Python代码片段:
Exists = os.path.exists('/run/thermostat/Cursor') # Check to see the Cursor position has been set
if Exists:
Fsize = os.path.getsize('/run/thermostat/Cursor')
if Fsize > 0:
with open("/run/thermostat/Cursor","r") as f: # Get the Cursor position
Cursor = int(f.read())
else:
write_Cursor() # No value in Cursor file
else:
write_Cursor() # Cursor file does not exist
RunStatus = ["Heating On", "Cooling On", "Fan On", "Idle"]
print('Content-type: text/html\n')
print("\n")
with open("/usr/lib/cgi-bin/index.txt","r") as f:
x = f.read()
print(x)
######################################################################
## Places the cursor image at a position specified by variable Cursor#
######################################################################
print('<img src="/images/Thermostat-Cursor.png" width="100" height="29" style="position: absolute; left: 15px; top: ',65 + 30 * Cursor,'px;">', sep='')
############################################################
## Check for cursor position and set color text accordingly#
############################################################
bColor = ["0", "0", "0", "0", "0", "0", "0"]
for x in range(0, 7):
if Cursor == x:
bColor[x] = "black"
else:
bColor[x] = "rgb(200,200,200)"
## Print each <div> with appropriate color
print('<div style="color:',bColor[0],'" class="Scale">',ClassValues[0][LineValue[0]],'</div>', sep="")
print('<div style="color:',bColor[1],'" class="Clock">',ClassValues[1][LineValue[1]],'</div>', sep="")
print('<div style="color:',bColor[2],'" class="Barometer">',ClassValues[2][LineValue[2]],'</div>', sep="")
print('<div style="color:',bColor[3],'" class="Server">',ClassValues[3][LineValue[3]],'</div>', sep="")
print('<div style="color:',bColor[4],'" class="Control">',ClassValues[4][LineValue[4]],'</div>', sep="")
if LineValue[4] < 2:
print('<div style="color:',bColor[5],'" class="Hot">Cool: ',ClassValues[5],'</div>', sep="")
if LineValue[4] == 0 or LineValue[4] == 2:
print('<div style="color:',bColor[6],'" class="Cold">Heat: ',ClassValues[6],'</div>', sep="")