Skip to content

Instantly share code, notes, and snippets.

@sourceperl
Last active September 23, 2015 19:58
Show Gist options
  • Save sourceperl/cad46bddb199e7baa8f8 to your computer and use it in GitHub Desktop.
Save sourceperl/cad46bddb199e7baa8f8 to your computer and use it in GitHub Desktop.
Manage a Rpi Panel with pygame and framebuffer interface
import os
import pygame
import time
import signal
#some const
BLACK = (0, 0, 0)
RED = (200, 0, 0)
WHITE = (255,255,255)
class Panel:
screen = None;
def __init__(self):
"""Ininitializes a new pygame screen using the framebuffer
"""
# init pygame for first framebuffer
os.putenv('SDL_FBDEV', '/dev/fb0')
pygame.display.init()
# request panel size
self.xmax = pygame.display.Info().current_w
self.ymax = pygame.display.Info().current_h
print "framebuffer size: %d x %d" % (self.xmax, self.ymax)
# some params
self.cur_font = "freesans"
self.cur_font_size = int(0.1 * self.ymax)
self.screen = pygame.display.set_mode((self.xmax, self.ymax),
pygame.FULLSCREEN)
# initialise
self.screen.fill(BLACK)
pygame.mouse.set_visible(False)
pygame.font.init()
pygame.display.update()
def c_print(self, msg, y_pos=10, color=WHITE):
"""Print message on panel at x center
"""
font = pygame.font.SysFont(self.cur_font, self.cur_font_size, bold=True)
r_str = font.render(msg, True, color)
(x_with, y_width) = r_str.get_size()
x_center = self.xmax/2 - x_with/2
self.screen.blit(r_str, (x_center, y_pos))
def update(self):
"""Call this to refresh framebuffer
"""
# screen is black
self.screen.fill(BLACK)
# build time/date string
self.c_print(time.strftime("S%V %d/%m/%Y %H:%M:%S", time.localtime()))
# say hello at center
self.c_print("Hello in RED !!!", y_pos=self.ymax/2, color=RED)
# update display
pygame.display.update()
# exit handler
def sigterm_handler(signum, frame):
global is_run
is_run = False
# manage SIGTERM for clean exit with supervisord
signal.signal(signal.SIGTERM, sigterm_handler)
# main task
panel = Panel()
is_run = True
while(is_run):
panel.update()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment