1
我使用pygame覆盆子pi和adafruit tft屏幕来显示内容。pygame不关闭,并返回到控制台(python)
#!/usr/bin/python
from subprocess import *
from numpy import genfromtxt
import pygame
from pygame.locals import *
import os
import time
from time import strftime
#from pygame.locals import*
#from matplotlib.dates import DateFormatter
import RPi.GPIO as GPIO
import datetime
def run_cmd(cmd):
"""Used to run Linux commands"""
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output
def displayText(text, size, line, color, clearScreen):
"""Used to display text to the screen. displayText is only configured to display
two lines on the TFT. Only clear screen when writing the first line"""
if clearScreen:
screen.fill((0, 0, 0))
font = pygame.font.Font(None, size)
text = font.render(text, 0, color)
textRotated = pygame.transform.rotate(text, -90)
textpos = textRotated.get_rect()
textpos.centery = 80
if line == 1:
textpos.centerx = 90
screen.blit(textRotated,textpos)
elif line == 2:
textpos.centerx = 40
screen.blit(textRotated,textpos)
def displayTime():
"""Used to display date and time on the TFT"""
screen.fill((0, 0, 0))
font = pygame.font.Font(None, 50)
now=time.localtime()
for setting in [("%H:%M:%S",60),("%d %b",10)] :
timeformat,dim=setting
currentTimeLine = strftime(timeformat, now)
text = font.render(currentTimeLine, 0, (0,250,150))
Surf = pygame.transform.rotate(text, -90)
screen.blit(Surf,(dim,20))
def main():
global ax,fig,screen
global firstTime,lines
global TwentyFourHours,TwelveHours,OneWeek
global dataFile
size = width, height = 128, 160
TFTxSize = 2.28
TFTySize = 1.63
TwentyFourHours = 288
TwelveHours = 144
OneWeek = 2016
firstTime = True #Used to work out if a function has already been run
whatToDisplay = 1 #What do display on screen
rotate = 0 #Used when automatically rotating the display.
startTime = time.time() #Used to work out how much time has passed when rotating.
#Set the framebuffer device to be the TFT
os.environ["SDL_FBDEV"] = "/dev/fb1"
#Setup pygame display
pygame.init()
pygame.mouse.set_visible(0)
screen = pygame.display.set_mode(size)
try:
while True:
time.sleep(.5)
firstTime = True #If button pressed, set this to True
if whatToDisplay == 1: #Display time
displayTime()
elif whatToDisplay == 2: #Display last temperature recorded.
displayText('Current Temp', True)
elif whatToDisplay == 3: #Rotate display
elapsedTime = time.time() - startTime
#Write to TFT
pygame.display.flip()
except KeyboardInterrupt:
pygame.quit()
sys.exit()
if __name__ == '__main__':
main()
按下^ C不会关闭屏幕显示并且不会返回到控制台。 我必须重新启动pi,这对测试内容来说非常痛苦。 显示效果很好,当我可以正确关闭时,我会解决任何其他问题。
太谢谢你了。发挥魅力。我已经标记你是一个正确的答案,但我还不能公开提议。 – Gregws
@Gregws不客气的朋友! – cdonts