SatCat5
codec_rtttl.h
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Ring Tone Text Transfer Language (RTTTL) interpreter
6 
7 #pragma once
8 
9 #include <satcat5/log.h>
10 #include <satcat5/pkt_buffer.h>
11 #include <satcat5/polling.h>
12 
13 // Default buffer size for a queued song.
14 #ifndef SATCAT5_RTTTL_BUFFER
15 #define SATCAT5_RTTTL_BUFFER 256
16 #endif
17 
18 namespace satcat5 {
19  namespace io {
34  public:
38  constexpr RtttlDecoder(satcat5::io::Writeable* spkr, u64 refclk_hz)
39  : m_spkr(spkr)
40  , m_scale((1ull << 48) / refclk_hz)
41  , m_duration(0)
42  , m_octave(0)
43  , m_whole_note(0)
44  , m_queue(m_raw, sizeof(m_raw), 0)
45  , m_raw{}
46  {} // Nothing else to initialize
47 
49  inline void flush()
50  { m_queue.clear(); }
51 
54  bool play(const char* src);
55 
58  bool play(satcat5::io::Readable* src);
59 
60  protected:
61  void data_rcvd(satcat5::io::Readable* src) override;
62  bool read_note(satcat5::io::Readable* src);
63 
65  const u64 m_scale;
66  u32 m_duration;
67  u32 m_octave;
70  u8 m_raw[SATCAT5_RTTTL_BUFFER];
71  };
72 
74  constexpr char RTTTL_BEETHOVEN[] =
75  "5thSymph:d=16,o=5,b=100:"\
76  "g,g,g,4d#,4p,f,f,f,4d,4p,g,g,g,d#,g#,g#,g#,g,d#6,d#6,d#6,4c6,8p";
77 
79  constexpr char RTTTL_HAUNTED[] =
80  "HauntHouse: d=4,o=5,b=108: 2a4, 2e, 2d#, 2b4, 2a4, 2c, 2d, 2a#4, 2e.";
81 
83  constexpr char RTTTL_NOKIA[] =
84  "Nokia:d=4,o=5,b=225:8e6,8d6,f#,g#,8c#6,8b,d,e,8b,8a,c#,e,2a";
85 
87  constexpr char RTTTL_RICK[] =
88  "Rick:d=8,o=4,b=225:g,a,c5,a,4e5,p,4e5,p,4.d5,4.p,g,a,c5,a,"\
89  "4d5,p,4d5,p,4c5,b,4.a,g,a,c5,a,2c5,4d5,4b,4a,4.g,2d5,2.c5";
90 
92  constexpr char RTTTL_STARTUP[] =
93  "Circles:d=16,o=6,b=180:a,a5,c,e,8a";
94  }
95 
96  namespace log {
102  class ToBeep
104  , protected satcat5::poll::Timer {
105  public:
107  explicit ToBeep(satcat5::io::RtttlDecoder* codec);
108 
110  void log_event(s8 priority, unsigned nbytes, const char* msg) override;
111 
114  inline void set_cooldown(unsigned msec) { m_cooldown = msec; }
115 
116  protected:
118  void timer_event() override {}
119 
120  // Internal state.
121  satcat5::io::RtttlDecoder* const m_codec;
122  unsigned m_cooldown;
123  bool m_enable;
124  };
125  }
126 }
Event-handler interface for newly received data.
Definition: io_readable.h:43
The PacketBuffer class is a wrapper for a circular buffer, with optional logic to support retention o...
Definition: pkt_buffer.h:66
void clear()
Reset buffer contents.
Definition: pkt_buffer.cc:22
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Ring Tone Text Transfer Language (RTTTL) interpreter.
Definition: codec_rtttl.h:33
u32 m_octave
Default octave.
Definition: codec_rtttl.h:67
u32 m_whole_note
Duration of whole note.
Definition: codec_rtttl.h:68
bool play(const char *src)
Decode and play the specified song (string input).
Definition: codec_rtttl.cc:48
satcat5::io::PacketBuffer m_queue
Playback queue.
Definition: codec_rtttl.h:69
u32 m_duration
Default note duration.
Definition: codec_rtttl.h:66
satcat5::io::Writeable *const m_spkr
Output device.
Definition: codec_rtttl.h:64
const u64 m_scale
Frequency conversion.
Definition: codec_rtttl.h:65
void flush()
If playback is in progress, halt immediately.
Definition: codec_rtttl.h:49
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
Definition: codec_rtttl.cc:111
u8 m_raw[SATCAT5_RTTTL_BUFFER]
Raw working buffer.
Definition: codec_rtttl.h:70
constexpr RtttlDecoder(satcat5::io::Writeable *spkr, u64 refclk_hz)
Link this decoder to a playback device.
Definition: codec_rtttl.h:38
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Defines the interface for accepting Log messages.
Definition: log.h:72
Respond to log messages by playing a few musical notes.
Definition: codec_rtttl.h:104
ToBeep(satcat5::io::RtttlDecoder *codec)
Constructor binds this object to an RTTTL decoder.
Definition: codec_rtttl.cc:194
void log_event(s8 priority, unsigned nbytes, const char *msg) override
Callback for each formatted Log message.
Definition: codec_rtttl.cc:197
void set_cooldown(unsigned msec)
Set minimum time between beeps.
Definition: codec_rtttl.h:114
void timer_event() override
End-of-cooldown callback does nothing.
Definition: codec_rtttl.h:118
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
Diagnostic logging to UART and/or Ethernet ports.
Core event-processing loop for SatCat5 software.