1 Star 0 Fork 0

上海市虹口区青少年活动中心/Morse-Pjct(摩尔斯码)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
morse.h 26.05 KB
一键复制 编辑 原始数据 按行查看 历史
= 提交于 2020-07-14 21:53 . Bug fix
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
#include "ports.h"
#pragma region TimeUnit
#ifndef M_TIMEUNIT
#define M_TIMEUNIT 200
#endif
#define M (M_TIMEUNIT/5.0f)
#define M_ M_TIMEUNIT
#define M__ (M_TIMEUNIT*3)
#define M___ (M_TIMEUNIT*7)
#pragma endregion
#pragma region RawCode
#define MM 0b00000000 //null
#define M_A 0b00000101 //A
#define M_B 0b00011000 //B
#define M_C 0b00011010 //C
#define M_D 0b00001100 //D
#define M_E 0b00000010 //E
#define M_F 0b00010010 //F
#define M_G 0b00001110 //G
#define M_H 0b00010000 //H
#define M_I 0b00000100 //I
#define M_J 0b00010111 //J
#define M_K 0b00001101 //K
#define M_L 0b00010100 //L
#define M_M 0b00000111 //M
#define M_N 0b00000110 //N
#define M_O 0b00001111 //O
#define M_P 0b00010110 //P
#define M_Q 0b00011101 //Q
#define M_R 0b00001010 //R
#define M_S 0b00001000 //S
#define M_T 0b00000011 //T
#define M_U 0b00001001 //U
#define M_V 0b00010001 //V
#define M_W 0b00001011 //W
#define M_X 0b00011001 //X
#define M_Y 0b00011011 //Y
#define M_Z 0b00011100 //Z
#define M_0 0b00111111 //0
#define M_1 0b00101111 //1
#define M_2 0b00100111 //2
#define M_3 0b00100011 //3
#define M_4 0b00100001 //4
#define M_5 0b00100000 //5
#define M_6 0b00110000 //6
#define M_7 0b00111000 //7
#define M_8 0b00111100 //8
#define M_9 0b00111110 //9
#define M__A 0b01110011 //,
#define M__B 0b01010101 //.
#define M__C 0b01101011 //!
#define M__D 0b01001100 //?
#define M__E 0b01101010 //;
#define M__F 0b01111000 //:
#define M__G 0b01011110 //'
#define M__H 0b01010010 //"
#define M__I 0b01001101 //_
#define M__J 0b00110110 //(
#define M__K 0b01101101 //)
#define M__L 0b00110001 //=
#define M__M 0b00110010 ///
#define M__N 0b01100001 //-
#define M__O 0b00101010 //+
#define M__P 0b00101000 //&
#define M__Q 0b01011010 //@
#define M__R 0b10001001 //$
#pragma endregion
namespace Morse
{
namespace Digital
{
class MorseSender : public Ports::DOPort
{
public:
MorseSender(int port, bool reversed = false) : Ports::DOPort(port, reversed) {}
void set(bool Hol)
{
digitalWrite(port, Hol ^ reversed);
}
void set(int length) //turn on for length(in msec) and then turn off
{
set((bool)HIGH);
delay(length);
set((bool)LOW);
}
void send(int rawCode) //send RawCode
{
int translator = rawCode;
if(translator == 0)
{
delay(M__);
}
for(int i = 0; i < 8; i++)
{
if(translator & 0b10000000)
{
translator <<= 1;
i++;
for(; i < 8; i++)
{
if(translator & 0b10000000)
{
set(M__);
}
else
{
set(M_);
}
if(i < 7)
{
delay(M_);
}
translator <<= 1;
}
}
translator <<= 1;
}
}
void send(char ch) //send character
{
switch (ch) //switch to RawCode
{
case 'a':case 'A': send(M_A);break;
case 'b':case 'B': send(M_B);break;
case 'c':case 'C': send(M_C);break;
case 'd':case 'D': send(M_D);break;
case 'e':case 'E': send(M_E);break;
case 'f':case 'F': send(M_F);break;
case 'g':case 'G': send(M_G);break;
case 'h':case 'H': send(M_H);break;
case 'i':case 'I': send(M_I);break;
case 'j':case 'J': send(M_J);break;
case 'k':case 'K': send(M_K);break;
case 'l':case 'L': send(M_L);break;
case 'm':case 'M': send(M_M);break;
case 'n':case 'N': send(M_N);break;
case 'o':case 'O': send(M_O);break;
case 'p':case 'P': send(M_P);break;
case 'q':case 'Q': send(M_Q);break;
case 'r':case 'R': send(M_R);break;
case 's':case 'S': send(M_S);break;
case 't':case 'T': send(M_T);break;
case 'u':case 'U': send(M_U);break;
case 'v':case 'V': send(M_V);break;
case 'w':case 'W': send(M_W);break;
case 'x':case 'X': send(M_X);break;
case 'y':case 'Y': send(M_Y);break;
case 'z':case 'Z': send(M_Z);break;
case '0': send(M_0);break;
case '1': send(M_1);break;
case '2': send(M_2);break;
case '3': send(M_3);break;
case '4': send(M_4);break;
case '5': send(M_5);break;
case '6': send(M_6);break;
case '7': send(M_7);break;
case '8': send(M_8);break;
case '9': send(M_9);break;
case ',': send(M__A);break;
case '.': send(M__B);break;
case '!': send(M__C);break;
case '?': send(M__D);break;
case ';': send(M__E);break;
case ':': send(M__F);break;
case '\'': send(M__G);break;
case '\"': send(M__H);break;
case '_': send(M__I);break;
case '(': send(M__J);break;
case ')': send(M__K);break;
case '=': send(M__L);break;
case '/': send(M__M);break;
case '-': send(M__N);break;
case '+': send(M__O);break;
case '&': send(M__P);break;
case '@': send(M__Q);break;
case '$': send(M__R);break;
default: send(MM);break;
}
}
void send(const char* str) //send sentence
{
char* ptr = (char*)str;
int temp = 0;
while(*ptr != '\0')
{
ptr++;
temp++;
}
for(int i = 0; i < temp; i++)
{
send(str[i]);
if(i < temp - 1)
{
delay(M__);
}
}
}
template<typename T>
MorseSender operator<<(T object) //template support
{
send(object);
return *this;
}
};
class MorseRecver : public Ports::DIPort
{
private:
bool currentStaticStatus(int length) //get the status when it get static
{
bool status = false;
unsigned int counter = 0;
while(counter < length)
{
if(get())
{
if(!status)
{
status = true;
counter = 0;
}
}
else
{
if(status)
{
status = false;
counter = 0;
}
}
counter++;
}
return status;
}
public:
MorseRecver(int port, bool reversed = false) : Ports::DIPort(port, reversed) {}
bool get(void)
{
return digitalRead(port) ^ reversed;
}
void get(int* buffer) //get RawCode
{
int& temp = *buffer;
get(temp);
}
void get(int& buffer) //get RawCode
{
unsigned int counter = 0;
while(!get())
{
if(counter >= M___)
{
buffer = 0;
return;
}
counter++;
delay(1);
}
buffer = 1;
for(int i = 0; i < 7; i++)
{
counter = 0;
while(counter < M__)
{
if(get())
{
if(currentStaticStatus(M))
{
break;
}
}
counter++;
delay(1);
}
if(counter == M__)
{
return;
}
counter = 0;
while(get())
{
if(!get())
{
if(!currentStaticStatus(M))
{
break;
}
}
counter++;
delay(1);
}
if(counter == 0)
{
return;
}
buffer <<= 1;
if(counter > M_)
{
buffer += 1;
}
}
}
char recv(void) //return the next received character
{
int temp = 0;
get(temp);
switch(temp)
{
case M_A: return 'A';
case M_B: return 'B';
case M_C: return 'C';
case M_D: return 'D';
case M_E: return 'E';
case M_F: return 'F';
case M_G: return 'G';
case M_H: return 'H';
case M_I: return 'I';
case M_J: return 'J';
case M_K: return 'K';
case M_L: return 'L';
case M_M: return 'M';
case M_N: return 'N';
case M_O: return 'O';
case M_P: return 'P';
case M_Q: return 'Q';
case M_R: return 'R';
case M_S: return 'S';
case M_T: return 'T';
case M_U: return 'U';
case M_V: return 'V';
case M_W: return 'W';
case M_X: return 'X';
case M_Y: return 'Y';
case M_Z: return 'Z';
case M_0: return '0';
case M_1: return '1';
case M_2: return '2';
case M_3: return '3';
case M_4: return '4';
case M_5: return '5';
case M_6: return '6';
case M_7: return '7';
case M_8: return '8';
case M_9: return '9';
case M__A: return ',';
case M__B: return '.';
case M__C: return '!';
case M__D: return '?';
case M__E: return ';';
case M__F: return ':';
case M__G: return '\'';
case M__H: return '\"';
case M__I: return '_';
case M__J: return '(';
case M__K: return ')';
case M__L: return '=';
case M__M: return '/';
case M__N: return '-';
case M__O: return '+';
case M__P: return '&';
case M__Q: return '@';
case M__R: return '$';
default: return ' ';
}
}
void recv(char& buffer) //get the next received character
{
buffer = recv();
}
void recv(char* buffer) //get the next received character
{
*buffer = recv();
}
void recv(char* buffer, int maxLength) //get the next received string(length depends on the second parameter)
{
for(int i = 0; i < maxLength; i++)
{
*buffer = recv();
buffer++;
}
}
template<typename T>
MorseRecver operator>>(T object) //template support
{
recv(object);
return *this;
}
};
}
namespace Analog
{
class MorseSender : Ports::AOPort
{
private:
int defaultLvl;
public:
MorseSender(int port, bool reversed = false, int defaultLvl = 255) : AOPort(port, reversed) { this->defaultLvl = defaultLvl; }
void set(bool Hol)
{
analogWrite(port, reversed ? 255 - (Hol ? defaultLvl : 0) : (Hol ? defaultLvl : 0));
}
void set(int length)
{
set((bool)HIGH);
delay(length);
set((bool)LOW);
}
void send(int rawCode) //send RawCode
{
int translator = rawCode;
if(translator == 0)
{
delay(M__);
}
for(int i = 0; i < 8; i++)
{
if(translator & 0b10000000)
{
translator <<= 1;
i++;
for(; i < 8; i++)
{
if(translator & 0b10000000)
{
set(M__);
}
else
{
set(M_);
}
if(i < 7)
{
delay(M_);
}
translator <<= 1;
}
}
translator <<= 1;
}
}
void send(char ch) //send character
{
switch (ch) //switch to RawCode
{
case 'a':case 'A': send(M_A);break;
case 'b':case 'B': send(M_B);break;
case 'c':case 'C': send(M_C);break;
case 'd':case 'D': send(M_D);break;
case 'e':case 'E': send(M_E);break;
case 'f':case 'F': send(M_F);break;
case 'g':case 'G': send(M_G);break;
case 'h':case 'H': send(M_H);break;
case 'i':case 'I': send(M_I);break;
case 'j':case 'J': send(M_J);break;
case 'k':case 'K': send(M_K);break;
case 'l':case 'L': send(M_L);break;
case 'm':case 'M': send(M_M);break;
case 'n':case 'N': send(M_N);break;
case 'o':case 'O': send(M_O);break;
case 'p':case 'P': send(M_P);break;
case 'q':case 'Q': send(M_Q);break;
case 'r':case 'R': send(M_R);break;
case 's':case 'S': send(M_S);break;
case 't':case 'T': send(M_T);break;
case 'u':case 'U': send(M_U);break;
case 'v':case 'V': send(M_V);break;
case 'w':case 'W': send(M_W);break;
case 'x':case 'X': send(M_X);break;
case 'y':case 'Y': send(M_Y);break;
case 'z':case 'Z': send(M_Z);break;
case '0': send(M_0);break;
case '1': send(M_1);break;
case '2': send(M_2);break;
case '3': send(M_3);break;
case '4': send(M_4);break;
case '5': send(M_5);break;
case '6': send(M_6);break;
case '7': send(M_7);break;
case '8': send(M_8);break;
case '9': send(M_9);break;
case ',': send(M__A);break;
case '.': send(M__B);break;
case '!': send(M__C);break;
case '?': send(M__D);break;
case ';': send(M__E);break;
case ':': send(M__F);break;
case '\'': send(M__G);break;
case '\"': send(M__H);break;
case '_': send(M__I);break;
case '(': send(M__J);break;
case ')': send(M__K);break;
case '=': send(M__L);break;
case '/': send(M__M);break;
case '-': send(M__N);break;
case '+': send(M__O);break;
case '&': send(M__P);break;
case '@': send(M__Q);break;
case '$': send(M__R);break;
default: send(MM);break;
}
}
void send(const char* str) //send sentence
{
char* ptr = (char*)str;
int temp = 0;
while(*ptr != '\0')
{
ptr++;
temp++;
}
for(int i = 0; i < temp; i++)
{
send(str[i]);
if(i < temp - 1)
{
delay(M__);
}
}
}
template<typename T>
MorseSender operator<<(T object) //template support
{
send(object);
return *this;
}
void setLvl(int Lvl)
{
defaultLvl = Lvl;
}
};
class MorseRecver : Ports::AIPort
{
private:
int defaultLvl;
bool currentStaticStatus(int length) //get the status when it get static
{
bool status = false;
unsigned int counter = 0;
while(counter < length)
{
if(get())
{
if(!status)
{
status = true;
counter = 0;
}
}
else
{
if(status)
{
status = false;
counter = 0;
}
}
counter++;
}
return status;
}
public:
MorseRecver(int port, bool reversed = false, int defaultLvl = 511) : Ports::AIPort(port, reversed) { this->defaultLvl = defaultLvl; }
bool get(void)
{
return reversed ? (analogRead(port) <= 255 - defaultLvl) : (analogRead(port) >= defaultLvl);
}
void get(int* buffer) //get RawCode
{
int& temp = *buffer;
get(temp);
}
void get(int& buffer) //get RawCode
{
unsigned int counter = 0;
while(!get())
{
if(counter >= M___)
{
buffer = 0;
return;
}
counter++;
delay(1);
}
buffer = 1;
for(int i = 0; i < 7; i++)
{
counter = 0;
while(counter < M__)
{
if(get())
{
if(currentStaticStatus(M))
{
break;
}
}
counter++;
delay(1);
}
if(counter == M__)
{
return;
}
counter = 0;
while(get())
{
if(!get())
{
if(!currentStaticStatus(M))
{
break;
}
}
counter++;
delay(1);
}
if(counter == 0)
{
return;
}
buffer <<= 1;
if(counter > M_)
{
buffer += 1;
}
}
}
char recv(void) //return the next received character
{
int temp = 0;
get(temp);
switch(temp)
{
case M_A: return 'A';
case M_B: return 'B';
case M_C: return 'C';
case M_D: return 'D';
case M_E: return 'E';
case M_F: return 'F';
case M_G: return 'G';
case M_H: return 'H';
case M_I: return 'I';
case M_J: return 'J';
case M_K: return 'K';
case M_L: return 'L';
case M_M: return 'M';
case M_N: return 'N';
case M_O: return 'O';
case M_P: return 'P';
case M_Q: return 'Q';
case M_R: return 'R';
case M_S: return 'S';
case M_T: return 'T';
case M_U: return 'U';
case M_V: return 'V';
case M_W: return 'W';
case M_X: return 'X';
case M_Y: return 'Y';
case M_Z: return 'Z';
case M_0: return '0';
case M_1: return '1';
case M_2: return '2';
case M_3: return '3';
case M_4: return '4';
case M_5: return '5';
case M_6: return '6';
case M_7: return '7';
case M_8: return '8';
case M_9: return '9';
case M__A: return ',';
case M__B: return '.';
case M__C: return '!';
case M__D: return '?';
case M__E: return ';';
case M__F: return ':';
case M__G: return '\'';
case M__H: return '\"';
case M__I: return '_';
case M__J: return '(';
case M__K: return ')';
case M__L: return '=';
case M__M: return '/';
case M__N: return '-';
case M__O: return '+';
case M__P: return '&';
case M__Q: return '@';
case M__R: return '$';
default: return ' ';
}
}
void recv(char& buffer) //get the next received character
{
buffer = recv();
}
void recv(char* buffer) //get the next received character
{
*buffer = recv();
}
void recv(char* buffer, int maxLength) //get the next received string(length depends on the second parameter)
{
for(int i = 0; i < maxLength; i++)
{
*buffer = recv();
buffer++;
}
}
template<typename T>
MorseRecver operator>>(T object) //template support
{
recv(object);
return *this;
}
void setLvl(int Lvl)
{
defaultLvl = Lvl;
}
};
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Arduino
1
https://gitee.com/HKQQSNHDZX/Morse-Pjct.git
git@gitee.com:HKQQSNHDZX/Morse-Pjct.git
HKQQSNHDZX
Morse-Pjct
Morse-Pjct(摩尔斯码)
master

搜索帮助