MORSE CODE PROGRAM

SHOUT OUT TO SHAWN MEAS FOR ALLOWING ME TO USE HIS MORSE.C PROGRAM AND REFERENCING HIS MORSE CODE PROGRAM

The following is the necessary code to run the morse code program:

morse.c

  1 #include "morse.h"
  2 
  3 void init_morse()
  4 {
  5     morse[0][0] = '0';
  6     morse[0][1] = '1';
  7     morse[0][2] = '2';
  8     morse[0][3] = '2';
  9     morse[0][4] = '2';
 10     morse[0][5] = '2';
 11 
 12     morse[1][0] = '1';
 13     morse[1][1] = '0';
 14     morse[1][2] = '0';
 15     morse[1][3] = '0';
 16     morse[1][4] = '2';
 17     morse[1][5] = '2';
 18 
 19     morse[2][0] = '1';
 20     morse[2][1] = '0';
 21     morse[2][2] = '1';
 22     morse[2][3] = '0';
 23     morse[2][4] = '2';
 24     morse[2][5] = '2';
 25 
 26     morse[3][0] = '1';
 27     morse[3][1] = '0';
 28     morse[3][2] = '0';
 29     morse[3][3] = '2';
 30     morse[3][4] = '2';
 31     morse[3][5] = '2';
 32 
 33     morse[4][0] = '0';
 34     morse[4][1] = '2';
 35     morse[4][2] = '2';
 36     morse[4][3] = '2';
 37     morse[4][4] = '2';
 38     morse[4][5] = '2';
 39 
 40     morse[5][0] = '0';
 41     morse[5][1] = '0';
 42     morse[5][2] = '1';
 43     morse[5][3] = '0';
 44     morse[5][4] = '2';
 45     morse[5][5] = '2';
 46 
 47     morse[6][0] = '1';
 48     morse[6][1] = '1';
 49     morse[6][2] = '0';
 50     morse[6][3] = '2';
 51     morse[6][4] = '2';
 52     morse[6][5] = '2';
 53 
 54     morse[7][0] = '0';
 55     morse[7][1] = '0';
 56     morse[7][2] = '0';
 57     morse[7][3] = '0';
 58     morse[7][4] = '2';
 59     morse[7][5] = '2';
 60 
 61     morse[8][0] = '0';
 62     morse[8][1] = '0';
 63     morse[8][2] = '2';
 64     morse[8][3] = '2';
 65     morse[8][4] = '2';
 66     morse[8][5] = '2';
 67 
 68     morse[9][0] = '0';
 69     morse[9][1] = '1';
 70     morse[9][2] = '1';
 71     morse[9][3] = '1';
 72     morse[9][4] = '2';
 73     morse[9][5] = '2';
 74 
 75     morse[10][0] = '1';
 76     morse[10][1] = '0';
 77     morse[10][2] = '1';
 78     morse[10][3] = '2';
 79     morse[10][4] = '2';
 80     morse[10][5] = '2';
 81 
 82     morse[11][0] = '0';
 83     morse[11][1] = '1';
 84     morse[11][2] = '0';
 85     morse[11][3] = '0';
 86     morse[11][4] = '2';
 87     morse[11][5] = '2';
 88 
 89     morse[12][0] = '1';
 90     morse[12][1] = '1';
 91     morse[12][2] = '2';
 92     morse[12][3] = '2';
 93     morse[12][4] = '2';
 94     morse[12][5] = '2';
 95 
 96     morse[13][0] = '1';
 97     morse[13][1] = '0';
 98     morse[13][2] = '2';
 99     morse[13][3] = '2';
100     morse[13][4] = '2';
101     morse[13][5] = '2';
102 
103     morse[14][0] = '1';
104     morse[14][1] = '1';
105     morse[14][2] = '1';
106     morse[14][3] = '2';
107     morse[14][4] = '2';
108     morse[14][5] = '2';
109 
110     morse[15][0] = '0';
111     morse[15][1] = '1';
112     morse[15][2] = '1';
113     morse[15][3] = '0';
114     morse[15][4] = '2';
115     morse[15][5] = '2';
116 
117     morse[16][0] = '1';
118     morse[16][1] = '1';
119     morse[16][2] = '0';
120     morse[16][3] = '1';
121     morse[16][4] = '2';
122     morse[16][5] = '2';
123 
124     morse[17][0] = '0';
125     morse[17][1] = '1';
126     morse[17][2] = '0';
127     morse[17][3] = '2';
128     morse[17][4] = '2';
129     morse[17][5] = '2';
130 
131     morse[18][0] = '0';
132     morse[18][1] = '0';
133     morse[18][2] = '0';
134     morse[18][3] = '2';
135     morse[18][4] = '2';
136     morse[18][5] = '2';
137 
138     morse[19][0] = '1';
139     morse[19][1] = '2';
140     morse[19][2] = '2';
141     morse[19][3] = '2';
142     morse[19][4] = '2';
143     morse[19][5] = '2';
144 
145     morse[20][0] = '0';
146     morse[20][1] = '0';
147     morse[20][2] = '1';
148     morse[20][3] = '2';
149     morse[20][4] = '2';
150     morse[20][5] = '2';
151 
152     morse[21][0] = '0';
153     morse[21][1] = '0';
154     morse[21][2] = '0';
155     morse[21][3] = '1';
156     morse[21][4] = '2';
157     morse[21][5] = '2';
158 
159     morse[22][0] = '0';
160     morse[22][1] = '1';
161     morse[22][2] = '1';
162     morse[22][3] = '2';
163     morse[22][4] = '2';
164     morse[22][5] = '2';
165 
166     morse[23][0] = '1';
167     morse[23][1] = '0';
168     morse[23][2] = '0';
169     morse[23][3] = '1';
170     morse[23][4] = '2';
171     morse[23][5] = '2';
172 
173     morse[24][0] = '1';
174     morse[24][1] = '0';
175     morse[24][2] = '1';
176     morse[24][3] = '1';
177     morse[24][4] = '2';
178     morse[24][5] = '2';
179 
180     morse[25][0] = '1';
181     morse[25][1] = '1';
182     morse[25][2] = '0';
183     morse[25][3] = '0';
184     morse[25][4] = '2';
185     morse[25][5] = '2';
186 
187     morse[26][0] = '1';
188     morse[26][1] = '1';
189     morse[26][2] = '1';
190     morse[26][3] = '1';
191     morse[26][4] = '1';
192     morse[26][5] = '2';
193 
194     morse[27][0] = '0';
195     morse[27][1] = '1';
196     morse[27][2] = '1';
197     morse[27][3] = '1';
198     morse[27][4] = '1';
199     morse[27][5] = '2';
200 
201     morse[28][0] = '0';
202     morse[28][1] = '0';
203     morse[28][2] = '1';
204     morse[28][3] = '1';
205     morse[28][4] = '1';
206     morse[28][5] = '2';
207 
208     morse[29][0] = '0';
209     morse[29][1] = '0';
210     morse[29][2] = '0';
211     morse[29][3] = '1';
212     morse[29][4] = '1';
213     morse[29][5] = '2';
214 
215     morse[30][0] = '0';
216     morse[30][1] = '0';
217     morse[30][2] = '0';
218     morse[30][3] = '0';
219     morse[30][4] = '1';
220     morse[30][5] = '2';
221 
222     morse[31][0] = '0';
223     morse[31][1] = '0';
224     morse[31][2] = '0';
225     morse[31][3] = '0';
226     morse[31][4] = '0';
227     morse[31][5] = '2';
228 
229     morse[32][0] = '1';
230     morse[32][1] = '0';
231     morse[32][2] = '0';
232     morse[32][3] = '0';
233     morse[32][4] = '0';
234     morse[32][5] = '2';
235 
236     morse[33][0] = '1';
237     morse[33][1] = '1';
238     morse[33][2] = '0';
239     morse[33][3] = '0';
240     morse[33][4] = '0';
241     morse[33][5] = '2';
242 
243     morse[34][0] = '1';
244     morse[34][1] = '1';
245     morse[34][2] = '1';
246     morse[34][3] = '0';
247     morse[34][4] = '0';
248     morse[34][5] = '2';
249 
250     morse[35][0] = '1';
251     morse[35][1] = '1';
252     morse[35][2] = '1';
253     morse[35][3] = '1';
254     morse[35][4] = '0';
255     morse[35][5] = '2';
256 
257 }

moco.c

  1 #include "rpi.h"
  2 #include "morse.h"
  3 #include <string.h>
  4 
  5 #define MAX_STRING_LEN 1000
  6 
  7 int main()
  8 {
  9     if( map_peripheral(&gpio) == -1 )
 10     {
 11         printf("Failed to map the physical GPIO registers into the virtual memory space.\n");
 12         return -1;
 13     }
 14 
 15     int i, j, k;    // iteration variables for cases 1 and 2
 16     int menu;           // switch option variable 
 17     char sendchar;      // input character variable
 18     char sendstr[MAX_STRING_LEN];   // input string variable
 19     int sendcharvalue;  // sent character value variable
 20     int sendstrlength;  // sent string length variable
 21 
 22     init_morse();
 23 
 24     do {
 25         printf("Make a selection:\n");
 26         printf("1: Character\n");
 27         printf("2: String\n");
 28         printf("0: Quit\n\n");
 29          scanf("%d", &menu);
 30 
 31         switch( menu )
 32         {
 33             case 1:
 34                 printf("Enter character: ");
 35                 scanf(" %c", &sendchar);
 36 
 37                 printf("\n\nASCII value: %d\nCharacter: %c\n\n", sendchar, sendchar);
 38 
 39                 if( sendchar >= 97 )
 40                     sendcharvalue = sendchar - 97;
 41                 else if( sendchar >= 48 || sendchar <= 57 )
 42                     sendcharvalue = sendchar - 22;
 43                 else
 44                     sendcharvalue = sendchar - 65;
 45 
 46                 for( j = 0; j < 6; j++ )
 47                 {
 48                     if( morse[sendcharvalue][j] != '2' )
 49                         printf("%c", morse[sendcharvalue][j]);
 50                 }
 51 
 52                 printf("\n");
 53                 break;
 54 
 55             case 2:
 56                 getchar();
 57 
 58                 printf("Enter String: ");
 59 
 60                 fgets(sendstr, sizeof(sendstr), stdin); // accepting the string and assigning it to the sendstr array
 61 
 62                 sendstrlength = strlen(sendstr); // finding and assigning the string length to a variable for later use
 63                 printf("\n\nsendstrlength is: %d\n\n", sendstrlength - 1); // showing string length
 64                 sendstr[sendstrlength] = '\0';
 65 
 66                 printf("String:\t%s\n\n", sendstr);
 67 
 68                 for( k = 0; k < sendstrlength; k++ ) // runs throughout the entire string
 69                 {
 70                     sendchar = sendstr[k]; // singles out characters to print them out as morse code
 71                     if( sendchar != ' ' ) // runs through if statement if not a space, else outputs a 'break' (line 91)
 72                     {
 73                         if( sendchar >= 97 ) // used to properly access data in the morse.c file, which holds the morse values
 74                             sendcharvalue = sendchar - 97;
 75                         else if( sendchar >= 48 || sendchar <= 57 ) // accounts for numbers 0 - 9
 76                             sendcharvalue = sendchar - 22;
 77                         else
 78                             sendcharvalue = sendchar - 65; // an attempt at accounting for capital letters
 79 
 80                         for( i = 0; i < 6; i++ ) // loop which prints morse code, stops printing when value in morse array is not '0' or '1'
 81                         {
 82                             if( morse[sendcharvalue][i] == '0' )
 83                             {
 84                                 printf("0");
 85                             }
 86                             else if( morse[sendcharvalue][i] == '1' )
 87                             {
 88                                 printf("1");
 89                             }
 90                             else
 91                                 break;
 92                         }
 93                         printf(" ");
 94                     }
 95                     else
 96                     {
 97                         printf("break ");
 98                     }
 99                 }
100                 printf("\n");
101                 break;
102  
103         }
104     }while( menu != 0 );
105 
106     return 0;
107 }

The output:

pi@raspberrypi ~/RPI/morse $ sudo ./moco
Make a selection:
1: Character
2: String
0: Quit

2
Enter String: thing me to say tweet


sendstrlength is: 21

String:	thing me to say tweet


1 0000 00 10 110 break 11 0 break 1 111 break 000 01 1011 break 1 011 0 0 1  
Make a selection:
1: Character
2: String
0: Quit

SHOUT OUT TO SHAWN MEAS FOR ALLOWING ME TO USE HIS MORSE.C PROGRAM AND REFERENCING HIS MORSE CODE PROGRAM