From 0b59dc20ad89c3f0ff7938d03c77de5d10bdc2c5 Mon Sep 17 00:00:00 2001 From: Tait Hoyem <44244401+TTWNO@users.noreply.github.com> Date: Mon, 10 Jun 2019 14:35:57 +0000 Subject: [PATCH] Add Makefile and write main code --- Makefile | 4 ++++ main.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 Makefile create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bb98320 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +defualt: all + +all: + gcc -O2 main.c -o main.out diff --git a/main.c b/main.c new file mode 100644 index 0000000..124efb7 --- /dev/null +++ b/main.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include + +#define MAX_STRING_LENGTH 1024 +#define NUM_OF_LETTERS 36 +#define DASH_PAUSE_LENGTH 300000 +#define DOT_PAUSE_LENGTH 100000 + +int main(){ + + static const char 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 MORSE[NUM_OF_LETTERS][6] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"}; + + char rawInputString[MAX_STRING_LENGTH]; + char inputString[MAX_STRING_LENGTH]; + int sizeOfInputString, sizeOfMorseCode; + + printf("Enter a string: "); + scanf("%[^\n]s", &rawInputString); + sizeOfInputString = strlen(rawInputString); + strcpy(inputString, rawInputString); + // change data in copied string if it is uppercase + for (int i = 0; i < sizeOfInputString; i++){ + if (isupper(rawInputString[i])){ + inputString[i] = tolower(rawInputString[i]); + } + } + + printf("\"%s\" has length of %d\n", inputString, sizeOfInputString); + // Loop through every character in the input string + for (int i = 0; i < sizeOfInputString; i++){ + // Loop through every character in the list of letters + for (int j = 0; j < NUM_OF_LETTERS; j++){ + // If the character in the input string matches the character the character from the list + if (inputString[i] == LETTERS[j]){ + sizeOfMorseCode = strlen(MORSE[j]); + // Print chracter in ascii in brackets before the morse code + printf("(%c)", inputString[i], 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); + } + } + printf(" "); + } + } + } + printf("\n"); + return 0; +} +