0

I'm trying to solve this problem.

I would like to boot a raspbery directly into pygame without starting the GUI.

I have a raspberry pi generation 2 with the current "2021-01-11-raspios-buster-armhf.img" installed. This comes with an installation on python3.7 and pygame

I have a very simple pygame program that works, when I SSH into the pi as the "pi" user and run it with "sudo python3 pg.py" (Example code below). The program writes into the framebuffer "/dev/fb0" which causes a display that's on HDMI to show two images. Sudo rights are needed to allow pygame to access this buffer.

How can I get the pi to start this program as a sudo user on boot instead of showing a prompt?

I have set up the pi to boot into the promp for the "pi" user without starting the gui

After a boot I can type in

  • "cd /opt/pygame_prototype"

  • "sudo /usr/bin/python3 pg.py"

which starts pygame successfully.

So this is what I need to automate.

Any help would be welcome.

Program code pg.py

import logzero
from logzero import logger

logzero.logfile("/tmp/logzero-demo.log")
logger.info("info - autostart")

import os, pygame, time

logger.info(f"{time.time()}")


def setSDLVariables(driver):
    logger.info("Setting SDL variables...")
    os.environ["SDL_FBDEV"] = "/dev/fb0"
    os.environ["SDL_VIDEODRIVER"] = driver
    logger.info("...done")


def printSDLVariables():
    logger.info("Checking current env variables...")
    logger.info("SDL_VIDEODRIVER = {0}".format(os.getenv("SDL_VIDEODRIVER")))
    logger.info("SDL_FBDEV = {0}".format(os.getenv("SDL_FBDEV")))


setSDLVariables('fbcon')
printSDLVariables()
logger.info("Try init")
try:
    pygame.init()
except pygame.error:
    print("Driver '{0}' failed!".format(driver))
logger.info("init complete")
# size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
size = (640, 480)
logger.info("Detected screen size: {0}".format(size))
try:
    screen = pygame.display.set_mode(size)
    clock = pygame.time.Clock()
    pygame.mouse.set_visible(False)
    i1 = pygame.image.load(r"1.jpg")
    i2 = pygame.image.load(r"2.png")
    for x in range(10):
        screen.fill((0, 0, 0))
        screen.blit(i1, (x, x))
        pygame.display.flip()
        clock.tick(1)
        screen.fill((200, 200, 0))
        screen.blit(i2, (x * 10, x * 10))
        pygame.display.flip()
        clock.tick(1)
except Exception as e:
    logger.info(e)
    raise

1 Answer 1

0

Looks like I tried too many complicated solutions, because I could not get it to work with systemd.

An easy sulation that works is adding the two commands to

/etc/rc.local above the "exit 0" line.

"cd /opt/pygame_prototype"
"/usr/bin/python3 pg.py"

This file is run with root rights at the end of the login process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.