OK, before we begin, 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
用户1406868 2015-10-23 14:36
用户3641644 2015-8-5 11:53
How about coding each letter as a series of bits, with a 1 meaning the LED is on, and a 0 meaning it is off. For instance,
A = 10111,
B = 111010101,
...
J = 1011101110111
...
I've shown J as an example because it has one of the longest Morse sequences, coming out at 13 bits by this method. This scheme would allow you to store the Morse equivalent for each letter in two bytes, with leading zeros that are ignored. This seems fairly economical to me.
Including numerals in this scheme would be more wasteful, requiring 3 bytes per character (e.g. 19 bits for "0", the longest), but as the Morse equivalent of numerals is more formulaic, you could just leave them out and have your LED flasher function work out their encoding on the fly.
Hope this helps!
John
用户3724585 2015-8-3 16:34
My suggestion is pretty basic... you would have an array of arrays (a jagged array).
The major index would represent a character. Since standard ASCII only allows for a maximum of 256 different 'characters', then you could have this many major indexes (or manipulate the ASCII code passed in to get a truncated index if memory is a big constraint).
Each element of the minor index would then be the length of time that the particular output should be turned on (as compared to the unit time).
This would allow you do code quite easily as
foreach (char c in inp_string)
foreach (int i in morse_codes[c])
turn_on_for( i );
turn_off_for( 1 );
turn_off_for( 3 );
The morse array for ' ' (space) should be { 0, 0, 0, 0 }, for 'a' should be { 1, 3 }, for 'b' it would be {3, 1, 1, 1} etc