Fix Cyrillic encoding

This commit is contained in:
srcejon 2023-09-03 09:02:53 +01:00
parent 52c798cc46
commit fdd73c9d99
2 changed files with 73 additions and 58 deletions

View File

@ -81,8 +81,8 @@ const QStringList Baudot::m_usFigure = {
const QStringList Baudot::m_russianLetter = { const QStringList Baudot::m_russianLetter = {
"\0", "Е", "\n", "А", " ", "С", "И", "У", "\0", "Е", "\n", "А", " ", "С", "И", "У",
"\r", "Д", "П", "Й", "Н", "Ф", "Ц", "К", "\r", "Д", "Р", "Й", "Ч", "Ф", "Ц", "К",
"Т", "З", "Л", "В", "Х", "Ы", "P", "Я", "Т", "З", "Л", "В", "Х", "Ы", "П", "Я",
"О", "Б", "Г", "<", "М", "Ь", "Ж", ">" "О", "Б", "Г", "<", "М", "Ь", "Ж", ">"
}; };
@ -206,36 +206,37 @@ void BaudotEncoder::setCharacterSet(Baudot::CharacterSet characterSet)
switch (m_characterSet) switch (m_characterSet)
{ {
case Baudot::ITA2: case Baudot::ITA2:
m_letters = Baudot::m_ita2Letter; m_chars[LETTERS] = Baudot::m_ita2Letter;
m_figures = Baudot::m_ita2Figure; m_chars[FIGURES] = Baudot::m_ita2Figure;
break; break;
case Baudot::UK: case Baudot::UK:
m_letters = Baudot::m_ukLetter; m_chars[LETTERS] = Baudot::m_ukLetter;
m_figures = Baudot::m_ukFigure; m_chars[FIGURES] = Baudot::m_ukFigure;
break; break;
case Baudot::EUROPEAN: case Baudot::EUROPEAN:
m_letters = Baudot::m_europeanLetter; m_chars[LETTERS] = Baudot::m_europeanLetter;
m_figures = Baudot::m_europeanFigure; m_chars[FIGURES] = Baudot::m_europeanFigure;
break; break;
case Baudot::US: case Baudot::US:
m_letters = Baudot::m_usLetter; m_chars[LETTERS] = Baudot::m_usLetter;
m_figures = Baudot::m_usFigure; m_chars[FIGURES] = Baudot::m_usFigure;
break; break;
case Baudot::RUSSIAN: case Baudot::RUSSIAN:
m_letters = Baudot::m_russianLetter; m_chars[LETTERS] = Baudot::m_ita2Letter;
m_figures = Baudot::m_russianFigure; m_chars[FIGURES] = Baudot::m_russianFigure;
break; break;
case Baudot::MURRAY: case Baudot::MURRAY:
m_letters = Baudot::m_murrayLetter; m_chars[LETTERS] = Baudot::m_murrayLetter;
m_figures = Baudot::m_murrayFigure; m_chars[FIGURES] = Baudot::m_murrayFigure;
break; break;
default: default:
qDebug() << "BaudotEncoder::BaudotEncoder: Unsupported character set " << m_characterSet; qDebug() << "BaudotEncoder::BaudotEncoder: Unsupported character set " << m_characterSet;
m_letters = Baudot::m_ita2Letter; m_chars[LETTERS] = Baudot::m_ita2Letter;
m_figures = Baudot::m_ita2Figure; m_chars[FIGURES] = Baudot::m_ita2Figure;
m_characterSet = Baudot::ITA2; m_characterSet = Baudot::ITA2;
break; break;
} }
m_chars[(int)CYRILLIC] = Baudot::m_russianLetter;
} }
void BaudotEncoder::setUnshiftOnSpace(bool unshiftOnSpace) void BaudotEncoder::setUnshiftOnSpace(bool unshiftOnSpace)
@ -262,14 +263,11 @@ void BaudotEncoder::setStopBits(int stopBits)
void BaudotEncoder::init() void BaudotEncoder::init()
{ {
m_figure = false; m_page = LETTERS;
} }
bool BaudotEncoder::encode(QChar c, unsigned &bits, unsigned int &bitCount) bool BaudotEncoder::encode(QChar c, unsigned &bits, unsigned int &bitCount)
{ {
unsigned int code;
const unsigned int codeLen = 5;
bits = 0; bits = 0;
bitCount = 0; bitCount = 0;
@ -277,50 +275,63 @@ bool BaudotEncoder::encode(QChar c, unsigned &bits, unsigned int &bitCount)
c = c.toUpper(); c = c.toUpper();
QString s(c); QString s(c);
// We could create reverse look-up tables to speed this up, but it's only 200 baud... if (s == '>')
if (m_letters.contains(s))
{ {
if (m_figure) addCode(bits, bitCount, m_chars[m_page].indexOf(s));
{ m_page = LETTERS;
// Switch to letters
addStartBits(bits, bitCount);
code = reverseBits(m_letters.indexOf(">"), codeLen);
addBits(bits, bitCount, code, codeLen);
addStopBits(bits, bitCount);
m_figure = false;
}
addStartBits(bits, bitCount);
code = reverseBits(m_letters.indexOf(s), codeLen);
addBits(bits, bitCount, code, codeLen);
addStopBits(bits, bitCount);
return true; return true;
} }
else if (m_figures.contains(s)) else if (s == '<')
{ {
if (!m_figure) addCode(bits, bitCount, m_chars[m_page].indexOf(s));
{ m_page = FIGURES;
// Switch to figures return true;
addStartBits(bits, bitCount); }
code = reverseBits(m_letters.indexOf("<"), codeLen); else if ((m_characterSet == Baudot::RUSSIAN) && (s == '\0'))
addBits(bits, bitCount, code, codeLen); {
addStopBits(bits, bitCount); addCode(bits, bitCount, m_chars[m_page].indexOf(s));
m_figure = true; m_page = CYRILLIC;
} return true;
addStartBits(bits, bitCount); }
code = reverseBits(m_figures.indexOf(s), codeLen);
addBits(bits, bitCount, code, codeLen); // We could create reverse look-up tables to speed this up, but it's only 200 baud...
addStopBits(bits, bitCount);
if ((s == " ") && m_unshiftOnSpace) { // Is character in current page? If so, use that, as it avoids switching
m_figure = false; if (m_chars[m_page].contains(s))
} {
addCode(bits, bitCount, m_chars[m_page].indexOf(s));
return true;
} }
else else
{ {
qDebug() << "BaudotEncoder::encode: Can't encode" << c; // Look for character in other pages
return false; const QString switchPage[] = { ">", "<", "\0" };
for (int page = m_page == LETTERS ? 1 : 0; page < (m_characterSet == Baudot::RUSSIAN) ? 3 : 2; page++)
{
if (m_chars[page].contains(s))
{
// Switch to page
addCode(bits, bitCount, m_chars[m_page].indexOf(switchPage[page]));
m_page = (BaudotEncoder::Page)page;
addCode(bits, bitCount, m_chars[m_page].indexOf(s));
return true;
}
}
} }
return true; return false;
}
void BaudotEncoder::addCode(unsigned& bits, unsigned int& bitCount, unsigned int code) const
{
const unsigned int codeLen = 5;
addStartBits(bits, bitCount);
code = reverseBits(code, codeLen);
addBits(bits, bitCount, code, codeLen);
addStopBits(bits, bitCount);
} }
void BaudotEncoder::addStartBits(unsigned& bits, unsigned int& bitCount) const void BaudotEncoder::addStartBits(unsigned& bits, unsigned int& bitCount) const

View File

@ -88,6 +88,7 @@ public:
private: private:
void addCode(unsigned& bits, unsigned int& bitCount, unsigned int code) const;
void addStartBits(unsigned int& bits, unsigned int& bitCount) const; void addStartBits(unsigned int& bits, unsigned int& bitCount) const;
void addStopBits(unsigned int& bits, unsigned int& bitCount) const; void addStopBits(unsigned int& bits, unsigned int& bitCount) const;
void addBits(unsigned int& bits, unsigned int& bitCount, int data, int count) const; void addBits(unsigned int& bits, unsigned int& bitCount, int data, int count) const;
@ -96,9 +97,12 @@ private:
Baudot::CharacterSet m_characterSet; Baudot::CharacterSet m_characterSet;
bool m_unshiftOnSpace; bool m_unshiftOnSpace;
QStringList m_letters; QStringList m_chars[3];
QStringList m_figures; enum Page {
bool m_figure; LETTERS,
FIGURES,
CYRILLIC
} m_page;
bool m_msbFirst; bool m_msbFirst;
int m_startBits; int m_startBits;
int m_stopBits; int m_stopBits;