Skip to content

Commit

Permalink
Merge pull request #1648 from cobra18t/dev
Browse files Browse the repository at this point in the history
Add "use_dots_for_commas" setting to segment displays
  • Loading branch information
jabdoa2 authored Aug 12, 2022
2 parents 4623085 + e1121c6 commit e1eabcb
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 140 deletions.
1 change: 1 addition & 0 deletions mpf/config_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,7 @@ segment_displays:
size: single|int|7
integrated_dots: single|bool|false
integrated_commas: single|bool|false
use_dots_for_commas: single|bool|false
default_color: list|color|white
default_transition_update_hz: single|float_or_token|30
platform_settings: single|dict|None
Expand Down
13 changes: 9 additions & 4 deletions mpf/devices/segment_display/segment_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ async def _initialize(self):
"See error above.".format(self.name)) from ex

text = SegmentDisplayText.from_str("", self.size, self.config['integrated_dots'],
self.config['integrated_commas'], self._default_color)
self.config['integrated_commas'], self.config['use_dots_for_commas'],
self._default_color)
self._update_display(SegmentDisplayState(text, FlashingType.NO_FLASH, ''))

def add_virtual_connector(self, virtual_connector):
Expand Down Expand Up @@ -240,6 +241,7 @@ def _update_stack(self) -> None:
transition = TransitionManager.get_transition(self.size,
self.config['integrated_dots'],
self.config['integrated_commas'],
self.config['use_dots_for_commas'],
transition_config)
if previous_text_stack_entry:
previous_text = previous_text_stack_entry.text
Expand Down Expand Up @@ -278,7 +280,8 @@ def _update_stack(self) -> None:

# update the display
text = SegmentDisplayText.from_str(new_text, self.size, self.config['integrated_dots'],
self.config['integrated_commas'], colors)
self.config['integrated_commas'], self.config['use_dots_for_commas'],
colors)
self._update_display(SegmentDisplayState(text, flashing, flash_mask))

def _current_placeholder_changed(self, future=None, **kwargs) -> None:
Expand All @@ -291,7 +294,8 @@ def _current_placeholder_changed(self, future=None, **kwargs) -> None:
new_text, self._current_placeholder_future = self._current_placeholder.evaluate_and_subscribe({})
self._current_placeholder_future.add_done_callback(self._current_placeholder_changed)
text = SegmentDisplayText.from_str(new_text, self.size, self.config['integrated_dots'],
self.config['integrated_commas'], self._current_state.text.get_colors())
self.config['integrated_commas'], self.config['use_dots_for_commas'],
self._current_state.text.get_colors())
self._update_display(SegmentDisplayState(text, self._current_state.flashing,
self._current_state.flash_mask))

Expand All @@ -304,7 +308,8 @@ def set_color(self, colors: List[RGBColor]):
assert isinstance(colors, list)
assert self.hw_display is not None
text = SegmentDisplayText.from_str(self._current_state.text.convert_to_str(), self.size,
self.config['integrated_dots'], self.config['integrated_commas'], colors)
self.config['integrated_dots'], self.config['integrated_commas'],
self.config['use_dots_for_commas'], colors)
self._update_display(SegmentDisplayState(text,
self._current_state.flashing,
self._current_state.flash_mask))
Expand Down
39 changes: 21 additions & 18 deletions mpf/devices/segment_display/segment_display_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,48 @@ class SegmentDisplayText(metaclass=abc.ABCMeta):

"""A list of characters with specialized functions for segment displays. Use for display text effects."""

__slots__ = ["_text", "embed_dots", "embed_commas"]
__slots__ = ["_text", "embed_dots", "embed_commas", "use_dots_for_commas"]

def __init__(self, char_list, embed_dots, embed_commas):
def __init__(self, char_list, embed_dots, embed_commas, use_dots_for_commas):
"""Initialize segment display text."""
self.embed_dots = embed_dots
self.embed_commas = embed_commas
self.use_dots_for_commas = use_dots_for_commas
self._text = char_list

# pylint: disable=too-many-arguments
@classmethod
def from_str_with_color(cls, text: str, display_size: int, collapse_dots: bool, collapse_commas: bool,
colors: List[RGBColor]) -> "ColoredSegmentDisplayText":
use_dots_for_commas: bool, colors: List[RGBColor]) -> "ColoredSegmentDisplayText":
"""Create colored text."""
return ColoredSegmentDisplayText(
cls._create_characters(text, display_size, collapse_dots, collapse_commas, colors[:]),
collapse_dots, collapse_commas)
cls._create_characters(text, display_size, collapse_dots, collapse_commas, use_dots_for_commas, colors[:]),
collapse_dots, collapse_commas, use_dots_for_commas)

# pylint: disable=too-many-arguments
@classmethod
def from_str(cls, text: str, display_size: int, collapse_dots: bool, collapse_commas: bool,
colors: Optional[List[RGBColor]] = None) -> \
def from_str(cls, text: str, display_size: int, collapse_dots: bool, collapse_commas: bool,
use_dots_for_commas: bool, colors: Optional[List[RGBColor]] = None) -> \
Union["ColoredSegmentDisplayText", "UncoloredSegmentDisplayText"]:
"""Create from string."""
if colors:
return cls.from_str_with_color(text, display_size, collapse_dots, collapse_commas, colors[:])
return cls.from_str_with_color(text, display_size, collapse_dots, collapse_commas,
use_dots_for_commas, colors[:])

char_colors = [None] * len(text)
return UncoloredSegmentDisplayText(
cls._create_characters(text, display_size, collapse_dots, collapse_commas, char_colors),
collapse_dots, collapse_commas)
cls._create_characters(text, display_size, collapse_dots, collapse_commas, use_dots_for_commas,
char_colors), collapse_dots, collapse_commas, use_dots_for_commas)

@classmethod
def _embed_dots_and_commas(cls, text: str, collapse_dots: bool, collapse_commas: bool):
def _embed_dots_and_commas(cls, text: str, collapse_dots: bool, collapse_commas: bool, use_dots_for_commas: bool):
"""Return text with embedded dots and commas."""
char_has_dot = False
char_has_comma = False
char_list = []
for char in reversed(text):
char_code = ord(char)
if collapse_dots and char_code == DOT_CODE:
char_code = ord(char)
if collapse_dots and char_code == DOT_CODE or use_dots_for_commas and char_code == COMMA_CODE:
char_has_dot = True
continue
if collapse_commas and char_code == COMMA_CODE:
Expand All @@ -69,8 +71,8 @@ def _embed_dots_and_commas(cls, text: str, collapse_dots: bool, collapse_commas:

# pylint: disable-msg=too-many-locals
@classmethod
def _create_characters(cls, text: str, display_size: int, collapse_dots: bool, collapse_commas: bool,
colors: List[Optional[RGBColor]]) -> List[DisplayCharacter]:
def _create_characters(cls, text: str, display_size: int, collapse_dots: bool, collapse_commas: bool,
use_dots_for_commas: bool, colors: List[Optional[RGBColor]]) -> List[DisplayCharacter]:
"""Create characters from text and color them.
- Colors are used from the left to the right (starting with the first character).
Expand All @@ -82,7 +84,7 @@ def _create_characters(cls, text: str, display_size: int, collapse_dots: bool, c
char_list = []
left_pad_color = colors[0] if colors else None
default_right_color = colors[len(colors) - 1] if colors else None
uncolored_chars = cls._embed_dots_and_commas(text, collapse_dots, collapse_commas)
uncolored_chars = cls._embed_dots_and_commas(text, collapse_dots, collapse_commas, use_dots_for_commas)
colors = colors[-len(uncolored_chars):]
for char_code, char_has_dot, char_has_comma in uncolored_chars:
color = colors.pop(0) if colors else default_right_color
Expand All @@ -104,7 +106,7 @@ def blank_segments(self, flash_mask) -> "SegmentDisplayText":
return ColoredSegmentDisplayText(
[char if mask != "F" else DisplayCharacter(SPACE_CODE, False, False, char.color)
for char, mask in zip(self._text, flash_mask)],
self.embed_dots, self.embed_commas
self.embed_dots, self.embed_commas, self.use_dots_for_commas
)

def convert_to_str(self):
Expand All @@ -126,7 +128,8 @@ def __len__(self):
def __getitem__(self, item):
"""Return item or slice."""
if isinstance(item, slice):
return self.__class__(self._text.__getitem__(item), self.embed_dots, self.embed_commas)
return self.__class__(self._text.__getitem__(item), self.embed_dots, self.embed_commas,
self.use_dots_for_commas)

return self._text.__getitem__(item)

Expand Down
6 changes: 4 additions & 2 deletions mpf/devices/segment_display/transition_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ class TransitionManager:
__slots__ = []

@staticmethod
def get_transition(output_length: int, collapse_dots: bool, collapse_commas: bool, transition_config=None):
def get_transition(output_length: int, collapse_dots: bool, collapse_commas: bool, use_dots_for_commas: bool,
transition_config=None):
"""Create a transition instance based on the specified configuration."""
if transition_config:
config = transition_config.copy()
config.pop('type')
return TRANSITIONS[transition_config['type']](output_length, collapse_dots, collapse_commas, config)
return TRANSITIONS[transition_config['type']](output_length, collapse_dots, collapse_commas,
use_dots_for_commas, config)

return None

Expand Down
Loading

0 comments on commit e1eabcb

Please sign in to comment.