summaryrefslogtreecommitdiff
path: root/morse.cpp
blob: f54800f770441b7eeb0179160539085f8bfb1bfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <led.h>
#include <iostream>
#include <string>
#include <map>
#include <ctime>
#include <cctype>
#include <unistd.h>
#include <cstring>
using namespace std;
void dot()
{
  cout << "." << flush;
  setLight(true);
  usleep(250000);
  setLight(false);
}
void dash()
{
  cout << "-" << flush;
  setLight(true);
  usleep(750000);
  setLight(false);
}
static map<string, string> table;
void transmit(const string& str)
{
  cout << "Transmitting message in Morse code via ACT LED..." << endl;
  for(unsigned long long i=0;i<str.length();++i)
    {
      char ch=str[i];
      string tmp;
      tmp=toupper(ch);
      string morse=table[tmp];
      if(morse!="")
	cout << (char)toupper(ch) << ": " << flush;
      for(unsigned int j=0;j<morse.length();++j)
	{
	  char morseCh=morse[j];
	  if(morseCh=='.')
	    dot();
	  else if(morseCh=='-')
	    dash(); 
	  usleep(200000);
	}
      if(morse!="")
	cout << " " << flush;
      usleep(250000); // between chars
      if(isspace(ch))
	{
	  cout << " " << flush;
	  usleep(500000); // extra for spaces
	}
    }
}

void init_morse()
{
  table["A"]=".-";
  table["B"]="-...";
  table["C"]="-.-.";
  table["D"]="-..";
  table["E"]=".";
  table["F"]="..-.";
  table["G"]="--.";
  table["H"]="....";
  table["I"]="..";
  table["J"]=".---";
  table["K"]="-.-";
  table["L"]=".-..";
  table["M"]="--";
  table["N"]="-.";
  table["O"]="---";
  table["P"]=".--.";
  table["Q"]="--.-";
  table["R"]=".-.";
  table["S"]="...";
  table["T"]="-";
  table["U"]="..-";
  table["V"]="...-";
  table["W"]=".--";
  table["X"]="-..-";
  table["Y"]="-.--";
  table["Z"]="--..";
  table["0"]="-----";
  table["1"]=".----";
  table["2"]="..---";
  table["3"]="...--";
  table["4"]="....-";
  table["5"]=".....";
  table["6"]="-....";
  table["7"]="--...";
  table["8"]="---..";
  table["9"]="----.";
  table["."]=".-.-.-";
  table[","]="--..--";
  table["("]="-.--.";
  table[")"]="-.--.-";
  table["/"]="-..-.";
  table["="]="-...-";
  table["+"]=".-.-.";
  table["-"]="-....-";
  table["\""]=".-..-.";
  table["@"]=".--.-.";
}