What's the best way to store Morse code sequences in C? - Embedded.com

What’s the best way to store Morse code sequences in C?

OK, before we start, let's remind ourselves that I'm a hardware designer by trade. When it comes to software in general — and C in particular — I know only enough to be dangerous.

I have a project on the go at the moment. This is a secret squirrel project whose purpose — for the present — must remain a riddle, wrapped in a mystery, inside an enigma. Suffice it to say that I wish to use a light-emitting diode LED to flash messages in Morse code.

My question pertains to the best way in which to represent, store, and access the Morse code dots and dashes used to represent the numbers and letters.

Ultimately, I want to be able to initialize a string with a message I wish to transmit using a statement like:

  char my_string = "HELLO WORLD";

Later, I'll pass this string as a parameter into the function that will actually flash the LED. So, once again, my question is: “What's the best way to represent the dots and dashes associated with the various characters?” My very first pass thought was to store these as character strings themselves — something like the following:

  char string_A = ".-";
  char string_B = "-...";
  char string_C = "-.-.";
   :

I know that this technique of using a separate character for each dot and dash doesn’t give the smallest memory footprint, but it does have the advantage of understandability, which is, of course, an important consideration. On the other hand, the scheme shown above — having individual strings for each of the character — would be difficult to use.

The bottom line is that every approach I can think of is less than optimal or incongruous, which is why I'm inviting your ideas. If you think of anything — preferably pertaining to this perplexing poser — please post it as a comment below and/or email me at max.maxfield@ubm.com.

23 thoughts on “What’s the best way to store Morse code sequences in C?

  1. “You want to use a binary decision tree where each node has a character and a link to the left for dot and one to the right for dash.nnSo the root of this tree would be:nnNULL, 'E', 'T'nnE would be 'E', 'I', 'A'nT would be 'T', 'N', 'M'nnand so on

    Log in to Reply
  2. “Sorry. What I said is good for receiving.nnWhat I have done fore transmitting is using two values per character:nOne is a length and the other is a binary pattern with 0 = dot, 1 = dash. The dots and dashes are stores so that lsb is the first bit trans

    Log in to Reply
  3. “Oooh … That really is nice encoding — both to minimize memory usage and (via putting the first dot/dash in the LSB) make it easy to use.”

    Log in to Reply
  4. “One problem arises if I decide I want to include punctuation — both a period and a comma require six dots/dashes. But there is a way around this if we use the three MS bits as follows:nn000 xxxxx zero data bitsn001 xxxxx one data bitn010 xxxxx two d

    Log in to Reply
  5. “I have had good success using 2-bits per dit/dah. A dit=01 (or, “1”), and a dah=11 (or, “3”). The largest morse character is period, with 6 symbols, so you need 12 bits. Because all entries are 2-bytes, you can use an uint16_t array. With only 40

    Log in to Reply
  6. “Yup, adding punctuation just needs a small adjustment to the idea since that needs 6 bits of data.nn11 xxxxxx would cover that well, just costing one more if statement.nnIn the program I wrote, I then used a state machine which would then walk through

    Log in to Reply
  7. “Some other folks suggested this 2-bit encoding scheme — but did you see the 1-bit per dot/dash scheme suggested in the comments below?”

    Log in to Reply
  8. “What you save in character encoding (about 40 bytes) will be swallowed by the more complex software required for decoding. By using the MSB's as a “length”, you are also required to have a temporary bit counter, requiring RAM space that is persistent d

    Log in to Reply
  9. “Good point — I'm going to try a couple of schemes and then compare the memory and processing times — watch out for my next blog…”

    Log in to Reply
  10. “I don't want to nitpick, but could the author please correct the declarations of the strings.nI guess you want for example “char *myString = “HELLO WORLD”;” or “char myString[] = “HELLO WORLD;”.nnThose need to be corrected:nnchar my_string =

    Log in to Reply
  11. “I'm not sure what you are saying. Let's start at the beginning where you say: I guess you want for example “char *myString = “HELLO WORLD”;” or “char myString[] = “HELLO WORLD;”.nnI don't actually do this. In my main loop I call my displayThis()

    Log in to Reply
  12. “You completely misunderstood my post. Read it again, please. I wrote:nStart of quote:nnThose need to be corrected: nnchar my_string = “HELLO WORLD”; nchar string_A = “.-“; nchar string_B = “-…”; nchar string_C = “-.-.”; nnEnd of quo

    Log in to Reply
  13. “Oh, and one more thing. nQuoting you:n”My understanding is that “char txtString[]” and “char *txtString” are equivalent”nnThey are not. The difference here is that:nnchar *s = “Hello world”;nnwill place Hello world in the read-only parts o

    Log in to Reply
  14. “Yes you are correct.nnchar x = “hello” only makes a storage space for a single character. I expect in this case it will assign x with the LCB of the pointer. But that is clearly a bug.nnchar *x = “hello”; As you say, that puts hello in storage som

    Log in to Reply
  15. “The longest morse 'signal' has 8 dots. It stands for 'error', previous word will be repeated. At least a morse decoder software must be able to handle this. And an encoder software too, if it is used for training purposes. I've done morse encoding and dec

    Log in to Reply
  16. “Eeek — it's my bad — for some reason when I saw your first comment I thought you were talking about my follow-up blog http://ubm.io/1fLyimr — that's why I was waffling on about my displayThis() function and stuff — I only now realized you were talk

    Log in to Reply
  17. “http://forum.4programmers.net/C_i_C++/120890-Dekoder_Morsea?p=431848#id431848nnIn coder dash and dots generated in reverse ordernmgr.Dobrovolski vel Xitami”

    Log in to Reply
  18. “@dl2iac:nnSurely the longest is SOS at 9 elements.nnSOS it is sent as a single burst of 9 elements (…—…). It is not sent as three distinct characters S,O,S(… pause — pause …).nnAnyway, I think it is a valid approach to have one mechanis

    Log in to Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.