Skip to content

Commit

Permalink
Add fast train icon to indicate which trains are fast or not.
Browse files Browse the repository at this point in the history
This also adds initial support for rendering glyphs, though generating the python files is a little finicky as it stands.

This will only show if "slow_stations" are provided.
  • Loading branch information
tomwardio committed Jan 15, 2024
1 parent e8bb4b5 commit 09bf090
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
Binary file added assets/fast_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
module('display.py', base_path='src')
module('epd29b.py', base_path='src')
module('fonts.py', base_path='src')
module('glyphs.py', base_path='src')
module('logging.py', base_path='src')
module('main.py', base_path='src')
module('ssd1322.py', base_path='src')
Expand Down
4 changes: 3 additions & 1 deletion src/assets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@

from . import dot_matrix_regular
from . import dot_matrix_bold
from . import dot_matrix_bold_tall
from . import dot_matrix_bold_tall

from . import fast_train_icon
23 changes: 23 additions & 0 deletions src/assets/fast_train_icon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Code generated by data_to_py.py.
version = '0.1'

_data =\
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00'\
b'\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\xff\xff'\
b'\xff\xff\xff\x00\x00\x00\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00'\
b'\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00'\
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff'\
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00'\
b'\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'\
b'\xff\xff\xff\xff\xff\xff\x00\x00'

_mvdata = memoryview(_data)

def data():
return _mvdata

def width():
return 15

def height():
return 8
54 changes: 54 additions & 0 deletions src/glyphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (c) 2024 Tom Ward
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""Icons class that wraps up a font_to_python module."""

import framebuf
import uctypes

import assets


class Glyph:
"""Helper class that wraps data_to_py module.
Provides helper functions for rendering glyphs into a given display."""

def __init__(self, glyph):
self._glyph = glyph
self._framebuf = framebuf.FrameBuffer(
uctypes.bytearray_at(
uctypes.addressof(glyph.data()), len(glyph.data())
),
glyph.width(),
glyph.height(),
framebuf.GS8,
)

def render_glyph(
self, framebuffer: framebuf.FrameBuffer, x: int, y: int
) -> None:
"""Renders glyph into the provided display at position [x, y]."""
framebuffer.blit(self._framebuf, x, y, -1)

def max_bounds(self) -> tuple[int, int]:
"""Returns the max bounds for Glyph."""
return self._glyph.width(), self._glyph.height()


FAST_TRAIN_ICON = Glyph(assets.fast_train_icon)
3 changes: 2 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
import config as config_module
import display
import fonts
import glyphs
import logging
from setup import server
import time_range
import trains
import utils
import widgets
Expand Down Expand Up @@ -132,6 +132,7 @@ def _render_thread(
fonts.DEFAULT_FONT,
# Don't render seconds on e-paper displays.
render_seconds=(config.display.type != 'epd29b'),
fast_train_icon=glyphs.FAST_TRAIN_ICON,
)
non_active = widgets.MessageWidget(
screen, _DISPLAY_NOT_ACTIVE, fonts.DEFAULT_FONT
Expand Down
13 changes: 12 additions & 1 deletion src/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import display
import fonts
import glyphs
import trains


Expand Down Expand Up @@ -163,19 +164,24 @@ def __init__(
font: fonts.Font,
width: int,
status_font: fonts.Font | None = None,
fast_train_icon: glyphs.Glyph | None = None,
):
super().__init__(screen)
self._font = font
self._width = width
self._status_font = status_font if status_font else font
self._fast_train_icon = fast_train_icon
self._max_clock_width = self._font.calculate_bounds('00:00')[0]

self._last_departure = None

def bounds(self) -> tuple[int, int]:
return self._width, max(
max_height = max(
self._font.max_bounds()[1], self._status_font.max_bounds()[1]
)
if self._fast_train_icon:
max_height = max(max_height, self._fast_train_icon.max_bounds()[1])
return self._width, max_height

def render(
self, departure: trains.Departure | None, x: int, y: int, w: int, h: int
Expand All @@ -193,6 +199,9 @@ def render(
self._font.render_text(departure_time, self._screen, x, y)

x += self._max_clock_width + 4
if departure.fast_train and self._fast_train_icon:
self._fast_train_icon.render_glyph(self._screen, x, y)
x += self._fast_train_icon.max_bounds()[0] + 2
self._font.render_text(departure.destination, self._screen, x, y)

if departure.cancelled:
Expand Down Expand Up @@ -220,6 +229,7 @@ def __init__(
tall_font: fonts.Font,
default_font: fonts.Font,
render_seconds: bool = True,
fast_train_icon: glyphs.Glyph | None = None,
):
super().__init__(screen)
self._departure_updater = departure_updater
Expand All @@ -244,6 +254,7 @@ def __init__(
bold_font if i == 0 else default_font,
screen.width,
default_font,
fast_train_icon,
)
)

Expand Down

0 comments on commit 09bf090

Please sign in to comment.