format code

This commit is contained in:
Martin Brodbeck 2024-02-29 09:27:59 +01:00
parent da54029403
commit 3311e7d790
2 changed files with 115 additions and 111 deletions

View File

@ -1,5 +1,5 @@
#include <cstring>
#include <cstdio>
#include <cstring>
#include "pico/stdio.h"
@ -7,59 +7,62 @@
MorseCode::MorseCode() { buildMap(); }
bool MorseCode::charToMorse(const unsigned char ch, char *morseSymbols) {
size_t index{0};
bool MorseCode::charToMorse(const unsigned char ch, char *morseSymbols)
{
size_t index {0};
if (ch != ' ' && morseMap[ch] == nullptr) {
return false;
}
if (ch == ' ') {
morseSymbols[index++] = 'w';
} else {
for (unsigned int i = 0; i < strlen(morseMap[ch]); i++) {
char m = morseMap[ch][i];
morseSymbols[index++] = m;
if (i < strlen(morseMap[ch]) - 1) {
morseSymbols[index++] = 'i';
}
if (ch != ' ' && morseMap[ch] == nullptr) {
return false;
}
morseSymbols[index++] = 'c';
}
if (ch == ' ') {
morseSymbols[index++] = 'w';
} else {
for (unsigned int i = 0; i < strlen(morseMap[ch]); i++) {
char m = morseMap[ch][i];
morseSymbols[index] = '\0';
morseSymbols[index++] = m;
return true;
if (i < strlen(morseMap[ch]) - 1) {
morseSymbols[index++] = 'i';
}
}
morseSymbols[index++] = 'c';
}
morseSymbols[index] = '\0';
return true;
}
void MorseCode::sendCharacter(const char ch) {
char morseSymbols[32]{0};
void MorseCode::sendCharacter(const char ch)
{
char morseSymbols[32] {0};
if (!charToMorse(ch, morseSymbols)) {
return;
}
if (!charToMorse(ch, morseSymbols)) {
return;
}
for (unsigned int i = 0; i < strlen(morseSymbols); i++) {
printf("%c", morseSymbols[i]);
}
for (unsigned int i = 0; i < strlen(morseSymbols); i++) {
printf("%c", morseSymbols[i]);
}
}
void MorseCode::buildMap() {
for (size_t i = 0; i < 256; i++) {
if (i == '\"')
morseMap[i] = morseTable[0];
else if (i == '$')
morseMap[i] = morseTable[1];
else if (i == '\'')
morseMap[i] = morseTable[2];
else if (i == '(')
morseMap[i] = morseTable[3];
else if (i == ')')
morseMap[i] = morseTable[4];
else if (i >= '+' && i <= ']')
morseMap[i] = morseTable[i - 0x26];
}
void MorseCode::buildMap()
{
for (size_t i = 0; i < 256; i++) {
if (i == '\"')
morseMap[i] = morseTable[0];
else if (i == '$')
morseMap[i] = morseTable[1];
else if (i == '\'')
morseMap[i] = morseTable[2];
else if (i == '(')
morseMap[i] = morseTable[3];
else if (i == ')')
morseMap[i] = morseTable[4];
else if (i >= '+' && i <= ']')
morseMap[i] = morseTable[i - 0x26];
}
}

View File

@ -1,70 +1,71 @@
#pragma once
class MorseCode final {
public:
MorseCode();
void sendCharacter(const char ch);
bool charToMorse(const unsigned char ch, char *morseSymbols);
class MorseCode final
{
public:
MorseCode();
void sendCharacter(const char ch);
bool charToMorse(const unsigned char ch, char *morseSymbols);
private:
void buildMap();
const char *morseMap[256] {nullptr};
static constexpr const char *morseTable[] = {
".-..-.", // " => <RR>
"...-..-", // $ => <SX>
".----.", // ' => <WG>
"-.--.", // ( => <KN>
"-.--.-", // ) => <KK>
".-.-.", // + => <AR>
"--..--", // ,
"-....-", // - => <DU>
".-.-.-", // .
"-..-.", // / => <DN>
"-----", // 0
".----", // 1
"..---", // 2
"...--", // 3
"....-", // 4
".....", // 5
"-....", // 6
"--...", // 7
"---..", // 8
"----.", // 9
"-.--.", // : => <KN>
".-.-", // ; => <AA>
".-.-.", // < => <AR> [sic!]
"-...-", // = => <BT>
"...-.-", // > == <SK>
"..--..", // ?
".--.-.", // @ => <AC>
".-", // 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
".-...", // [ => <AS>
"-..-.", // \ => <DN>
"-.--.", // ] => <KN> [sic!]
};
private:
void buildMap();
const char *morseMap[256] {nullptr};
static constexpr const char *morseTable[] = {
".-..-.", // " => <RR>
"...-..-", // $ => <SX>
".----.", // ' => <WG>
"-.--.", // ( => <KN>
"-.--.-", // ) => <KK>
".-.-.", // + => <AR>
"--..--", // ,
"-....-", // - => <DU>
".-.-.-", // .
"-..-.", // / => <DN>
"-----", // 0
".----", // 1
"..---", // 2
"...--", // 3
"....-", // 4
".....", // 5
"-....", // 6
"--...", // 7
"---..", // 8
"----.", // 9
"-.--.", // : => <KN>
".-.-", // ; => <AA>
".-.-.", // < => <AR> [sic!]
"-...-", // = => <BT>
"...-.-", // > == <SK>
"..--..", // ?
".--.-.", // @ => <AC>
".-", // 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
".-...", // [ => <AS>
"-..-.", // \ => <DN>
"-.--.", // ] => <KN> [sic!]
};
};