2019-12-18 01:32:44 -05:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include "ubitx.h"
|
2020-01-04 02:11:55 -05:00
|
|
|
#include "settings.h"
|
2019-12-18 01:32:44 -05:00
|
|
|
#include "morse.h"
|
|
|
|
|
|
|
|
struct Morse {
|
|
|
|
char letter;
|
2020-01-04 02:11:55 -05:00
|
|
|
unsigned char code;
|
2019-12-18 01:32:44 -05:00
|
|
|
};
|
|
|
|
|
2020-01-17 02:36:58 -05:00
|
|
|
/*
|
|
|
|
* Each byte of the morse table stores one letter.
|
|
|
|
* The 0 is a dot, a 1 is a dash
|
|
|
|
* From the Most significant byte onwards, the letter is padded with 1s.
|
|
|
|
* The first zero after the 1s indicates the start of the letter, it MUST be discarded
|
|
|
|
*/
|
2019-12-18 01:32:44 -05:00
|
|
|
static const PROGMEM struct Morse morse_table[] = {
|
|
|
|
{'a', 0xf9}, // 11111001
|
|
|
|
{'b', 0xe8}, // 11101000
|
|
|
|
{'c', 0xea}, // 11101010
|
|
|
|
{'d', 0xf4}, // 11110100
|
|
|
|
{'e', 0xfc}, // 11111100
|
2020-01-07 00:50:21 -05:00
|
|
|
{'f', 0xe2}, // 11100010
|
2019-12-18 01:32:44 -05:00
|
|
|
{'g', 0xf6}, // 11110110
|
|
|
|
{'h', 0xe0}, // 11100000
|
|
|
|
{'i', 0xf8}, // 11111000
|
|
|
|
{'j', 0xe7}, // 11100111
|
|
|
|
{'k', 0xf6}, // 11110101
|
|
|
|
{'l', 0xe4}, // 11100100
|
|
|
|
{'m', 0xfb}, // 11111011
|
|
|
|
{'n', 0xfa}, // 11111010
|
|
|
|
{'o', 0xf7}, // 11110111
|
|
|
|
{'p', 0xe6}, // 11100110
|
|
|
|
{'q', 0xed}, // 11101101
|
|
|
|
{'r', 0xf2}, // 11110010
|
|
|
|
{'s', 0xf0}, // 11110000
|
|
|
|
{'t', 0xfd}, // 11111101
|
|
|
|
{'u', 0xf1}, // 11110001
|
|
|
|
{'v', 0xe1}, // 11100001
|
|
|
|
{'w', 0xf3}, // 11110011
|
|
|
|
{'x', 0xe9}, // 11101001
|
|
|
|
{'y', 0xe3}, // 11101011
|
|
|
|
{'z', 0xec}, // 11101100
|
|
|
|
{'1', 0xcf}, // 11001111
|
|
|
|
{'2', 0xc7}, // 11000111
|
|
|
|
{'3', 0xc3}, // 11000011
|
|
|
|
{'4', 0xc1}, // 11000001
|
|
|
|
{'5', 0xc0}, // 11000000
|
|
|
|
{'6', 0xd0}, // 11010000
|
|
|
|
{'7', 0xd8}, // 11011000
|
|
|
|
{'8', 0xdc}, // 11011100
|
|
|
|
{'9', 0xde}, // 11011110
|
|
|
|
{'0', 0xdf}, // 11011111
|
2020-01-17 02:36:58 -05:00
|
|
|
{'.', 0x95}, // 10010101
|
|
|
|
{',', 0xb3}, // 10110011
|
|
|
|
{'?', 0x8c}, // 10001100
|
2020-01-22 02:17:47 -05:00
|
|
|
{ 2 , 0xd5}, // 11010101 ASCII 0x02 is Start of Text - <CT>
|
|
|
|
{ 4 , 0x85}, // 10000101 ASCII 0x04 is End of Transmission - <CL> is too long for our encoding scheme in 8 bits, but <SK> fits
|
2019-12-18 01:32:44 -05:00
|
|
|
};
|
|
|
|
|
2020-01-19 23:15:01 -05:00
|
|
|
static void morseLetter(char c, uint16_t dit_duration_ms){
|
2020-01-22 01:07:11 -05:00
|
|
|
if(!globalSettings.morseMenuOn){
|
|
|
|
return;
|
|
|
|
}
|
2019-12-18 01:32:44 -05:00
|
|
|
unsigned char mask = 0x80;
|
|
|
|
|
|
|
|
//handle space character as three dashes
|
|
|
|
if (c == ' '){
|
2020-01-19 23:15:01 -05:00
|
|
|
active_delay(7 * dit_duration_ms);
|
2020-01-17 02:36:58 -05:00
|
|
|
//Serial.print(' ');
|
2019-12-18 01:32:44 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(morse_table)/ sizeof(struct Morse); i++){
|
|
|
|
struct Morse m;
|
|
|
|
memcpy_P(&m, morse_table + i, sizeof(struct Morse));
|
|
|
|
|
|
|
|
if (m.letter == tolower(c)){
|
|
|
|
unsigned char code = m.code;
|
|
|
|
//Serial.print(m.letter); Serial.println(' ');
|
|
|
|
|
|
|
|
while(mask & code && mask > 1)
|
|
|
|
mask = mask >> 1;
|
|
|
|
//now we are at the first zero, skip and carry on
|
|
|
|
mask = mask >> 1;
|
|
|
|
while(mask){
|
2020-01-04 02:11:55 -05:00
|
|
|
tone(CW_TONE, globalSettings.cwSideToneFreq,10000);
|
2019-12-18 01:32:44 -05:00
|
|
|
if (mask & code){
|
2020-01-19 23:15:01 -05:00
|
|
|
delay(3 * dit_duration_ms);
|
2019-12-18 01:32:44 -05:00
|
|
|
//Serial.print('-');
|
|
|
|
}
|
|
|
|
else{
|
2020-01-19 23:15:01 -05:00
|
|
|
delay(dit_duration_ms);
|
2019-12-18 01:32:44 -05:00
|
|
|
//Serial.print('.');
|
|
|
|
}
|
|
|
|
//Serial.print('#');
|
|
|
|
noTone(CW_TONE);
|
2020-01-19 23:15:01 -05:00
|
|
|
delay(dit_duration_ms); // space between dots and dashes
|
2019-12-18 01:32:44 -05:00
|
|
|
mask = mask >> 1;
|
|
|
|
}
|
|
|
|
//Serial.println('@');
|
2020-01-19 23:15:01 -05:00
|
|
|
delay(2*dit_duration_ms); // space between letters is a dash (3 dots), one dot's space has already been sent
|
2020-01-17 02:36:58 -05:00
|
|
|
break;//We've played the letter, so don't bother checking the rest of the list
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 23:15:01 -05:00
|
|
|
void morseText(char *text, uint16_t dit_duration_ms){
|
2019-12-18 01:32:44 -05:00
|
|
|
while(*text){
|
2020-01-19 23:15:01 -05:00
|
|
|
morseLetter(*text++, dit_duration_ms);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|