Sunday 17 July 2016

"Hello World!"....in Morse Code!



My forey into electronics! It's a simple setup, just some blinking lights. Really simple C code. This video was a bunch of colored lights blinking, but I was also playing around with getting the blinking lights to blink in Morse Code. It's not that hard, but getting the lights to blink once in C is like 5 lines of code or so. Then you have Morse, which is like 3 or 4 blink patterns per letter. Rewriting all that every time I want to test different phrases is tedious with so I wrote out an array in C of the Morse Code, and a Python file that will convert text I type in into something for Arduino.

----------------------------
import string, os
from os import path, listdir

class MorseCode():

    def __init__(self):
        self.MorseCodex = {'a':'alphaA ();', 'b':'alphaB ();', 'c':'alphaC ();', 'd':'alphaD ();', 'e':'alphaE ();', 
                 'f':'alphaF ();', 'g':'alphaG ();', 'h':'alphaH ();', 'i':'alphaI ();', 'j':'alphaJ ();',
                 'k':'alphaK ();', 'l':'alphaL ();', 'm':'alphaM ();', 'n':'alphaN ();', 'o':'alphaO ();',
                 'p':'alphaP ();', 'q':'alphaQ ();', 'r':'alphaR ();', 's':'alphaS ();', 't':'alphaT ();',
                 'u':'alphaU ();', 'v':'alphaV ();', 'w':'alphaW ();', 'x':'alphaX ();', 'y':'alphaY ();',
                 'z':'alphaZ ();', '0':'Null ();', '1':'Eins ();', '2':'Zwei ();', '3':'Drei ();', '4':'Vier ();',
                 '5':'Funf ();', '6':'Sechs ();', '7':'Sieben ();', '8':'Acht ();', '9':'Neun ();', ' ':'wordSpace ();', 
                 '.':'sentenceSpace ();'}

    def returnMorseCodex(self):
        return self.MorseCodex
 

class DirectoryInput():
    def __init__(self):
        self.dirInput='C:\\Users\\HP-OK\\Desktop\\MorseCodeConverterText'

    def formatDirPath(self):
        workDir=self.dirInput
        fLst=os.listdir(workDir)
        return fLst

class UserEntry():

    def __init__(self):
        self.engText = raw_input("Enter Your Text: ")

    def formatToList(self):
        lowerAllText = self.engText.lower()
        charList = list(lowerAllText)
        return charList

    #def tst(self):
        #self.testing = MorseCode()
        #return self.testing.returnMorseCodex()

class Converter():

    def __init__(self):
        userInput = UserEntry()
        dirPathInput = DirectoryInput()
        morseCodeLib = MorseCode()
        self.letterList = userInput.formatToList()
        self.dirPath = dirPathInput.formatDirPath()
        self.morseLib = morseCodeLib.returnMorseCodex()
        
    def convertEngToMorse(self):
        morseList = list()
        lineBreak = '\nletterSpace ();\n'

        for item in self.letterList:
            for key, val in self.morseLib.iteritems():
                if item == key:
                    morseList.append(val)

        convertedText = lineBreak.join(morseList)

        bseTemplate = 'void loop() {\n\nplaceholder\nletterSpace ();\n\n}'

        if 'placeholder' in bseTemplate:
            self.newTemplate = bseTemplate.replace('placeholder', convertedText)

        for fname in self.dirPath:
            f_open=open(fname, 'w+')
            f_open.write(self.newTemplate)
            f_open.close()


myString = Converter()

myString.convertEngToMorse()

    
----------------------------


No comments:

Post a Comment