hschumann2/TempleOS-Source-Code
0847
1 2#define M_DASH_THRESHOLD 0.1403#define M_SPACE_THRESHOLD 0.3004 5U8 *morse_lst=6 7"A.-\0"8"B-...\0"9"C-.-.\0"10"D-..\0"11"E.\0"12"F..-.\0"13"G--.\0"14"H....\0"15"I..\0"16"J.---\0"17"K-.-\0"18"L.-..\0"19"M--\0"20"N-.\0"21"O---\0"22"P.--.\0"23"Q--.-\0"24"R.-.\0"25"S...\0"26"T-\0"27"U..-\0"28"V...-\0"29"W.--\0"30"X-..-\0"31"Y-.--\0"32"Z--..\0"33 34"1.----\0"35"2..---\0"36"3...--\0"37"4....-\0"38"5.....\0"39"6-....\0"40"7--...\0"41"8---..\0"42"9----.\0"43"0-----\0"44 45"/-..-.\0"46"+.-.-.\0"47"..-.-.-\0"48",--..--\0"49"?..--..\0"50"(-.--.\0"51")-.--.-\0"52"--....-\0"53"\".-..-.\0"54"_..--.-\0"55"'.----.\0"56":---...\0"57";-.-.-.\0"58"=-...-\0";59 60U0 MorseTable()61{62 U8 *st;63 I64 i=0;64 for (i=0;i<13;i++ ) {65 if (st=LstSub(i,morse_lst))66 "$RED$%C$FG$ %-7s ",*st,st+1;67 if (st=LstSub(i+13,morse_lst))68 "$RED$%C$FG$ %-7s ",*st,st+1;69 if (st=LstSub(i+26,morse_lst))70 "$RED$%C$FG$ %-7s ",*st,st+1;71 if (st=LstSub(i+39,morse_lst))72 "$RED$%C$FG$ %-7s ",*st,st+1;73 '\n';74 }75 '\n';76}77 78F64 m_start,m_end,dt_down,dt_up;79Bool space_sent;80 81U8 *MorseTimes(CDoc *,CDocEntry *,CTask *mem_task)82{83 U8 *st=MAlloc(64,mem_task);84 StrPrint(st,"Down:%10.6f Up:%10.6f",dt_down,dt_up);85 return st;86}87 88I64 MorseWaitKey()89{90 I64 ch;91 F64 dt;92 while (TRUE) {93 if (m_start) {94 GetMsg(NULL,NULL,1<<MSG_KEY_UP);95 m_end=tS;96 Snd;97 dt_down=m_end-m_start;98 m_start=0;99 space_sent=FALSE;100 if (dt_down<M_DASH_THRESHOLD)101 return '.';102 else103 return '-';104 } else {105 if (!space_sent) {106 while (!ScanMsg(&ch,NULL,1<<MSG_KEY_DOWN)) {107 dt=tS-m_end;108 if (dt>=M_SPACE_THRESHOLD) {109 space_sent=TRUE;110 return CH_SPACE;111 }112 Yield;113 }114 } else115 ch=GetChar(,FALSE);116 m_start=tS;117 Snd(74);118 if (ch==CH_SHIFT_ESC || ch==CH_ESC) {119 '\n';120 Snd;121 throw; //exit program122 }123 dt_up=m_start-m_end;124 if (!space_sent && dt_up>=M_SPACE_THRESHOLD) {125 space_sent=TRUE;126 return CH_SPACE;127 }128 }129 }130}131 132U0 Morse()133{134 CDocEntry *doc_e;135 I64 ch;136 U8 buf[8],*dst,*src;137 138 MorseTable;139 m_start=0;140 m_end=tS;141 dt_down=0;142 dt_up=0;143 space_sent=TRUE;144 145 "$GREEN$";146 147 doc_e=DocPrint(DocPut,"$TX+TC,\" \"$");148 doc_e->tag_cb=&MorseTimes;149 150 "$FG$\n";151 152 dst=buf;153 while (TRUE) {154 ch=MorseWaitKey;155 if (ch==CH_SPACE) {156 *dst=0;157 src=morse_lst;158 while (*src) {159 if (!StrCmp(src+1,buf)) {160 "$GREEN$%C$FG$",*src;161 break;162 } else163 src+=StrLen(src)+1;164 }165 '' CH_SPACE;166 dst=buf;167 } else {168 if (dst-buf<sizeof(buf)-1) {169 '' ch;170 *dst++=ch;171 }172 }173 }174 Snd;175}176 177Morse;178 