Codice

mcd logic

1
2
3
4
5
/* filename mcd_general.h
 *    applied to all modules
 */
 
#define DBG   1
 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
/* filename mcd.ino (morse code device)
 */
 
#include "mcd_general.h"
#include "mcd_out.h"

#define FOREVER 1
void play(char m[]);

void setup() {
    #ifdef DBG
        Serial.begin(9600);
    #endif
    init_out();
}

void loop() {
    #ifdef DBG
        Serial.println("> loop()");
    #endif
    play("sos sos");
    
    //play_phrase("un mondo intero si e svelato");
//    delay(2000);
//    set_time_unit(120);
//    play_phrase("SOS");
    #ifdef DBG
        while(FOREVER);
    #endif
    
    delay(5000);
    
    #ifdef DBG
        Serial.println("< loop");
    #endif
}

void play(char m[]){
    #ifdef DBG 
        Serial.print("> play(");
        Serial.print(m);
        Serial.println(")");
    #endif
    play_phrase(m);
    delay(2000);
    set_farnsworth(true);
    play_phrase(m);
    #ifdef DBG 
        Serial.println("< play");
    #endif
}

mcd out

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
/* filename mcd_out.h (send phrase)
 */

void init_out(void);
void set_time_unit(int t);
void set_frequency(unsigned int f);
void set_farnsworth(bool mode);

int play_phrase(char p[]);
int play_char(char c);
int play_signal(int s, int i);
  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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*  filename mcd_out.c  (send phrase)
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <Arduino.h>

#include "mcd_general.h"
#include "mcd_out.h"
//#include "mcd_utils.h"


#define BUZZER 7    // buzzer on pin 7

#define L1  1       // intra character space time duration
#define L3  3       // inter characters space time duration
#define L7  7       // inter words space time duration

#define CODE_SZ 6    // max morse code size: 5 + 1 (\0)
#define FRNSWORTH  3 // Farnsworth expantion factor between chars and words


static unsigned int F = 330;      // a frequency (MI4) to play
/*
DO5 - C5 - 523 Hz
DO#5 - C#5 - 554 Hz
RE5 - D5 - 587 Hz
RE#5 - D#5 - 622 Hz
MI5 - E5 - 659 Hz
FA5 - F5 - 698 Hz
FA#5 - F#5 - 740 Hz
SOL5 - G5 - 784 Hz
SOL#5 - G#5 - 831 Hz
LA5 - A5 - 880 Hz
LA#5 - A#5 - 932 Hz
SI5 - B5 - 988 Hz 
 */

static int  Tu = 60;       // time unit length in milliseconds
static bool Farnsworth = false;

void init_out(){
    pinMode(BUZZER, OUTPUT);
}

void set_time_unit(int t){
    Tu = t;
}

void set_frequency(unsigned int f){
    F = f;
}


void set_farnsworth(bool mode){
    Farnsworth = mode;
}

struct node{
    char character;
    char code[CODE_SZ];
};

static struct node mt[] = {
    {'a', "*-"},
    {'b', "-***"},
    {'c', "-*-*"},
    {'d', "-**"},
    {'e', "*"},
    {'f', "**-*"},
    {'g', "--*"},
    {'h', "****"},
    {'i', "**"},
    {'j', "*---"},
    {'k', "-*-"},
    {'l', "*-**"},
    {'m', "--"},
    {'n', "-*"},
    {'o', "---"},
    {'p', "*--*"},
    {'q', "--*-"},
    {'r', "*-*"},
    {'s', "***"},
    {'t', "-"},
    {'u', "**-"},
    {'v', "***-"},
    {'w', "*--"},
    {'x', "-**-"},
    {'y', "-*--"},
    {'z', "--**"},
    {'1', "*----"},
    {'2', "**---"},
    {'3', "***--"},
    {'4', "****-"},
    {'5', "*****"},
    {'6', "-****"},
    {'7', "--***"},
    {'8', "---**"},
    {'9', "----*"},
    {'0', "-----"},
};

char * get_code(char c){
    int mt_len = sizeof(mt) / sizeof(node);
    static char code[CODE_SZ];
    #ifdef DBG
        Serial.print("> get_code(");
        Serial.print(c);
        Serial.println(")");
    #endif
    memset(code, '\0', CODE_SZ);
    for(int i=0; i<mt_len; i++){
        if(mt[i].character==c){
            strcpy(code, mt[i].code);
            break;
        }
    }
    #ifdef DBG
        Serial.print("< get_code, ret ");
        Serial.println(code);
    #endif
    return code;
}

/* int play_phrase(char phrase_to_send[])
 *
 * our state machine:
 *
 *  start <-------+-------------------------+------+
 *    |           |                         |      |
 *    +-- space --+                         |      |
 *    |                                     |      |
 *    +-- char --> send char                |      |
 *                    |                     |      |
 *                    +-- char --> push back char  |
 *                    |            send L3         |
 *                    |                            |
 *                    +-- space -> send L7 --------+
 *                    |                            |
 *                    +-- \0 ----------------------+
 */
int play_phrase(char p[]){
    int i = 0;
     #ifdef DBG
         Serial.print("> send_phrase(");
         Serial.print(p);
         Serial.println(")");
         // prt("> send_phrase(%s)\n", p); //to DEBUG
     #endif
    while(p[i] != '\0'){                        // start
        if(p[i]==' '){                           // got space: back to start
            i++;
            continue;
        }
        else{                                   // got char, send it
            play_char(p[i]);
            i++;
            if(p[i]!=' ' && p[i]!='\0'){        //    next: char; push back char (no needed); send L3; back to start
                play_signal(LOW, L3);
                #ifdef DBG
                    Serial.println();  // this is CR for play_signal debug
                #endif
                continue;
            }
            else if(p[i]==' '){                 //    next: space; send L7; back to start
                play_signal(LOW, L7);
                #ifdef DBG
                    Serial.println();  // this is CR for play_signal debug
                #endif
                continue;
            }
            else{
                #ifdef DBG
                    Serial.println();  // this is CR for play_signal debug
                #endif
                ;                               //     next: \0; back to start
            }
        }
    }
     #ifdef DBG
         Serial.println("< send_phrase: ret 0");
     #endif
    return 0;
}

/* int play_char(char char_to_send) */
int play_char(char c){
    char code[CODE_SZ];
    #ifdef DBG
        Serial.print("> play_char(");
        Serial.print(c);
        Serial.println(")");
    #endif
    c = tolower(c);
    strcpy(code, get_code(c));
//    #ifdef DBG
//        Serial.print("- play_char: code to send is ");
//        Serial.println(code);
//    #endif

    for(int i=0;code[i]!='\0';i++){
        if(code[i]=='*'){
            play_signal(HIGH, L1);
        }
        else{
            play_signal(HIGH, L3);
        }
        if(code[i+1]!='\0')
            play_signal(LOW, L1);
    }
    
    #ifdef DBG
        Serial.println();  // CR for play signal character debug
    #endif
    
    #ifdef DBG
        Serial.println("< play_char, ret 0");
    #endif
    return 0;
}

int play_signal(int s, int i){
    int t = Tu * i;
    if(s==HIGH){
        #ifdef DBG
            Serial.print("H");
            Serial.print(i);
            Serial.print(" ");
        #endif
        tone(BUZZER, (unsigned int) F);
        delay(t);
        noTone(BUZZER);
    }
    else{
        #ifdef DBG
            Serial.print("L");
            Serial.print(i);
            Serial.print(" ");
            
        #endif
        if(Farnsworth && i!=1){
            delay(t*FRNSWORTH);
        }
        else
            delay(t);
    }
    return 0;
}