partofthething / infopanel

Show live data, animations, pictures, or anything on simple displays like RGB matrices
https://partofthething.com/infopanel/
GNU General Public License v3.0
33 stars 12 forks source link

A Time-Date Sprite #11

Open trlafleur opened 5 years ago

trlafleur commented 5 years ago

This is a sprite that will display time or date formatted by Linux style "strftime"

     sprites:
       - DayTime: 
           x: 25 
           y: 10
           ticks_per_movement: 2
           dx: -1
           can_flip: false
           strftime: '%I:%M:%S %p'
           pallete: 
             text: 
               - 255
               - 0
               - 255 
# ************************************************************** #
# ************************************************************** #
# ver: 1.0 tom@lafleur.us 15Dec2018  

class DayTime(FancyText):

    CONF = FancyText.CONF.extend ({vol.Optional('strftime', default= '%I:%M:%S %P'): str})

    def __init__(self, max_x, max_y, data_source):
        """Construct a sprite."""
        FancyText.__init__(self, max_x, max_y, data_source=data_source)
        self.last_val = None
        self.value = None
        self.now = None
        self.text = None
        self.pallete = {'text':[0, 255, 255]}
        self.strftime = None

    def apply_config(self, conf):
        """Validate and apply configuration to this sprite."""
        conf = FancyText.apply_config(self, conf)
        self._make_text()
        return conf 

    def _make_text(self):

        """Draw the current time."""
        self.now = datetime.datetime.now()
        self.text = self.now.strftime(self.strftime)
        self.last_val = self.text
        DayTime.add(self, self.text, self.pallete['text'])    

    def update_value(self):
        """Update, but only if the value has changed."""
        val = self.value() if callable(self.value) else self.value  # pylint: disable=not-callable
        if val != self.last_val:
            # only do lookup when things change for speed.
            self.clear()
            self._make_text()

    def render(self, display):
        """Render a frame and advance."""
        self.update_value()
        return FancyText.render(self, display)

# ************************************************************** #
partofthething commented 5 years ago

Looks sweet. You should make this as a pull request!

I'll accept it, no doubt. This looks very nice.