diff --git a/debug.h b/debug.h new file mode 100644 index 0000000..e9a261d --- /dev/null +++ b/debug.h @@ -0,0 +1,12 @@ +#pragma once + +#include + + +#define LOG_INFO 0 +#define LOG_WARN 1 +#define LOG_ERROR 2 +#define LOG_FATAL 3 + + +#define LOG(level, ...) if (level >= LOG_LEVEL) printf(__VA_ARGS__) diff --git a/gen_ft8.cpp b/gen_ft8.cpp index d1e05f3..791b8d9 100644 --- a/gen_ft8.cpp +++ b/gen_ft8.cpp @@ -10,14 +10,18 @@ #include "encode_91.h" -void synth_fsk(const uint8_t *symbols, int nSymbols, float f0, float spacing, float symbol_rate, float signal_rate, float *signal) { +// Convert a sequence of symbols (tones) into a sinewave of continuous phase (FSK). +// Symbol 0 gets encoded as a sine of frequency f0, the others are spaced in incresing +// fashion. +void synth_fsk(const uint8_t *symbols, int num_symbols, float f0, float spacing, + float symbol_rate, float signal_rate, float *signal) { float phase = 0; float dt = 1/signal_rate; float dt_sym = 1/symbol_rate; float t = 0; int j = 0; int i = 0; - while (j < nSymbols) { + while (j < num_symbols) { float f = f0 + symbols[j] * spacing; phase += 2 * M_PI * f / signal_rate; signal[i] = sin(phase); @@ -31,7 +35,7 @@ void synth_fsk(const uint8_t *symbols, int nSymbols, float f0, float spacing, fl } } - +// Save signal in floating point format (-1 .. +1) as a WAVE file using 16-bit signed integers. void save_wav(const float *signal, int num_samples, int sample_rate, const char *path) { FILE *f = fopen(path, "wb"); char subChunk1ID[4] = {'f', 'm', 't', ' '}; diff --git a/test.cpp b/test.cpp index 231451c..8d64252 100644 --- a/test.cpp +++ b/test.cpp @@ -3,29 +3,17 @@ #include #include -#include -#include - #include "text.h" - #include "pack.h" +#include "pack_77.h" #include "encode.h" - +#include "encode_91.h" #include "unpack.h" -#include "pack_77.h" -#include "encode_91.h" - +#include "debug.h" #define LOG_LEVEL LOG_INFO -#define LOG_INFO 0 -#define LOG_WARN 1 -#define LOG_ERROR 2 -#define LOG_FATAL 3 - -#define LOG(level, ...) if (level >= LOG_LEVEL) printf(__VA_ARGS__) - void convert_8bit_to_6bit(uint8_t *dst, const uint8_t *src, int nBits) { // Zero-fill the destination array as we will only be setting bits later @@ -53,28 +41,6 @@ void convert_8bit_to_6bit(uint8_t *dst, const uint8_t *src, int nBits) { } } -class TestCase { -public: - TestCase(const std::string &name) : _name(name) { - //_all_cases.push_back(this); - } - - virtual bool run(const std::string ¶ms) = 0; -private: - std::string _name; - //static std::vector _all_cases; -}; - -//std::vector TestCase::_all_cases; - -class TestCase1 : public TestCase { -public: - TestCase1() : TestCase("Test Case 1") {} - bool run(const std::string ¶ms) override { - return true; - } -}; - bool test1() { //const char *msg = "CQ DL7ACA JO40"; // 62, 32, 32, 49, 37, 27, 59, 2, 30, 19, 49, 16 @@ -116,16 +82,16 @@ void test2() { encode174(test_in, test_out); for (int j = 0; j < 22; ++j) { - printf("%02x ", test_out[j]); + LOG(LOG_INFO, "%02x ", test_out[j]); } - printf("\n"); + LOG(LOG_INFO, "\n"); } void test3() { uint8_t test_in2[10] = { 0x11, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x10, 0x04, 0x01, 0x00 }; uint16_t crc1 = ft8_crc(test_in2, 76); // Calculate CRC of 76 bits only - printf("CRC: %04x\n", crc1); // should be 0x0708 + LOG(LOG_INFO, "CRC: %04x\n", crc1); // should be 0x0708 } diff --git a/unpack.cpp b/unpack.cpp index 6f6278f..eb61045 100644 --- a/unpack.cpp +++ b/unpack.cpp @@ -41,55 +41,66 @@ void unpackcall(uint32_t nc, char *callsign) { // extract maidenhead locator -void unpackgrid(uint32_t ng, char *grid) { - // // start of special grid locators for sig strength &c. - // NGBASE = 180*180 +void unpackgrid(uint16_t ng, char *grid) { + // start of special grid locators for sig strength &c. + constexpr uint16_t NGBASE = 180*180; - // if ng == NGBASE+1: - // return " " + if (ng == NGBASE + 1) { + grid[0] = 0; + return; + } // if ng >= NGBASE+1 and ng < NGBASE+31: // return " -%02d" % (ng - (NGBASE+1)) // sig str, -01 to -30 DB // if ng >= NGBASE+31 and ng < NGBASE+62: // return "R-%02d" % (ng - (NGBASE+31)) - // if ng == NGBASE+62: - // return "RO " - // if ng == NGBASE+63: - // return "RRR " - // if ng == NGBASE+64: - // return "73 " + if (ng == NGBASE + 62) { + strcpy(grid, "RO"); + return; + } + if (ng == NGBASE + 63) { + strcpy(grid, "RRR"); + return; + } + if (ng == NGBASE + 64) { + strcpy(grid, "73"); + return; + } - // lat = (ng % 180) - 90 - // ng = int(ng / 180) - // lng = (ng * 2) - 180 + int16_t lat = (int16_t)(ng % 180) - 90; + int16_t lng = ((int16_t)(ng / 180) * 2) - 180; - // g = "%c%c%c%c" % (ord('A') + int((179-lng)/20), - // ord('A') + int((lat+90)/10), - // ord('0') + int(((179-lng)%20)/2), - // ord('0') + (lat+90)%10) + grid[0] = 'A' + ((179 - lng) / 20); + grid[1] = 'A' + ((90 + lat) / 10); + grid[2] = '0' + (((179 - lng) % 20) / 2); + grid[3] = '0' + ((90 + lat) % 10); + grid[4] = 0; - // if g[0:2] == "KA": - // // really + signal strength - // sig = int(g[2:4]) - 50 - // return "+%02d" % (sig) - - // if g[0:2] == "LA": - // // really R+ signal strength - // sig = int(g[2:4]) - 50 - // return "R+%02d" % (sig) - - grid[0] = 0; + if ((grid[0] == 'K') && (grid[1] == 'A')) { + // really + signal strength + // sig = int(g[2:4]) - 50 + // return "+%02d" % (sig) + return; + } + else if ((grid[0] == 'L') && (grid[1] == 'A')) { + // really R+ signal strength + // sig = int(g[2:4]) - 50 + // return "R+%02d" % (sig) + return; + } } void unpacktext(uint32_t nc1, uint32_t nc2, uint16_t ng, char *text) { - uint32_t nc3 = ng & 0x7FFF; + uint32_t nc3 = (ng & 0x7FFF); - if (nc1 & 1 != 0) + // Check for bit 0 and copy it to nc3 + if ((nc1 & 1) != 0) nc3 |= 0x08000; - nc1 >>= 1; - if (nc2 & 1 != 0) + if ((nc2 & 1) != 0) nc3 |= 0x10000; + + nc1 >>= 1; nc2 >>= 1; for (int i = 4; i >= 0; --i) { @@ -169,6 +180,7 @@ int unpack(const uint8_t *a72, char *message) { return 0; } + // Standard two-call exchange char c1[7]; unpackcall(nc1, c1);