Skip to content

Commit

Permalink
band relays config for hl2, test commit, may break things
Browse files Browse the repository at this point in the history
  • Loading branch information
sannysanoff committed Feb 5, 2024
1 parent a8fbc68 commit 54aa33b
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 13 deletions.
153 changes: 153 additions & 0 deletions source_modules/hl2_source/src/bandconfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@

#include <bandconfig.h>
#include <gui/smgui.h>

AllBands allBandsConfig = {
{
{ "160M", 0, 200000, 1 },
{ "80M", 200000, (int)4e6, 2 },
{ "60M", (int)4e6, (int)6e6, 4 },
{ "40M", (int)6e6, (int)9e6, 4 },
{ "30M", (int)9e6, (int)12e6, 8 },
{ "20M", (int)12e6, (int)16e6, 8 },
{ "17M", (int)16e6, (int)19e6, 16 },
{ "15M", (int)19e6, (int)23e6, 16 },
{ "12M", (int)23e6, (int)25e6, 32 },
{ "10M", (int)25e6, (int)60e6, 32 },
},
};

AllBands rxBands;
AllBands txBands;

void initBands() {
rxBands = allBandsConfig;
txBands = allBandsConfig;
}

json toJson(const BandSpec& data) {
json c;
c["label"] = data.label;
c["low"] = data.lowRange;
c["high"] = data.highRange;
c["bits"] = data.bits;
return c;
}
json toJson(const AllBands& data) {
auto arr = json::array();
for (auto& v : data.values) {
arr.push_back(toJson(v));
}
return arr;
}

BandSpec parseBandSpec(json j) {
BandSpec rv;
if (j.contains("label")) {
rv.label = j["label"];
}
else {
rv.label = "";
}
if (j.contains("low")) {
rv.lowRange = j["low"];
}
else {
rv.lowRange = 0;
}
if (j.contains("high")) {
rv.highRange = j["high"];
}
else {
rv.lowRange = 0;
}
if (j.contains("bits")) {
rv.bits = j["bits"];
}
else {
rv.lowRange = 0;
}
return rv;
}

AllBands parseAllBands(json j) {
AllBands rv;
if (j.is_array()) {
for (const auto & r : j) {
auto bs = parseBandSpec(r);
if (!bs.label.empty()) {
rv.values.emplace_back(bs);
}
}
}
return rv;
}

void saveBandsConfig(ConfigManager &config) {
json rxtx;
rxtx["tx"] = toJson(txBands);
rxtx["rx"] = toJson(rxBands);
config.conf["bandsBits"] = rxtx;
}

// returns if some change happened
bool renderCheckboxes(ConfigManager &manager, AllBands &bands, int blockOffset) {
bool retval = false;
for (auto &b : bands.values) {
bool bits[8];
for(int i=0; i<7; i++) {
bits[i] = ((b.bits >> i) & 0x01)!= 0;
char theId[100];
snprintf(theId, sizeof(theId), "##hl2bcb_%d_%s_%d", blockOffset, b.label.c_str(), i);
if (SmGui::Checkbox(theId, &bits[i])) {
retval = true;
if (bits[i]) {
b.bits |= (int)(0x1 << i);
} else {
b.bits &= (~(int)(0x1 << i)) & 0xFF;
}
manager.acquire();
saveBandsConfig(manager);
manager.release(true);
}
SmGui::SameLine();
}
SmGui::Text(b.label.c_str());
}
return retval;
}

int getBitsForBand(int frequency, bool tx) {
AllBands &scan = tx ? txBands : rxBands;
for (auto &b : scan.values) {
if (frequency >= b.lowRange && frequency < b.highRange) {
return b.bits;
}
}
return 0;
}

bool bandsEditor(ConfigManager &config) {
SmGui::Text("Bands relays config");
SmGui::Text("RX mode:");
auto changed = renderCheckboxes(config, rxBands, 1);
SmGui::Text("TX mode:");
changed |= renderCheckboxes(config, txBands, 2);
return changed;
}


void loadBandsConfig(ConfigManager &config) {
if (config.conf.contains("bandsBits")) {
auto bits = config.conf["bandsBits"];
auto newRX = parseAllBands(bits["rx"]);
auto newTX = parseAllBands(bits["tx"]);
if (newRX.values.empty() || newTX.values.empty()) {
// ignore
} else {
rxBands = newRX;
txBands = newTX;
}
}
}

24 changes: 24 additions & 0 deletions source_modules/hl2_source/src/bandconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <module.h>
#include <gui/gui.h>
#include <signal_path/signal_path.h>
#include <core.h>
#include <config.h>


struct BandSpec {
std::string label;
int lowRange, highRange; // in hz
int bits;
};

struct AllBands {
std::vector<BandSpec> values;
};


bool bandsEditor(ConfigManager &config);
void loadBandsConfig(ConfigManager &config);
void initBands();
int getBitsForBand(int frequency, bool tx);
3 changes: 2 additions & 1 deletion source_modules/hl2_source/src/hl2_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ struct HL2Device {



void setSevenRelays(int sevenRelays) {
// by default, in original hermes lite, 160 band is 1 << 1, 80 band is 2 << 1, 40 band is 4 << 1 etc.
void setSevenRelays(int sevenRelays) { // sevenRelays is 7-bit value (low bits).
deviceControl[0x0].C2 &= 1;
deviceControl[0x0].C2 |= sevenRelays << 1;
deviceControlDirty[0x0] = 1;
Expand Down
48 changes: 36 additions & 12 deletions source_modules/hl2_source/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "hl2_device.h"
#include "utils/stream_tracker.h"
#include "bandconfig.h"

#define CONCAT(a, b) ((std::string(a) + (b)).c_str())

Expand Down Expand Up @@ -58,6 +59,8 @@ class HermesLite2SourceModule : public ModuleManager::Instance, public Transmitt
handler.tuneHandler = tune;
handler.stream = &stream;

initBands();


config.acquire();
std::string devSerial = config.conf["device"];
Expand All @@ -71,6 +74,7 @@ class HermesLite2SourceModule : public ModuleManager::Instance, public Transmitt
if (config.conf.contains("directIP")) {
directIP = config.conf["directIP"];
}
loadBandsConfig(config);
config.release();

refresh();
Expand Down Expand Up @@ -244,7 +248,7 @@ class HermesLite2SourceModule : public ModuleManager::Instance, public Transmitt
std::vector<dsp::complex_t> incomingBuffer;

StreamTracker hermesSamples;
bool showStaticIp = false;
bool showMore = false;
bool directIP = false;
bool scanIP = true;
char staticIp[20]={0};
Expand Down Expand Up @@ -320,10 +324,21 @@ class HermesLite2SourceModule : public ModuleManager::Instance, public Transmitt
sigpath::transmitter = nullptr;
}


void updateBandRelays() {
if (device) {
auto istx = this->getTXStatus();
auto bits = getBitsForBand(tunedFrequency, istx);
device->setSevenRelays(bits);
}
}

static void tune(double freq, void* ctx) {
auto* _this = (HermesLite2SourceModule*)ctx;
if (_this->device) {
_this->device->setFrequency((int)freq);
_this->tunedFrequency = (int)freq;
_this->updateBandRelays();
}
flog::info("HermerList2SourceModule '{0}': Tune: {1}!", _this->name, freq);
}
Expand All @@ -350,11 +365,11 @@ class HermesLite2SourceModule : public ModuleManager::Instance, public Transmitt
uiLock.unlock();
SmGui::SameLine();
SmGui::FillWidth();
if (SmGui::Button("Methods")) {
showStaticIp = !showStaticIp;
if (SmGui::Button("More")) {
showMore = !showMore;
}

if (showStaticIp) {
if (showMore) {
SmGui::LeftLabel("Probe IP:");
SmGui::SameLine();
SmGui::FillWidth();
Expand All @@ -374,6 +389,10 @@ class HermesLite2SourceModule : public ModuleManager::Instance, public Transmitt
config.release(true);
}

if (bandsEditor(config)) {
updateBandRelays();
}

}

auto updateSampleRate = [&](int srid) {
Expand Down Expand Up @@ -430,16 +449,17 @@ class HermesLite2SourceModule : public ModuleManager::Instance, public Transmitt
config.release(true);
}
}
/*
for(int q=0; q<6; q++) {
char strr[100];
switch(q) {
case 0: sprintf(strr, "=160"); break;
case 1: sprintf(strr, "=80"); break;
case 2: sprintf(strr, "=40"); break;
case 3: sprintf(strr, "=20"); break;
case 4: sprintf(strr, "=15"); break;
case 5: sprintf(strr, "=10"); break;
default: sprintf(strr, "???"); break;
case 0: snprintf(strr, sizeof(strr), "=160"); break;
case 1: snprintf(strr, sizeof(strr), "=80"); break;
case 2: snprintf(strr, sizeof(strr), "=40"); break;
case 3: snprintf(strr, sizeof(strr), "=20"); break;
case 4: snprintf(strr, sizeof(strr), "=15"); break;
case 5: snprintf(strr, sizeof(strr), "=10"); break;
default: snprintf(strr, sizeof(strr), "???"); break;
}
if (q >=0) {
if (SmGui::RadioButton(strr, sevenRelays[q])) {
Expand Down Expand Up @@ -467,7 +487,9 @@ class HermesLite2SourceModule : public ModuleManager::Instance, public Transmitt
if (q != 5 && q != 2) {
SmGui::SameLine();
}
}
*/

//}

}

Expand All @@ -487,6 +509,7 @@ class HermesLite2SourceModule : public ModuleManager::Instance, public Transmitt
std::vector<uint32_t> sampleRateList;
std::string sampleRateListTxt;
std::shared_ptr<HL2Device> device;
int tunedFrequency = 0;


int getInputStreamFramerate() override {
Expand All @@ -495,6 +518,7 @@ class HermesLite2SourceModule : public ModuleManager::Instance, public Transmitt
void setTransmitStatus(bool status) override {
device->setTune(false);
device->setPTT(status);
updateBandRelays();

}
void setTransmitStream(dsp::stream<dsp::complex_t>* astream) override {
Expand Down

0 comments on commit 54aa33b

Please sign in to comment.