Morse



  • Gruesse!

    Erstmal vornweg mit meinem gestern geposteten Problem http://www.c-plusplus.net/forum/viewtopic-var-t-is-181978.html bin ich kein Stueck weiter! Scheinbar doch nicht so trivial. Allen Anschein nach brauch ich ein externes Modem.

    So nun aber zu Morse. Ich hab hier ein Source womit ich allerdings nicht zufrieden bin:

    /*
     * codegen.c -> Ein Programm zur Generierung von Morsecode. Der erzeugte Code wird
     * sowohl optisch(Morsezeichen) als auch akkustisch(PC-Lautsprecher) ausgegeben.
     * ----------
     * coded Manuel Gebele <forensixs[at]gmx.de>
     *
     * 20052K07
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/ioctl.h>
    #include <linux/kd.h>
    
    /* Default Ticks for XT's timer chip 
     * f = CLOCK_TICK_RATE / FREQUENCY */
    #ifndef CLOCK_TICK_RATE
    #define CLOCK_TICK_RATE 1193180
    #endif
    
    #define FREQUENCY	2000.0 // Frequency
    #define POINT_LENGTH	60     // Beep-length point(ms) -> 20 WPM
    #define POINT_DELAY     60     // Beep-delay point(ms) -> 20 WPM
    #define LINE_LENGTH	POINT_LENGTH*3     // Line-length(ms)
    #define LINE_DELAY	POINT_DELAY        // Line-delay == Point-delay
    #define PAUSE		POINT_DELAY*3*1000 // Char pause
    #define WORD_PAUSE	POINT_DELAY*7*1000 // Word pause
    
    static void dit(int, unsigned);
    static void dah(int, unsigned);
    static void morse(FILE *, FILE *);
    
    static char *console = "/dev/console";
    
    int main(int argc, char *argv[])
    {
    	morse(stdin, stdout);
    
    	exit(0);
    }
    
    static void dit(int cd, unsigned rep)
    {
    	int i;
    
    	errno = 0;
    	for(i=0; i < rep; i++)
    	{
    		if(ioctl(cd, KIOCSOUND, (int)(CLOCK_TICK_RATE/FREQUENCY)) < 0)
    		{
    			fprintf(stderr, "codegen: ioctl failure: %s\n",
    				strerror(errno));
    			exit(1);
    		}
    		usleep(1000*POINT_LENGTH);
    		ioctl(cd, KIOCSOUND, 0);
    		usleep(1000*POINT_DELAY);
    	}
    	return;
    }
    
    static void dah(int cd, unsigned rep)
    {
    	int i;
    
    	errno = 0;
    	for(i=0; i < rep; i++)
    	{
    		if(ioctl(cd, KIOCSOUND, (int)(CLOCK_TICK_RATE/FREQUENCY)) < 0)
    		{
    			fprintf(stderr, "codegen: ioctl failure: %s\n",
    				strerror(errno));
    			exit(1);
    		}
    		usleep(1000*LINE_LENGTH);
    		ioctl(cd, KIOCSOUND, 0);
    		usleep(1000*LINE_DELAY);
    	}
    	return;
    }
    
    static void morse(FILE *instream, FILE *outstream)
    {
    	int ch, flag = 1;
    	int console_fd = -1;
    
    	while((ch = getc(instream)) != EOF)
    	{
    		if(flag)
    		{
    			fprintf(outstream, "Generating morse code....\n");
    
    			errno = 0;
    			if((console_fd = open(console, O_WRONLY)) == -1)
    			{
    				printf("\07");
    				fprintf(stderr, "codegen: open failure: %s\n",
    					strerror(errno));
    				exit(1);
    			}
    			flag = 0;
    			sleep(1);
    		}
    		switch(ch)
    		{
    			/* Word pause */
    			case ' ':
    				usleep(WORD_PAUSE);
    				break;
    			/* Alphas */
    			case 'A':
    			case 'a': 
    				fprintf(outstream, "._");
    				dit(console_fd, 1); dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'B':
    			case 'b':
    				fprintf(outstream, "_...");
    				dah(console_fd, 1); dit(console_fd, 3);
    				usleep(PAUSE);
    				break;
    			case 'C':
    			case 'c':
    			        /* Special char */
    				if((ch = getc(instream)) == 'H' || (ch == 'h'))
    				{	
    					fprintf(outstream, "____");
    					dah(console_fd, 4);
    					usleep(PAUSE);
    					break;
    				}
    				else ungetc(ch, instream);
    				fprintf(outstream, "_._.");
    				dah(console_fd, 1); dit(console_fd, 1);
    				dah(console_fd, 1); dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'D':
    			case 'd':
    				fprintf(outstream, "_..");
    				dah(console_fd, 1); dit(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case 'E':
    			case 'e':
    				fprintf(outstream, ".");
    				dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'F':
    			case 'f':
    				fprintf(outstream, ".._.");
    				dit(console_fd, 2); dah(console_fd, 1);
    				dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'G':
    			case 'g':
    				fprintf(outstream, "__.");
    				dah(console_fd, 2); dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'H':
    			case 'h':
    				fprintf(outstream, "....");
    				dit(console_fd, 4);
    				usleep(PAUSE);
    				break;
    			case 'I':
    			case 'i':
    				fprintf(outstream, "..");
    				dit(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case 'J':
    			case 'j':
    				fprintf(outstream, ".___");
    				dit(console_fd, 1); dah(console_fd, 3);
    				usleep(PAUSE);
    				break;
    			case 'K':
    			case 'k':
    				fprintf(outstream, "_._");
    				dah(console_fd, 1); dit(console_fd, 1);
    				dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'L':
    			case 'l':
    				fprintf(outstream, "._..");
    				dit(console_fd, 1); dah(console_fd, 1);
    				dit(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case 'M':
    			case 'm':
    				fprintf(outstream, "__");
    				dah(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case 'N':
    			case 'n':
    				fprintf(outstream, "_.");
    				dah(console_fd, 1); dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'O':
    			case 'o':
    				fprintf(outstream, "___");
    				dah(console_fd, 3);
    				usleep(PAUSE);
    				break;
    			case 'P':
    			case 'p':
    				fprintf(outstream, ".__.");
    				dit(console_fd, 1); dah(console_fd, 2);
    				dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'Q':
    			case 'q':
    				fprintf(outstream, "__._");
    				dah(console_fd, 2); dit(console_fd, 1);
    				dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'R':
    			case 'r':
    				fprintf(outstream, "._.");
    				dit(console_fd, 1); dah(console_fd, 1);
    				dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'S':
    			case 's':
    				fprintf(outstream, "...");
    				dit(console_fd, 3);
    				usleep(PAUSE);
    				break;
    			case 'T':
    			case 't':
    				fprintf(outstream, "_");
    				dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'U':
    			case 'u':
    				fprintf(outstream, ".._");
    				dit(console_fd, 2); dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'V':
    			case 'v':
    				fprintf(outstream, "..._");
    				dit(console_fd, 3); dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'W':
    			case 'w':
    				fprintf(outstream, ".__");
    				dit(console_fd, 1); dah(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case 'X':
    			case 'x':
    				fprintf(outstream, "_.._");
    				dah(console_fd, 1); dit(console_fd, 2);
    				dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'Y':
    			case 'y':
    				fprintf(outstream, "_.__");
    				dah(console_fd, 1); dit(console_fd, 1);
    				dah(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case 'Z':
    			case 'z':
    				fprintf(outstream, "__..");
    				dah(console_fd, 2); dit(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			/* Numbers */
    			case '1':
    				fprintf(outstream, ".____");
    				dit(console_fd, 1); dah(console_fd, 4);
    				usleep(PAUSE);
    				break;
    			case '2':
    				fprintf(outstream, "..___");
    				dit(console_fd, 2); dah(console_fd, 3);
    				usleep(PAUSE);
    				break;
    			case '3':
    				fprintf(outstream, "...__");
    				dit(console_fd, 3); dah(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case '4':
    				fprintf(outstream, "...._");
    				dit(console_fd, 4); dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case '5':
    				fprintf(outstream, ".....");
    				dit(console_fd, 5);
    				usleep(PAUSE);
    				break;
    			case '6':
    				fprintf(outstream, "_....");
    				dah(console_fd, 1); dit(console_fd, 4);
    				usleep(PAUSE);
    				break;
    			case '7':
    				fprintf(outstream, "__...");
    				dah(console_fd, 2); dit(console_fd, 3);
    				usleep(PAUSE);
    				break;
    			case '8':
    				fprintf(outstream, "___..");
    				dah(console_fd, 3); dit(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case '9':
    				fprintf(outstream, "____.");
    				dah(console_fd, 4); dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case '0':
    				fprintf(outstream, "_____");
    				dah(console_fd, 5);
    				usleep(PAUSE);
    				break;
    			/* Special/Sentence chars */
    			case 'Ä':
    			case 'ä':
    				fprintf(outstream, "._._");
    				dit(console_fd, 1); dah(console_fd, 1);
    				dit(console_fd, 1); dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'Ö':
    			case 'ö':
    				fprintf(outstream, "___.");
    				dah(console_fd, 3); dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case 'Ü':
    			case 'ü':
    				fprintf(outstream, "..__");
    				dit(console_fd, 2); dah(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case 'ß':
    				fprintf(outstream, "...__..");
    				dit(console_fd, 3); dah(console_fd, 2);
    				dit(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case '.':
    				fprintf(outstream, "._._._");
    				dit(console_fd, 1); dah(console_fd, 1);
    				dit(console_fd, 1); dah(console_fd, 1);
    				dit(console_fd, 1); dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case ',':
    				fprintf(outstream, "__..__");
    				dah(console_fd, 2); dit(console_fd, 2);
    				dah(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case ':':
    				fprintf(outstream, "___...");
    				dah(console_fd, 3); dit(console_fd, 3);
    				usleep(PAUSE);
    				break;
    			case ';':
    				fprintf(outstream, "_._._.");
    				dah(console_fd, 1); dit(console_fd, 1);
    				dah(console_fd, 1); dit(console_fd, 1);
    				dah(console_fd, 1); dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case '?':
    				fprintf(outstream, "..__..");
    				dit(console_fd, 2); dah(console_fd, 2);
    				dit(console_fd, 2);
    				usleep(PAUSE);
    				break;
    			case '-':
    				fprintf(outstream, "_...._");
    				dah(console_fd, 1); dit(console_fd, 4);
    				dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case '(':
    				fprintf(outstream, "_.__.");
    				dah(console_fd, 1); dit(console_fd, 1);
    				dah(console_fd, 2); dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case ')':
    				fprintf(outstream, "_.__._");
    				dah(console_fd, 1); dit(console_fd, 1);
    				dah(console_fd, 2); dit(console_fd, 1);
    				dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case '\'':
    				fprintf(outstream, ".____.");
    				dit(console_fd, 1); dah(console_fd, 4);
    				dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case '=':
    				fprintf(outstream, "_..._");
    				dah(console_fd, 1); dit(console_fd, 3);
    				dah(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case '+':
    				fprintf(outstream, "._._.");
    				dit(console_fd, 1); dah(console_fd, 1);
    				dit(console_fd, 1); dah(console_fd, 1);
    				dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case '/':
    				fprintf(outstream, "_.._.");
    				dah(console_fd, 1); dit(console_fd, 2);
    				dah(console_fd, 1); dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			case '@':
    				fprintf(outstream, ".__._.");
    				dit(console_fd, 1); dah(console_fd, 2);
    				dit(console_fd, 1); dah(console_fd, 1);
    				dit(console_fd, 1);
    				usleep(PAUSE);
    				break;
    			default: break;
    		}
    	}
    	close(console_fd);
    	return;
    }
    

    Ich hoffe Ihr seid nicht eingeschlafen beim lesen der morse-func. Irgendwie kann ich mich mit ihr nicht anfreunden sie so extrem LANG und eintoenig. Gibt es da denn nicht ne bessere Lsg.?

    Bin fuer einen kritischen Dialog dankbar! 😉



  • Du könntest eine Stuktur der Form

    typedef struct {
      char c; /* das Zeichen */
      char *code; /* der Morse-Code */
    } Character;
    

    anlegen.

    Die könntest du auch statisch initialisieren

    Character table[] = {
       { 'a', "._" },
       { 'e', "." },
       usw...
    }
    

    Für jedes Zeichen suchst du dann einfach in deinem Array. Wenn du es gefunden hast, rufst du einfach je nach String, deine dit und dah Funktionen auf. Außerdem kannst du mit der Funktion tolower alle Buchstaben in Kleinbuchstaben unwandeln. Dann musst du nur noch auf Kleinbuchstaben prüfen.


Anmelden zum Antworten