Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions apps/btreceiver/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Bluetooth Receiver playing: a large cover on the left; beside it the phone's name, the title, artist, album, track number and genre, a small spectrum, a progress line with the codec between the times, and thin round transport buttons with a volume slider](docs/images/playing.png)
![Bluetooth Receiver playing: a large cover on the left; beside it the phone's name, the title, artist, album, track number and genre, a small spectrum, a progress line with the codec between the times, and thin round transport buttons beside shuffle, lit, and repeat over a volume slider](docs/images/playing.png)

# Bluetooth Receiver

Expand All @@ -21,6 +21,12 @@ needs audiodsp's `audiometer`; without it the app plays with no spectrum.
Made for an 800 x 480 screen. The cover and its buffers take about 350 KB,
so it wants a board with PSRAM.

![Bluetooth Receiver on an ESP32-S31 board with a 4.3-inch screen: the track beside its cover (blurred here), the phone's battery at the top right, and shuffle and repeat both lit](docs/images/board.png)

Here it runs on an ESP32-S31 board with a 4.3-inch screen. Shuffle and
repeat were turned on from the phone, and the buttons on the board follow
them.

## Use it

![The pairing screen: the Bluetooth mark in thin rings, the board's name as the title, three numbered steps and the PIN for older devices](docs/images/pairing.png)
Expand All @@ -30,6 +36,12 @@ Bluetooth list. Once paired, the phone is called back each time the app
opens. The buttons drive the phone's player: tap the middle one to play or
pause, the outer ones to skip, and hold an outer one to rewind or fast
forward. The slider is the phone's own volume, so either one moves both.
Above it, shuffle and repeat show the modes of the phone's player: tap
shuffle to turn it on or off, and repeat to go from off to all tracks to one
track, marked with a small 1. Each lights in the screen's colour while it's
on, and follows the phone when you change it there. A phone that doesn't
report them shows neither.

The phone's battery shows at the top right whenever the phone reports it,
amber when it runs low, and a weak signal is shown beside it.

Expand All @@ -43,9 +55,9 @@ button starts the phone's player. The battery here shows it charging.
![Connected and idle: an empty frame with a thin music note, the phone's name as the title and a hint to play something](docs/images/idle.png)

Paused, the spectrum rests as a dashed line and the middle button turns
back to play.
back to play. Repeat here is on for one track.

![Paused: the track's details beside its cover, a dashed line where the spectrum was and a play button in the middle](docs/images/paused.png)
![Paused: the track's details beside its cover, a dashed line where the spectrum was, a play button in the middle and repeat lit with a small 1](docs/images/paused.png)

A track with no cover gets the note in its place, and lines the phone
doesn't send, here the album and track number, are left out rather than
Expand All @@ -54,9 +66,10 @@ shown empty. This phone reports no battery, so none shows.
![A podcast episode with no cover: the thin note fills the frame, with the title on two lines and the show's name beneath](docs/images/nocover.png)

A long title wraps to a second line and pushes the details down, and a low
battery turns amber.
battery turns amber. This phone doesn't report shuffle or repeat, so the
volume sits beside the play button.

![A long title on two lines, with the battery icon in amber at the top right](docs/images/long.png)
![A long title on two lines, with the battery icon in amber at the top right and no shuffle or repeat](docs/images/long.png)

## On a computer

Expand Down
32 changes: 30 additions & 2 deletions apps/btreceiver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
says about the track (title, artist, album, track number, genre, length and
where it's up to) and about the link (the sender's name, the codec and the
rate). The buttons drive the sender's player (hold a skip button to fast
forward or rewind), and the volume slider is the one on the phone. A spectrum of what plays runs under the title.
forward or rewind), and the volume slider is the one on the phone. Shuffle
and repeat show the player's modes and change them, when the sender reports
them. A spectrum of what plays runs under the title.

It needs a firmware with ``btclassic``. The audio goes out through the
board's ``audio_out`` on the audio pump, so a redraw never holds it up. On a
Expand Down Expand Up @@ -36,6 +38,9 @@
# What Android sends for a field it doesn't know, the title included when
# nothing is playing.
UNSAID = "Not Provided"
# A tap moves repeat on the way a phone's player does, and turns shuffle on
# or off. "group" (a sender's own mode) goes back to off.
NEXT_REPEAT = {"off": "all", "all": "single"}


def _said(text):
Expand Down Expand Up @@ -113,7 +118,21 @@ def toggle():
def volume(v):
sink.volume = v

on = {"play": toggle, "next": sink.next, "previous": sink.previous, "seek": sink.seek, "volume": volume}
def setting(name, value):
# The sender answers with its new state, which the button then shows.
try:
sink.setting(name, value)
except (OSError, ValueError) as e:
print("btreceiver: %s:" % name, e)

def repeat():
setting("repeat", NEXT_REPEAT.get(getattr(sink, "repeat", None), "off"))

def shuffle():
setting("shuffle", "all" if getattr(sink, "shuffle", None) == "off" else "off")

on = {"play": toggle, "next": sink.next, "previous": sink.previous, "seek": sink.seek, "volume": volume,
"repeat": repeat, "shuffle": shuffle}
scr = Screen(scope.root, scope.size, on)
st = state = {"sink": sink, "out": None, "rate": 0, "levels": None, "screen": scr, "t0": ticks_ms()}
jpeg = bytearray(COVER_JPEG)
Expand Down Expand Up @@ -193,6 +212,10 @@ def show_link():
link["sample_rate"] = 0
scr.show_source(sink.peer_name if playing else "", link)

def show_modes():
# A firmware whose btclassic can't read them back has neither.
scr.show_modes(getattr(sink, "repeat", None), getattr(sink, "shuffle", None))

def show_all():
if not sink.connected:
scr.show_pairing(name, pin)
Expand All @@ -207,6 +230,8 @@ def show_all():
scr.show_idle(sink.peer_name)
show_link()

settings_bit = getattr(bits, "SETTINGS", 0)

def follow(t):
changed = sink.changes()
if changed:
Expand All @@ -219,6 +244,8 @@ def follow(t):
show_cover()
if changed & bits.VOLUME:
scr.show_volume(sink.volume)
if changed & (bits.CONNECTION | settings_bit):
show_modes()
if sink.connected and _said(sink.title):
scr.show_position(sink.position, sink.duration)

Expand All @@ -241,6 +268,7 @@ def close():
scope.on_close(close)
show_all()
show_cover()
show_modes()
scr.show_volume(sink.volume)
route()
scope.every(UI_MS, follow)
Expand Down
13 changes: 12 additions & 1 deletion apps/btreceiver/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
`DemoSink` answers like ``btclassic.AudioSink``: the same properties, the
same ``changes()`` bits and the same transport. It pairs a few seconds after
the app opens, plays a short list of made-up tracks, moves their position
along and follows play, pause, next, previous and volume. Its covers are
along and follows play, pause, next, previous, volume, repeat and shuffle. Its covers are
drawn by svgio and made into JPEGs by jpegio, so the app decodes them the
way it decodes a phone's.

Expand All @@ -28,6 +28,7 @@
POSITION = 64
PEER = 128
COVER = 256
SETTINGS = 1 << 26

PAIR_MS = 4000 # how long the demo waits to be paired, from the app opening
PLAY_MS = 2500 # and then before it plays
Expand Down Expand Up @@ -134,6 +135,9 @@ def __init__(self, state=None):
self._rate = 1 # how fast the position moves: 8 or -8 while seeking
# The phone's battery, as AVRCP reports it; the states show each one.
self.battery = {"idle": "external", "paused": "full", "long": "warning", "nocover": None}.get(self.state, "normal")
# The player's modes; the long title's phone doesn't report them.
self.repeat, self.shuffle = {"playing": ("off", "all"), "paused": ("single", "off"),
"long": (None, None)}.get(self.state, ("off", "off"))
self._clear_track()
st = self.state
if st in ("idle", "playing", "paused", "nocover", "long"):
Expand Down Expand Up @@ -241,6 +245,13 @@ def seek(self, direction):
def refresh(self):
self._bits |= POSITION

def setting(self, name, value):
if name not in ("equalizer", "repeat", "shuffle", "scan"):
raise ValueError("setting is equalizer, repeat, shuffle or scan")
if self.connected and name in ("repeat", "shuffle") and getattr(self, name) is not None:
setattr(self, name, value)
self._bits |= SETTINGS

def disconnect(self):
self.connected = False
self._clear_track()
Expand Down
Binary file added apps/btreceiver/docs/images/board.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified apps/btreceiver/docs/images/idle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified apps/btreceiver/docs/images/long.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified apps/btreceiver/docs/images/nocover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified apps/btreceiver/docs/images/paused.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified apps/btreceiver/docs/images/playing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 69 additions & 2 deletions apps/btreceiver/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def _font(*sizes):
F_BIG = _font(24, 20, 16)
F_BODY = _font(16, 14)
F_SMALL = _font(14, 12)
F_TINY = _font(12, 14)


def clock(ms):
Expand Down Expand Up @@ -144,14 +145,19 @@ def _show(obj, on):
PROGRESS = (416, 292, 360)
TRANSPORT = (416, 326, 56, 72, 14) # x, y, small ring, play ring, gap
VOLUME = (650, 362, 126) # x, middle y, width
# Shuffle and repeat sit over the volume, which moves down to make room; the
# two rows stay centred on the play button.
MODES = (650, 340, 384) # x, the buttons' middle y, and the volume's under them
MODE_ICON = 24
MODE_HIT = 40 # each button's square, a finger's size around the icon
PAIR_RING = 300


class Screen:
"""Everything on the receiver's screen, in *root* (*size* pixels).

*on* maps ``"play"``, ``"next"``, ``"previous"`` and ``"volume"`` to what
the buttons and the slider do.
*on* maps ``"play"``, ``"next"``, ``"previous"``, ``"seek"``, ``"repeat"``,
``"shuffle"`` and ``"volume"`` to what the buttons and the slider do.
"""

def __init__(self, root, size, on):
Expand All @@ -174,6 +180,7 @@ def __init__(self, root, size, on):
self._build_progress()
self._build_transport()
self._build_volume()
self._build_modes()
x, y, w, h = SPECTRUM
self.bars = SpectrumBars(root, x, y, w, h, BLACK, bands=BANDS, gap=w // BANDS * 3 // 5, color=TEAL)
self.set_accent(TEAL)
Expand Down Expand Up @@ -340,6 +347,33 @@ def _build_volume(self):
s.set_style_outline_width(0, lv.PART.MAIN | lv.STATE.FOCUSED)
s.add_event_cb(self._vol_event, lv.EVENT.ALL, None)

def _build_modes(self):
# Repeat and shuffle, lit in the accent while on; "single" marks the
# repeat with a small 1, as iconkit has no repeat-one icon. Shuffle
# lines up with the volume's mark, repeat with the slider's end.
x, y, _ = MODES
margin = (MODE_HIT - MODE_ICON) // 2
self._modes = (None, None)
self._modes_on = False
self.shuffle, self.shuffle_icon = self._mode_button(x - margin, y, "shuffle")
end = VOLUME[0] + VOLUME[2] - 8
self.repeat, self.repeat_icon = self._mode_button(end - MODE_ICON - margin, y, "repeat")
one = self.repeat_one = _label(self.repeat, F_TINY, TEXT)
one.set_text("1")
one.align_to(self.repeat_icon, lv.ALIGN.TOP_RIGHT, 6, -8)
_show(one, False)

def _mode_button(self, x, y, name):
"""A button for mode *name* with iconkit's icon of that name: ``(button, icon)``."""
b = _box(self.root, x, y - MODE_HIT // 2, MODE_HIT, MODE_HIT)
b.add_flag(lv.obj.FLAG.CLICKABLE)
b.add_event_cb(lambda e: self.on[name](), lv.EVENT.CLICKED, None)
img = lvicons.image(b, name, MODE_ICON, lv.color_hex(MUTED))
img.center()
img.remove_flag(lv.obj.FLAG.CLICKABLE)
_show(b, False)
return b, img

def _vol_event(self, e):
code = e.get_code()
if code == lv.EVENT.PRESSED:
Expand All @@ -366,6 +400,7 @@ def set_accent(self, rgb):
for ring in self.step_rings:
ring.set_style_border_color(c, 0)
glyphs.tint(self.pair_mark, c)
self._paint_modes()

def show_pairing(self, name, pin=None):
"""Nothing connected: the name to pick on the phone, and the steps."""
Expand Down Expand Up @@ -454,6 +489,18 @@ def show_battery(self, state):
self.battery.set_style_image_recolor(lv.color_hex(kind[1]), 0)
_show(self.battery, True)

def show_modes(self, repeat, shuffle):
"""The sender's *repeat* ("off", "single", "all" or "group") and
*shuffle* ("off", "all" or "group"). A button for a mode the sender
doesn't report (None) is hidden; with neither, the volume moves back
beside the play button."""
modes = (repeat, shuffle)
if modes == self._modes:
return
self._modes = modes
self._place_modes()
self._paint_modes()

def show_position(self, pos, length):
if pos is None or not length:
_show(self.head, False)
Expand Down Expand Up @@ -503,6 +550,24 @@ def show_levels(self, levels):

# -- inside ---------------------------------------------------------

def _place_modes(self):
repeat, shuffle = self._modes
on = self._modes_on
_show(self.repeat, on and repeat is not None)
_show(self.shuffle, on and shuffle is not None)
x, y, w = VOLUME
y = MODES[2] if repeat is not None or shuffle is not None else y
self.vol_mark.set_y(y - 9)
self.vol.set_y(y)

def _paint_modes(self):
repeat, shuffle = self._modes
for icon, mode in ((self.repeat_icon, repeat), (self.shuffle_icon, shuffle)):
lit = mode not in (None, "off")
icon.set_style_image_recolor(lv.color_hex(self.accent if lit else MUTED), 0)
_show(self.repeat_one, repeat == "single")
self.repeat_one.set_style_text_color(lv.color_hex(self.accent), 0)

def _pairing(self, on):
_show(self.pair, on)
_show(self.steps, on)
Expand Down Expand Up @@ -547,3 +612,5 @@ def _playback(self, on, transport=None):
_show(b.obj, t)
for o in (self.vol, self.vol_mark):
_show(o, t)
self._modes_on = t
self._place_modes()
39 changes: 39 additions & 0 deletions tests/btreceiver_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def tap(obj):
check("pairing hides the transport and the progress",
not shown(scr.play.obj) and not shown(scr.vol) and not shown(scr.track) and not shown(scr.bars.obj))
check("pairing shows the rings, not the cover", shown(scr.pair) and not shown(scr.cover_box))
check("pairing hides shuffle and repeat", not shown(scr.shuffle) and not shown(scr.repeat))
scope.close()

# ---- Connected, nothing playing -----------------------------------------------
Expand Down Expand Up @@ -165,6 +166,36 @@ def tap(obj):
"%s %s" % (text(scr.t_pos), text(scr.t_len)))
check("the cover shows, and gives the screen its colour", shown(scr.art) and scr.accent != screenmod.TEAL,
"%06x" % scr.accent)


def lit(icon):
return icon.get_style_image_recolor(0).to_int() & 0xFFFFFF == scr.accent


def volume_y():
return scr.vol.get_y()


check("shuffle and repeat show when the phone reports them", shown(scr.shuffle) and shown(scr.repeat))
check("shuffle on is lit in the accent, repeat off is not", lit(scr.shuffle_icon) and not lit(scr.repeat_icon))
check("the volume moves under them", volume_y() == screenmod.MODES[2], str(volume_y()))
tap(scr.repeat)
scope.run()
check("repeat goes off to all on the phone, and lights", sink.repeat == "all" and lit(scr.repeat_icon), str(sink.repeat))
tap(scr.repeat)
scope.run()
check("then all to single, marked with a 1", sink.repeat == "single" and shown(scr.repeat_one), str(sink.repeat))
tap(scr.repeat)
scope.run()
check("then single back to off", sink.repeat == "off" and not lit(scr.repeat_icon) and not shown(scr.repeat_one),
str(sink.repeat))
tap(scr.shuffle)
scope.run()
check("shuffle turns off on the phone, and dims", sink.shuffle == "off" and not lit(scr.shuffle_icon), str(sink.shuffle))
sink.shuffle = "all"
sink._bits |= demo.SETTINGS
scope.run()
check("shuffle turned on at the phone lights here", lit(scr.shuffle_icon))
tap(scr.play.obj)
scope.run()
check("pause pauses the phone and says so", not sink.playing and text(scr.eyebrow) == "PAUSED", text(scr.eyebrow))
Expand All @@ -191,6 +222,14 @@ def tap(obj):
scope.close()
check("closing the app lets the phone go", not sink.connected)

# ---- A phone that doesn't report its modes -----------------------------------

scope, sink, scr = opened("long")
check("a phone that reports no modes shows no shuffle or repeat", not shown(scr.shuffle) and not shown(scr.repeat))
check("and the volume stays beside the play button",
scr.vol.get_y() == screenmod.VOLUME[1], str(scr.vol.get_y()))
scope.close()

# ---- A track with no cover ----------------------------------------------------

scope, sink, scr = opened("nocover")
Expand Down