Last active
May 24, 2017 13:46
-
-
Save dsample/ce5452aaddf979f037a09150dd35318b to your computer and use it in GitHub Desktop.
Adafruit Pi Framebuffer https://learn.adafruit.com/pi-video-output-using-pygame/pointing-pygame-to-the-framebuffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pygame | |
import time | |
import random | |
class pyscope : | |
screen = None; | |
def __init__(self): | |
"Ininitializes a new pygame screen using the framebuffer" | |
# Based on "Python GUI in Linux frame buffer" | |
# http://www.karoltomala.com/blog/?p=679 | |
disp_no = os.getenv("DISPLAY") | |
if disp_no: | |
print "I'm running under X display = {0}".format(disp_no) | |
# Check which frame buffer drivers are available | |
# Start with fbcon since directfb hangs with composite output | |
drivers = ['fbcon', 'directfb', 'svgalib'] | |
found = False | |
for driver in drivers: | |
# Make sure that SDL_VIDEODRIVER is set | |
if not os.getenv('SDL_VIDEODRIVER'): | |
os.putenv('SDL_VIDEODRIVER', driver) | |
try: | |
pygame.display.init() | |
except pygame.error: | |
print 'Driver: {0} failed.'.format(driver) | |
continue | |
found = True | |
break | |
if not found: | |
raise Exception('No suitable video driver found!') | |
size = (pygame.display.Info().current_w, pygame.display.Info().current_h) | |
print "Framebuffer size: %d x %d" % (size[0], size[1]) | |
self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN) | |
# Clear the screen to start | |
self.screen.fill((0, 0, 0)) | |
# Initialise font support | |
pygame.font.init() | |
# Hide the mouse cursor | |
pygame.mouse.set_visible(0) | |
# Render the screen | |
pygame.display.update() | |
def __del__(self): | |
"Destructor to make sure pygame shuts down, etc." | |
def background(self): | |
# Fill the screen with red (255, 0, 0) | |
red = (255, 0, 0) | |
self.screen.fill(red) | |
# Update the display | |
pygame.display.update() | |
def draw_rect(self): | |
pygame.draw.rect(self.screen, (0,0,255), (200,150,100,50)) | |
pygame.display.update() | |
# Create an instance of the PyScope class | |
scope = pyscope() | |
scope.background() | |
time.sleep(1) | |
scope.draw_rect() | |
time.sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment