LCD Manager – display server

Our LCD Manager would be better with display server, something that will take care of rendering at constant FPS. If you look at Piader v2 you see that there is a main_loop function. In it we refresh lcd content at some fps but it is quite annoying because it shouldn’t be our problem. Manager should take care of it.

Our task for now: add display server.

LCDManager@Bitbucket
Piader@Bitbucket

Display server

Lets think for a while about what we need. First we need whole fps calculation. Secondly we need to flush lcd. Refreshing buffer still goes to our logic not manager.

Thirdly it would be nice to be able to add any number of lcds into display server, drawback ? All with same fps.

Fourthly ability to set fps.

Ok we have some idea, lets get to work.

Core

Our server require Threads so we need to extend it. Other function are quite simple. One function require some brief – run. In this function we will iterate over managers dictionary and call flush on each manager. Also we will put old trick with fps calculation.

Create file display.py in lcdmanager package:

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Display server"""
from threading import Thread


class Display(Thread):
    """Class Display"""
    def __init__(self, fps=1):
        Thread.__init__(self)
        self.fps = fps
        self.work = True
        self.managers = {}

    def add(self, manager, name):
        """adds manager"""
        self.managers[name] = manager

     def run(self):
        """starts server and refresh screen"""
        try:
            while self.work:
                start = time.time()
                for manager in self.managers:
                    self.managers[manager].flush()

                end = time.time()
                if end - start < self.fps:
                    t_delta = end - start
                    time.sleep(max(0, self.fps - t_delta))
        finally:
            pass

    def join(self, timeout=None):
        """stop thread"""
        self.work = False
        Thread.join(self, timeout)

Lets see if this works. Go to Piader project, open game.py and:

  • add code to __init__
        self.display = display.Display(0.5)
        self.display.add(self.game_manager, 'one')
        if self.score_manager:
            self.display.add(self.score_manager, 'two')
  • remove tick function
  • instead of self.tick() paste:
self.game_manager.render()
if self.score_manager:
    self.score_manager.render()

Run Piader and.. it works !

Upgrade – render

Our Display class only refresh display. But what if we add render call into it ? Maybe even some variable to control if we want rendering or not ?

Lets modify __init__ add another parameter render=False and assignment to body:

self.render = render

Next change for in run function:

for manager in self.managers:
    if self.render:
        self.managers[manager].render()
    self.managers[manager].flush()

Finally from Piader remove render() calls on managers and change Display initialization to:

self.display = display.Display(0.5, True)

Stop! Jenkins time!

Not much here. But I found that pylint has report file but not chars :/ Will work on it later

Summary

We have slightly improved Manager now it gives you a class that can refresh displays at fixed fps. This class is a foot hold for next step 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s