Skip to content

Commit

Permalink
Finish adding UHF support (at least the initial pass, critical stuff)…
Browse files Browse the repository at this point in the history
…. This includes work done by GitHub user aldebaran27 in PR #182 which were cherry-picked to be used in the WIP version of this.
  • Loading branch information
VanceVagell committed Jan 21, 2025
1 parent 2740de9 commit 3c9a273
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,25 @@ public class RadioAudioService extends Service {
private Runnable aprsBeaconRunnable = null;

// Radio params and related settings
private String activeFrequencyStr = "144.000";
private static final float VHF_MIN_FREQ = 134.0f; // DRA818V lower limit, in MHz
private static final float VHF_MIN_FREQ_US = 144.0f; // US 2m band lower limit, in MHz
private static final float VHF_MAX_FREQ_US = 148.0f; // US 2m band upper limit, in MHz
private static final float VHF_MAX_FREQ = 174.0f; // DRA818V upper limit, in MHz

private static final float UHF_MIN_FREQ = 400.0f; // DRA818U lower limit, in MHz
private static final float UHF_MIN_FREQ_US = 420.0f; // US 70cm band lower limit, in MHz
private static final float UHF_MAX_FREQ_US = 450.0f; // US 70cm band upper limit, in MHz
private static final float UHF_MAX_FREQ = 470.0f; // DRA818U upper limit, in MHz

private String activeFrequencyStr = String.format(java.util.Locale.US, "%.4f", VHF_MIN_FREQ_US); // 4 decimal places, in MHz
private int squelch = 0;
private String callsign = null;
private int consecutiveSilenceBytes = 0; // To determine when to move scan after silence
private int activeMemoryId = -1; // -1 means we're in simplex mode
private static int maxFreq = 148; // in MHz // TODO replace this with getMaxFreq() and base it on radioType. also need getMinFreq().
private static float minRadioFreq = VHF_MIN_FREQ; // in MHz
private static float maxRadioFreq = VHF_MAX_FREQ; // in MHz
private static float minHamFreq = VHF_MIN_FREQ_US; // in MHz
private static float maxHamFreq = VHF_MAX_FREQ_US; // in MHz
private MicGainBoost micGainBoost = MicGainBoost.NONE;
private String bandwidth = "Wide";
private boolean txAllowed = true;
Expand Down Expand Up @@ -289,8 +302,32 @@ public void setBandwidth(String bandwidth) {
this.bandwidth = bandwidth;
}

public static void setMaxFreq(int newMaxFreq) {
maxFreq = newMaxFreq;
public void setMinRadioFreq(float newMinFreq) {
minRadioFreq = newMinFreq;

// Detect if we're moving from VHF to UHF, and move fix active frequency to within band.
if (activeFrequencyStr != null && Float.parseFloat(activeFrequencyStr) < minRadioFreq) {
tuneToFreq(String.format(java.util.Locale.US, "%.4f", UHF_MIN_FREQ_US), squelch, true);
callbacks.forceTunedToFreq(activeFrequencyStr);
}
}

public void setMaxRadioFreq(float newMaxFreq) {
maxRadioFreq = newMaxFreq;

// Detect if we're moving from UHF to VHF, and move fix active frequency to within band.
if (activeFrequencyStr != null && Float.parseFloat(activeFrequencyStr) > maxRadioFreq) {
tuneToFreq(String.format(java.util.Locale.US, "%.4f", VHF_MIN_FREQ_US), squelch, true);
callbacks.forceTunedToFreq(activeFrequencyStr);
}
}

public void setMinFreq(float newMinFreq) {
minHamFreq = newMinFreq;
}

public void setMaxFreq(float newMaxFreq) {
maxHamFreq = newMaxFreq;
}

public void setAprsBeaconPosition(boolean aprsBeaconPosition) {
Expand Down Expand Up @@ -439,6 +476,7 @@ public interface RadioAudioServiceCallbacks {
public void aprsBeaconing(boolean beaconing, int accuracy);
public void sentAprsBeacon(double latitude, double longitude);
public void unknownLocation();
public void forceTunedToFreq(String newFreqStr);
}

public void setCallbacks(RadioAudioServiceCallbacks callbacks) {
Expand Down Expand Up @@ -548,8 +586,8 @@ public void tuneToFreq(String frequencyStr, int squelchLevel, boolean forceTune)

try {
Float freq = Float.parseFloat(makeSafe2MFreq(activeFrequencyStr));
Float offsetMaxFreq = maxFreq - (bandwidth.equals("W") ? 0.025f : 0.0125f);
if (freq < 144.0f || freq > offsetMaxFreq) {
Float offsetMaxFreq = maxHamFreq - (bandwidth.equals("W") ? 0.025f : 0.0125f);
if (freq < minHamFreq|| freq > offsetMaxFreq) {
txAllowed = false;
} else {
txAllowed = true;
Expand All @@ -564,16 +602,16 @@ public static String makeSafe2MFreq(String strFreq) {
try {
freq = Float.parseFloat(strFreq);
} catch (NumberFormatException nfe) {
return "144.0000";
return String.format(java.util.Locale.US, "%.4f", VHF_MIN_FREQ_US); // 4 decimal places, in MHz
}
while (freq > 500.0f) { // Handle cases where user inputted "1467" or "14670" but meant "146.7".
freq /= 10;
}

if (freq < 134.0f) {
freq = 134.0f; // Lowest freq supported by radio module
} else if (freq > 174.0f) {
freq = 174.0f; // Highest freq supported
if (freq < minRadioFreq) {
freq = minRadioFreq; // Lowest freq supported by radio module
} else if (freq > maxRadioFreq) {
freq = maxRadioFreq; // Highest freq supported
}

strFreq = String.format(java.util.Locale.US,"%.4f", freq);
Expand Down Expand Up @@ -639,8 +677,8 @@ public void tuneToMemory(ChannelMemory memory, int squelchLevel, boolean forceTu

try {
Float txFreq = Float.parseFloat(getTxFreq(memory.frequency, memory.offset, memory.offsetKhz));
Float offsetMaxFreq = maxFreq - (bandwidth.equals("W") ? 0.025f : 0.0125f);
if (txFreq < 144.0f || txFreq > offsetMaxFreq) {
Float offsetMaxFreq = maxHamFreq - (bandwidth.equals("W") ? 0.025f : 0.0125f);
if (txFreq < minHamFreq || txFreq > offsetMaxFreq) {
txAllowed = false;
} else {
txAllowed = true;
Expand Down Expand Up @@ -913,16 +951,33 @@ public void run() {
* @param radioType should be RADIO_TYPE_UHF or RADIO_TYPE_VHF
*/
public void setRadioType(String radioType) {
if (this.radioType.equals(radioType)) {
this.radioType = radioType;
} else { // If we're changing radio type, need to reinit connection to ESP32 so it knows what kind of module it has.
if (!this.radioType.equals(radioType)) {
this.radioType = radioType;

// Ensure frequencies we're using match the radioType
if (radioType.equals(RADIO_MODULE_VHF)) {
setMinRadioFreq(VHF_MIN_FREQ);
setMinFreq(VHF_MIN_FREQ_US);
setMaxFreq(VHF_MAX_FREQ_US);
setMaxRadioFreq(VHF_MAX_FREQ);
} else if (radioType.equals(RADIO_MODULE_UHF)) {
setMinRadioFreq(UHF_MIN_FREQ);
setMinFreq(UHF_MIN_FREQ_US);
setMaxFreq(UHF_MAX_FREQ_US);
setMaxRadioFreq(UHF_MAX_FREQ);
}

// Re-init connection to ESP32 so it knows what kind of module it has.
setMode(MODE_STARTUP);
checkedFirmwareVersion = false;
checkFirmwareVersion();
}
}

public String getRadioType() {
return radioType;
}

private void checkFirmwareVersion() {
checkedFirmwareVersion = true; // To prevent multiple USB connect events from spamming the ESP32 with requests (which can cause logic errors).

Expand Down Expand Up @@ -1202,15 +1257,15 @@ public static UsbSerialPort getUsbSerialPort() {
}

private void handleESP32Data(byte[] data) {
// Log.d("DEBUG", "Got bytes from ESP32: " + Arrays.toString(data));
// Log.d("DEBUG", "Got bytes from ESP32: " + Arrays.toString(data));
/* try {
String dataStr = new String(data, "UTF-8");
if (dataStr.length() < 100 && dataStr.length() > 0)
Log.d("DEBUG", "Str data from ESP32: " + dataStr);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} */
// Log.d("DEBUG", "Num bytes from ESP32: " + data.length);
// Log.d("DEBUG", "Num bytes from ESP32: " + data.length);

if (mode == MODE_STARTUP) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ kv4p HT (see http://kv4p.com)

public class AddEditMemoryActivity extends AppCompatActivity {
private boolean isAdd = true; // false means we're editing a memory, not adding
private boolean isVhfRadio = true; // false means UHF radio
private List<String> mTones;
private ThreadPoolExecutor threadPoolExecutor = null;
private int mMemoryId;
Expand Down Expand Up @@ -103,6 +104,7 @@ protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
isAdd = (extras.getInt("requestCode") == MainActivity.REQUEST_ADD_MEMORY);
isVhfRadio = (extras.getBoolean("isVhfRadio"));
if (!isAdd) { // Edit
mMemoryId = extras.getInt("memoryId");
threadPoolExecutor.execute(new Runnable() {
Expand All @@ -113,6 +115,8 @@ public void run() {
}
});
} else { // Add
populateDefaults();

mMemoryId = -1; // This ID is never used, just to help with debugging.

String activeFrequencyStr = extras.getString("activeFrequencyStr");
Expand Down Expand Up @@ -238,6 +242,16 @@ private void populateTones() {
editToneRxTextView.setAdapter(arrayAdapter2);
}

private void populateDefaults() {
TextInputEditText customOffsetTextInputEditText = findViewById(R.id.customOffsetTextInputEditText);

if (isVhfRadio) {
customOffsetTextInputEditText.setText("600");
} else {
customOffsetTextInputEditText.setText("5000");
}
}

private void populateOriginalValues() {
if (isAdd) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public void onMemoryEdit(ChannelMemory memory) {
Intent intent = new Intent("com.vagell.kv4pht.EDIT_MEMORY_ACTION");
intent.putExtra("requestCode", REQUEST_EDIT_MEMORY);
intent.putExtra("memoryId", memory.memoryId);
intent.putExtra("isVhfRadio", (radioAudioService != null && radioAudioService.getRadioType().equals(RadioAudioService.RADIO_MODULE_VHF)));
startActivityForResult(intent, REQUEST_EDIT_MEMORY);
}
});
Expand Down Expand Up @@ -484,6 +485,13 @@ public void run() {
public void unknownLocation() {
showSimpleSnackbar("Can't find your location, no beacon sent");
}

@Override
public void forceTunedToFreq(String newFreqStr) {
// This is called when RadioAudioService is changing bands, and we need
// to reflect that in the UI.
tuneToFreqUi(newFreqStr);
}
};

radioAudioService.setCallbacks(callbacks);
Expand Down Expand Up @@ -1006,7 +1014,9 @@ public void run() {

if (maxFreqSetting != null) {
int maxFreq = Integer.parseInt(maxFreqSetting.value);
RadioAudioService.setMaxFreq(maxFreq); // Called statically so static frequency formatter can use it.
if (radioAudioService != null) {
radioAudioService.setMaxFreq(maxFreq); // Called statically so static frequency formatter can use it.
}
}

if (micGainBoostSetting != null) {
Expand Down Expand Up @@ -1784,7 +1794,8 @@ public void addMemoryClicked(View view) {
intent.putExtra("requestCode", REQUEST_ADD_MEMORY);
intent.putExtra("activeFrequencyStr", activeFrequencyStr);
intent.putExtra("selectedMemoryGroup", selectedMemoryGroup);

intent.putExtra("isVhfRadio", (radioAudioService != null && radioAudioService.getRadioType().equals(RadioAudioService.RADIO_MODULE_VHF)));

startActivityForResult(intent, REQUEST_ADD_MEMORY);
}

Expand Down

0 comments on commit 3c9a273

Please sign in to comment.