Skip to content

Commit f151346

Browse files
authored
Merge pull request #2 from FoamyGuy/bell_and_fruitjam_example
ASCII Bell support and Fruit Jam example
2 parents 1ea46e4 + ffc1a99 commit f151346

File tree

7 files changed

+169
-3
lines changed

7 files changed

+169
-3
lines changed

adafruit_color_terminal.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
`adafruit_color_terminal`
66
================================================================================
77
8-
Extension of supports ANSI color escapes for subsets of text
8+
Extension of supports ANSI color escapes for subsets of text and optionally the
9+
ASCII bell escape code.
910
1011
1112
* Author(s): Tim Cocks
@@ -28,6 +29,7 @@
2829
__version__ = "0.0.0+auto.0"
2930
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Color_Terminal.git"
3031

32+
from audiocore import WaveFile
3133
from displayio import Palette, TileGrid
3234
from terminalio import Terminal
3335
from tilepalettemapper import TilePaletteMapper
@@ -63,9 +65,20 @@ class ColorTerminal:
6365
:param height: The height of the terminal in characters.
6466
:param custom_palette: A custom palette of colors to use instead of the default ones.
6567
Must contain at least 9 colors.
68+
:param audio_interface: The audio interface to use for playing ASCII bell escape codes.
69+
:param bell_audio_file: The wave audio file to use for the ASCII bell escape codes.
70+
Defaults to beep.wav
6671
"""
6772

68-
def __init__(self, font, width, height, custom_palette=None):
73+
def __init__(
74+
self,
75+
font,
76+
width,
77+
height,
78+
custom_palette=None,
79+
audio_interface=None,
80+
bell_audio_file="/beep.wav",
81+
):
6982
if custom_palette is None:
7083
self.terminal_palette = Palette(9)
7184
self.terminal_palette[0] = 0x000000
@@ -99,6 +112,11 @@ def __init__(self, font, width, height, custom_palette=None):
99112

100113
self.cur_color_mapping = [0, 1]
101114

115+
self.audio_interface = audio_interface
116+
if audio_interface is not None:
117+
beep_wave_file = open(bell_audio_file, "rb")
118+
self.beep_wave = WaveFile(beep_wave_file)
119+
102120
@staticmethod
103121
def parse_ansi_colors(text):
104122
"""
@@ -195,6 +213,13 @@ def write(self, s):
195213

196214
if not color_map:
197215
self.terminal.write(s)
216+
if (
217+
"\x07" in s
218+
and self.audio_interface is not None
219+
and not self.audio_interface.playing
220+
):
221+
print("playing beep")
222+
self.audio_interface.play(self.beep_wave)
198223
return
199224

200225
idx = 0
@@ -222,6 +247,12 @@ def write(self, s):
222247

223248
self.apply_color(cur_slice)
224249
self.terminal.write(cur_slice)
250+
if (
251+
"\x07" in cur_slice
252+
and self.audio_interface is not None
253+
and not self.audio_interface.playing
254+
):
255+
self.audio_interface.play(self.beep_wave)
225256

226257
# index after last can be in the color map if color code is last thing in string
227258
if idx in color_map:

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# Uncomment the below if you use native CircuitPython modules such as
2626
# digitalio, micropython and busio. List the modules you use. Without it, the
2727
# autodoc module docs will fail to generate with a warning.
28-
autodoc_mock_imports = ["terminalio", "tilepalettemapper", "displayio"]
28+
autodoc_mock_imports = ["terminalio", "tilepalettemapper", "displayio", "audiocore"]
2929

3030
autodoc_preserve_defaults = True
3131

examples/beep.wav

9.42 KB
Binary file not shown.

examples/beep.wav.license

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
import time
5+
6+
import supervisor
7+
from adafruit_fruitjam import peripherals
8+
from displayio import Group
9+
from terminalio import FONT
10+
11+
from adafruit_color_terminal import ColorTerminal
12+
13+
main_group = Group()
14+
15+
display = supervisor.runtime.display
16+
if display is None or peripherals.get_display_config()[2] < 4:
17+
print(
18+
"To use ColorTerminal there must be an initialized display with a color depth at least 4."
19+
)
20+
print(
21+
"Initializing display to default size 640x480 with color depth 8, ",
22+
"if you do not want this configuration then initialize the display ",
23+
"before running this example.",
24+
)
25+
peripherals.request_display_config(640, 480, 8)
26+
27+
fruitjam_peripherals = peripherals.Peripherals()
28+
29+
fruitjam_peripherals.dac.headphone_output = True
30+
fruitjam_peripherals.dac.configure_clocks(sample_rate=16000, bit_depth=16)
31+
32+
font_bb = FONT.get_bounding_box()
33+
screen_size = (display.width // font_bb[0], display.height // font_bb[1])
34+
35+
terminal = ColorTerminal(
36+
FONT, screen_size[0], screen_size[1], audio_interface=fruitjam_peripherals.audio
37+
)
38+
main_group.append(terminal.tilegrid)
39+
40+
black = chr(27) + "[30m"
41+
red = chr(27) + "[31m"
42+
green = chr(27) + "[32m"
43+
yellow = chr(27) + "[33m"
44+
blue = chr(27) + "[34m"
45+
magenta = chr(27) + "[35m"
46+
cyan = chr(27) + "[36m"
47+
white = chr(27) + "[37m"
48+
reset = chr(27) + "[0m"
49+
50+
51+
message = f"Hello {green}World{reset} {yellow}ANSI\n"
52+
terminal.write(message)
53+
print(message, end="")
54+
55+
message = f"{magenta}Terminal {red}Colors{reset}"
56+
terminal.write(message)
57+
print(message)
58+
59+
display.root_group = main_group
60+
61+
print(terminal.cursor_x, terminal.cursor_y)
62+
63+
move_cursor = chr(27) + "[10;10H"
64+
terminal.write(f" Something {move_cursor}{cyan} Else{reset}")
65+
66+
time.sleep(2)
67+
68+
terminal.write(" beep\x07")
69+
70+
while True:
71+
pass
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import supervisor
6+
from adafruit_fruitjam import peripherals
7+
from displayio import Group
8+
from terminalio import FONT
9+
10+
from adafruit_color_terminal import ColorTerminal
11+
12+
main_group = Group()
13+
14+
display = supervisor.runtime.display
15+
if display is None or peripherals.get_display_config()[2] < 4:
16+
print(
17+
"To use ColorTerminal there must be an initialized display with a color depth at least 4."
18+
)
19+
print(
20+
"Initializing display to default size 640x480 with color depth 8, ",
21+
"if you do not want this configuration then initialize the display ",
22+
"before running this example.",
23+
)
24+
peripherals.request_display_config(640, 480, 8)
25+
26+
font_bb = FONT.get_bounding_box()
27+
screen_size = (display.width // font_bb[0], display.height // font_bb[1])
28+
29+
terminal = ColorTerminal(FONT, screen_size[0], screen_size[1])
30+
main_group.append(terminal.tilegrid)
31+
32+
black = chr(27) + "[30m"
33+
red = chr(27) + "[31m"
34+
green = chr(27) + "[32m"
35+
yellow = chr(27) + "[33m"
36+
blue = chr(27) + "[34m"
37+
magenta = chr(27) + "[35m"
38+
cyan = chr(27) + "[36m"
39+
white = chr(27) + "[37m"
40+
reset = chr(27) + "[0m"
41+
42+
43+
message = f"Hello {green}World{reset} {yellow}ANSI\n"
44+
terminal.write(message)
45+
print(message, end="")
46+
47+
message = f"{magenta}Terminal {red}Colors{reset}"
48+
terminal.write(message)
49+
print(message)
50+
51+
display.root_group = main_group
52+
53+
print(terminal.cursor_x, terminal.cursor_y)
54+
55+
move_cursor = chr(27) + "[10;10H"
56+
terminal.write(f" Something {move_cursor}{cyan} Else{reset}")
57+
58+
while True:
59+
pass

ruff.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ ignore = [
9292
"PLR2004", # magic-value-comparison
9393
"UP030", # format literals
9494
"PLW1514", # unspecified-encoding
95+
"PLR0913", # too many args
96+
"PLR0917", # too many positional args
9597

9698
]
9799

0 commit comments

Comments
 (0)