mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-07-09 18:45:22 +02:00
Merge pull request #1310 from AlexandreRouma/master
New sink/stream system
This commit is contained in:
@ -1,20 +1,20 @@
|
||||
#pragma once
|
||||
#include "../demod.h"
|
||||
#include <dsp/demod/broadcast_fm.h>
|
||||
#include <dsp/clock_recovery/mm.h>
|
||||
#include <dsp/clock_recovery/fd.h>
|
||||
#include <dsp/taps/root_raised_cosine.h>
|
||||
#include <dsp/digital/binary_slicer.h>
|
||||
#include <dsp/digital/manchester_decoder.h>
|
||||
#include <dsp/digital/differential_decoder.h>
|
||||
#include "../rds_demod.h"
|
||||
#include <gui/widgets/symbol_diagram.h>
|
||||
#include <fstream>
|
||||
#include <rds.h>
|
||||
|
||||
namespace demod {
|
||||
enum RDSRegion {
|
||||
RDS_REGION_EUROPE,
|
||||
RDS_REGION_NORTH_AMERICA
|
||||
};
|
||||
|
||||
class WFM : public Demodulator {
|
||||
public:
|
||||
WFM() {}
|
||||
WFM() : diag(0.5, 4096) {}
|
||||
|
||||
WFM(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) : diag(0.5, 4096) {
|
||||
init(name, config, input, bandwidth, audioSR);
|
||||
@ -29,10 +29,18 @@ namespace demod {
|
||||
this->name = name;
|
||||
_config = config;
|
||||
|
||||
// Define RDS regions
|
||||
rdsRegions.define("eu", "Europe", RDS_REGION_EUROPE);
|
||||
rdsRegions.define("na", "North America", RDS_REGION_NORTH_AMERICA);
|
||||
|
||||
// Register FFT draw handler
|
||||
fftRedrawHandler.handler = fftRedraw;
|
||||
fftRedrawHandler.ctx = this;
|
||||
gui::waterfall.onFFTRedraw.bindHandler(&fftRedrawHandler);
|
||||
|
||||
// Default
|
||||
std::string rdsRegionStr = "eu";
|
||||
|
||||
// Load config
|
||||
_config->acquire();
|
||||
bool modified = false;
|
||||
@ -45,33 +53,50 @@ namespace demod {
|
||||
if (config->conf[name][getName()].contains("rds")) {
|
||||
_rds = config->conf[name][getName()]["rds"];
|
||||
}
|
||||
if (config->conf[name][getName()].contains("rdsInfo")) {
|
||||
_rdsInfo = config->conf[name][getName()]["rdsInfo"];
|
||||
}
|
||||
if (config->conf[name][getName()].contains("rdsRegion")) {
|
||||
rdsRegionStr = config->conf[name][getName()]["rdsRegion"];
|
||||
}
|
||||
_config->release(modified);
|
||||
|
||||
// Define structure
|
||||
// Load RDS region
|
||||
if (rdsRegions.keyExists(rdsRegionStr)) {
|
||||
rdsRegionId = rdsRegions.keyId(rdsRegionStr);
|
||||
rdsRegion = rdsRegions.value(rdsRegionId);
|
||||
}
|
||||
else {
|
||||
rdsRegion = RDS_REGION_EUROPE;
|
||||
rdsRegionId = rdsRegions.valueId(rdsRegion);
|
||||
}
|
||||
|
||||
// Init DSP
|
||||
demod.init(input, bandwidth / 2.0f, getIFSampleRate(), _stereo, _lowPass, _rds);
|
||||
recov.init(&demod.rdsOut, 5000.0 / 2375, omegaGain, muGain, 0.01);
|
||||
slice.init(&recov.out);
|
||||
manch.init(&slice.out);
|
||||
diff.init(&manch.out, 2);
|
||||
hs.init(&diff.out, rdsHandler, this);
|
||||
rdsDemod.init(&demod.rdsOut, _rdsInfo);
|
||||
hs.init(&rdsDemod.out, rdsHandler, this);
|
||||
reshape.init(&rdsDemod.soft, 4096, (1187 / 30) - 4096);
|
||||
diagHandler.init(&reshape.out, _diagHandler, this);
|
||||
|
||||
// Init RDS display
|
||||
diag.lines.push_back(-0.8);
|
||||
diag.lines.push_back(0.8);
|
||||
}
|
||||
|
||||
void start() {
|
||||
demod.start();
|
||||
recov.start();
|
||||
slice.start();
|
||||
manch.start();
|
||||
diff.start();
|
||||
rdsDemod.start();
|
||||
hs.start();
|
||||
reshape.start();
|
||||
diagHandler.start();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
demod.stop();
|
||||
recov.stop();
|
||||
slice.stop();
|
||||
manch.stop();
|
||||
diff.stop();
|
||||
rdsDemod.stop();
|
||||
hs.stop();
|
||||
reshape.stop();
|
||||
diagHandler.stop();
|
||||
}
|
||||
|
||||
void showMenu() {
|
||||
@ -94,14 +119,129 @@ namespace demod {
|
||||
_config->release(true);
|
||||
}
|
||||
|
||||
// if (_rds) {
|
||||
// if (rdsDecode.countryCodeValid()) { ImGui::Text("Country code: %d", rdsDecode.getCountryCode()); }
|
||||
// if (rdsDecode.programCoverageValid()) { ImGui::Text("Program coverage: %d", rdsDecode.getProgramCoverage()); }
|
||||
// if (rdsDecode.programRefNumberValid()) { ImGui::Text("Reference number: %d", rdsDecode.getProgramRefNumber()); }
|
||||
// if (rdsDecode.programTypeValid()) { ImGui::Text("Program type: %d", rdsDecode.getProgramType()); }
|
||||
// if (rdsDecode.PSNameValid()) { ImGui::Text("Program name: [%s]", rdsDecode.getPSName().c_str()); }
|
||||
// if (rdsDecode.radioTextValid()) { ImGui::Text("Radiotext: [%s]", rdsDecode.getRadioText().c_str()); }
|
||||
// }
|
||||
// TODO: This might break when the entire radio module is disabled
|
||||
if (!_rds) { ImGui::BeginDisabled(); }
|
||||
if (ImGui::Checkbox(("Advanced RDS Info##_radio_wfm_rds_info_" + name).c_str(), &_rdsInfo)) {
|
||||
setAdvancedRds(_rdsInfo);
|
||||
_config->acquire();
|
||||
_config->conf[name][getName()]["rdsInfo"] = _rdsInfo;
|
||||
_config->release(true);
|
||||
}
|
||||
ImGui::SameLine();
|
||||
ImGui::FillWidth();
|
||||
if (ImGui::Combo(("##_radio_wfm_rds_region_" + name).c_str(), &rdsRegionId, rdsRegions.txt)) {
|
||||
rdsRegion = rdsRegions.value(rdsRegionId);
|
||||
_config->acquire();
|
||||
_config->conf[name][getName()]["rdsRegion"] = rdsRegions.key(rdsRegionId);
|
||||
_config->release(true);
|
||||
}
|
||||
if (!_rds) { ImGui::EndDisabled(); }
|
||||
|
||||
float menuWidth = ImGui::GetContentRegionAvail().x;
|
||||
|
||||
if (_rds && _rdsInfo) {
|
||||
ImGui::BeginTable(("##radio_wfm_rds_info_tbl_" + name).c_str(), 2, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_RowBg | ImGuiTableFlags_Borders);
|
||||
if (rdsDecode.piCodeValid()) {
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("PI Code");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
if (rdsRegion == RDS_REGION_NORTH_AMERICA) {
|
||||
ImGui::Text("0x%04X (%s)", rdsDecode.getPICode(), rdsDecode.getCallsign().c_str());
|
||||
}
|
||||
else {
|
||||
ImGui::Text("0x%04X", rdsDecode.getPICode());
|
||||
}
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("Country Code");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::Text("%d", rdsDecode.getCountryCode());
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("Program Coverage");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::Text("%s (%d)", rds::AREA_COVERAGE_TO_STR[rdsDecode.getProgramCoverage()], rdsDecode.getProgramCoverage());
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("Reference Number");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::Text("%d", rdsDecode.getProgramRefNumber());
|
||||
}
|
||||
else {
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("PI Code");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
if (rdsRegion == RDS_REGION_NORTH_AMERICA) {
|
||||
ImGui::TextUnformatted("0x---- (----)");
|
||||
}
|
||||
else {
|
||||
ImGui::TextUnformatted("0x----");
|
||||
}
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("Country Code");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::TextUnformatted("--"); // TODO: String
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("Program Coverage");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::TextUnformatted("------- (--)");
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("Reference Number");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::TextUnformatted("--");
|
||||
}
|
||||
|
||||
if (rdsDecode.programTypeValid()) {
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("Program Type");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
if (rdsRegion == RDS_REGION_NORTH_AMERICA) {
|
||||
ImGui::Text("%s (%d)", rds::PROGRAM_TYPE_US_TO_STR[rdsDecode.getProgramType()], rdsDecode.getProgramType());
|
||||
}
|
||||
else {
|
||||
ImGui::Text("%s (%d)", rds::PROGRAM_TYPE_EU_TO_STR[rdsDecode.getProgramType()], rdsDecode.getProgramType());
|
||||
}
|
||||
}
|
||||
else {
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("Program Type");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::TextUnformatted("------- (--)"); // TODO: String
|
||||
}
|
||||
|
||||
if (rdsDecode.musicValid()) {
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("Music");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::Text("%s", rdsDecode.getMusic() ? "Yes":"No");
|
||||
}
|
||||
else {
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
ImGui::TextUnformatted("Music");
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::TextUnformatted("---");
|
||||
}
|
||||
|
||||
ImGui::EndTable();
|
||||
|
||||
ImGui::SetNextItemWidth(menuWidth);
|
||||
diag.draw();
|
||||
}
|
||||
}
|
||||
|
||||
void setBandwidth(double bandwidth) {
|
||||
@ -137,12 +277,24 @@ namespace demod {
|
||||
demod.setStereo(_stereo);
|
||||
}
|
||||
|
||||
void setAdvancedRds(bool enabled) {
|
||||
rdsDemod.setSoftEnabled(enabled);
|
||||
_rdsInfo = enabled;
|
||||
}
|
||||
|
||||
private:
|
||||
static void rdsHandler(uint8_t* data, int count, void* ctx) {
|
||||
WFM* _this = (WFM*)ctx;
|
||||
_this->rdsDecode.process(data, count);
|
||||
}
|
||||
|
||||
static void _diagHandler(float* data, int count, void* ctx) {
|
||||
WFM* _this = (WFM*)ctx;
|
||||
float* buf = _this->diag.acquireBuffer();
|
||||
memcpy(buf, data, count * sizeof(float));
|
||||
_this->diag.releaseBuffer();
|
||||
}
|
||||
|
||||
static void fftRedraw(ImGui::WaterFall::FFTRedrawArgs args, void* ctx) {
|
||||
WFM* _this = (WFM*)ctx;
|
||||
if (!_this->_rds) { return; }
|
||||
@ -184,23 +336,31 @@ namespace demod {
|
||||
}
|
||||
|
||||
dsp::demod::BroadcastFM demod;
|
||||
dsp::clock_recovery::FD recov;
|
||||
dsp::digital::BinarySlicer slice;
|
||||
dsp::digital::ManchesterDecoder manch;
|
||||
dsp::digital::DifferentialDecoder diff;
|
||||
RDSDemod rdsDemod;
|
||||
dsp::sink::Handler<uint8_t> hs;
|
||||
EventHandler<ImGui::WaterFall::FFTRedrawArgs> fftRedrawHandler;
|
||||
|
||||
rds::RDSDecoder rdsDecode;
|
||||
dsp::buffer::Reshaper<float> reshape;
|
||||
dsp::sink::Handler<float> diagHandler;
|
||||
ImGui::SymbolDiagram diag;
|
||||
|
||||
rds::Decoder rdsDecode;
|
||||
|
||||
ConfigManager* _config = NULL;
|
||||
|
||||
bool _stereo = false;
|
||||
bool _lowPass = true;
|
||||
bool _rds = false;
|
||||
bool _rdsInfo = false;
|
||||
float muGain = 0.01;
|
||||
float omegaGain = (0.01*0.01)/4.0;
|
||||
|
||||
int rdsRegionId = 0;
|
||||
RDSRegion rdsRegion = RDS_REGION_EUROPE;
|
||||
|
||||
OptionList<std::string, RDSRegion> rdsRegions;
|
||||
|
||||
|
||||
std::string name;
|
||||
};
|
||||
}
|
@ -3,6 +3,8 @@
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
|
||||
#include <utils/flog.h>
|
||||
|
||||
namespace rds {
|
||||
std::map<uint16_t, BlockType> SYNDROMES = {
|
||||
{ 0b1111011000, BLOCK_TYPE_A },
|
||||
@ -20,6 +22,98 @@ namespace rds {
|
||||
{ BLOCK_TYPE_D, 0b0110110100 }
|
||||
};
|
||||
|
||||
std::map<uint16_t, const char*> THREE_LETTER_CALLS = {
|
||||
{ 0x99A5, "KBW" },
|
||||
{ 0x99A6, "KCY" },
|
||||
{ 0x9990, "KDB" },
|
||||
{ 0x99A7, "KDF" },
|
||||
{ 0x9950, "KEX" },
|
||||
{ 0x9951, "KFH" },
|
||||
{ 0x9952, "KFI" },
|
||||
{ 0x9953, "KGA" },
|
||||
{ 0x9991, "KGB" },
|
||||
{ 0x9954, "KGO" },
|
||||
{ 0x9955, "KGU" },
|
||||
{ 0x9956, "KGW" },
|
||||
{ 0x9957, "KGY" },
|
||||
{ 0x99AA, "KHQ" },
|
||||
{ 0x9958, "KID" },
|
||||
{ 0x9959, "KIT" },
|
||||
{ 0x995A, "KJR" },
|
||||
{ 0x995B, "KLO" },
|
||||
{ 0x995C, "KLZ" },
|
||||
{ 0x995D, "KMA" },
|
||||
{ 0x995E, "KMJ" },
|
||||
{ 0x995F, "KNX" },
|
||||
{ 0x9960, "KOA" },
|
||||
{ 0x99AB, "KOB" },
|
||||
{ 0x9992, "KOY" },
|
||||
{ 0x9993, "KPQ" },
|
||||
{ 0x9964, "KQV" },
|
||||
{ 0x9994, "KSD" },
|
||||
{ 0x9965, "KSL" },
|
||||
{ 0x9966, "KUJ" },
|
||||
{ 0x9995, "KUT" },
|
||||
{ 0x9967, "KVI" },
|
||||
{ 0x9968, "KWG" },
|
||||
{ 0x9996, "KXL" },
|
||||
{ 0x9997, "KXO" },
|
||||
{ 0x996B, "KYW" },
|
||||
{ 0x9999, "WBT" },
|
||||
{ 0x996D, "WBZ" },
|
||||
{ 0x996E, "WDZ" },
|
||||
{ 0x996F, "WEW" },
|
||||
{ 0x999A, "WGH" },
|
||||
{ 0x9971, "WGL" },
|
||||
{ 0x9972, "WGN" },
|
||||
{ 0x9973, "WGR" },
|
||||
{ 0x999B, "WGY" },
|
||||
{ 0x9975, "WHA" },
|
||||
{ 0x9976, "WHB" },
|
||||
{ 0x9977, "WHK" },
|
||||
{ 0x9978, "WHO" },
|
||||
{ 0x999C, "WHP" },
|
||||
{ 0x999D, "WIL" },
|
||||
{ 0x997A, "WIP" },
|
||||
{ 0x99B3, "WIS" },
|
||||
{ 0x997B, "WJR" },
|
||||
{ 0x99B4, "WJW" },
|
||||
{ 0x99B5, "WJZ" },
|
||||
{ 0x997C, "WKY" },
|
||||
{ 0x997D, "WLS" },
|
||||
{ 0x997E, "WLW" },
|
||||
{ 0x999E, "WMC" },
|
||||
{ 0x999F, "WMT" },
|
||||
{ 0x9981, "WOC" },
|
||||
{ 0x99A0, "WOI" },
|
||||
{ 0x9983, "WOL" },
|
||||
{ 0x9984, "WOR" },
|
||||
{ 0x99A1, "WOW" },
|
||||
{ 0x99B9, "WRC" },
|
||||
{ 0x99A2, "WRR" },
|
||||
{ 0x99A3, "WSB" },
|
||||
{ 0x99A4, "WSM" },
|
||||
{ 0x9988, "WWJ" },
|
||||
{ 0x9989, "WWL" }
|
||||
};
|
||||
|
||||
std::map<uint16_t, const char*> NAT_LOC_LINKED_STATIONS = {
|
||||
{ 0xB01, "NPR-1" },
|
||||
{ 0xB02, "CBC - Radio One" },
|
||||
{ 0xB03, "CBC - Radio Two" },
|
||||
{ 0xB04, "Radio-Canada - Première Chaîne" },
|
||||
{ 0xB05, "Radio-Canada - Espace Musique" },
|
||||
{ 0xB06, "CBC" },
|
||||
{ 0xB07, "CBC" },
|
||||
{ 0xB08, "CBC" },
|
||||
{ 0xB09, "CBC" },
|
||||
{ 0xB0A, "NPR-2" },
|
||||
{ 0xB0B, "NPR-3" },
|
||||
{ 0xB0C, "NPR-4" },
|
||||
{ 0xB0D, "NPR-5" },
|
||||
{ 0xB0E, "NPR-6" }
|
||||
};
|
||||
|
||||
// 9876543210
|
||||
const uint16_t LFSR_POLY = 0b0110111001;
|
||||
const uint16_t IN_POLY = 0b1100011011;
|
||||
@ -28,7 +122,7 @@ namespace rds {
|
||||
const int DATA_LEN = 16;
|
||||
const int POLY_LEN = 10;
|
||||
|
||||
void RDSDecoder::process(uint8_t* symbols, int count) {
|
||||
void Decoder::process(uint8_t* symbols, int count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
// Shift in the bit
|
||||
shiftReg = ((shiftReg << 1) & 0x3FFFFFF) | (symbols[i] & 1);
|
||||
@ -54,18 +148,26 @@ namespace rds {
|
||||
type = (BlockType)((lastType + 1) % _BLOCK_TYPE_COUNT);
|
||||
}
|
||||
|
||||
// Save block while correcting errors (NOT YET)
|
||||
// Save block while correcting errors (NOT YET) <- idk why the "not yet is here", TODO: find why
|
||||
blocks[type] = correctErrors(shiftReg, type, blockAvail[type]);
|
||||
|
||||
// Update continous group count
|
||||
if (type == BLOCK_TYPE_A) { contGroup = 1; }
|
||||
else if (type == BLOCK_TYPE_B && lastType == BLOCK_TYPE_A) { contGroup++; }
|
||||
// If block type is A, decode it directly, otherwise, update continous count
|
||||
if (type == BLOCK_TYPE_A) {
|
||||
decodeBlockA();
|
||||
}
|
||||
else if (type == BLOCK_TYPE_B) { contGroup = 1; }
|
||||
else if ((type == BLOCK_TYPE_C || type == BLOCK_TYPE_CP) && lastType == BLOCK_TYPE_B) { contGroup++; }
|
||||
else if (type == BLOCK_TYPE_D && (lastType == BLOCK_TYPE_C || lastType == BLOCK_TYPE_CP)) { contGroup++; }
|
||||
else { contGroup = 0; }
|
||||
else {
|
||||
// If block B is available, decode it alone.
|
||||
if (contGroup == 1) {
|
||||
decodeBlockB();
|
||||
}
|
||||
contGroup = 0;
|
||||
}
|
||||
|
||||
// If we've got an entire group, process it
|
||||
if (contGroup >= 4) {
|
||||
if (contGroup >= 3) {
|
||||
contGroup = 0;
|
||||
decodeGroup();
|
||||
}
|
||||
@ -76,7 +178,7 @@ namespace rds {
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t RDSDecoder::calcSyndrome(uint32_t block) {
|
||||
uint16_t Decoder::calcSyndrome(uint32_t block) {
|
||||
uint16_t syn = 0;
|
||||
|
||||
// Calculate the syndrome using a LFSR
|
||||
@ -95,7 +197,7 @@ namespace rds {
|
||||
return syn;
|
||||
}
|
||||
|
||||
uint32_t RDSDecoder::correctErrors(uint32_t block, BlockType type, bool& recovered) {
|
||||
uint32_t Decoder::correctErrors(uint32_t block, BlockType type, bool& recovered) {
|
||||
// Subtract the offset from block
|
||||
block ^= (uint32_t)OFFSETS[type];
|
||||
uint32_t out = block;
|
||||
@ -124,96 +226,259 @@ namespace rds {
|
||||
return out;
|
||||
}
|
||||
|
||||
void RDSDecoder::decodeGroup() {
|
||||
std::lock_guard<std::mutex> lck(groupMtx);
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
anyGroupLastUpdate = now;
|
||||
void Decoder::decodeBlockA() {
|
||||
// Acquire lock
|
||||
std::lock_guard<std::mutex> lck(blockAMtx);
|
||||
|
||||
// Make sure blocks A and B are available
|
||||
if (!blockAvail[BLOCK_TYPE_A] || !blockAvail[BLOCK_TYPE_B]) { return; }
|
||||
// If it didn't decode properly return
|
||||
if (!blockAvail[BLOCK_TYPE_A]) { return; }
|
||||
|
||||
// Decode PI code
|
||||
piCode = (blocks[BLOCK_TYPE_A] >> 10) & 0xFFFF;
|
||||
countryCode = (blocks[BLOCK_TYPE_A] >> 22) & 0xF;
|
||||
programCoverage = (AreaCoverage)((blocks[BLOCK_TYPE_A] >> 18) & 0xF);
|
||||
programRefNumber = (blocks[BLOCK_TYPE_A] >> 10) & 0xFF;
|
||||
callsign = decodeCallsign(piCode);
|
||||
|
||||
// Update timeout
|
||||
blockALastUpdate = std::chrono::high_resolution_clock::now();;
|
||||
}
|
||||
|
||||
void Decoder::decodeBlockB() {
|
||||
// Acquire lock
|
||||
std::lock_guard<std::mutex> lck(blockBMtx);
|
||||
|
||||
// If it didn't decode properly return (TODO: Make sure this is not needed)
|
||||
if (!blockAvail[BLOCK_TYPE_B]) { return; }
|
||||
|
||||
// Decode group type and version
|
||||
uint8_t groupType = (blocks[BLOCK_TYPE_B] >> 22) & 0xF;
|
||||
GroupVersion groupVer = (GroupVersion)((blocks[BLOCK_TYPE_B] >> 21) & 1);
|
||||
groupType = (blocks[BLOCK_TYPE_B] >> 22) & 0xF;
|
||||
groupVer = (GroupVersion)((blocks[BLOCK_TYPE_B] >> 21) & 1);
|
||||
|
||||
// Decode traffic program and program type
|
||||
trafficProgram = (blocks[BLOCK_TYPE_B] >> 20) & 1;
|
||||
programType = (ProgramType)((blocks[BLOCK_TYPE_B] >> 15) & 0x1F);
|
||||
|
||||
if (groupType == 0) {
|
||||
group0LastUpdate = now;
|
||||
trafficAnnouncement = (blocks[BLOCK_TYPE_B] >> 14) & 1;
|
||||
music = (blocks[BLOCK_TYPE_B] >> 13) & 1;
|
||||
uint8_t diBit = (blocks[BLOCK_TYPE_B] >> 12) & 1;
|
||||
uint8_t offset = ((blocks[BLOCK_TYPE_B] >> 10) & 0b11);
|
||||
uint8_t diOffset = 3 - offset;
|
||||
uint8_t psOffset = offset * 2;
|
||||
|
||||
if (groupVer == GROUP_VER_A && blockAvail[BLOCK_TYPE_C]) {
|
||||
alternateFrequency = (blocks[BLOCK_TYPE_C] >> 10) & 0xFFFF;
|
||||
// Update timeout
|
||||
blockBLastUpdate = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void Decoder::decodeGroup0() {
|
||||
// Acquire lock
|
||||
std::lock_guard<std::mutex> lck(group0Mtx);
|
||||
|
||||
// Decode Block B data
|
||||
trafficAnnouncement = (blocks[BLOCK_TYPE_B] >> 14) & 1;
|
||||
music = (blocks[BLOCK_TYPE_B] >> 13) & 1;
|
||||
uint8_t diBit = (blocks[BLOCK_TYPE_B] >> 12) & 1;
|
||||
uint8_t offset = ((blocks[BLOCK_TYPE_B] >> 10) & 0b11);
|
||||
uint8_t diOffset = 3 - offset;
|
||||
uint8_t psOffset = offset * 2;
|
||||
|
||||
// Decode Block C data
|
||||
if (groupVer == GROUP_VER_A && blockAvail[BLOCK_TYPE_C]) {
|
||||
alternateFrequency = (blocks[BLOCK_TYPE_C] >> 10) & 0xFFFF;
|
||||
}
|
||||
|
||||
// Write DI bit to the decoder identification
|
||||
decoderIdent &= ~(1 << diOffset);
|
||||
decoderIdent |= (diBit << diOffset);
|
||||
|
||||
// Write chars at offset the PSName
|
||||
if (blockAvail[BLOCK_TYPE_D]) {
|
||||
programServiceName[psOffset] = (blocks[BLOCK_TYPE_D] >> 18) & 0xFF;
|
||||
programServiceName[psOffset + 1] = (blocks[BLOCK_TYPE_D] >> 10) & 0xFF;
|
||||
}
|
||||
|
||||
// Update timeout
|
||||
group0LastUpdate = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void Decoder::decodeGroup2() {
|
||||
// Acquire lock
|
||||
std::lock_guard<std::mutex> lck(group2Mtx);
|
||||
|
||||
// Get char offset and write chars in the Radiotext
|
||||
bool nAB = (blocks[BLOCK_TYPE_B] >> 14) & 1;
|
||||
uint8_t offset = (blocks[BLOCK_TYPE_B] >> 10) & 0xF;
|
||||
|
||||
// Clear text field if the A/B flag changed
|
||||
if (nAB != rtAB) {
|
||||
radioText = " ";
|
||||
}
|
||||
rtAB = nAB;
|
||||
|
||||
// Write char at offset in Radiotext
|
||||
if (groupVer == GROUP_VER_A) {
|
||||
uint8_t rtOffset = offset * 4;
|
||||
if (blockAvail[BLOCK_TYPE_C]) {
|
||||
radioText[rtOffset] = (blocks[BLOCK_TYPE_C] >> 18) & 0xFF;
|
||||
radioText[rtOffset + 1] = (blocks[BLOCK_TYPE_C] >> 10) & 0xFF;
|
||||
}
|
||||
|
||||
// Write DI bit to the decoder identification
|
||||
decoderIdent &= ~(1 << diOffset);
|
||||
decoderIdent |= (diBit << diOffset);
|
||||
|
||||
// Write chars at offset the PSName
|
||||
if (blockAvail[BLOCK_TYPE_D]) {
|
||||
programServiceName[psOffset] = (blocks[BLOCK_TYPE_D] >> 18) & 0xFF;
|
||||
programServiceName[psOffset + 1] = (blocks[BLOCK_TYPE_D] >> 10) & 0xFF;
|
||||
radioText[rtOffset + 2] = (blocks[BLOCK_TYPE_D] >> 18) & 0xFF;
|
||||
radioText[rtOffset + 3] = (blocks[BLOCK_TYPE_D] >> 10) & 0xFF;
|
||||
}
|
||||
}
|
||||
else if (groupType == 2) {
|
||||
group2LastUpdate = now;
|
||||
// Get char offset and write chars in the Radiotext
|
||||
bool nAB = (blocks[BLOCK_TYPE_B] >> 14) & 1;
|
||||
uint8_t offset = (blocks[BLOCK_TYPE_B] >> 10) & 0xF;
|
||||
|
||||
// Clear text field if the A/B flag changed
|
||||
if (nAB != rtAB) {
|
||||
radioText = " ";
|
||||
else {
|
||||
uint8_t rtOffset = offset * 2;
|
||||
if (blockAvail[BLOCK_TYPE_D]) {
|
||||
radioText[rtOffset] = (blocks[BLOCK_TYPE_D] >> 18) & 0xFF;
|
||||
radioText[rtOffset + 1] = (blocks[BLOCK_TYPE_D] >> 10) & 0xFF;
|
||||
}
|
||||
rtAB = nAB;
|
||||
}
|
||||
|
||||
// Write char at offset in Radiotext
|
||||
if (groupVer == GROUP_VER_A) {
|
||||
uint8_t rtOffset = offset * 4;
|
||||
if (blockAvail[BLOCK_TYPE_C]) {
|
||||
radioText[rtOffset] = (blocks[BLOCK_TYPE_C] >> 18) & 0xFF;
|
||||
radioText[rtOffset + 1] = (blocks[BLOCK_TYPE_C] >> 10) & 0xFF;
|
||||
}
|
||||
if (blockAvail[BLOCK_TYPE_D]) {
|
||||
radioText[rtOffset + 2] = (blocks[BLOCK_TYPE_D] >> 18) & 0xFF;
|
||||
radioText[rtOffset + 3] = (blocks[BLOCK_TYPE_D] >> 10) & 0xFF;
|
||||
}
|
||||
// Update timeout
|
||||
group2LastUpdate = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void Decoder::decodeGroup10() {
|
||||
// Acquire lock
|
||||
std::lock_guard<std::mutex> lck(group10Mtx);
|
||||
|
||||
// Check if the text needs to be cleared
|
||||
bool ab = (blocks[BLOCK_TYPE_B] >> 14) & 1;
|
||||
if (ab != ptnAB) {
|
||||
programTypeName = " ";
|
||||
}
|
||||
ptnAB = ab;
|
||||
|
||||
// Decode segment address
|
||||
bool addr = (blocks[BLOCK_TYPE_B] >> 10) & 1;
|
||||
|
||||
// Save text depending on address
|
||||
if (addr) {
|
||||
if (blockAvail[BLOCK_TYPE_C]) {
|
||||
programTypeName[4] = (blocks[BLOCK_TYPE_C] >> 18) & 0xFF;
|
||||
programTypeName[5] = (blocks[BLOCK_TYPE_C] >> 10) & 0xFF;
|
||||
}
|
||||
if (blockAvail[BLOCK_TYPE_D]) {
|
||||
programTypeName[6] = (blocks[BLOCK_TYPE_D] >> 18) & 0xFF;
|
||||
programTypeName[7] = (blocks[BLOCK_TYPE_D] >> 10) & 0xFF;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (blockAvail[BLOCK_TYPE_C]) {
|
||||
programTypeName[0] = (blocks[BLOCK_TYPE_C] >> 18) & 0xFF;
|
||||
programTypeName[1] = (blocks[BLOCK_TYPE_C] >> 10) & 0xFF;
|
||||
}
|
||||
if (blockAvail[BLOCK_TYPE_D]) {
|
||||
programTypeName[2] = (blocks[BLOCK_TYPE_D] >> 18) & 0xFF;
|
||||
programTypeName[3] = (blocks[BLOCK_TYPE_D] >> 10) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
// Update timeout
|
||||
group10LastUpdate = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void Decoder::decodeGroup() {
|
||||
// Make sure blocks B is available
|
||||
if (!blockAvail[BLOCK_TYPE_B]) { return; }
|
||||
|
||||
// Decode block B
|
||||
decodeBlockB();
|
||||
|
||||
// Decode depending on group type
|
||||
switch (groupType) {
|
||||
case 0:
|
||||
decodeGroup0();
|
||||
break;
|
||||
case 2:
|
||||
decodeGroup2();
|
||||
break;
|
||||
case 10:
|
||||
decodeGroup10();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string Decoder::base26ToCall(uint16_t pi) {
|
||||
// Determin first better based on offset
|
||||
bool w = (pi >= 21672);
|
||||
std::string callsign(w ? "W" : "K");
|
||||
|
||||
// Base25 decode the rest
|
||||
std::string restStr;
|
||||
int rest = pi - (w ? 21672 : 4096);
|
||||
while (rest) {
|
||||
restStr += 'A' + (rest % 26);
|
||||
rest /= 26;
|
||||
}
|
||||
|
||||
// Reorder chars
|
||||
for (int i = restStr.size() - 1; i >= 0; i--) {
|
||||
callsign += restStr[i];
|
||||
}
|
||||
|
||||
return callsign;
|
||||
}
|
||||
|
||||
std::string Decoder::decodeCallsign(uint16_t pi) {
|
||||
if ((pi >> 8) == 0xAF) {
|
||||
// AFXY -> XY00
|
||||
return base26ToCall((pi & 0xFF) << 8);
|
||||
}
|
||||
else if ((pi >> 12) == 0xA) {
|
||||
// AXYZ -> X0YZ
|
||||
return base26ToCall((((pi >> 8) & 0xF) << 12) | (pi & 0xFF));
|
||||
}
|
||||
else if (pi >= 0x9950 && pi <= 0x9EFF) {
|
||||
// 3 letter callsigns
|
||||
if (THREE_LETTER_CALLS.find(pi) != THREE_LETTER_CALLS.end()) {
|
||||
return THREE_LETTER_CALLS[pi];
|
||||
}
|
||||
else {
|
||||
uint8_t rtOffset = offset * 2;
|
||||
if (blockAvail[BLOCK_TYPE_D]) {
|
||||
radioText[rtOffset] = (blocks[BLOCK_TYPE_D] >> 18) & 0xFF;
|
||||
radioText[rtOffset + 1] = (blocks[BLOCK_TYPE_D] >> 10) & 0xFF;
|
||||
}
|
||||
return "Not Assigned";
|
||||
}
|
||||
}
|
||||
else if (pi >= 0x1000 && pi <= 0x994F) {
|
||||
// Normal encoding
|
||||
if ((pi & 0xFF) == 0 || ((pi >> 8) & 0xF) == 0) {
|
||||
return "Not Assigned";
|
||||
}
|
||||
else {
|
||||
return base26ToCall(pi);
|
||||
}
|
||||
}
|
||||
else if (pi >= 0xB000 && pi <= 0xEFFF) {
|
||||
uint16_t _pi = ((pi >> 12) << 8) | (pi & 0xFF);
|
||||
if (NAT_LOC_LINKED_STATIONS.find(_pi) != NAT_LOC_LINKED_STATIONS.end()) {
|
||||
return NAT_LOC_LINKED_STATIONS[_pi];
|
||||
}
|
||||
else {
|
||||
return "Not Assigned";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Not Assigned";
|
||||
}
|
||||
}
|
||||
|
||||
bool RDSDecoder::anyGroupValid() {
|
||||
bool Decoder::blockAValid() {
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - anyGroupLastUpdate)).count() < 5000.0;
|
||||
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - blockALastUpdate)).count() < RDS_BLOCK_A_TIMEOUT_MS;
|
||||
}
|
||||
|
||||
bool RDSDecoder::group0Valid() {
|
||||
bool Decoder::blockBValid() {
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - group0LastUpdate)).count() < 5000.0;
|
||||
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - blockBLastUpdate)).count() < RDS_BLOCK_B_TIMEOUT_MS;
|
||||
}
|
||||
|
||||
bool RDSDecoder::group2Valid() {
|
||||
bool Decoder::group0Valid() {
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - group2LastUpdate)).count() < 5000.0;
|
||||
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - group0LastUpdate)).count() < RDS_GROUP_0_TIMEOUT_MS;
|
||||
}
|
||||
|
||||
bool Decoder::group2Valid() {
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - group2LastUpdate)).count() < RDS_GROUP_2_TIMEOUT_MS;
|
||||
}
|
||||
|
||||
bool Decoder::group10Valid() {
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - group10LastUpdate)).count() < RDS_GROUP_10_TIMEOUT_MS;
|
||||
}
|
||||
}
|
@ -4,6 +4,12 @@
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
|
||||
#define RDS_BLOCK_A_TIMEOUT_MS 5000.0
|
||||
#define RDS_BLOCK_B_TIMEOUT_MS 5000.0
|
||||
#define RDS_GROUP_0_TIMEOUT_MS 5000.0
|
||||
#define RDS_GROUP_2_TIMEOUT_MS 5000.0
|
||||
#define RDS_GROUP_10_TIMEOUT_MS 5000.0
|
||||
|
||||
namespace rds {
|
||||
enum BlockType {
|
||||
BLOCK_TYPE_A,
|
||||
@ -20,22 +26,42 @@ namespace rds {
|
||||
};
|
||||
|
||||
enum AreaCoverage {
|
||||
AREA_COVERAGE_LOCAL,
|
||||
AREA_COVERAGE_INTERNATIONAL,
|
||||
AREA_COVERAGE_NATIONAL,
|
||||
AREA_COVERAGE_SUPRA_NATIONAL,
|
||||
AREA_COVERAGE_REGIONAL1,
|
||||
AREA_COVERAGE_REGIONAL2,
|
||||
AREA_COVERAGE_REGIONAL3,
|
||||
AREA_COVERAGE_REGIONAL4,
|
||||
AREA_COVERAGE_REGIONAL5,
|
||||
AREA_COVERAGE_REGIONAL6,
|
||||
AREA_COVERAGE_REGIONAL7,
|
||||
AREA_COVERAGE_REGIONAL8,
|
||||
AREA_COVERAGE_REGIONAL9,
|
||||
AREA_COVERAGE_REGIONAL10,
|
||||
AREA_COVERAGE_REGIONAL11,
|
||||
AREA_COVERAGE_REGIONAL12
|
||||
AREA_COVERAGE_INVALID = -1,
|
||||
AREA_COVERAGE_LOCAL = 0,
|
||||
AREA_COVERAGE_INTERNATIONAL = 1,
|
||||
AREA_COVERAGE_NATIONAL = 2,
|
||||
AREA_COVERAGE_SUPRA_NATIONAL = 3,
|
||||
AREA_COVERAGE_REGIONAL1 = 4,
|
||||
AREA_COVERAGE_REGIONAL2 = 5,
|
||||
AREA_COVERAGE_REGIONAL3 = 6,
|
||||
AREA_COVERAGE_REGIONAL4 = 7,
|
||||
AREA_COVERAGE_REGIONAL5 = 8,
|
||||
AREA_COVERAGE_REGIONAL6 = 9,
|
||||
AREA_COVERAGE_REGIONAL7 = 10,
|
||||
AREA_COVERAGE_REGIONAL8 = 11,
|
||||
AREA_COVERAGE_REGIONAL9 = 12,
|
||||
AREA_COVERAGE_REGIONAL10 = 13,
|
||||
AREA_COVERAGE_REGIONAL11 = 14,
|
||||
AREA_COVERAGE_REGIONAL12 = 15
|
||||
};
|
||||
|
||||
inline const char* AREA_COVERAGE_TO_STR[] = {
|
||||
"Local",
|
||||
"International",
|
||||
"National",
|
||||
"Supra-National",
|
||||
"Regional 1",
|
||||
"Regional 2",
|
||||
"Regional 3",
|
||||
"Regional 4",
|
||||
"Regional 5",
|
||||
"Regional 6",
|
||||
"Regional 7",
|
||||
"Regional 8",
|
||||
"Regional 9",
|
||||
"Regional 10",
|
||||
"Regional 11",
|
||||
"Regional 12",
|
||||
};
|
||||
|
||||
enum ProgramType {
|
||||
@ -108,6 +134,76 @@ namespace rds {
|
||||
PROGRAM_TYPE_EU_ALARM = 31
|
||||
};
|
||||
|
||||
inline const char* PROGRAM_TYPE_EU_TO_STR[] = {
|
||||
"None",
|
||||
"News",
|
||||
"Current Affairs",
|
||||
"Information",
|
||||
"Sports",
|
||||
"Education",
|
||||
"Drama",
|
||||
"Culture",
|
||||
"Science",
|
||||
"Varied",
|
||||
"Pop Music",
|
||||
"Rock Music",
|
||||
"Easy Listening Music",
|
||||
"Light Classical",
|
||||
"Serious Classical",
|
||||
"Other Music",
|
||||
"Weather",
|
||||
"Finance",
|
||||
"Children Program",
|
||||
"Social Affairs",
|
||||
"Religion",
|
||||
"Phone-in",
|
||||
"Travel",
|
||||
"Leisure",
|
||||
"Jazz Music",
|
||||
"Country Music",
|
||||
"National Music",
|
||||
"Oldies Music",
|
||||
"Folk Music",
|
||||
"Documentary",
|
||||
"Alarm Test",
|
||||
"Alarm",
|
||||
};
|
||||
|
||||
inline const char* PROGRAM_TYPE_US_TO_STR[] = {
|
||||
"None",
|
||||
"News",
|
||||
"Information",
|
||||
"Sports",
|
||||
"Talk",
|
||||
"Rock",
|
||||
"Classic Rock",
|
||||
"Adult Hits",
|
||||
"Soft Rock",
|
||||
"Top 40",
|
||||
"Country",
|
||||
"Oldies",
|
||||
"Soft",
|
||||
"Nostalgia",
|
||||
"Jazz",
|
||||
"Classical",
|
||||
"Rythm and Blues",
|
||||
"Soft Rythm and Blues",
|
||||
"Foreign Language",
|
||||
"Religious Music",
|
||||
"Religious Talk",
|
||||
"Personality",
|
||||
"Public",
|
||||
"College",
|
||||
"Unassigned",
|
||||
"Unassigned",
|
||||
"Unassigned",
|
||||
"Unassigned",
|
||||
"Unassigned",
|
||||
"Weather",
|
||||
"Emergency Test",
|
||||
"Emergency",
|
||||
};
|
||||
|
||||
enum DecoderIdentification {
|
||||
DECODER_IDENT_STEREO = (1 << 0),
|
||||
DECODER_IDENT_ARTIFICIAL_HEAD = (1 << 1),
|
||||
@ -115,35 +211,49 @@ namespace rds {
|
||||
DECODER_IDENT_VARIABLE_PTY = (1 << 0)
|
||||
};
|
||||
|
||||
class RDSDecoder {
|
||||
class Decoder {
|
||||
public:
|
||||
void process(uint8_t* symbols, int count);
|
||||
|
||||
bool countryCodeValid() { std::lock_guard<std::mutex> lck(groupMtx); return anyGroupValid(); }
|
||||
uint8_t getCountryCode() { std::lock_guard<std::mutex> lck(groupMtx); return countryCode; }
|
||||
bool programCoverageValid() { std::lock_guard<std::mutex> lck(groupMtx); return anyGroupValid(); }
|
||||
uint8_t getProgramCoverage() { std::lock_guard<std::mutex> lck(groupMtx); return programCoverage; }
|
||||
bool programRefNumberValid() { std::lock_guard<std::mutex> lck(groupMtx); return anyGroupValid(); }
|
||||
uint8_t getProgramRefNumber() { std::lock_guard<std::mutex> lck(groupMtx); return programRefNumber; }
|
||||
bool programTypeValid() { std::lock_guard<std::mutex> lck(groupMtx); return anyGroupValid(); }
|
||||
ProgramType getProgramType() { std::lock_guard<std::mutex> lck(groupMtx); return programType; }
|
||||
bool piCodeValid() { std::lock_guard<std::mutex> lck(blockAMtx); return blockAValid(); }
|
||||
uint16_t getPICode() { std::lock_guard<std::mutex> lck(blockAMtx); return piCode; }
|
||||
uint8_t getCountryCode() { std::lock_guard<std::mutex> lck(blockAMtx); return countryCode; }
|
||||
uint8_t getProgramCoverage() { std::lock_guard<std::mutex> lck(blockAMtx); return programCoverage; }
|
||||
uint8_t getProgramRefNumber() { std::lock_guard<std::mutex> lck(blockAMtx); return programRefNumber; }
|
||||
std::string getCallsign() { std::lock_guard<std::mutex> lck(blockAMtx); return callsign; }
|
||||
|
||||
bool programTypeValid() { std::lock_guard<std::mutex> lck(blockBMtx); return blockBValid(); }
|
||||
ProgramType getProgramType() { std::lock_guard<std::mutex> lck(blockBMtx); return programType; }
|
||||
|
||||
bool musicValid() { std::lock_guard<std::mutex> lck(groupMtx); return group0Valid(); }
|
||||
bool getMusic() { std::lock_guard<std::mutex> lck(groupMtx); return music; }
|
||||
bool PSNameValid() { std::lock_guard<std::mutex> lck(groupMtx); return group0Valid(); }
|
||||
std::string getPSName() { std::lock_guard<std::mutex> lck(groupMtx); return programServiceName; }
|
||||
bool musicValid() { std::lock_guard<std::mutex> lck(group0Mtx); return group0Valid(); }
|
||||
bool getMusic() { std::lock_guard<std::mutex> lck(group0Mtx); return music; }
|
||||
bool PSNameValid() { std::lock_guard<std::mutex> lck(group0Mtx); return group0Valid(); }
|
||||
std::string getPSName() { std::lock_guard<std::mutex> lck(group0Mtx); return programServiceName; }
|
||||
|
||||
bool radioTextValid() { std::lock_guard<std::mutex> lck(groupMtx); return group2Valid(); }
|
||||
std::string getRadioText() { std::lock_guard<std::mutex> lck(groupMtx); return radioText; }
|
||||
bool radioTextValid() { std::lock_guard<std::mutex> lck(group2Mtx); return group2Valid(); }
|
||||
std::string getRadioText() { std::lock_guard<std::mutex> lck(group2Mtx); return radioText; }
|
||||
|
||||
bool programTypeNameValid() { std::lock_guard<std::mutex> lck(group10Mtx); return group10Valid(); }
|
||||
std::string getProgramTypeName() { std::lock_guard<std::mutex> lck(group10Mtx); return programTypeName; }
|
||||
|
||||
private:
|
||||
static uint16_t calcSyndrome(uint32_t block);
|
||||
static uint32_t correctErrors(uint32_t block, BlockType type, bool& recovered);
|
||||
void decodeBlockA();
|
||||
void decodeBlockB();
|
||||
void decodeGroup0();
|
||||
void decodeGroup2();
|
||||
void decodeGroup10();
|
||||
void decodeGroup();
|
||||
|
||||
bool anyGroupValid();
|
||||
static std::string base26ToCall(uint16_t pi);
|
||||
static std::string decodeCallsign(uint16_t pi);
|
||||
|
||||
bool blockAValid();
|
||||
bool blockBValid();
|
||||
bool group0Valid();
|
||||
bool group2Valid();
|
||||
bool group10Valid();
|
||||
|
||||
// State machine
|
||||
uint32_t shiftReg = 0;
|
||||
@ -154,17 +264,26 @@ namespace rds {
|
||||
uint32_t blocks[_BLOCK_TYPE_COUNT];
|
||||
bool blockAvail[_BLOCK_TYPE_COUNT];
|
||||
|
||||
// All groups
|
||||
std::mutex groupMtx;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> anyGroupLastUpdate;
|
||||
// Block A (All groups)
|
||||
std::mutex blockAMtx;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> blockALastUpdate{}; // 1970-01-01
|
||||
uint16_t piCode;
|
||||
uint8_t countryCode;
|
||||
AreaCoverage programCoverage;
|
||||
uint8_t programRefNumber;
|
||||
std::string callsign;
|
||||
|
||||
// Block B (All groups)
|
||||
std::mutex blockBMtx;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> blockBLastUpdate{}; // 1970-01-01
|
||||
uint8_t groupType;
|
||||
GroupVersion groupVer;
|
||||
bool trafficProgram;
|
||||
ProgramType programType;
|
||||
|
||||
// Group type 0
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> group0LastUpdate;
|
||||
std::mutex group0Mtx;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> group0LastUpdate{}; // 1970-01-01
|
||||
bool trafficAnnouncement;
|
||||
bool music;
|
||||
uint8_t decoderIdent;
|
||||
@ -172,9 +291,16 @@ namespace rds {
|
||||
std::string programServiceName = " ";
|
||||
|
||||
// Group type 2
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> group2LastUpdate;
|
||||
std::mutex group2Mtx;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> group2LastUpdate{}; // 1970-01-01
|
||||
bool rtAB = false;
|
||||
std::string radioText = " ";
|
||||
|
||||
// Group type 10
|
||||
std::mutex group10Mtx;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> group10LastUpdate{}; // 1970-01-01
|
||||
bool ptnAB = false;
|
||||
std::string programTypeName = " ";
|
||||
|
||||
};
|
||||
}
|
102
decoder_modules/radio/src/rds_demod.h
Normal file
102
decoder_modules/radio/src/rds_demod.h
Normal file
@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
#include <dsp/processor.h>
|
||||
#include <dsp/loop/fast_agc.h>
|
||||
#include <dsp/loop/costas.h>
|
||||
#include <dsp/taps/band_pass.h>
|
||||
#include <dsp/filter/fir.h>
|
||||
#include <dsp/convert/complex_to_real.h>
|
||||
#include <dsp/clock_recovery/mm.h>
|
||||
#include <dsp/digital/binary_slicer.h>
|
||||
#include <dsp/digital/differential_decoder.h>
|
||||
|
||||
class RDSDemod : public dsp::Processor<dsp::complex_t, uint8_t> {
|
||||
using base_type = dsp::Processor<dsp::complex_t, uint8_t>;
|
||||
public:
|
||||
RDSDemod() {}
|
||||
RDSDemod(dsp::stream<dsp::complex_t>* in, bool enableSoft) { init(in, enableSoft); }
|
||||
~RDSDemod() {}
|
||||
|
||||
void init(dsp::stream<dsp::complex_t>* in, bool enableSoft) {
|
||||
// Save config
|
||||
this->enableSoft = enableSoft;
|
||||
|
||||
// Initialize the DSP
|
||||
agc.init(NULL, 1.0, 1e6, 0.1);
|
||||
costas.init(NULL, 0.005f);
|
||||
taps = dsp::taps::bandPass<dsp::complex_t>(0, 2375, 100, 5000);
|
||||
fir.init(NULL, taps);
|
||||
double baudfreq = dsp::math::hzToRads(2375.0/2.0, 5000);
|
||||
costas2.init(NULL, 0.01, 0.0, baudfreq, baudfreq - (baudfreq*0.1), baudfreq + (baudfreq*0.1));
|
||||
recov.init(NULL, 5000.0 / (2375.0 / 2.0), 1e-6, 0.01, 0.01);
|
||||
diff.init(NULL, 2);
|
||||
|
||||
// Free useless buffers
|
||||
agc.out.free();
|
||||
fir.out.free();
|
||||
costas2.out.free();
|
||||
recov.out.free();
|
||||
|
||||
// Init the rest
|
||||
base_type::init(in);
|
||||
}
|
||||
|
||||
void setSoftEnabled(bool enable) {
|
||||
assert(base_type::_block_init);
|
||||
std::lock_guard<std::recursive_mutex> lck(base_type::ctrlMtx);
|
||||
base_type::tempStop();
|
||||
enableSoft = enable;
|
||||
base_type::tempStart();
|
||||
}
|
||||
|
||||
void reset() {
|
||||
assert(base_type::_block_init);
|
||||
std::lock_guard<std::recursive_mutex> lck(base_type::ctrlMtx);
|
||||
base_type::tempStop();
|
||||
agc.reset();
|
||||
costas.reset();
|
||||
fir.reset();
|
||||
costas2.reset();
|
||||
recov.reset();
|
||||
diff.reset();
|
||||
base_type::tempStart();
|
||||
}
|
||||
|
||||
inline int process(int count, dsp::complex_t* in, float* softOut, uint8_t* hardOut) {
|
||||
count = agc.process(count, in, costas.out.readBuf);
|
||||
count = costas.process(count, costas.out.readBuf, costas.out.writeBuf);
|
||||
count = fir.process(count, costas.out.writeBuf, costas.out.writeBuf);
|
||||
count = costas2.process(count, costas.out.writeBuf, costas.out.readBuf);
|
||||
count = dsp::convert::ComplexToReal::process(count, costas.out.readBuf, softOut);
|
||||
count = recov.process(count, softOut, softOut);
|
||||
count = dsp::digital::BinarySlicer::process(count, softOut, diff.out.readBuf);
|
||||
count = diff.process(count, diff.out.readBuf, hardOut);
|
||||
return count;
|
||||
}
|
||||
|
||||
int run() {
|
||||
int count = base_type::_in->read();
|
||||
if (count < 0) { return -1; }
|
||||
|
||||
count = process(count, base_type::_in->readBuf, soft.writeBuf, base_type::out.writeBuf);
|
||||
|
||||
base_type::_in->flush();
|
||||
if (!base_type::out.swap(count)) { return -1; }
|
||||
if (enableSoft) {
|
||||
if (!soft.swap(count)) { return -1; }
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
dsp::stream<float> soft;
|
||||
|
||||
private:
|
||||
bool enableSoft = false;
|
||||
|
||||
dsp::loop::FastAGC<dsp::complex_t> agc;
|
||||
dsp::loop::Costas<2> costas;
|
||||
dsp::tap<dsp::complex_t> taps;
|
||||
dsp::filter::FIR<dsp::complex_t, dsp::complex_t> fir;
|
||||
dsp::loop::Costas<2> costas2;
|
||||
dsp::clock_recovery::MM<float> recov;
|
||||
dsp::digital::DifferentialDecoder diff;
|
||||
};
|
Reference in New Issue
Block a user