|
5 | 5 | `adafruit_color_terminal` |
6 | 6 | ================================================================================ |
7 | 7 |
|
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. |
9 | 10 |
|
10 | 11 |
|
11 | 12 | * Author(s): Tim Cocks |
|
28 | 29 | __version__ = "0.0.0+auto.0" |
29 | 30 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Color_Terminal.git" |
30 | 31 |
|
| 32 | +from audiocore import WaveFile |
31 | 33 | from displayio import Palette, TileGrid |
32 | 34 | from terminalio import Terminal |
33 | 35 | from tilepalettemapper import TilePaletteMapper |
@@ -63,9 +65,20 @@ class ColorTerminal: |
63 | 65 | :param height: The height of the terminal in characters. |
64 | 66 | :param custom_palette: A custom palette of colors to use instead of the default ones. |
65 | 67 | 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 |
66 | 71 | """ |
67 | 72 |
|
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 | + ): |
69 | 82 | if custom_palette is None: |
70 | 83 | self.terminal_palette = Palette(9) |
71 | 84 | self.terminal_palette[0] = 0x000000 |
@@ -99,6 +112,11 @@ def __init__(self, font, width, height, custom_palette=None): |
99 | 112 |
|
100 | 113 | self.cur_color_mapping = [0, 1] |
101 | 114 |
|
| 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 | + |
102 | 120 | @staticmethod |
103 | 121 | def parse_ansi_colors(text): |
104 | 122 | """ |
@@ -195,6 +213,13 @@ def write(self, s): |
195 | 213 |
|
196 | 214 | if not color_map: |
197 | 215 | 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) |
198 | 223 | return |
199 | 224 |
|
200 | 225 | idx = 0 |
@@ -222,6 +247,12 @@ def write(self, s): |
222 | 247 |
|
223 | 248 | self.apply_color(cur_slice) |
224 | 249 | 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) |
225 | 256 |
|
226 | 257 | # index after last can be in the color map if color code is last thing in string |
227 | 258 | if idx in color_map: |
|
0 commit comments