Merge pull request #580 from F4FXL/FM_Ext

Pack and unpack using shifting and masking
This commit is contained in:
Jonathan Naylor 2020-05-08 16:33:47 +01:00 committed by GitHub
commit 9a2dd6a5e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -49,21 +49,17 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length)
for (unsigned int i = 0U; i < length; i += 3U) {
unsigned short sample1 = 0U;
unsigned short sample2 = 0U;
unsigned short MASK = 0x0001U;
unsigned int MASK = 0x00000FFFU;
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;
unsigned int pack = 0U;
unsigned char* packPointer = (unsigned char*)&pack;
bool b1 = READ_BIT(base, pos1) != 0U;
bool b2 = READ_BIT(base, pos2) != 0U;
packPointer[1U] = data[i];
packPointer[2U] = data[i + 1U];
packPointer[3U] = data[i + 2U];
if (b1)
sample1 |= MASK;
if (b2)
sample2 |= MASK;
}
sample2 = (short)(pack & MASK);
sample1 = (short)(pack >> 12);
// Convert from unsigned short (0 - +4095) to float (-1.0 - +1.0)
samples[nSamples++] = (float(sample1) - 2048.0F) / 2048.0F;
@ -108,18 +104,24 @@ unsigned int CFMControl::readModem(unsigned char* data, unsigned int space)
// 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++;
}
unsigned int pack = 0U;
unsigned char* packPointer = (unsigned char*)&pack;
unsigned int j = 0U;
unsigned int i = 0U;
for (; i < nSamples && j < space; i += 2U, j += 3U) {
unsigned short sample1 = (unsigned short)((samples[i] + 1.0F) * 2048.0F + 0.5F);
unsigned short sample2 = (unsigned short)((samples[i + 1] + 1.0F) * 2048.0F + 0.5F);
pack = 0;
pack = ((unsigned int)sample1) << 12;
pack |= sample2;
data[j] = packPointer[1U];
data[j + 1U] = packPointer[2U];
data[j + 2U] = packPointer[3U];
}
return nSamples;
return j;//return the number of bytes written
}
void CFMControl::clock(unsigned int ms)