2020-04-22 06:13:14 +02:00
|
|
|
#include <Arduino.h>//tone
|
|
|
|
|
2020-04-25 20:47:11 +02:00
|
|
|
#include "encoder.h"
|
2019-12-18 07:32:44 +01:00
|
|
|
#include "morse.h"
|
2020-04-22 06:13:14 +02:00
|
|
|
#include "pin_definitions.h"
|
|
|
|
#include "settings.h"
|
2019-12-18 07:32:44 +01:00
|
|
|
|
|
|
|
struct Morse {
|
|
|
|
char letter;
|
2020-01-04 08:11:55 +01:00
|
|
|
unsigned char code;
|
2019-12-18 07:32:44 +01:00
|
|
|
};
|
|
|
|
|
2020-01-17 08:36:58 +01: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 07:32:44 +01:00
|
|
|
static const PROGMEM struct Morse morse_table[] = {
|
2020-01-22 08:54:42 +01:00
|
|
|
{'a', 0b11111001},
|
|
|
|
{'b', 0b11101000},
|
|
|
|
{'c', 0b11101010},
|
|
|
|
{'d', 0b11110100},
|
|
|
|
{'e', 0b11111100},
|
|
|
|
{'f', 0b11100010},
|
|
|
|
{'g', 0b11110110},
|
|
|
|
{'h', 0b11100000},
|
|
|
|
{'i', 0b11111000},
|
|
|
|
{'j', 0b11100111},
|
|
|
|
{'k', 0b11110101},
|
|
|
|
{'l', 0b11100100},
|
|
|
|
{'m', 0b11111011},
|
|
|
|
{'n', 0b11111010},
|
|
|
|
{'o', 0b11110111},
|
|
|
|
{'p', 0b11100110},
|
|
|
|
{'q', 0b11101101},
|
|
|
|
{'r', 0b11110010},
|
|
|
|
{'s', 0b11110000},
|
|
|
|
{'t', 0b11111101},
|
|
|
|
{'u', 0b11110001},
|
|
|
|
{'v', 0b11100001},
|
|
|
|
{'w', 0b11110011},
|
|
|
|
{'x', 0b11101001},
|
|
|
|
{'y', 0b11101011},
|
|
|
|
{'z', 0b11101100},
|
|
|
|
{'1', 0b11001111},
|
|
|
|
{'2', 0b11000111},
|
|
|
|
{'3', 0b11000011},
|
|
|
|
{'4', 0b11000001},
|
|
|
|
{'5', 0b11000000},
|
|
|
|
{'6', 0b11010000},
|
|
|
|
{'7', 0b11011000},
|
|
|
|
{'8', 0b11011100},
|
|
|
|
{'9', 0b11011110},
|
|
|
|
{'0', 0b11011111},
|
|
|
|
{'.', 0b10010101},
|
|
|
|
{',', 0b10110011},
|
|
|
|
{'?', 0b10001100},
|
|
|
|
{ 2 , 0b11010101}, // ASCII 0x02 is Start of Text - <CT>
|
|
|
|
{ 4 , 0b10000101}, // ASCII 0x04 is End of Transmission - <CL> is too long for our encoding scheme in 8 bits, but <SK> fits
|
2019-12-18 07:32:44 +01:00
|
|
|
};
|
|
|
|
|
2020-01-26 20:12:07 +01:00
|
|
|
void morseLetter(char c, uint16_t dit_duration_ms){
|
2020-01-22 07:07:11 +01:00
|
|
|
if(!globalSettings.morseMenuOn){
|
|
|
|
return;
|
|
|
|
}
|
2019-12-18 07:32:44 +01:00
|
|
|
unsigned char mask = 0x80;
|
|
|
|
|
|
|
|
//handle space character as three dashes
|
|
|
|
if (c == ' '){
|
2020-02-16 02:10:27 +01:00
|
|
|
delay(7 * dit_duration_ms);
|
2020-01-17 08:36:58 +01:00
|
|
|
//Serial.print(' ');
|
2019-12-18 07:32:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-26 19:39:01 +01:00
|
|
|
for (unsigned int i = 0; i < sizeof(morse_table)/ sizeof(struct Morse); i++){
|
2019-12-18 07:32:44 +01:00
|
|
|
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-04-22 06:13:14 +02:00
|
|
|
tone(PIN_CW_TONE, globalSettings.cwSideToneFreq,10000);
|
2019-12-18 07:32:44 +01:00
|
|
|
if (mask & code){
|
2020-01-20 05:15:01 +01:00
|
|
|
delay(3 * dit_duration_ms);
|
2019-12-18 07:32:44 +01:00
|
|
|
//Serial.print('-');
|
|
|
|
}
|
|
|
|
else{
|
2020-01-20 05:15:01 +01:00
|
|
|
delay(dit_duration_ms);
|
2019-12-18 07:32:44 +01:00
|
|
|
//Serial.print('.');
|
|
|
|
}
|
|
|
|
//Serial.print('#');
|
2020-04-22 06:13:14 +02:00
|
|
|
noTone(PIN_CW_TONE);
|
2020-01-20 05:15:01 +01:00
|
|
|
delay(dit_duration_ms); // space between dots and dashes
|
2019-12-18 07:32:44 +01:00
|
|
|
mask = mask >> 1;
|
|
|
|
}
|
|
|
|
//Serial.println('@');
|
2020-01-20 05:15:01 +01: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 08:36:58 +01:00
|
|
|
break;//We've played the letter, so don't bother checking the rest of the list
|
2019-12-18 07:32:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 01:31:32 +01:00
|
|
|
static const uint8_t RELATIVE_OFFSET_HZ = 100;
|
2020-02-10 02:04:00 +01:00
|
|
|
void morseText(const char *text, uint16_t dit_duration_ms){
|
2020-01-25 20:27:58 +01:00
|
|
|
int16_t total_counts = 0;
|
2020-01-26 01:31:32 +01:00
|
|
|
morseBool(false);
|
2020-01-25 20:27:58 +01:00
|
|
|
enc_read();//Don't count initial tone against total_counts
|
|
|
|
while(*text && (abs(total_counts) < 10)){
|
2020-01-20 05:15:01 +01:00
|
|
|
morseLetter(*text++, dit_duration_ms);
|
2020-01-25 20:27:58 +01:00
|
|
|
total_counts += enc_read();
|
2019-12-18 07:32:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 01:31:32 +01:00
|
|
|
void morseBool(bool val){
|
2020-01-26 20:15:11 +01:00
|
|
|
if(!globalSettings.morseMenuOn){
|
|
|
|
return;
|
|
|
|
}
|
2020-04-22 06:13:14 +02:00
|
|
|
tone(PIN_CW_TONE, globalSettings.cwSideToneFreq + (val ? RELATIVE_OFFSET_HZ : -RELATIVE_OFFSET_HZ));
|
2020-01-26 01:31:32 +01:00
|
|
|
delay(3*globalSettings.cwDitDurationMs);
|
2020-04-22 06:13:14 +02:00
|
|
|
noTone(PIN_CW_TONE);
|
2020-01-26 01:31:32 +01:00
|
|
|
delay(3*globalSettings.cwDitDurationMs);
|
|
|
|
}
|