Friday, 15 March 2013

Snowboard HUD Part 4 - Basic display

This posts describes the construction of a basic display with some placeholders.

The idea is to produce something like this that displays the speed, altitude and temperature.

I'm using a Raspberry Pi and I want to avoid booting into the GUI so I need a way of creating a graphical display from the terminal display. I discovered that this is possible using Python and PyGame.

I think this was already installed but in case it isn't, the easiest thing to do is use aptitide to install python setup tools and then you can install any packages you want.

sudo apt-get install python-setuptools
sudo apt-get install python-pip

then you can use pip to install other Python packages e.g. PySerial

sudo pip install pyserial

So to create the display I'm going to use PyGame. This may not be the most sensible thing to do, but it does seem to work. Alternative suggestions welcome.

I used a PyGame tutorial from Pete Shinners to get me started, which was very helpful.

This first version of the display creates some text in fixed positions in the display as placeholders until I can actually start reading some data. There are also a lot of hard coded positions , which at the moment doesn't matter because it is set up to be displayed on the MyVu glasses.

# coding=UTF-8

import pygame
import sys
import datetime

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 =
    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...
    the_speed = 0.0
    the_speed_string = '{} mph'.format(int(the_speed)) # display as whole number

    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

def draw_altitude(screen):
    """Draws the altitude to the display"""
    the_altitude = 0
    the_altitude_string = '{} m'.format(the_altitude)

    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__':


    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

        # Draw all the stuff

        # Update the display

And what this produces is:

You can see that this is actually being run on a Mac, which is good for editing the display layout, maybe doing some keyboard based control later, but the actual display needs to run on the Pi connected to the glasses.

You can see the same here being displayed in the glasses.

As a side note, I'm actually getting the code onto the Pi by copying it to a public dropbox folder and downloading with wget. I'm doing a lot of this through SSH-ing into the Pi, but I don't know how to start the script from the remote login and have the display update on the glasses that are connected via composite. Please let me know if you know if this is possible. You can just about hear from the video that I have a keyboard connected the the Pi in order to run the Python script.

I also had to mess around quite a lot with the resolution of the display. If the MyVu didn't like the resolution I would get a blank screen inside the glasses and couldn't get rid of it without restarting the Pi. The line configuring the PyGame display:

size = width, height = 650, 400

isn't the maximum resolution that I could probably use, but it does work.

No comments:

Post a Comment