Skip to content

Commit d898957

Browse files
Bandwatch 0.1 (#976)
* Bandwatch App v0.1 * BandWatch v0.1 - Fixed webp preview compression. * chore: auto-update manifest metadata --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent f243f06 commit d898957

16 files changed

Lines changed: 1216 additions & 0 deletions

‎apps/bandwatch/bands_page.star‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
Band conditions page for BandWatch.
3+
4+
A sun and moon on the top line head the day and night columns, followed by one
5+
line per band group with its day and night indicator lights. The page reads
6+
only the band conditions interface described in `n0nbh.star`, so it works with
7+
any library that provides it.
8+
9+
API:
10+
render_bands(conditions): the page for a band conditions dict, or None
11+
"""
12+
13+
load("bitmaps.star", "bitmap")
14+
load("display.star", "COLOR_FAIR", "COLOR_GOOD", "COLOR_MOON", "COLOR_POOR", "COLOR_SUN", "COLOR_UNKNOWN", "place", "text")
15+
load("render.star", "render")
16+
17+
#-------------------------------------------------------------------------------
18+
# Constants
19+
#-------------------------------------------------------------------------------
20+
21+
# Band page layout. Pixels right on the edge of the display can be hard to
22+
# see on some hardware, so the page is centered with a margin on each side.
23+
# Each band row is 7 pixels tall because tb-8 digits are 6 pixels high.
24+
BAND_LABEL_X = 6
25+
BAND_DAY_X = 36
26+
BAND_NIGHT_X = 52
27+
BAND_FIRST_ROW_Y = 5
28+
BAND_ROW_HEIGHT = 7
29+
FONT_DIGIT_TOP = 1 # blank rows above a digit in tb-8
30+
31+
# Band groups from the band conditions interface, top to bottom, with the
32+
# label shown for each.
33+
BAND_ROWS = [
34+
("80m-40m", "80-40"),
35+
("30m-20m", "30-20"),
36+
("17m-15m", "17-15"),
37+
("12m-10m", "12-10"),
38+
]
39+
40+
# Indicator lights for each rating in the band conditions interface: solid
41+
# discs colored by rating, and a grey outline for an unknown rating.
42+
LIGHTS = {
43+
"good": ("light_rated", COLOR_GOOD),
44+
"fair": ("light_rated", COLOR_FAIR),
45+
"poor": ("light_rated", COLOR_POOR),
46+
"unknown": ("light_outline", COLOR_UNKNOWN),
47+
}
48+
49+
#-------------------------------------------------------------------------------
50+
# Page
51+
#-------------------------------------------------------------------------------
52+
53+
def light(conditions, period, group):
54+
"""The indicator light for one band group.
55+
56+
When the conditions are out of date every light becomes an outline in its
57+
rating color.
58+
59+
Args:
60+
conditions: Band conditions dict.
61+
period: "day" or "night".
62+
group: Band group key.
63+
64+
Returns:
65+
A bitmap widget.
66+
"""
67+
rating = conditions.get(period, {}).get(group, "unknown")
68+
icon, color = LIGHTS.get(rating, LIGHTS["unknown"])
69+
if conditions["stale"]:
70+
icon = "light_outline"
71+
return bitmap(icon, color)
72+
73+
def render_bands(conditions):
74+
"""Render the band conditions page.
75+
76+
Args:
77+
conditions: Band conditions dict, or None if there are none.
78+
79+
Returns:
80+
A `render.Root` object.
81+
"""
82+
if conditions == None:
83+
conditions = {"stale": True, "day": {}, "night": {}}
84+
85+
children = [
86+
place(BAND_DAY_X + 1, 0, bitmap("sun", COLOR_SUN)),
87+
place(BAND_NIGHT_X + 1, 0, bitmap("moon", COLOR_MOON)),
88+
]
89+
for row, (group, label) in enumerate(BAND_ROWS):
90+
y = BAND_FIRST_ROW_Y + row * BAND_ROW_HEIGHT
91+
children.append(place(BAND_LABEL_X, y - FONT_DIGIT_TOP, text(label)))
92+
children.append(place(BAND_DAY_X, y, light(conditions, "day", group)))
93+
children.append(place(BAND_NIGHT_X, y, light(conditions, "night", group)))
94+
95+
return render.Root(child = render.Stack(children = children))

‎apps/bandwatch/bandwatch.star‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Applet: BandWatch
3+
Summary: HF bands and space weather
4+
Description: Display HF band conditions from N0NBH or current space weather from NOAA SWPC.
5+
Author: Brian McLaughlin (SpinStabilized)
6+
"""
7+
8+
# The app is split into libraries:
9+
# n0nbh.star band conditions from N0NBH (the band conditions interface)
10+
# swpc.star space weather from NOAA SWPC
11+
# bands_page.star the band conditions page
12+
# space_page.star the space weather page
13+
# display.star font, colors, and layout helpers shared by the pages
14+
# bitmaps.star icons, read from the CSV files in `icons`
15+
#
16+
# To use a different band conditions library, change the n0nbh.star load
17+
# below to the new library. It must provide `get_band_conditions(config)` as
18+
# described in n0nbh.star.
19+
20+
load("bands_page.star", "render_bands")
21+
load("n0nbh.star", "get_band_conditions")
22+
load("schema.star", "schema")
23+
load("space_page.star", "render_space_weather")
24+
load("swpc.star", "get_space_weather")
25+
26+
#-------------------------------------------------------------------------------
27+
# Constants
28+
#-------------------------------------------------------------------------------
29+
30+
# The following DEFAULT_ configurations are strings for compatibility with the
31+
# `config` and `schema` libraries.
32+
DEFAULT_PAGE = "bands"
33+
34+
#-------------------------------------------------------------------------------
35+
# Main
36+
#-------------------------------------------------------------------------------
37+
38+
def main(config):
39+
"""Main function body.
40+
41+
Args:
42+
config: A Tronbyt configuration object
43+
44+
Returns:
45+
A `render.Root` object.
46+
"""
47+
page = config.str("page", DEFAULT_PAGE)
48+
49+
if page == "space":
50+
return render_space_weather(get_space_weather())
51+
return render_bands(get_band_conditions(config))
52+
53+
def get_schema():
54+
"""Provide the schema for the Tronbyt app configuration.
55+
56+
Returns:
57+
A `schema.Schema` object.
58+
"""
59+
return schema.Schema(
60+
version = "1",
61+
fields = [
62+
schema.Dropdown(
63+
id = "page",
64+
name = "Page",
65+
desc = "Which page to show. Install the app twice to see both. " +
66+
"Out-of-date values show as grey numbers or empty circles.",
67+
icon = "towerBroadcast",
68+
options = [
69+
schema.Option(display = "Band Conditions", value = "bands"),
70+
schema.Option(display = "Space Weather", value = "space"),
71+
],
72+
default = DEFAULT_PAGE,
73+
),
74+
],
75+
)

‎apps/bandwatch/bandwatch.webp‎

242 Bytes
Loading

‎apps/bandwatch/bitmaps.star‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
"""
2+
Bitmap icons for BandWatch.
3+
4+
Each icon is a CSV file in the `icons` folder, one row of the file per row of
5+
pixels, with 1 for a lit pixel and 0 for an unlit pixel. Every row of an icon
6+
must have the same number of columns. The icons carry no color, so the same
7+
shape can be drawn in any color, for example one indicator light for good,
8+
fair, and poor ratings.
9+
10+
To add an icon, put its CSV file in `icons`, add a `load()` line for it below,
11+
and add it to ICONS under the name the render code will use.
12+
13+
API:
14+
bitmap(name, color): widget for an icon drawn in a color
15+
bitmap_width(name): width of an icon in pixels
16+
bitmap_height(name): height of an icon in pixels
17+
"""
18+
19+
load("encoding/csv.star", "csv")
20+
load("icons/arrow_down.csv", ARROW_DOWN_CSV = "file")
21+
load("icons/arrow_flat.csv", ARROW_FLAT_CSV = "file")
22+
load("icons/arrow_up.csv", ARROW_UP_CSV = "file")
23+
load("icons/light_outline.csv", LIGHT_OUTLINE_CSV = "file")
24+
load("icons/light_rated.csv", LIGHT_RATED_CSV = "file")
25+
load("icons/moon.csv", MOON_CSV = "file")
26+
load("icons/sun.csv", SUN_CSV = "file")
27+
load("render.star", "render")
28+
29+
#-------------------------------------------------------------------------------
30+
# Loading
31+
#-------------------------------------------------------------------------------
32+
33+
def read_bitmap(csv_file):
34+
"""Read an icon from a CSV file.
35+
36+
Args:
37+
csv_file: A file loaded with `load("icons/NAME.csv", NAME_CSV = "file")`.
38+
39+
Returns:
40+
List of rows, each a list of "0" and "1" strings.
41+
"""
42+
rows = csv.read_all(csv_file.readall())
43+
if len(rows) == 0:
44+
fail("{} has no rows".format(csv_file.path))
45+
for row in rows:
46+
for cell in row:
47+
if cell not in ("0", "1"):
48+
fail("{} has a value other than 0 or 1: {}".format(csv_file.path, cell))
49+
return rows
50+
51+
# Icons are read once, when the library is loaded.
52+
ICONS = {
53+
"arrow_down": read_bitmap(ARROW_DOWN_CSV),
54+
"arrow_flat": read_bitmap(ARROW_FLAT_CSV),
55+
"arrow_up": read_bitmap(ARROW_UP_CSV),
56+
"light_outline": read_bitmap(LIGHT_OUTLINE_CSV),
57+
"light_rated": read_bitmap(LIGHT_RATED_CSV),
58+
"moon": read_bitmap(MOON_CSV),
59+
"sun": read_bitmap(SUN_CSV),
60+
}
61+
62+
#-------------------------------------------------------------------------------
63+
# Drawing
64+
#-------------------------------------------------------------------------------
65+
66+
def bitmap_width(name):
67+
"""Width of an icon.
68+
69+
Args:
70+
name: Icon name, a key of ICONS.
71+
72+
Returns:
73+
Width in pixels.
74+
"""
75+
return len(ICONS[name][0])
76+
77+
def bitmap_height(name):
78+
"""Height of an icon.
79+
80+
Args:
81+
name: Icon name, a key of ICONS.
82+
83+
Returns:
84+
Height in pixels.
85+
"""
86+
return len(ICONS[name])
87+
88+
def bitmap(name, color):
89+
"""Draw an icon in a color.
90+
91+
Each horizontal run of lit pixels becomes one `render.Box`, which keeps the
92+
number of widgets low compared with drawing pixel by pixel.
93+
94+
Args:
95+
name: Icon name, a key of ICONS.
96+
color: Hex color for the lit pixels.
97+
98+
Returns:
99+
A `render.Stack` exactly the size of the icon.
100+
"""
101+
rows = ICONS[name]
102+
children = [render.Box(width = bitmap_width(name), height = bitmap_height(name))]
103+
for y, row in enumerate(rows):
104+
start = None
105+
106+
# The extra "0" closes a run that reaches the right edge.
107+
for x, cell in enumerate(row + ["0"]):
108+
if cell == "1" and start == None:
109+
start = x
110+
elif cell != "1" and start != None:
111+
children.append(
112+
render.Padding(
113+
pad = (start, y, 0, 0),
114+
child = render.Box(width = x - start, height = 1, color = color),
115+
),
116+
)
117+
start = None
118+
return render.Stack(children = children)

‎apps/bandwatch/display.star‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Shared look for the BandWatch pages: font, colors, and small layout helpers.
3+
4+
API:
5+
FONT, LINE_HEIGHT, LABEL_GAP: text settings
6+
COLOR_*: the app's colors
7+
text(content, color): text in the app font
8+
gap(width): empty horizontal space
9+
place(x, y, child): a widget at an absolute screen position
10+
"""
11+
12+
load("render.star", "render")
13+
14+
#-------------------------------------------------------------------------------
15+
# Constants
16+
#-------------------------------------------------------------------------------
17+
18+
FONT = "tb-8"
19+
LINE_HEIGHT = 8
20+
LABEL_GAP = 3 # pixels between a grey label and its value
21+
22+
COLOR_TEXT = "#ffffff"
23+
COLOR_LABEL = "#969696"
24+
COLOR_GOOD = "#00d23c"
25+
COLOR_FAIR = "#ffb000"
26+
COLOR_POOR = "#e61e1e"
27+
COLOR_UNKNOWN = "#505050"
28+
COLOR_STALE = "#6e6e6e" # values that are out of date or unavailable
29+
COLOR_SUN = "#ffc800"
30+
COLOR_MOON = "#aaaaff"
31+
32+
#-------------------------------------------------------------------------------
33+
# Helpers
34+
#-------------------------------------------------------------------------------
35+
36+
def text(content, color = COLOR_TEXT):
37+
"""Text in the app font.
38+
39+
Args:
40+
content: The string to show.
41+
color: Hex color for the text.
42+
43+
Returns:
44+
A `render.Text` object.
45+
"""
46+
return render.Text(content = content, font = FONT, color = color)
47+
48+
def gap(width):
49+
"""Empty horizontal space.
50+
51+
Args:
52+
width: Width in pixels.
53+
54+
Returns:
55+
A transparent `render.Box`.
56+
"""
57+
return render.Box(width = width, height = 1)
58+
59+
def place(x, y, child):
60+
"""Position a widget at an absolute screen location.
61+
62+
Args:
63+
x: Tronbyt display x position [0...63]
64+
y: Tronbyt display y position [0...31]
65+
child: The widget to place.
66+
67+
Returns:
68+
A `render.Padding` object for the location.
69+
"""
70+
return render.Padding(pad = (x, y, 0, 0), child = child)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1,1,1,1,1
2+
0,1,1,1,0
3+
0,0,1,0,0
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0,0,0,0,0
2+
1,1,1,1,1
3+
0,0,0,0,0

‎apps/bandwatch/icons/arrow_up.csv‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0,0,1,0,0
2+
0,1,1,1,0
3+
1,1,1,1,1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
0,1,1,1,1,0
2+
1,0,0,0,0,1
3+
1,0,0,0,0,1
4+
1,0,0,0,0,1
5+
1,0,0,0,0,1
6+
0,1,1,1,1,0
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
0,1,1,1,1,0
2+
1,1,1,1,1,1
3+
1,1,1,1,1,1
4+
1,1,1,1,1,1
5+
1,1,1,1,1,1
6+
0,1,1,1,1,0

0 commit comments

Comments
 (0)