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

Color is not pickup from config file. New class DynamicText #12

Open trlafleur opened 5 years ago

trlafleur commented 5 years ago

I did a new version of your DynamicFancyText sprite that allows the color to be in the config file... it also builds this format: label-data-units --> CO2: 234.2 ppm, all in one sprite

What happens is the color is set to the defaults from class Sprite, and it stays that way until I send an MQTT data value then it changes to the colors from the config file... what am I doing wrong???

If I remove in update_value, the if val != self.last_val:, from the program, it works correctly, as it forces a refresh...

I've also notice that fancytext may have the same issue, it only uses the default color is there a way to force the color change on setup?? thanks.....

you need to add pallete for 'units' in sprite CONF as below...

    CONF = vol.Schema({vol.Optional('dx', default=0): int,
                       vol.Optional('dy', default=0): int,
                       vol.Optional('ticks_per_frame', default=1): int,
                       vol.Optional('ticks_per_movement', default=1): int,
                       vol.Optional('ticks_per_phrase', default=200): int,
                       vol.Optional('min_ticks_per_phrase', default=100): int,
                       vol.Optional('max_ticks_per_phrase', default=400): int,
                       vol.Optional('x', default=0): int,
                       vol.Optional('y', default=0): int,
                       vol.Optional('font_name', default='5x8.bdf'): str,
                       vol.Optional('phrases', default=['']): list,
                       vol.Optional('pallete', default={1: [255, 255, 255],
                                                        'text':[0, 255, 0],
                                                        'label':[255, 255, 0],
                                                        'units':[255,0,255]}): PALLETE_SCHEMA,
                       vol.Optional('frames', default=None): FRAMES_SCHEMA,
                       vol.Optional('text', default=''): str,
                       vol.Optional('can_flip', default=True): bool,
                       vol.Optional('reverse_frame_loop', default=True): bool})
# ************************************************************** #
# ************************************************************** # 
# ver: 1.0 tom@lafleur.us 15Dec2018  

class DynamicText(FancyText):  # pylint:disable=too-many-instance-attributes
    """
    FancyText that can have a changing/live data source. With Data_label, then data, then units

    This has a ``label`` configuration to render something like:
    ``Label: [value]``
    """

    CONF = FancyText.CONF.extend({'label': vol.Coerce(str),
                                  vol.Optional('data_label'): vol.Coerce(str),
                                  vol.Optional('label_fmt', default='{}'): str,
                                  vol.Optional('val_fmt', default='{}'): str,
                                  vol.Optional('units', default=''): 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.label = None
        self.value = None
        self.label_fmt = None
        self.val_fmt = None
        self.data_label = None
        self.pallete = None # {'label':[0, 255, 255], 'units':[255, 0, 0], 'text': [255, 255, 255]}
        self.units = None

    def apply_config(self, conf):
        """Validate and apply configuration to this sprite."""
        conf = FancyText.apply_config(self, conf)
        if conf['data_label']:
            # make this a callable function to enable live/updating data
            self.value = lambda: self._convert_data(self.data_source[conf['data_label']])
        self._make_text()
        return conf

    def _convert_data(self, val):  # pylint: disable=no-self-use
        return val

    def _make_text(self):
        """Render the label and live value."""
        if self.label:
            DynamicText.add(self, self.label_fmt.format(self.label), self.pallete['text'])        # <--------------

        val = self.value() if callable(self.value) else self.value  # pylint: disable=not-callable
        text = self.val_fmt.format(val)
        self.last_val = val
        DynamicText.add(self, text, self.pallete['label'])

        if self.units:
            DynamicText.add(self, self.units, self.pallete['units'])

    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

For FancyText it appears to be working as expected. In the test_config.yaml I set

vehicle:
    type: FancyText
    text: VROOM!
    pallete:
       text:
          - 255
          - 100
          - 255

And this gives the appropriate color on startup:

screenshot from 2018-12-17 08-58-16

This is being handled by FancyText.apply_config.

for DynamicFancyText the color will default to the value of value_color in the config instead of the text palette. See code here:

https://github.com/partofthething/infopanel/blob/8c62d90236ecf03419fdd225e757051c566e565d/infopanel/sprites.py#L391

I really like the idea of adding units to DynamicFancyText so I'll add that.

partofthething commented 5 years ago

On second thought you can get the units already without a code change by setting in the config:

val_fmt: "{} ppm"

trlafleur commented 5 years ago

Thanks...... for the update....the issue with my version is that the colors default, until their data value changes... also, my version allows me to set the color of each field...

~~ /) ~~~~ /) ~~ _/) ~~ _/) ~~

Tom Lafleur

On Mon, Dec 17, 2018 at 9:09 AM Nick Touran notifications@github.com wrote:

On second thought you can get the units already without a code change by setting in the config:

val_fmt: {} ppm

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/partofthething/infopanel/issues/12#issuecomment-447922093, or mute the thread https://github.com/notifications/unsubscribe-auth/AEXCKx3frVmrp1dcm0Nxktv6n3S4eYvnks5u58-9gaJpZM4ZUygg .

trlafleur commented 5 years ago

I'm setting in the scene, not in the sprites... This allows the color to change by scene... so maybe my concept on how this should works is wrong??

scenes: RSF1: sprites:

   - RSF1:
       x: 2
       y: 8
       pallete:
         text:
           - 0
           - 255
           - 255

sprites:

RSF1: type: FancyText text: 'RSF-1' font_name: '5x8.bdf'

~~ /) ~~~~ /) ~~ _/) ~~ _/) ~~

Tom Lafleur

On Mon, Dec 17, 2018 at 9:07 AM Nick Touran notifications@github.com wrote:

For FancyText it appears to be working as expected. In the test_config.yaml I set

vehicle: type: FancyText text: VROOM! pallete: text:

  • 255
  • 100
  • 255

And this gives the appropriate color on startup:

[image: screenshot from 2018-12-17 08-58-16] https://user-images.githubusercontent.com/5360219/50102577-46924480-01da-11e9-9429-2b4d05204211.png

This is being handled by FancyText.apply_config.

for DynamicFancyText the color will default to the value of value_color in the config instead of the text palette. See code here:

https://github.com/partofthething/infopanel/blob/8c62d90236ecf03419fdd225e757051c566e565d/infopanel/sprites.py#L391

I really like the idea of adding units to DynamicFancyText so I'll add that.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/partofthething/infopanel/issues/12#issuecomment-447921338, or mute the thread https://github.com/notifications/unsubscribe-auth/AEXCK4iQM_Npd1kdxTJpTB9GqQ74Bv8fks5u5884gaJpZM4ZUygg .

trlafleur commented 5 years ago

also, you may want to change the MQTT port from 8883 to the more common values of 1883 in your documentation...

let me explain the issue with my version...

on launch, the colors are set by the defaults in the program, and NOT what I requested by the pallete:... when the first MQTT data arrives, the colors change to what I have set in pallete:...

also, I'm planning on adding your code to change color based on value limits... I did a quick try to inherit that from Duration, but there were some issues, data was overlapping on display......

sprites: Maine-CO2: type: DynamicText units: 'ppm' label: 'CO2: ' label_fmt: '{}' data_label: 'Maine/CO2' font_name: 6x9.bdf


scenes: Maine: sprites:

~~ /) ~~~~ /) ~~ _/) ~~ _/) ~~

Tom Lafleur

On Mon, Dec 17, 2018 at 9:35 AM Tom Lafleur lafleur@lafleur.us wrote:

Thanks...... for the update....the issue with my version is that the colors default, until their data value changes... also, my version allows me to set the color of each field...

~~ /) ~~~~ /) ~~ _/) ~~ _/) ~~

Tom Lafleur

On Mon, Dec 17, 2018 at 9:09 AM Nick Touran notifications@github.com wrote:

On second thought you can get the units already without a code change by setting in the config:

val_fmt: {} ppm

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/partofthething/infopanel/issues/12#issuecomment-447922093, or mute the thread https://github.com/notifications/unsubscribe-auth/AEXCKx3frVmrp1dcm0Nxktv6n3S4eYvnks5u58-9gaJpZM4ZUygg .