Skip to content

Commit

Permalink
Add setting to change bandwidth narrow or wide. Implements issue #126.
Browse files Browse the repository at this point in the history
  • Loading branch information
VanceVagell committed Dec 14, 2024
1 parent a022c8a commit 1b76e78
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public class RadioAudioService extends Service {
private int activeMemoryId = -1; // -1 means we're in simplex mode
private static int maxFreq = 148; // in MHz
private MicGainBoost micGainBoost = MicGainBoost.NONE;
private String bandwidth = "Wide";

// Safety constants
private static int RUNAWAY_TX_TIMEOUT_SEC = 180; // Stop runaway tx after 3 minutes
Expand Down Expand Up @@ -278,6 +279,10 @@ public void setMicGainBoost(String micGainBoost) {
this.micGainBoost = MicGainBoost.parse(micGainBoost);
}

public void setBandwidth(String bandwidth) {
this.bandwidth = bandwidth;
}

public void setRxSampleRate(String rxSampleRate) {
this.rxSampleRate = SampleRate.parse(rxSampleRate);
initAudioTrack();
Expand Down Expand Up @@ -498,7 +503,8 @@ public void tuneToFreq(String frequencyStr, int squelchLevel, boolean forceTune)
sendCommandToESP32(ESP32Command.TUNE_TO, makeSafe2MFreq(activeFrequencyStr) +
makeSafe2MFreq(activeFrequencyStr) + "00" + squelchLevel +
String.format("%05d", (int) (SampleRate.toInt(rxSampleRate) * rxSampleRateMult)) + // e.g. "44100"
String.format("%05d", SampleRate.toInt(txSampleRate))); // e.g. "44100"
String.format("%05d", SampleRate.toInt(txSampleRate)) + // e.g. "44100"
(bandwidth.equals("Wide") ? "W" : "N"));
}

// Reset audio prebuffer
Expand Down Expand Up @@ -589,7 +595,8 @@ public void tuneToMemory(ChannelMemory memory, int squelchLevel, boolean forceTu
makeSafe2MFreq(memory.frequency) +
getToneIdxStr(memory.tone) + squelchLevel +
String.format("%05d", (int) (SampleRate.toInt(rxSampleRate) * rxSampleRateMult)) + // e.g. "44100"
String.format("%05d", SampleRate.toInt(txSampleRate))); // e.g. "44100"
String.format("%05d", SampleRate.toInt(txSampleRate)) + // e.g. "44100"
(bandwidth.equals("Wide") ? "W" : "N"));
}

// Reset audio prebuffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ public void run() {
AppSetting lowpassSetting = viewModel.appDb.appSettingDao().getByName("lowpass");
AppSetting stickyPTTSetting = viewModel.appDb.appSettingDao().getByName("stickyPTT");
AppSetting disableAnimationsSetting = viewModel.appDb.appSettingDao().getByName("disableAnimations");
AppSetting bandwidthSetting = viewModel.appDb.appSettingDao().getByName("bandwidth");
AppSetting maxFreqSetting = viewModel.appDb.appSettingDao().getByName("maxFreq");
AppSetting micGainBoostSetting = viewModel.appDb.appSettingDao().getByName("micGainBoost");
AppSetting rxSampleRateSetting = viewModel.appDb.appSettingDao().getByName("rxSampleRate");
Expand Down Expand Up @@ -897,6 +898,12 @@ public void run() {
}
}

if (bandwidthSetting != null) {
if (radioAudioService != null) {
radioAudioService.setBandwidth(bandwidthSetting.value);
}
}

if (maxFreqSetting != null) {
int maxFreq = Integer.parseInt(maxFreqSetting.value);
RadioAudioService.setMaxFreq(maxFreq); // Called statically so static frequency formatter can use it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected void onCreate(Bundle savedInstanceState) {
2, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

populateOriginalValues();
populateBandwidths();
populateMaxFrequencies();
populateMicGainOptions();
populateSampleRateOptions();
Expand All @@ -88,6 +89,17 @@ protected void onResume() {
2, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
}

private void populateBandwidths() {
AutoCompleteTextView bandwidthTextView = findViewById(R.id.bandwidthTextView);

List<String> bandwidths = new ArrayList<String>();
bandwidths.add("Wide");
bandwidths.add("Narrow");

ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.dropdown_item, bandwidths);
bandwidthTextView.setAdapter(arrayAdapter);
}

private void populateMaxFrequencies() {
AutoCompleteTextView maxFreqTextView = findViewById(R.id.maxFreqTextView);

Expand Down Expand Up @@ -141,6 +153,7 @@ public void run() {
AppSetting lowpassSetting = MainViewModel.appDb.appSettingDao().getByName("lowpass");
AppSetting stickyPTTSetting = MainViewModel.appDb.appSettingDao().getByName("stickyPTT");
AppSetting disableAnimationsSetting = MainViewModel.appDb.appSettingDao().getByName("disableAnimations");
AppSetting bandwidthSetting = MainViewModel.appDb.appSettingDao().getByName("bandwidth");
AppSetting maxFreqSetting = MainViewModel.appDb.appSettingDao().getByName("maxFreq");
AppSetting micGainBoostSetting = MainViewModel.appDb.appSettingDao().getByName("micGainBoost");
AppSetting rxSampleRateSetting = MainViewModel.appDb.appSettingDao().getByName("rxSampleRate");
Expand Down Expand Up @@ -185,6 +198,11 @@ public void run() {
noAnimationsSwitch.setChecked(Boolean.parseBoolean(disableAnimationsSetting.value));
}

if (bandwidthSetting != null) {
AutoCompleteTextView bandwidthTextVIew = (AutoCompleteTextView) findViewById(R.id.bandwidthTextView);
bandwidthTextVIew.setText(bandwidthSetting.value, false);
}

if (maxFreqSetting != null) {
AutoCompleteTextView maxFreqTextView = (AutoCompleteTextView) findViewById(R.id.maxFreqTextView);
maxFreqTextView.setText(maxFreqSetting.value + "MHz", false);
Expand Down Expand Up @@ -304,6 +322,23 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});

TextView bandwidthTextView = findViewById(R.id.bandwidthTextView);
bandwidthTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
String newText = ((TextView) findViewById(R.id.bandwidthTextView)).getText().toString().trim();
setBandwidth(newText);
}
});

TextView maxFreqTextView = findViewById(R.id.maxFreqTextView);
maxFreqTextView.addTextChangedListener(new TextWatcher() {
@Override
Expand Down Expand Up @@ -390,6 +425,30 @@ public void afterTextChanged(Editable s) {
});
}

/**
* @param bandwidth Change the bandwidth to use, either "Wide" or "Narrow". (25kHz or 12.5kHz)
*/
private void setBandwidth(String bandwidth) {
if (threadPoolExecutor == null) {
return;
}

threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
AppSetting setting = MainViewModel.appDb.appSettingDao().getByName("bandwidth");

if (setting == null) {
setting = new AppSetting("bandwidth", bandwidth);
MainViewModel.appDb.appSettingDao().insertAll(setting);
} else {
setting.value = bandwidth;
MainViewModel.appDb.appSettingDao().update(setting);
}
}
});
}

/**
* @param maxFreq Megahertz as a string, e.g. "148".
*/
Expand Down
39 changes: 39 additions & 0 deletions android-src/KV4PHT/app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,45 @@
android:textSize="16dp"
android:layout_marginTop="24dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/jost"
android:text="Bandwidth"
android:textColor="@color/primary"
android:textSize="18dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:labelFor="@id/bandwidthTextView"/>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:padding="0dp"
android:layout_weight="0"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
app:hintTextColor="@color/primary_deselected"
app:boxStrokeColor="@color/primary"
android:textColorHint="@color/primary_deselected">

<AutoCompleteTextView
android:id="@+id/bandwidthTextView"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:inputType="none"
android:text="Wide"
android:textSize="20dp"
android:textColor="@color/primary"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void loop() {
// If we haven't received all the parameters needed for COMMAND_TUNE_TO, wait for them before continuing.
// This can happen if ESP32 has pulled part of the command+params from the buffer before Android has completed
// putting them in there. If so, we take byte-by-byte until we get the full params.
int paramBytesMissing = 29;
int paramBytesMissing = 30;
String paramsStr = "";
if (paramBytesMissing > 0) {
uint8_t paramPartsBuffer[paramBytesMissing];
Expand All @@ -256,18 +256,17 @@ void loop() {
}

// Example:
// 145.4500144.85000614410044100
// 8 chars for tx, 8 chars for rx, 2 chars for tone, 1 char for squelch, 5 char for rx sample rate, 5 char for tx sample rate (29 bytes total for params)
// 145.4500144.85000614410044100W
// 8 chars for tx, 8 chars for rx, 2 chars for tone, 1 char for squelch, 5 char for rx sample rate, 5 char for tx sample rate, 1 for bandwidth W/N (30 bytes total for params)
float freqTxFloat = paramsStr.substring(0, 8).toFloat();
float freqRxFloat = paramsStr.substring(8, 16).toFloat();
int toneInt = paramsStr.substring(16, 18).toInt();
int squelchInt = paramsStr.substring(18, 19).toInt();
int newRxSampleRate = paramsStr.substring(19, 24).toInt();
int newTxSampleRate = paramsStr.substring(24, 29).toInt();
String bandwidth = paramsStr.substring(29, 30);

// Serial.println("PARAMS: " + paramsStr.substring(0, 16) + " freqTxFloat: " + String(freqTxFloat) + " freqRxFloat: " + String(freqRxFloat) + " toneInt: " + String(toneInt));

tuneTo(freqTxFloat, freqRxFloat, toneInt, squelchInt, newRxSampleRate, newTxSampleRate);
tuneTo(freqTxFloat, freqRxFloat, toneInt, squelchInt, newRxSampleRate, newTxSampleRate, bandwidth);
}
break;
case COMMAND_FILTERS:
Expand Down Expand Up @@ -333,7 +332,7 @@ void loop() {
// If we haven't received all the parameters needed for COMMAND_TUNE_TO, wait for them before continuing.
// This can happen if ESP32 has pulled part of the command+params from the buffer before Android has completed
// putting them in there. If so, we take byte-by-byte until we get the full params.
int paramBytesMissing = 29;
int paramBytesMissing = 30;
String paramsStr = "";
if (paramBytesMissing > 0) {
uint8_t paramPartsBuffer[paramBytesMissing];
Expand All @@ -353,18 +352,17 @@ void loop() {
}

// Example:
// 145.4500144.85000614410044100
// 8 chars for tx, 8 chars for rx, 2 chars for tone, 1 char for squelch, 1 char for rx sample rate, 1 char for tx sample rate (29 bytes total for params)
// 145.4500144.85000614410044100W
// 8 chars for tx, 8 chars for rx, 2 chars for tone, 1 char for squelch, 5 char for rx sample rate, 5 char for tx sample rate, 1 for bandwidth W/N (30 bytes total for params)
float freqTxFloat = paramsStr.substring(0, 8).toFloat();
float freqRxFloat = paramsStr.substring(8, 16).toFloat();
int toneInt = paramsStr.substring(16, 18).toInt();
int squelchInt = paramsStr.substring(18, 19).toInt();
int newRxSampleRate = paramsStr.substring(19, 24).toInt();
int newTxSampleRate = paramsStr.substring(24, 29).toInt();
String bandwidth = paramsStr.substring(29, 30);

// Serial.println("PARAMS: " + paramsStr.substring(0, 16) + " freqTxFloat: " + String(freqTxFloat) + " freqRxFloat: " + String(freqRxFloat) + " toneInt: " + String(toneInt));

tuneTo(freqTxFloat, freqRxFloat, toneInt, squelchInt, newRxSampleRate, newTxSampleRate);
tuneTo(freqTxFloat, freqRxFloat, toneInt, squelchInt, newRxSampleRate, newTxSampleRate, bandwidth);
}
break;
case COMMAND_FILTERS:
Expand Down Expand Up @@ -524,14 +522,19 @@ void loop() {
}
}

void tuneTo(float freqTx, float freqRx, int tone, int squelch, int newRxSampleRate, int newTxSampleRate) {
void tuneTo(float freqTx, float freqRx, int tone, int squelch, int newRxSampleRate, int newTxSampleRate, String bandwidth) {
rxAudioSampleRate = newRxSampleRate;
txAudioSampleRate = newTxSampleRate;

initI2SRx();

// Tell radio module to tune
int result = dra->group(DRA818_25K, freqTx, freqRx, tone, squelch, 0);
int result = 0;
if (bandwidth.equals("W")) {
result = dra->group(DRA818_25K, freqTx, freqRx, tone, squelch, 0);
} else if (bandwidth.equals("N")) {
result = dra->group(DRA818_12K5, freqTx, freqRx, tone, squelch, 0);
}
// Serial.println("tuneTo: " + String(result));
}

Expand Down

0 comments on commit 1b76e78

Please sign in to comment.