87 lines
2.7 KiB
C++
87 lines
2.7 KiB
C++
module;
|
|
import sys;
|
|
import io;
|
|
export module morse;
|
|
|
|
static const char * const morse_code [] = { /* nedefinované znaky nahrazeny mezrou */
|
|
" ", /* */ " ", /*!*/ ".-..-.", /*"*/ " ", /*#*/ " ", /*$*/
|
|
" ", /*%*/ " ", /*&*/ ".----.", /*'*/ "-.--.", /*(*/ "-.--.-", /*)*/
|
|
" ", /***/ ".-.-.", /*+*/ "--..--", /*,*/ "-....-", /*-*/ ".-.-.-", /*.*/ "-..-." , /*/*/
|
|
"-----", /*0*/ ".----", /*1*/ "..---", /*2*/ "...--", /*3*/ "....-", /*4*/
|
|
".....", /*5*/ "-....", /*6*/ "--...", /*7*/ "---..", /*8*/ "----.", /*9*/
|
|
"---...", /*:*/ "-.-.-.", /*;*/ " ", /*<*/ "-...-" , /*=*/ " ", /*>*/ "..--..", /*?*/
|
|
".--.-.", /*@*/
|
|
".-", /*A*/ "-...", /*B*/ "-.-.", /*C*/ "-..", /*D*/ ".", /*E*/ "..-.", /*F*/
|
|
"--.", /*G*/ "....", /*H*/ "..", /*I*/ ".---", /*J*/ "-.-", /*K*/ ".-..", /*L*/
|
|
"--", /*M*/ "-.", /*N*/ "---", /*O*/ ".--.", /*P*/ "--.-", /*Q*/ ".-.", /*R*/
|
|
"...", /*S*/ "-", /*T*/ "..-", /*U*/ "...-", /*V*/ ".--", /*W*/ "-..-", /*X*/
|
|
"-.--", /*Y*/ "--..", /*Z*/ " ", " ", " ", " ", "..--.-", /*_*/
|
|
};
|
|
|
|
static unsigned slen (const char * str) {
|
|
unsigned n = 0;
|
|
while (*str++) n++;
|
|
return n;
|
|
}
|
|
|
|
export class Morse {
|
|
const unsigned unit;
|
|
const io::GpioClass led;
|
|
|
|
public:
|
|
explicit Morse (const unsigned ms = 100) noexcept : unit (ms), led (io::GpioPortA, 10) {
|
|
sys::init ();
|
|
}
|
|
void operator<< (const char * text) const;
|
|
protected:
|
|
void out (const char * text) const;
|
|
};
|
|
void Morse::operator<< (const char * text) const {
|
|
const unsigned len = slen (text);
|
|
for (unsigned n=0; n<len; n++) {
|
|
const char c = text [n];
|
|
const char * mp = nullptr;
|
|
if (c < '\x20') {
|
|
mp = morse_code [0];
|
|
} else if (c < '`') {
|
|
const int i = c - '\x20';
|
|
mp = morse_code [i];
|
|
} else if (c == '`') {
|
|
mp = morse_code [0];
|
|
} else if (c <= 'z') {
|
|
const int i = c - '\x40';
|
|
mp = morse_code [i];
|
|
} else {
|
|
mp = morse_code [0];
|
|
}
|
|
if (!mp) return;
|
|
out (mp);
|
|
}
|
|
sys::delay (10 * unit);
|
|
}
|
|
/* . => 1 x unit
|
|
* - => 3 x unit
|
|
* mezera mezi značkami => 1 x unit
|
|
* mezera mezi znaky => 3 x unit
|
|
* mezera mezi slovy => 7 x unit
|
|
* */
|
|
void Morse::out (const char * text) const {
|
|
const unsigned len = slen(text);
|
|
for (unsigned n=0; n<len; n++) {
|
|
led << false;
|
|
sys::delay (unit);
|
|
const char c = text [n];
|
|
if (c == '.') {
|
|
led << true;
|
|
sys::delay (1 * unit);
|
|
} else if (c == '-') {
|
|
led << true;
|
|
sys::delay (3 * unit);
|
|
} else {
|
|
sys::delay (4 * unit);
|
|
}
|
|
led << false;
|
|
}
|
|
sys::delay (3 * unit);
|
|
}
|
|
|