# HG changeset patch
# User Jim Unroe <rock.unroe@gmail.com>
# Date 1637947400 18000
#      Fri Nov 26 12:23:20 2021 -0500
# Node ID b47e61ebc2e98383c89c8a76cc88e5283cc54d34
# Parent  39b995895e9de14ddaecb3f58725285114595ca8
[UV-5X3] Add saved VFOs to BTECH UV-5X3

It was discovered that the BTECH UV-5X3 has 3 memories for each VFO display
line that can save frequencies, one for each of the 3 bands (VHF/VHF2/UHF).
Tapping the [BAND] button of the radio will cycle through the 3 memories
associated with the selected display line and place the currently selected
memory into the currently selected VFO.

This patch adds support to allow the viewing/editing of these 6 memories.

related to #9515

diff -r 39b995895e9d -r b47e61ebc2e9 chirp/drivers/uv5x3.py
--- a/chirp/drivers/uv5x3.py	Fri Nov 26 09:46:04 2021 -0500
+++ b/chirp/drivers/uv5x3.py	Fri Nov 26 12:23:20 2021 -0500
@@ -116,6 +116,7 @@
 
     _ranges = [(0x0000, 0x0DF0),
                (0x0E00, 0x1800),
+               (0x1E00, 0x1E70),
                (0x1EE0, 0x1EF0),
                (0x1F60, 0x1F70),
                (0x1F80, 0x1F90),
@@ -333,6 +334,25 @@
       u8 unknown[9];
     } names[128];
 
+    struct subvfo {
+      u8 freq[8];
+      u8 unknown0[8];
+    };
+
+    #seekto 0x1E00;
+    struct {
+      struct subvfo vhf;
+      struct subvfo vhf2;
+      struct subvfo uhf;
+    } subvfoa;
+
+    #seekto 0x1E40;
+    struct {
+      struct subvfo vhf;
+      struct subvfo vhf2;
+      struct subvfo uhf;
+    } subvfob;
+
     #seekto 0x1ED0;
     struct {
       char line1[7];
@@ -796,6 +816,36 @@
                 raise InvalidValueError(msg % (_uhf_upper))
             return chirp_common.format_freq(value)
 
+        def my_vhf_validate(value):
+            _vhf_lower = int(_mem.limits.vhf.lower)
+            _vhf_upper = int(_mem.limits.vhf.upper)
+            value = chirp_common.parse_freq(value)
+            msg = ("Must be between %i.0000-%i.9975")
+            if (_vhf_upper + 1) * 1000000 <= value or \
+                    value < _vhf_lower * 1000000:
+                raise InvalidValueError(msg % (_vhf_lower, _vhf_upper))
+            return chirp_common.format_freq(value)
+
+        def my_vhf2_validate(value):
+            _vhf2_lower = int(_mem.limits.vhf2.lower)
+            _vhf2_upper = int(_mem.limits.vhf2.upper)
+            value = chirp_common.parse_freq(value)
+            msg = ("Must be between %i.0000-%i.9975")
+            if (_vhf2_upper + 1) * 1000000 <= value or \
+                    value < _vhf2_lower * 1000000:
+                raise InvalidValueError(msg % (_vhf2_lower, _vhf2_upper))
+            return chirp_common.format_freq(value)
+
+        def my_uhf_validate(value):
+            _uhf_lower = int(_mem.limits.uhf.lower)
+            _uhf_upper = int(_mem.limits.uhf.upper)
+            value = chirp_common.parse_freq(value)
+            msg = ("Must be between %i.0000-%i.9975")
+            if (_uhf_upper + 1) * 1000000 <= value or \
+                    value < _uhf_lower * 1000000:
+                raise InvalidValueError(msg % (_uhf_lower, _uhf_upper))
+            return chirp_common.format_freq(value)
+
         def apply_freq(setting, obj):
             value = chirp_common.parse_freq(str(setting.value)) / 10
             for i in range(7, -1, -1):
@@ -816,6 +866,48 @@
         rs.set_apply_callback(apply_freq, _mem.vfo.b)
         work.append(rs)
 
+        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
+                                            _mem.subvfoa.vhf.freq))
+        val1a.set_validate_callback(my_vhf_validate)
+        rs = RadioSetting("subvfoa.vhf.freq", "VFO A VHF (Saved)", val1a)
+        rs.set_apply_callback(apply_freq, _mem.subvfoa.vhf)
+        work.append(rs)
+
+        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
+                                            _mem.subvfob.vhf.freq))
+        val1b.set_validate_callback(my_vhf_validate)
+        rs = RadioSetting("subvfob.vhf.freq", "VFO B VHF (Saved)", val1b)
+        rs.set_apply_callback(apply_freq, _mem.subvfob.vhf)
+        work.append(rs)
+
+        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
+                                            _mem.subvfoa.vhf2.freq))
+        val1a.set_validate_callback(my_vhf2_validate)
+        rs = RadioSetting("subvfoa.vhf2.freq", "VFO A VHF2 (Saved)", val1a)
+        rs.set_apply_callback(apply_freq, _mem.subvfoa.vhf2)
+        work.append(rs)
+
+        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
+                                            _mem.subvfob.vhf2.freq))
+        val1b.set_validate_callback(my_vhf2_validate)
+        rs = RadioSetting("subvfob.vhf2.freq", "VFO B VHF2 (Saved)", val1b)
+        rs.set_apply_callback(apply_freq, _mem.subvfob.vhf2)
+        work.append(rs)
+
+        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
+                                            _mem.subvfoa.uhf.freq))
+        val1a.set_validate_callback(my_uhf_validate)
+        rs = RadioSetting("subvfoa.uhf.freq", "VFO A UHF (Saved)", val1a)
+        rs.set_apply_callback(apply_freq, _mem.subvfoa.uhf)
+        work.append(rs)
+
+        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
+                                            _mem.subvfob.uhf.freq))
+        val1b.set_validate_callback(my_uhf_validate)
+        rs = RadioSetting("subvfob.uhf.freq", "VFO B UHF (Saved)", val1b)
+        rs.set_apply_callback(apply_freq, _mem.subvfob.uhf)
+        work.append(rs)
+
         rs = RadioSetting("vfo.a.sftd", "VFO A Shift",
                           RadioSettingValueList(
                               LIST_SHIFTD, LIST_SHIFTD[_mem.vfo.a.sftd]))
