use crate::{Error, Result};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum StopBits {
One,
OnePointFive,
Two,
}
impl StopBits {
#[must_use]
pub const fn bits(self) -> f64 {
match self {
Self::One => 1.0,
Self::OnePointFive => 1.5,
Self::Two => 2.0,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum CharacterSet {
#[default]
UsTty,
Ita2,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RttyProfile {
pub baud: f64,
pub shift_hz: f64,
pub stop_bits: StopBits,
}
impl RttyProfile {
pub const RTTY_45_170: Self = Self::new(45.45, 170.0, StopBits::OnePointFive);
pub const RTTY_50_170: Self = Self::new(50.0, 170.0, StopBits::OnePointFive);
pub const RTTY_75_170: Self = Self::new(75.0, 170.0, StopBits::OnePointFive);
pub const RTTY_50_425: Self = Self::new(50.0, 425.0, StopBits::OnePointFive);
pub const RTTY_50_850: Self = Self::new(50.0, 850.0, StopBits::OnePointFive);
#[must_use]
pub const fn new(baud: f64, shift_hz: f64, stop_bits: StopBits) -> Self {
Self {
baud,
shift_hz,
stop_bits,
}
}
pub fn validate(&self) -> Result<()> {
if !self.baud.is_finite() || !(20.0..=300.0).contains(&self.baud) {
return Err(Error::InvalidConfiguration(
"RTTY baud must be between 20 and 300",
));
}
if !self.shift_hz.is_finite() || !(20.0..=1_200.0).contains(&self.shift_hz) {
return Err(Error::InvalidConfiguration(
"RTTY shift must be between 20 and 1200 Hz",
));
}
Ok(())
}
pub fn check_tones(&self, tones: &RttyTones) -> Result<()> {
if (tones.shift_hz() - self.shift_hz).abs() > 1.0 {
return Err(Error::InvalidConfiguration(
"RTTY tones must be separated by the profile shift",
));
}
Ok(())
}
#[must_use]
pub fn bit_seconds(&self) -> f64 {
1.0 / self.baud
}
#[must_use]
pub fn character_seconds(&self) -> f64 {
(6.0 + self.stop_bits.bits()) / self.baud
}
}
impl Default for RttyProfile {
fn default() -> Self {
Self::RTTY_45_170
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RttyTones {
pub mark_hz: f64,
pub space_hz: f64,
}
impl RttyTones {
#[must_use]
pub const fn new(mark_hz: f64, space_hz: f64) -> Self {
Self { mark_hz, space_hz }
}
#[must_use]
pub fn from_center(center_hz: f64, shift_hz: f64, mark_above_center: bool) -> Self {
let half = shift_hz / 2.0;
if mark_above_center {
Self::new(center_hz + half, center_hz - half)
} else {
Self::new(center_hz - half, center_hz + half)
}
}
#[must_use]
pub fn center_hz(&self) -> f64 {
(self.mark_hz + self.space_hz) / 2.0
}
#[must_use]
pub fn shift_hz(&self) -> f64 {
(self.mark_hz - self.space_hz).abs()
}
#[must_use]
pub fn reversed(&self) -> Self {
Self::new(self.space_hz, self.mark_hz)
}
pub fn validate(&self, sample_rate: u32) -> Result<()> {
let nyquist = f64::from(sample_rate) / 2.0;
for tone in [self.mark_hz, self.space_hz] {
if !tone.is_finite() || tone < 100.0 || tone > nyquist * 0.9 {
return Err(Error::InvalidConfiguration(
"RTTY tones must be above 100 Hz and below 90% of Nyquist",
));
}
}
if self.shift_hz() < 20.0 {
return Err(Error::InvalidConfiguration(
"RTTY mark and space tones must be at least 20 Hz apart",
));
}
Ok(())
}
}
impl Default for RttyTones {
fn default() -> Self {
Self::new(2_125.0, 2_295.0)
}
}
pub(crate) fn validate_sample_rate(sample_rate: u32) -> Result<()> {
if (8_000..=384_000).contains(&sample_rate) {
Ok(())
} else {
Err(Error::InvalidSampleRate(sample_rate))
}
}