morse_codec/
lib.rs

1//! Rust library for live decoding and encoding of morse code messages.
2//! Supports multiple embedded devices and operating systems by being no_std.
3//!
4//! You can create messages by sending individual high and low signals in milliseconds to decoder,
5//! from the keyboard, mouse clicks, or a button connected to some embedded device.
6//! Decoder supports three precision (difficulty) modes. Lazy (easiest), Accurate (hardest) and
7//! Farnsworth mode (somewhere inbetween)
8//!
9//! Use the encoder to turn your messages or characters into morse code strings or create a
10//! sequence of signals to drive an external component such as an LED, step motor or speaker.
11//!
12//! # Features
13//! * Decoder
14//! * Encoder
15//!
16//! UTF-8 is supported behind a feature flag.
17//! When not used it should not interfere with embedded device applications.
18//!
19//! The lib is no_std outside testing to make sure it will work on embedded devices
20//! as well as operating systems.
21
22// There're debug println!() statements in various parts of
23// the code marked by a "// DBG" sign on top. In order to use them on a development environment
24// with a proper OS and std, comment out the below attribute and uncomment the debug lines you want.
25
26#![cfg_attr(not(test), no_std)]
27
28#[cfg(not(feature = "utf8"))]
29pub type Character = u8;
30
31#[cfg(feature = "utf8")]
32pub type Character = char;
33
34// This is the array length for a sequence of morse signals or
35// character representation of those signals while encoding
36const MORSE_ARRAY_LENGTH: usize = 6;
37const LONG_SIGNAL_MULTIPLIER: u16 = 3;
38const WORD_SPACE_MULTIPLIER: u16 = 7;
39
40/// We use this character to fill message arrays so when we encounter this char
41/// it actually means there's no character there.
42///
43/// The character # is not a part of international morse code, so it's a good candidate.
44pub const FILLER: Character = '#' as Character;
45
46/// Char version of the [FILLER] coz why not? It's mainly used while generating bytes from
47/// &str slices. A [char] which is utf-8 by default in Rust, can be more than one byte.
48/// In ASCII mode, turning chars into bytes if they're only ascii makes sense.
49pub const FILLER_CHAR: char = '#';
50
51/// If a decoding error happens, we put this character as a placeholder.
52pub const DECODING_ERROR_CHAR: Character = '?' as Character;
53
54/// Building block of morse characters.
55///
56/// This enum can be used with the decoder to directly add signals to characters.
57#[derive(Clone, Debug, PartialEq)]
58pub enum MorseSignal {
59    Short,
60    Long,
61}
62
63type MorseCodeArray = [Option<MorseSignal>; MORSE_ARRAY_LENGTH];
64
65/// This corresponds to empty character ' ' which is the default character
66pub const MORSE_DEFAULT_CHAR: MorseCodeArray = [None, None, None, None, None, None];
67
68pub mod charsets;
69pub use charsets::{
70    CharacterSet,
71    MorseCodeSet,
72    DEFAULT_CHARACTER_SET_LENGTH,
73    DEFAULT_CHARACTER_SET,
74    DEFAULT_MORSE_CODE_SET,
75};
76
77#[cfg(feature = "decoder")]
78pub mod decoder;
79
80#[cfg(feature = "encoder")]
81pub mod encoder;
82
83pub mod message;