2016-12-18 30 views
0

我在滑雪时遇到了有关此HUD代码的问题。温度具有双重字符串 和零为空白时的速度。这里有什么问题? 对于风速和温度读数,我使用的是winspeed传感器通过现代电子,windsensor版本C.pygame字符串加倍和闪烁

# coding=UTF-8 

import pygame 
import sys 
import datetime 
import math 
import numpy 
import thread 
import RPi.GPIO as GPIO 
import time 
from adxl345 import ADXL345 
import Adafruit_GPIO.SPI as SPI 
from picamera import PiCamera 
import Adafruit_MCP3008 
import random 
volts = 0 
CLK = 18 
MISO = 20 
MOSI = 21 
CS = 8 
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI) 
temp=0 
angle = 0 
velocity = 0   







def draw_borders(screen): 
    #Draws some simple borders to the display"" 
    pygame.draw.lines(screen, (255, 255, 255), False, [(0, 30), (width, 30)], 2) 

def draw_time(screen): 
    #Draws the time to the display"" 
    the_time = datetime.datetime.now() 
    time_as_string = the_time.strftime('%H:%M') 
    font = pygame.font.Font(None, 42) 
    text = font.render(time_as_string, 1, (255, 255, 255)) 
    textpos = text.get_rect(centerx=screen.get_width()/2, centery=15) 
    screen.blit(text, textpos) # paste the text into the background 

def draw_speed(screen): 
    #Draws the speed to the display"" 
    # get the speed... 
    values = [0]*8 
    for i in range(8): 
     values[i] = mcp.read_adc(i) 
     #Get's channels 
     CHANNEL_ZERO = '{0:>4}'.format(*values) #Voltage out 
     CHANNEL_ONE = '{1:>4}'.format(*values) #Raw voltage 
     CHANNEL_TWO = '{2:>4}'.format(*values) 
     #Overflow Channels 
     CHANNEL_THREE = '{3:>4}'.format(*values) 
     CHANNEL_FOUR = '{4:>4}'.format(*values) 
     CHANNEL_FIVE = '{5:>4}'.format(*values) 
     CHANNEL_SIX = '{6:>4}'.format(*values) 
     CHANNEL_SEVEN = '{7:>4}'.format(*values) 
     #Makes channels integers to divide later 
     CHANNEL_ZERO = int(CHANNEL_ZERO) 
     CHANNEL_ONE = int(CHANNEL_ONE) 
     CHANNEL_TWO = int(CHANNEL_TWO) 
     thermVolts = CHANNEL_TWO *(3.3/1023) 
     #Temperature affects voltage, -1 degree celcius is a .5 drop in voltage 
     #If temp was 20 degrees celcius, therm = .5 
     therm = (30 * thermVolts) - 0 
     #Fahrenheight to celcius 
     therm = round(therm, 0) 
     volts = CHANNEL_ONE *(3.3/1023) #+ therm 
     #Temp is based on a 21 degree celcius temp, multiply accordingly 
     temp = (4.246*volts-9.3442) 
     #Get's volts data 
     speed = math.exp(temp) 
     #Get's velocity 
     kph = (speed * 1.609) 
     velocity = (kph * .278) 
     velocity = abs(round(velocity)) 
     the_speed_string = '{} mps'.format(int(velocity)) # display as whole number 
     #Wipes second string 
     if velocity == 0: 
      the_speed_string = 0 
     else: 
      font = pygame.font.Font(None, 160) 
      text = font.render(the_speed_string, 1, (255, 255, 255)) 
      textpos = text.get_rect(centerx=screen.get_width()/2, centery=screen.get_height()/2) 
      screen.blit(text, textpos) # paste the text into the background 
     #GIVES TEMPERATURE 
     the_temp_string = u'{}°F'.format(int(therm)) 
     if int(therm) == 0: 
      the_temp_string = 'ERROR' 
     else: 
      font = pygame.font.Font(None, 65) 
      text = font.render(the_temp_string, 1, (255, 255, 255)) 
      textpos = text.get_rect(centerx=screen.get_width()-80, centery=screen.get_height()-20) 
      screen.blit(text, textpos) # paste the text into the background 


def draw_altitude(screen): 
    accel = ADXL345() 
    axes1 = accel.getAxes(True) 
    x = axes1['x'] 
    z = axes1['z'] 
    y = axes1['y'] 
    #Get's 3D angle 
    angle = numpy.angle(x+y+z+1j, deg=True) 
    the_altitude_string = u'{} °'.format(round(angle,0)) 
    font = pygame.font.Font(None, 65) 
    text = font.render(the_altitude_string, 1, (255, 255, 255)) 
    textpos = text.get_rect(centerx=120, centery=screen.get_height()-20) 
    screen.blit(text, textpos) # paste the text into the background 




def draw_temp(screen): 
    #""Draws the temperature to the display 
    the_temp = 0 
    the_temp_string = u'{}°C'.format(the_temp) 
    font = pygame.font.Font(None, 65) 
    text = font.render(the_temp_string, 1, (255, 255, 255)) 
    textpos = text.get_rect(centerx=screen.get_width()-80, centery=screen.get_height()-20) 
    screen.blit(text, textpos) # paste the text into the background 





if __name__ == '__main__': 
    pygame.init() 
    size = width, height = 650, 400 
    black = 0, 0, 0 
    screen = pygame.display.set_mode(size) 
    while True: 
     # Checks for key presses, e.g. escape to quit 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: sys.exit() 
      if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE: sys.exit() 
     # Fill the screen with black background 
     screen.fill(black) 
     # Draw all the stuff 
     draw_borders(screen) 
     draw_time(screen) 
     draw_speed(screen) 
     draw_altitude(screen) 
     draw_temp(screen) 
     # Update the display 
     pygame.display.flip() 

回答

0

至于

温度具有双重串

我不不知道你在说什么,但原因为

the veloci TY当零空白

是你明确不若velocity == 0在你的代码,这部分画东西在屏幕上:

if velocity == 0: 
     the_speed_string = 0 
    else: 
     font = pygame.font.Font(None, 160) 
     text = font.render(the_speed_string, 1, (255, 255, 255)) 
     textpos = text.get_rect(centerx=screen.get_width()/2, centery=screen.get_height()/2) 
     screen.blit(text, textpos) # paste the text into the background 

也许你只想做

if velocity == 0: 
    the_speed_string = 0 

font = pygame.font.Font(None, 160) 
text = font.render(the_speed_string, 1, (255, 255, 255)) 
textpos = text.get_rect(centerx=screen.get_width()/2, centery=screen.get_height()/2) 
screen.blit(text, textpos) # paste the text into the background 
+0

我试过,但它仍然闪烁,并通过双字符串我的意思是一个字符串永久为0,并被正确的覆盖。 – Will4cat