mayhem-firmware/firmware/common/morse.cpp
furrtek 2022fe137c Morse TX bugfix: bad CW symbols, FM not stopping
Corrected fox hunt transmitter #s
Moved widgets a bit
Setting up proc_tones with 0 message length stops it
2017-03-03 08:06:11 +00:00

93 lines
2.4 KiB
C++

/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2016 Furrtek
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "morse.hpp"
#include "baseband_api.hpp"
#include "portapack.hpp"
using namespace portapack;
#include "utility.hpp"
namespace morse {
// Returns 0 if message is too long
size_t morse_encode(std::string& message, const uint32_t time_unit_ms,
const uint32_t tone, uint32_t * const time_units) {
size_t i, c;
uint16_t code, code_size;
uint8_t morse_message[256];
*time_units = 0;
ToneDef * tone_defs = shared_memory.bb_data.tones_data.tone_defs;
i = 0;
for (char& ch : message) {
if (i > 256) return 0; // Message too long
if ((ch >= 'a') && (ch <= 'z')) // Make uppercase
ch -= 32;
if ((ch >= '!') && (ch <= '_')) {
code = morse_ITU[ch - '!'];
} else {
code = 0; // Default to space char
}
if (!code) {
if (i)
morse_message[i - 1] = 4; // Word space
} else {
code_size = code & 7;
for (c = 0; c < code_size; c++) {
morse_message[i++] = ((code << c) & 0x8000) ? 1 : 0; // Dot/dash
morse_message[i++] = 2; // Symbol space
}
morse_message[i - 1] = 3; // Letter space
}
}
// Count time units
for (c = 0; c < i; c++) {
*time_units += morse_symbols[morse_message[c]];
}
memcpy(shared_memory.bb_data.tones_data.message, morse_message, i);
// Setup tone "symbols"
for (c = 0; c < 5; c++) {
if (c < 2)
tone_defs[c].delta = TONES_F2D(tone); // Dot and dash
else
tone_defs[c].delta = 0; // Pause
tone_defs[c].duration = (TONES_SAMPLERATE * morse_symbols[c] * time_unit_ms) / 1000;
}
return i;
}
} /* namespace morse */