Complete the CW keyer.

This commit is contained in:
Jonathan Naylor 2020-04-16 21:15:15 +01:00
parent 4a5f985d02
commit 95e76f387c
3 changed files with 63 additions and 18 deletions

View File

@ -24,7 +24,7 @@ const struct CTCSS_TABLE {
uint8_t frequency; uint8_t frequency;
uint16_t length; uint16_t length;
q15_t increment; q15_t increment;
} CTCSS_TABLE[] = { } CTCSS_TABLE_DATA[] = {
{ 67U, 358U, 92}, { 67U, 358U, 92},
{ 69U, 346U, 95}, { 69U, 346U, 95},
{ 71U, 334U, 99}, { 71U, 334U, 99},
@ -77,7 +77,7 @@ const struct CTCSS_TABLE {
{254U, 94U, 347} {254U, 94U, 347}
}; };
const uint8_t CTCSS_TABLE_LEN = 50U; const uint8_t CTCSS_TABLE_DATA_LEN = 50U;
CFMCTCSSTX::CFMCTCSSTX() : CFMCTCSSTX::CFMCTCSSTX() :
m_values(NULL), m_values(NULL),
@ -88,11 +88,11 @@ m_n(0U)
uint8_t CFMCTCSSTX::setParams(uint8_t frequency, uint8_t level) uint8_t CFMCTCSSTX::setParams(uint8_t frequency, uint8_t level)
{ {
struct CTCSS_TABLE* entry = NULL; const CTCSS_TABLE* entry = NULL;
for (uint8_t i = 0U; i < CTCSS_TABLE_LEN; i++) { for (uint8_t i = 0U; i < CTCSS_TABLE_DATA_LEN; i++) {
if (CTCSS_TABLE[i].frequency == frequency) { if (CTCSS_TABLE_DATA[i].frequency == frequency) {
entry = CTCSS_TABLE + i; entry = CTCSS_TABLE_DATA + i;
break; break;
} }
} }

View File

@ -76,18 +76,20 @@ const uint8_t BIT_MASK_TABLE[] = {0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02
#define READ_BIT(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7]) #define READ_BIT(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7])
CFMKeyer::CFMKeyer() : CFMKeyer::CFMKeyer() :
m_level(128 * 128),
m_wanted(false), m_wanted(false),
m_running(false),
m_poBuffer(), m_poBuffer(),
m_poLen(0U) m_poLen(0U),
m_poPos(0U),
m_dotLen(0U),
m_dotPos(0U),
m_audio(NULL),
m_audioLen(0U),
m_audioPos(0U)
{ {
} }
uint8_t CFMKeyer::setParams(const char* text, uint8_t speed, uint16_t frequency, uint8_t level) uint8_t CFMKeyer::setParams(const char* text, uint8_t speed, uint16_t frequency, uint8_t level)
{ {
m_level = q15_t(level * 128);
for (uint8_t i = 0U; text[i] != '\0'; i++) { for (uint8_t i = 0U; text[i] != '\0'; i++) {
for (uint8_t j = 0U; SYMBOL_LIST[j].c != 0U; j++) { for (uint8_t j = 0U; SYMBOL_LIST[j].c != 0U; j++) {
if (SYMBOL_LIST[j].c == text[i]) { if (SYMBOL_LIST[j].c == text[i]) {
@ -107,27 +109,66 @@ uint8_t CFMKeyer::setParams(const char* text, uint8_t speed, uint16_t frequency,
} }
} }
q15_t value = q15_t(level * 128);
m_dotLen = 24000U / speed; // In samples
m_audioLen = 24000U / frequency; // In samples
m_audio = new q15_t[m_audioLen];
for (uint16_t i = 0U; i < m_audioLen; i++) {
if (i < (m_audioLen / 2U))
m_audio[i] = value;
else
m_audio[i] = -value;
}
return 0U; return 0U;
} }
void CFMKeyer::getAudio(q15_t* samples, uint8_t length) void CFMKeyer::getAudio(q15_t* samples, uint8_t length)
{ {
if (!m_wanted && !m_running) if (!m_wanted)
return; return;
for (uint8_t i = 0U; i < length; i++) {
bool b = READ_BIT(m_poBuffer, m_poPos);
if (b)
samples[i] += m_audio[m_audioPos];
m_audioPos++;
if (m_audioPos >= m_audioLen)
m_audioPos = 0U;
m_dotPos++;
if (m_dotPos >= m_dotLen) {
m_dotPos = 0U;
m_poPos++;
if (m_poPos >= m_poLen) {
stop();
return;
}
}
}
} }
void CFMKeyer::start() void CFMKeyer::start()
{ {
m_wanted = true; m_wanted = true;
m_poPos = 0U;
m_dotPos = 0U;
m_audioPos = 0U;
} }
void CFMKeyer::stop() void CFMKeyer::stop()
{ {
m_wanted = false; m_wanted = false;
m_running = false; m_poPos = 0U;
m_dotPos = 0U;
m_audioPos = 0U;
} }
bool CFMKeyer::isRunning() const bool CFMKeyer::isRunning() const
{ {
return m_running; return m_poPos > 0U || m_dotPos > 0U || m_audioPos > 0U;
} }

View File

@ -35,11 +35,15 @@ public:
bool isRunning() const; bool isRunning() const;
private: private:
q15_t m_level;
bool m_wanted; bool m_wanted;
bool m_running;
uint8_t m_poBuffer[1000U]; uint8_t m_poBuffer[1000U];
uint16_t m_poLen; uint16_t m_poLen;
uint16_t m_poPos;
uint16_t m_dotLen;
uint16_t m_dotPos;
q15_t* m_audio;
uint16_t m_audioLen;
uint16_t m_audioPos;
}; };
#endif #endif