2020-05-07 20:49:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 by Jonathan Naylor G4KLX
|
|
|
|
*
|
|
|
|
* 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 of the License, 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; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "FMControl.h"
|
|
|
|
|
|
|
|
#include <string>
|
2020-05-08 12:12:37 +00:00
|
|
|
|
|
|
|
const uint8_t BIT_MASK_TABLE[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U };
|
|
|
|
|
|
|
|
#define WRITE_BIT(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7])
|
|
|
|
#define READ_BIT(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7])
|
2020-05-07 20:49:18 +00:00
|
|
|
|
|
|
|
CFMControl::CFMControl(CFMNetwork* network) :
|
|
|
|
m_network(network),
|
2020-05-08 12:12:37 +00:00
|
|
|
m_enabled(false)
|
2020-05-07 20:49:18 +00:00
|
|
|
{
|
|
|
|
assert(network != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFMControl::~CFMControl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-08 12:12:37 +00:00
|
|
|
bool CFMControl::writeModem(const unsigned char* data, unsigned int length)
|
2020-05-07 20:49:18 +00:00
|
|
|
{
|
|
|
|
assert(data != NULL);
|
2020-05-08 12:12:37 +00:00
|
|
|
assert(length > 0U);
|
|
|
|
assert(m_network != NULL);
|
|
|
|
|
|
|
|
float samples[170U];
|
|
|
|
unsigned int nSamples = 0U;
|
|
|
|
|
|
|
|
// Unpack the serial data into float values.
|
|
|
|
for (unsigned int i = 0U; i < length; i += 3U) {
|
|
|
|
unsigned short sample1 = 0U;
|
|
|
|
unsigned short sample2 = 0U;
|
|
|
|
unsigned short MASK = 0x0001U;
|
|
|
|
|
|
|
|
const unsigned char* base = data + i;
|
|
|
|
for (unsigned int j = 0U; j < 12U; j++, MASK <<= 1) {
|
|
|
|
unsigned int pos1 = j;
|
|
|
|
unsigned int pos2 = j + 12U;
|
|
|
|
|
|
|
|
bool b1 = READ_BIT(base, pos1) != 0U;
|
|
|
|
bool b2 = READ_BIT(base, pos2) != 0U;
|
|
|
|
|
|
|
|
if (b1)
|
|
|
|
sample1 |= MASK;
|
|
|
|
if (b2)
|
|
|
|
sample2 |= MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert from unsigned short (0 - +4095) to float (-1.0 - +1.0)
|
|
|
|
samples[nSamples++] = (float(sample1) - 2048.0F) / 2048.0F;
|
|
|
|
samples[nSamples++] = (float(sample2) - 2048.0F) / 2048.0F;
|
|
|
|
}
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-08 12:12:37 +00:00
|
|
|
// De-emphasise the data and any other processing needed (maybe a low-pass filter to remove the CTCSS)
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-08 12:12:37 +00:00
|
|
|
unsigned char out[350U];
|
|
|
|
unsigned int nOut = 0U;
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-08 12:12:37 +00:00
|
|
|
// Repack the data (8-bit unsigned values containing unsigned 16-bit data)
|
|
|
|
for (unsigned int i = 0U; i < nSamples; i++) {
|
|
|
|
unsigned short sample = (unsigned short)((samples[i] + 1.0F) * 32767.0F + 0.5F);
|
|
|
|
out[nOut++] = (sample >> 8) & 0xFFU;
|
|
|
|
out[nOut++] = (sample >> 0) & 0xFFU;
|
|
|
|
}
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-08 12:12:37 +00:00
|
|
|
return m_network->write(out, nOut);
|
2020-05-07 20:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CFMControl::readModem(unsigned char* data, unsigned int space)
|
|
|
|
{
|
|
|
|
assert(data != NULL);
|
|
|
|
assert(space > 0U);
|
2020-05-08 12:12:37 +00:00
|
|
|
assert(m_network != NULL);
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-08 12:12:37 +00:00
|
|
|
unsigned char netData[300U];
|
|
|
|
unsigned int length = m_network->read(netData, 270U);
|
|
|
|
if (length == 0U)
|
|
|
|
return 0U;
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-08 12:12:37 +00:00
|
|
|
float samples[170U];
|
|
|
|
unsigned int nSamples = 0U;
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-08 12:12:37 +00:00
|
|
|
// Convert the unsigned 16-bit data (+65535 - 0) to float (+1.0 - -1.0)
|
|
|
|
for (unsigned int i = 0U; i < length; i += 2U) {
|
|
|
|
unsigned short sample = (netData[i + 0U] << 8) | netData[i + 1U];
|
|
|
|
samples[nSamples++] = (float(sample) / 32767.0F) - 1.0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pre-emphasise the data and other stuff.
|
|
|
|
|
|
|
|
// Pack the floating point data (+1.0 to -1.0) to packed 12-bit samples (+2047 - -2048)
|
|
|
|
unsigned int offset = 0U;
|
|
|
|
for (unsigned int i = 0U; i < nSamples; i++) {
|
|
|
|
unsigned short sample = (unsigned short)((samples[i] + 1.0F) * 2048.0F + 0.5F);
|
|
|
|
unsigned short MASK = 0x0001U;
|
|
|
|
for (unsigned int j = 0U; j < 12U; j++, MASK <<= 1) {
|
|
|
|
bool b = (sample & MASK) != 0U;
|
|
|
|
WRITE_BIT(data, offset, b);
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-08 12:12:37 +00:00
|
|
|
return nSamples;
|
2020-05-07 20:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFMControl::clock(unsigned int ms)
|
|
|
|
{
|
|
|
|
// May not be needed
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFMControl::enable(bool enabled)
|
|
|
|
{
|
|
|
|
// May not be needed
|
|
|
|
}
|