From 7ddb2db9a638b9cf418e6079110d9a162d9d7188 Mon Sep 17 00:00:00 2001 From: Tait Hoyem <44244401+TTWNO@users.noreply.github.com> Date: Mon, 10 Jun 2019 16:22:48 +0000 Subject: [PATCH] Add gettysburg address. Add clean option to Makefile --- Makefile | 3 ++ gettysburg.txt | 1 + morse.c | 83 +++++++++++++++++++++++++++++--------------------- 3 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 gettysburg.txt diff --git a/Makefile b/Makefile index 6952fe7..ee5d34a 100644 --- a/Makefile +++ b/Makefile @@ -2,3 +2,6 @@ defualt: all all: gcc -O2 morse.c -o morse + +clean: + rm morse diff --git a/gettysburg.txt b/gettysburg.txt new file mode 100644 index 0000000..66782bb --- /dev/null +++ b/gettysburg.txt @@ -0,0 +1 @@ +"Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate — we can not consecrate — we can not hallow — this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us — that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion — that we here highly resolve that these dead shall not have died in vain — that this nation, under God, shall have a new birth of freedom — and that government of the people, by the people, for the people, shall not perish from the earth." diff --git a/morse.c b/morse.c index b344955..9292798 100644 --- a/morse.c +++ b/morse.c @@ -2,22 +2,21 @@ #include #include #include +#include +#include -#define MAX_STRING_LENGTH 1024 +#define MAX_INPUT_SIZE USHRT_MAX #define NUM_OF_LETTERS 36 #define DASH_PAUSE_LENGTH 1000000 #define DOT_PAUSE_LENGTH 500000 - -static char* CHRISTMAS_PHRASES[] = {"Merry Christmas", "Happy Haunika", "Happy Holidays!", "Jingle bells! Jingle bells!"}; - static const char LOWERCASE_LETTERS[NUM_OF_LETTERS] = {'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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; static const char UPPERCASE_LETTERS[NUM_OF_LETTERS] = {'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'}; static const char MORSE[NUM_OF_LETTERS][6] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"}; -void print_single_morse(char charToMorse){ - int sizeOfMorseCode; +void print_single_morse(char charToMorse, bool isSlow, bool addLetter){ + int sizeOfMorseCode; // Loop through every character in the list of letters for (int j = 0; j < NUM_OF_LETTERS; j++){ @@ -25,44 +24,58 @@ void print_single_morse(char charToMorse){ if (charToMorse == LOWERCASE_LETTERS[j] || charToMorse == UPPERCASE_LETTERS[j]){ sizeOfMorseCode = strlen(MORSE[j]); // Print chracter in ascii in brackets before the morse code - printf("(%c)", charToMorse, sizeOfMorseCode); - // Loop through each character in the morse code for that chracter - for (int k = 0; k < sizeOfMorseCode; k++){ - printf("%c", MORSE[j][k]); - // Force flush buffer so it is garunteed to be written to, with the delays in between - fflush(stdout); - // If the morse character is a dash - if (MORSE[j][k] == '-'){ - // ******* TODO: Replace with Arduino light code - // Long delay code - usleep(DASH_PAUSE_LENGTH); - } else { - // ******* TODO: Replace with Arduino light code - // Short delay code - usleep(DOT_PAUSE_LENGTH); + if (addLetter){ + printf("(%c)", charToMorse, sizeOfMorseCode); + } + // if speed restraints are being added for effect + if (isSlow){ + // Loop through each character in the morse code for that chracter + for (int k = 0; k < sizeOfMorseCode; k++){ + printf("%c", MORSE[j][k]); + // Force flush buffer so it is garunteed to be written to, with the delays in between + fflush(stdout); + // If the morse character is a dash + if (MORSE[j][k] == '-'){ + // ******* TODO: Replace with Arduino light code + // Long delay code + usleep(DASH_PAUSE_LENGTH); + } else { + // ******* TODO: Replace with Arduino light code + // Short delay code + usleep(DOT_PAUSE_LENGTH); + } } + printf(" "); + // If not slow + } else { + printf("%s ", MORSE[j]); } - printf(" "); } } } -void print_morse(char* strToMorse){ +void print_morse(char* strToMorse, bool isSlow, bool addLetter){ int sizeOfInputString; sizeOfInputString = strlen(strToMorse); // Loop through every character in the input string for (int i = 0; i < sizeOfInputString; i++){ - print_single_morse(strToMorse[i]); + print_single_morse(strToMorse[i], isSlow, addLetter); } } int main(int argc, char *argv[]){ - if (argc > 1){ - if (strcmp(argv[1], "--interactive") == 0 || strcmp(argv[1], "-i") == 0){ + + bool addLetterBeforeMorse = false; + bool isSlow = false; + + char inputString[MAX_INPUT_SIZE]; + + for (int argi = 0; argi < argc; argi++){ + char* arg = argv[argi]; + if (strcmp(arg, "--interactive") == 0 || strcmp(arg, "-i") == 0){ printf("Entering interactive mode!\n"); int sizeOfInputString; - char inputString[MAX_STRING_LENGTH]; printf("Enter a string: "); scanf("%[^\n]s", inputString); sizeOfInputString = strlen(inputString); @@ -70,16 +83,18 @@ int main(int argc, char *argv[]){ printf("\"%s\" has length of %d\n", inputString, sizeOfInputString); printf("\n"); - print_morse(inputString); + print_morse(inputString, isSlow, addLetterBeforeMorse); printf("\n"); - } - // If not interactive mode - } else { - for (int i = 0; i < 4; i++){ - print_morse(CHRISTMAS_PHRASES[i]); - printf("\n\n"); + } else if (strcmp(arg, "--slow") == 0 || strcmp(arg, "-s") == 0){ + isSlow = true; + } else if (strcmp(arg, "--verbose") == 0 || strcmp(arg, "-v") == 0){ + addLetterBeforeMorse = true; } } + scanf("%[^\n]s", inputString); + print_morse(inputString, isSlow, addLetterBeforeMorse); + printf("\n"); + return 0; }