using System.Collections;
namespace FEZMiniLab.BlinkingLed
{
/// <summary>
/// Enumerazione dei valori Morse.
/// </summary>
enum MorseValue
{
dot,
line
}
/// <summary>
/// Gestisce la conversione dei messaggi da testo alfanumerico a codice morse
/// </summary>
class MorseTable
{
private static MorseValue DOT = MorseValue.dot;
private static MorseValue LINE = MorseValue.line;
private Hashtable alfaNumericDictionary;
public MorseTable()
{
alfaNumericDictionary = new Hashtable();
alfaNumericDictionary.Add("A", new MorseValue[] { DOT, LINE });
alfaNumericDictionary.Add("B", new MorseValue[] { LINE, DOT, DOT, DOT });
alfaNumericDictionary.Add("C", new MorseValue[] { LINE, DOT, LINE, DOT });
alfaNumericDictionary.Add("D", new MorseValue[] { LINE, DOT, DOT });
alfaNumericDictionary.Add("E", new MorseValue[] { DOT });
alfaNumericDictionary.Add("F", new MorseValue[] { DOT, DOT, LINE, DOT });
alfaNumericDictionary.Add("G", new MorseValue[] { LINE, LINE, DOT });
alfaNumericDictionary.Add("H", new MorseValue[] { DOT, DOT, DOT, DOT });
alfaNumericDictionary.Add("I", new MorseValue[] { DOT, DOT });
alfaNumericDictionary.Add("J", new MorseValue[] { DOT, LINE, LINE, LINE });
alfaNumericDictionary.Add("K", new MorseValue[] { LINE, DOT, LINE });
alfaNumericDictionary.Add("L", new MorseValue[] { DOT, LINE, DOT, DOT });
alfaNumericDictionary.Add("M", new MorseValue[] { LINE, LINE });
alfaNumericDictionary.Add("N", new MorseValue[] { LINE, DOT });
alfaNumericDictionary.Add("O", new MorseValue[] { LINE, LINE, LINE });
alfaNumericDictionary.Add("Q", new MorseValue[] { DOT, LINE, LINE, DOT });
alfaNumericDictionary.Add("R", new MorseValue[] { DOT, LINE, DOT });
alfaNumericDictionary.Add("S", new MorseValue[] { DOT, DOT, DOT });
alfaNumericDictionary.Add("T", new MorseValue[] { LINE });
alfaNumericDictionary.Add("U", new MorseValue[] { DOT, DOT, LINE });
alfaNumericDictionary.Add("V", new MorseValue[] { DOT, DOT, DOT, LINE });
alfaNumericDictionary.Add("W", new MorseValue[] { DOT, LINE, LINE });
alfaNumericDictionary.Add("X", new MorseValue[] { LINE, DOT, DOT, LINE });
alfaNumericDictionary.Add("Y", new MorseValue[] { LINE, DOT, LINE, LINE });
alfaNumericDictionary.Add("Z", new MorseValue[] { LINE, LINE, DOT, DOT });
alfaNumericDictionary.Add("0", new MorseValue[] { LINE, LINE, LINE, LINE, LINE });
alfaNumericDictionary.Add("1", new MorseValue[] { DOT, LINE, LINE, LINE, LINE });
alfaNumericDictionary.Add("2", new MorseValue[] { DOT, DOT, LINE, LINE, LINE });
alfaNumericDictionary.Add("3", new MorseValue[] { DOT, DOT, DOT, LINE, LINE });
alfaNumericDictionary.Add("4", new MorseValue[] { DOT, DOT, DOT, DOT, LINE });
alfaNumericDictionary.Add("5", new MorseValue[] { DOT, DOT, DOT, DOT, DOT });
alfaNumericDictionary.Add("6", new MorseValue[] { LINE, DOT, DOT, DOT, DOT });
alfaNumericDictionary.Add("7", new MorseValue[] { LINE, LINE, DOT, DOT, DOT });
alfaNumericDictionary.Add("8", new MorseValue[] { LINE, LINE, LINE, DOT, DOT });
alfaNumericDictionary.Add("9", new MorseValue[] { LINE, LINE, LINE, LINE, DOT });
alfaNumericDictionary.Add(".", new MorseValue[] { DOT, LINE, DOT, LINE, DOT, LINE });
alfaNumericDictionary.Add(",", new MorseValue[] { LINE, LINE, DOT, DOT, LINE, LINE });
alfaNumericDictionary.Add(":", new MorseValue[] { LINE, LINE, LINE, DOT, DOT, DOT });
alfaNumericDictionary.Add("?", new MorseValue[] { DOT, DOT, LINE, LINE, DOT, DOT });
alfaNumericDictionary.Add("=", new MorseValue[] { LINE, DOT, DOT, DOT, LINE });
alfaNumericDictionary.Add("-", new MorseValue[] { LINE, DOT, DOT, DOT, DOT, LINE });
alfaNumericDictionary.Add("(", new MorseValue[] { LINE, DOT,LINE, LINE, DOT });
alfaNumericDictionary.Add(")", new MorseValue[] { LINE, DOT, LINE, LINE, DOT, LINE});
alfaNumericDictionary.Add("\"", new MorseValue[] { DOT, LINE, DOT, DOT, LINE, DOT });
alfaNumericDictionary.Add("'", new MorseValue[] { DOT, LINE, LINE, LINE, LINE, DOT });
alfaNumericDictionary.Add("/", new MorseValue[] { LINE, DOT, DOT, LINE, DOT });
alfaNumericDictionary.Add("_", new MorseValue[] { DOT, DOT, LINE, LINE, DOT, LINE });
alfaNumericDictionary.Add("@", new MorseValue[] { DOT, LINE, LINE, DOT, LINE, DOT });
}
public bool[] Encode(string msg)
{
ArrayList signalStream = new ArrayList();
MorseValue[] morseValues;
string lastLiteral = "";
string literal;
foreach(char c in msg)
{
literal = c.ToString().ToUpper();
if (literal == " ")
{
// Codifica lo spazio come un wordSpace solo se il precedente carattere
// non era uno spazio (in pratica elimina gli spazi ripetuti)
if (lastLiteral != " ")
{
// Aggiungi un wordSpaceSignal (una sequenza di 7 "off" signal)
signalStream.Add(false);
signalStream.Add(false);
signalStream.Add(false);
signalStream.Add(false);
signalStream.Add(false);
signalStream.Add(false);
signalStream.Add(false);
}
}
else
{
if ((lastLiteral != " ") && (lastLiteral.Length != 0))
{
// Aggiungi un charSpaceSignal (una sequenza di 3 "off" signal)
signalStream.Add(false);
signalStream.Add(false);
signalStream.Add(false);
}
// Da notare che nel Micro Framework (almeno fino alla versione 4.1) non ci sono i Generics
// quindi bisogna usare il cast esplicito al tipo MorseValue[]
// che è il tipo del valore della coppia <chiave, valore> della hash table.
morseValues = (MorseValue[])alfaNumericDictionary[literal];
bool firstValue = true;
foreach(MorseValue morseValue in morseValues)
{
if (!firstValue)
{
// Aggiungi un valueSpaceSignal (1 "off" signal)
signalStream.Add(false);
}
switch (morseValue)
{
case MorseValue.dot:
// Aggiungi un dotSignal (1 "on" signal)
signalStream.Add(true);
break;
case MorseValue.line:
// Aggiungi un dotSignal (una sequenza di 3 "on" signal)
signalStream.Add(true);
signalStream.Add(true);
signalStream.Add(true);
break;
default:
break;
}
firstValue = false;
}
}
lastLiteral = literal;
}
// Spenge il segnale
signalStream.Add(false);
// Ritorna un array di bool
return (bool[]) signalStream.ToArray(typeof(bool));
}
}
}