|
| 1 | +#include <imgui.h> |
| 2 | +#include <config.h> |
| 3 | +#include <core.h> |
| 4 | +#include <gui/style.h> |
| 5 | +#include <gui/gui.h> |
| 6 | +#include <signal_path/signal_path.h> |
| 7 | +#include <module.h> |
| 8 | +#include <gui/widgets/folder_select.h> |
| 9 | +#include <utils/optionlist.h> |
| 10 | +#include "decoder.h" |
| 11 | +#include "pocsag/decoder.h" |
| 12 | +#include "flex/decoder.h" |
| 13 | + |
| 14 | +#define CONCAT(a, b) ((std::string(a) + b).c_str()) |
| 15 | + |
| 16 | +SDRPP_MOD_INFO{ |
| 17 | + /* Name: */ "pager_decoder", |
| 18 | + /* Description: */ "POCSAG and Flex Pager Decoder" |
| 19 | + /* Author: */ "Ryzerth", |
| 20 | + /* Version: */ 0, 1, 0, |
| 21 | + /* Max instances */ -1 |
| 22 | +}; |
| 23 | + |
| 24 | +ConfigManager config; |
| 25 | + |
| 26 | +enum Protocol { |
| 27 | + PROTOCOL_INVALID = -1, |
| 28 | + PROTOCOL_POCSAG, |
| 29 | + PROTOCOL_FLEX |
| 30 | +}; |
| 31 | + |
| 32 | +class PagerDecoderModule : public ModuleManager::Instance { |
| 33 | +public: |
| 34 | + PagerDecoderModule(std::string name) { |
| 35 | + this->name = name; |
| 36 | + |
| 37 | + // Define protocols |
| 38 | + protocols.define("POCSAG", PROTOCOL_POCSAG); |
| 39 | + protocols.define("FLEX", PROTOCOL_FLEX); |
| 40 | + |
| 41 | + // Initialize VFO with default values |
| 42 | + vfo = sigpath::vfoManager.createVFO(name, ImGui::WaterfallVFO::REF_CENTER, 0, 12500, 24000, 12500, 12500, true); |
| 43 | + vfo->setSnapInterval(1); |
| 44 | + |
| 45 | + // Select the protocol |
| 46 | + selectProtocol(PROTOCOL_POCSAG); |
| 47 | + |
| 48 | + gui::menu.registerEntry(name, menuHandler, this, this); |
| 49 | + } |
| 50 | + |
| 51 | + ~PagerDecoderModule() { |
| 52 | + gui::menu.removeEntry(name); |
| 53 | + // Stop DSP |
| 54 | + if (enabled) { |
| 55 | + decoder->stop(); |
| 56 | + decoder.reset(); |
| 57 | + sigpath::vfoManager.deleteVFO(vfo); |
| 58 | + } |
| 59 | + |
| 60 | + sigpath::sinkManager.unregisterStream(name); |
| 61 | + } |
| 62 | + |
| 63 | + void postInit() {} |
| 64 | + |
| 65 | + void enable() { |
| 66 | + double bw = gui::waterfall.getBandwidth(); |
| 67 | + vfo = sigpath::vfoManager.createVFO(name, ImGui::WaterfallVFO::REF_CENTER, std::clamp<double>(0, -bw / 2.0, bw / 2.0), 12500, 24000, 12500, 12500, true); |
| 68 | + vfo->setSnapInterval(1); |
| 69 | + |
| 70 | + decoder->setVFO(vfo); |
| 71 | + decoder->start(); |
| 72 | + |
| 73 | + enabled = true; |
| 74 | + } |
| 75 | + |
| 76 | + void disable() { |
| 77 | + decoder->stop(); |
| 78 | + sigpath::vfoManager.deleteVFO(vfo); |
| 79 | + enabled = false; |
| 80 | + } |
| 81 | + |
| 82 | + bool isEnabled() { |
| 83 | + return enabled; |
| 84 | + } |
| 85 | + |
| 86 | + void selectProtocol(Protocol newProto) { |
| 87 | + // Cannot change while disabled |
| 88 | + if (!enabled) { return; } |
| 89 | + |
| 90 | + // If the protocol hasn't changed, no need to do anything |
| 91 | + if (newProto == proto) { return; } |
| 92 | + |
| 93 | + // Delete current decoder |
| 94 | + decoder.reset(); |
| 95 | + |
| 96 | + // Create a new decoder |
| 97 | + switch (newProto) { |
| 98 | + case PROTOCOL_POCSAG: |
| 99 | + decoder = std::make_unique<POCSAGDecoder>(name, vfo); |
| 100 | + break; |
| 101 | + case PROTOCOL_FLEX: |
| 102 | + decoder = std::make_unique<FLEXDecoder>(name, vfo); |
| 103 | + break; |
| 104 | + default: |
| 105 | + flog::error("Tried to select unknown pager protocol"); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + // Start the new decoder |
| 110 | + decoder->start(); |
| 111 | + |
| 112 | + // Save selected protocol |
| 113 | + proto = newProto; |
| 114 | + } |
| 115 | + |
| 116 | +private: |
| 117 | + static void menuHandler(void* ctx) { |
| 118 | + PagerDecoderModule* _this = (PagerDecoderModule*)ctx; |
| 119 | + |
| 120 | + float menuWidth = ImGui::GetContentRegionAvail().x; |
| 121 | + |
| 122 | + if (!_this->enabled) { style::beginDisabled(); } |
| 123 | + |
| 124 | + ImGui::LeftLabel("Protocol"); |
| 125 | + ImGui::FillWidth(); |
| 126 | + if (ImGui::Combo(("##pager_decoder_proto_" + _this->name).c_str(), &_this->protoId, _this->protocols.txt)) { |
| 127 | + _this->selectProtocol(_this->protocols.value(_this->protoId)); |
| 128 | + } |
| 129 | + |
| 130 | + if (_this->decoder) { _this->decoder->showMenu(); } |
| 131 | + |
| 132 | + ImGui::Button(("Record##pager_decoder_show_" + _this->name).c_str(), ImVec2(menuWidth, 0)); |
| 133 | + ImGui::Button(("Show Messages##pager_decoder_show_" + _this->name).c_str(), ImVec2(menuWidth, 0)); |
| 134 | + |
| 135 | + if (!_this->enabled) { style::endDisabled(); } |
| 136 | + } |
| 137 | + |
| 138 | + std::string name; |
| 139 | + bool enabled = true; |
| 140 | + |
| 141 | + Protocol proto = PROTOCOL_INVALID; |
| 142 | + int protoId = 0; |
| 143 | + |
| 144 | + OptionList<std::string, Protocol> protocols; |
| 145 | + |
| 146 | + // DSP Chain |
| 147 | + VFOManager::VFO* vfo; |
| 148 | + std::unique_ptr<Decoder> decoder; |
| 149 | + |
| 150 | + bool showLines = false; |
| 151 | +}; |
| 152 | + |
| 153 | +MOD_EXPORT void _INIT_() { |
| 154 | + // Create default recording directory |
| 155 | + json def = json({}); |
| 156 | + config.setPath(core::args["root"].s() + "/pager_decoder_config.json"); |
| 157 | + config.load(def); |
| 158 | + config.enableAutoSave(); |
| 159 | +} |
| 160 | + |
| 161 | +MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) { |
| 162 | + return new PagerDecoderModule(name); |
| 163 | +} |
| 164 | + |
| 165 | +MOD_EXPORT void _DELETE_INSTANCE_(void* instance) { |
| 166 | + delete (PagerDecoderModule*)instance; |
| 167 | +} |
| 168 | + |
| 169 | +MOD_EXPORT void _END_() { |
| 170 | + config.disableAutoSave(); |
| 171 | + config.save(); |
| 172 | +} |
0 commit comments