﻿# Quansheng UV-K5 driver (c) 2023 Jacek Lipkowski <sq5bpf@lipkowski.org>
#
# based on template.py Copyright 2012 Dan Smith <dsmith@danplanet.com>
#
# Adapted to IJV Firmware by Francesco IK8JHL
# Adapted to IJV V3 by IJV team
# jhl> adapted to IJV-vX3 999 Channel / Bank system
#
# IJV V3.71 Unified plugin â€” settings + 500 channels per bank
# Combines "IJV X3.60 Settings.py" and "3.50 - Banco 0.py" into one single driver.
#
# COMPATIBILE CON FIRMWARE IJV V4.0+
#
# LAYOUT EEPROM v4.0:
#   0x0000-0x1FFF  Area fissa:  VFO, impostazioni, calibrazione
#   0x2000-0x5E7F  Area canali: 500 canali × 32B = 0x3E80 (banco 0 / banco corrente lato radio)
#   0x5E80-0x5EFF  Nomi gruppi: 16 × 8B
#   0x5F00-0x5F07  Nome banco
#
# UTILIZZO:
#   Questo plugin gestisce sia le impostazioni radio che le memorie canali.
#   Nella tab "Settings" di CHIRP trovi tutti i parametri del firmware IJV.
#   Nella tab "Memories" trovi i 500 canali del banco attivo (selezionabile
#   dal menu della radio tramite MENU â†’ BANK).
#   NOTA IMPORTANTE: CHIRP non gestisce tutti i banchi insieme; legge e scrive
#   solo il banco attualmente selezionato sulla radio.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

from re import A
import struct
import logging

from chirp import chirp_common, directory, bitwise, memmap, errors, util
from chirp.settings import RadioSetting, RadioSettingGroup, \
    RadioSettingValueBoolean, RadioSettingValueList, \
    RadioSettingValueInteger, RadioSettingValueString, \
    RadioSettings, RadioSettingSubGroup

LOG = logging.getLogger(__name__)

DEBUG_SHOW_OBFUSCATED_COMMANDS = False
DEBUG_SHOW_MEMORY_ACTIONS = True

# ============================================================
# MEM_1 â€” struttura area fissa (0x0000-0x1FFF)
# Identica a IJV X3.60 Settings.py
# ============================================================
MEM_1 = """
// -------------------0x0000
ul16 call_channel;
u8 max_talk_time;
u8 tx_dev;
u8 key_lock;
u8 vox_switch;
u8 vox_level;
u8 mic_gain;
// -------------------0x0008
u8 beep_control;
u8 channel_display_mode;
u8 no_used2;
u8 battery_save;
u8 afc;
u8 backlight_auto_mode;
u8 tail_note_elimination;
u8 vfo_lock;
// -------------------0x0010
u8 flock;
u8 scan_resume_mode;
u8 auto_keypad_lock;
u8 power_on_dispmode;
u8 vfomode;
u8 no_used4;
u8 beacon;

u8 no_used5:1,
   bl_mode:2,
   micbar:1,
   power_on_beep:1,
   dtmf_live:2,
   tx_enable:1;

// -------------------0x0018
struct
{
    u8 val;
} agc[7];

u8 no_used6:2,
   upconv:2,
   satcom:1,
   signal_meter:1,
   no_used7:1,
   savedirect:1;

// -------------------0x0020
u8 alarm_mode;
u8 reminding_of_end_talk;
u8 repeater_tail_elimination;
u8 bands_tx;
u8 back_type;
u8 mem_speed;
u8 no_used10;
u8 no_used11;
// -------------------0x0028
struct {
    u8 side_tone;
    char separate_code;
    char group_call_code;
    u8 decode_response;
    u8 auto_reset_time;
    u8 preload_time;
    u8 first_code_persist_time;
    u8 hash_persist_time;
    u8 code_persist_time;
    u8 code_interval_time;
} dtmf_settings;

// -------------------0x0032
u16 custom_tone;
u8 no_used12;
u8 no_used13;
u8 no_used14;
u8 no_used15;
// -------------------0x0038
u8 ch_list;
u16 no_used16;
u16 no_used17;
u16 no_used18;
u8  no_used19;
// -------------------0x0040
ul32 fm_freq;
ul32 sat_freq;
ul32 no_used21;
ul32 no_used22;

// -------------------0x0050
ul16 screen_cha;
ul16 mr_cha;
ul16 freq_cha;
ul16 nooa_cha;
ul16 screen_chb;
ul16 mr_chb;
ul16 freq_chb;
ul16 nooa_chb;


// -------------------0x0150
#seekto 0x150;
char logo_line1[16];
char logo_line2[16];
char qrz_label[8];

// -------------------0x0178
struct
{
    char dtmf_local_code[8];
    char dtmf_up_code[8];
    char dtmf_down_code[8];
} dtmf_settings_numbers;

// -------------------0x0190
u8 key1_shortpress_action;
u8 key1_longpress_action;
u8 key2_shortpress_action;
u8 key2_longpress_action;

// -------------------0x0198
#seekto 0x0198;
ul32 custom_upconv;

// -------------------0x0200
#seekto 0x200;
struct
{
    char name[8];
    char number[8];
} dtmfcontact[16];

// -------------------0x5E80
#seekto 0x5E80;
struct
{
    char name[8];
} list_name[16];

//------------------------------- preset
// -------------------0x0380
#seekto 0x0380;
struct
{
  char name[8];
  ul32 freq_low;
  ul32 freq_up;
  u8 rxcode;
  u8 txcode;
  u8 tx_codetype:4,
     rx_codetype:4;
  u8 free:1,
     writeprot:1,
     enablescan:1,
     modulation:3,
     shift:2;
  u8 busylock:1,
     txpower:2,
     bw:4,
     reverse:1;
  u8 no_used9:2,
     scrambler:1,
     compander:2,
     agcmode:3;
  u8 squelch:4,
     step:4;
  u8 ptt_id:4,
     digcode:4;
} preset[12];

      //---------------------- Calibration
struct {
    struct {
        #seekto 0x1E00;
        u8 openRssiThr[10];
        #seekto 0x1E10;
        u8 closeRssiThr[10];
        #seekto 0x1E20;
        u8 openNoiseThr[10];
        #seekto 0x1E30;
        u8 closeNoiseThr[10];
        #seekto 0x1E40;
        u8 openGlitchThr[10];
        #seekto 0x1E50;
        u8 closeGlitchThr[10];
    } sqlBand4_7;

    struct {
        #seekto 0x1E60;
        u8 openRssiThr[10];
        #seekto 0x1E70;
        u8 closeRssiThr[10];
        #seekto 0x1E80;
        u8 openNoiseThr[10];
        #seekto 0x1E90;
        u8 closeNoiseThr[10];
        #seekto 0x1EA0;
        u8 openGlitchThr[10];
        #seekto 0x1EB0;
        u8 closeGlitchThr[10];
    } sqlBand1_3;

    #seekto 0x1EC0;
    struct {
        ul16 level1;
        ul16 level2;
        ul16 level4;
        ul16 level6;
    } rssiLevelsBands3_7;

    struct {
        ul16 level1;
        ul16 level2;
        ul16 level4;
        ul16 level6;
    } rssiLevelsBands1_2;

    struct {
        struct {
            u8 lower;
            u8 center;
            u8 upper;
        } low;
        struct {
            u8 lower;
            u8 center;
            u8 upper;
        } mid;
        struct {
            u8 lower;
            u8 center;
            u8 upper;
        } hi;
        #seek 7;
    } txp[7];


    #seekto 0x1F50;
    ul16 vox1Thr[10];

    #seekto 0x1F68;
    ul16 vox0Thr[10];

    #seekto 0x1F80;
    u8 micLevel[5];

    #seekto 0x1F88;
    il16 xtalFreq;

    #seekto 0x1F8E;
    u8 volumeGain;
    u8 dacGain;
} cal;


    #seekto 0x1F48;
    u8 batt_cal0;
    u8 batt_cal1;
    u8 batt_cal2;
    u8 batt_cal3;
    u8 batt_cal4;
    u8 batt_cal5;
    u8 batt_cal6;

    #seekto 0x1F90;
    u16 user_code0;
    u16 user_code1;
    u16 user_code2;
    u16 user_code3;
    u16 user_code4;
    u16 user_code5;
    u16 user_code6;
    u16 user_code7;
    u16 user_code8;
    u16 user_code9;
    u16 user_codeA;
    u16 user_codeB;
    u16 user_codeC;
    u16 user_codeD;
    u16 user_codeE;
    u16 user_codeF;

    #seekto 0x1FB0;
    u16 user_code_ms;

    """

# ============================================================
# MEM_CHAN â€” struttura canali (0x2000-0x5E7F, 500 canali Ã— 32B)
# Adattata da "3.50 - Banco 0.py" per 500 canali
# ============================================================
MEM_CHAN = """
    #seekto 0x2000;

    struct
    {
      // ---------------rec 1 + 2
      char name[10];
      u8 code_sel0:4,
         code_sel1:4;
      u8 code_sel2:4,
         code_sel3:4;
      u8 code_sel4:4,
         code_sel5:4;
      u8 code_sel6:4,
         code_sel7:4;
      u8 code_sel8:4,
         code_sel9:4;
      u8 group:4,
         band:4;

      // ---------------rec 3
      ul32 freq;
      ul32 offset;

      // ---------------rec 4
      u8 rxcode;
      u8 txcode;

      u8 tx_codetype:4,
         rx_codetype:4;

      u8 txlock:1,
         writeprot:1,
         enablescan:1,
         modulation:3,
         shift:2;

      u8 busylock:1,
         txpower:2,
         bw:4,
         reverse:1;

      u8 no_used4:2,
         scrambler:1,
         compander:2,
         agcmode:3;

      u8 squelch:4,
         step:4;

      u8 ptt_id:4,
         digcode:4;

    } channel[500];
"""

# MEM_FORMAT unificato: area fissa + array canali
MEM_FORMAT = MEM_1 + MEM_CHAN

# ============================================================
# Costanti di layout
# ============================================================
CHAN_MAX       = 500    # canali per banco
MEM_SIZE       = 0x6000 # download: 0x0000-0x5FFF (settings + canali + gruppi/nome banco)
PROG_SIZE_V    = 0x0050 # fine area VFO (primo blocco scrittura)
PROG_SIZE_U    = 0x0140 # inizio area utente (secondo blocco scrittura)
CAL_START      = 0x1E00 # inizio area calibrazione RF â€” MAI sovrascrivere da CHIRP
PROG_SIZE_S    = CAL_START # fine area settings: non includere calibrazione
                            # La calibrazione (0x1E00-0x1FFF) contiene dati fisici RF
                            # specifici del singolo esemplare hardware â€” CHIRP non deve
                            # mai sovrascriverli durante un upload di impostazioni.
START_MEM      = 0x2000 # inizio area canali
END_MEM        = 0x6000 # fine area canali + gruppi/nome banco del banco 0
MEM_BLOCK      = 0x80   # blocco massimo per scrittura/lettura affidabile

# ============================================================
# Costanti protocollo
# ============================================================
OFFSET_NONE  = 0b00
OFFSET_PLUS  = 0b01
OFFSET_MINUS = 0b10

POWER_LOW    = 0b00
POWER_MEDIUM = 0b01
POWER_HIGH   = 0b10

# ============================================================
# Liste valori (da Settings.py v3.60)
# ============================================================
TXPOWER_LIST = ["Low","Mid","High"]

BANDWIDTH_LIST = ["W 26k","W 23k","W 20k","W 17k","W 14k","W.12k",
                  "N 10k","N. 9k","U  7k","U  6k"]

MODULATION_LIST = ["FM","AM","USB","CW","WFM","DIG"]

PTTID_LIST = ["OFF", "CALL ID", "SEL CALL", "CODE BEGIN", "CODE END",
              "CODE BEG+END", "ROGER Single", "ROGER 2Tones",
              "Apollo Quindar"]

UVK5_POWER_LEVELS = [chirp_common.PowerLevel("Low",  watts=1.00),
                     chirp_common.PowerLevel("Med",  watts=2.50),
                     chirp_common.PowerLevel("High", watts=5.00)]

SCRAMBLER_LIST = ["OFF",
    "1000Hz","1050Hz","1100Hz","1150Hz","1200Hz","1250Hz","1300Hz","1350Hz","1400Hz","1450Hz",
    "1500Hz","1550Hz","1600Hz","1650Hz","1700Hz","1750Hz","1800Hz","1850Hz","1900Hz","1950Hz",
    "2000Hz","2050Hz","2100Hz","2150Hz","2200Hz","2250Hz","2300Hz","2350Hz","2400Hz","2450Hz",
    "2500Hz","2550Hz","2600Hz","2650Hz","2700Hz","2750Hz","2800Hz","2850Hz","2900Hz","2950Hz",
    "3000Hz","3050Hz","3100Hz","3150Hz","3200Hz","3250Hz","3300Hz","3350Hz","3400Hz","3450Hz",
    "3500Hz","3550Hz","3600Hz","3650Hz","3700Hz","3750Hz","3800Hz","3850Hz","3900Hz","3950Hz",
    "4000Hz","4050Hz","4100Hz"]

DIGITAL_CODE_LIST = ["OFF","DTMF","ZVEI1","ZVEI2","CCIR-1","CCIR-1F","ZVEI3","CCIR20","EEA"]

SQUELCH_LIST = ["Squelch 0","Squelch 1","Squelch 2","Squelch 3","Squelch 4",
                "Squelch 5","Squelch 6","Squelch 7","Squelch 8","Squelch 9","NO RX"]

CHANNELDISP_LIST = ["Frequency", "Channel No", "Channel Name", "Name_S Freq_L", "Name_L Freq_S"]

MR_LIST      = ["VFO 1","VFO 2","VFO 3","VFO 4","VFO 5","VFO 6","VFO 7"]
MRMODE_LIST  = ["MEMORY", "VFO"]
DUALMODE_LIST= ["Dual", "Single"]

BEACON_LIST  = ["OFF","5 sec","10 sec","30 sec","1 min","3 min","6 min","10 min","20 min"]

COMPANDER_LIST = ["OFF", "TX", "RX", "RX/TX"]

MICGAIN_LIST = ["+1.1dB","+4.0dB","+8.0dB","+12.0dB","+15.1dB"]

TALKTIME_LIST = ["OFF","30s","1min","3min","5min"]

BACKLIGHT_LIST = ["Off", "5s", "10s", "20s", "1min", "3min", "RX/TX", "ON"]

VFOMODE_LIST   = ["SINGLE","DOUBLE","DW_LOCK","DW_LINK","SPLIT"]
BANDS_TX_LIST  = ["A","B"]

SKIP_VALUES = ["", "S", "P"]

# steps
STEPS = [0.01, 0.05, 0.10, 0.50, 1.00, 2.50, 5.00, 6.25, 8.33, 9.00,
         10.00, 12.50, 20.00, 25.00, 50.00, 100.00]

STEP_LIST = ["   10 Hz","   50 Hz","  100 Hz","  500 Hz","   1 kHz"," 2.5 kHz",
             "   5 kHz","6.25 kHz","8.33 kHz","   9 kHz","  10 kHz","12.5 kHz",
             "  20 kHz","  25 kHz","  50 kHz"," 100 kHz"]

AGC_LIST = ["-84","-83","-82","-81","-80","-78","-76","-74","-72","-70","-68","-66",
            "-62","-60","-58","-56","-52","-50","-48","-46","-42","-40","-38","-35",
            "-31","-29","-28","-27","-26","-24","-21","-18","-16","-13","-11",
            "-8","-6","-5","-3","-2","-1","0"]

AGC_CORR = [18,26,26,26,18,18,18]

AGC_MODE = ["AUTO","MAN","FAST","NORM","SLOW"]

TMODES   = ["", "Tone", "DTCS", "DTCS"]
TONE_NONE  = 0
TONE_CTCSS = 1
TONE_DCS   = 2
TONE_RDCS  = 3

CTCSS_TONES = [
    67.0, 69.3, 71.9, 74.4, 77.0, 79.7, 82.5, 85.4,
    88.5, 91.5, 94.8, 97.4, 100.0, 103.5, 107.2, 110.9,
    114.8, 118.8, 123.0, 127.3, 131.8, 136.5, 141.3, 146.2,
    151.4, 156.7, 159.8, 162.2, 165.5, 167.9, 171.3, 173.8,
    177.3, 179.9, 183.5, 186.2, 189.9, 192.8, 196.6, 199.5,
    203.5, 206.5, 210.7, 218.1, 225.7, 229.1, 233.6, 241.8,
    250.3, 254.1,
]

DTCS_CODES = [
    23,  25,  26,  31,  32,  36,  43,  47,  51,  53,  54,
    65,  71,  72,  73,  74,  114, 115, 116, 122, 125, 131,
    132, 134, 143, 145, 152, 155, 156, 162, 165, 172, 174,
    205, 212, 223, 225, 226, 243, 244, 245, 246, 251, 252,
    255, 261, 263, 265, 266, 271, 274, 306, 311, 315, 325,
    331, 332, 343, 346, 351, 356, 364, 365, 371, 411, 412,
    413, 423, 431, 432, 445, 446, 452, 454, 455, 462, 464,
    465, 466, 503, 506, 516, 523, 526, 532, 546, 565, 606,
    612, 624, 627, 631, 632, 654, 662, 664, 703, 712, 723,
    731, 732, 734, 743, 754
]

BANDS = {
    0: [13.0,   107.9999],
    1: [108.0,  136.9999],
    2: [137.0,  173.9990],
    3: [174.0,  349.9999],
    4: [350.0,  399.9999],
    5: [400.0,  469.9999],
    6: [470.0, 1299.9999]
}
BANDSTX_CAL = [" (13~108 MHz)"," (108~137 MHz)"," (137~174 MHz)"," (174~350 MHz)",
               " (350~400 MHz)"," (400~470 MHz)"," (470~1300 MHz)"]

SPECIALS = {}

VFO_CHANNEL_NAMES = ["F1(50M-76M)A",   "F1(50M-76M)B",
                     "F2(108M-136M)A", "F2(108M-136M)B",
                     "F3(136M-174M)A", "F3(136M-174M)B",
                     "F4(174M-350M)A", "F4(174M-350M)B",
                     "F5(350M-400M)A", "F5(350M-400M)B",
                     "F6(400M-470M)A", "F6(400M-470M)B",
                     "F7(470M-600M)A", "F7(470M-600M)B"]

DTMF_CHARS        = "0123456789ABCDEF*# "
DTMF_CHARS_ID     = "0123456789ABCDabcdef#* "
DTMF_CHARS_UPDOWN = "0123456789ABCDEFabcdef#* "
DTMF_CODE_CHARS   = "ABCDEF*# "
DTMF_DECODE_RESPONSE_LIST = ["None", "Ring", "Reply", "Both"]
DLIVE_LIST = "off", "RAW", "POP"

KEYACTIONS_LIST = ["None", "Flashlight", "TX Power", "Monitor", "Scan on/off",
                   "FM radio on/off", "VFO Change", "VFO Swap", "SQL +", "SQL -",
                   "REGA Test", "REGA Alarm", "Preset", "AGC MAN", "CH LIST",
                   "SCRAMBLER", "PHONE BOOK", "CW Call CQ",
                   "Scansione Veloce"]   # v3.75: panorama RSSI 128 freq (ACTION_OPT_FASTSCAN=18)

GROUP_FALLBACK_LIST = ["ALL"] + [""] * 15
GROUP_LIST = list(GROUP_FALLBACK_LIST)

UPCONV_LIST = ["OFF","50 MHz", "125 MHz","CUSTOM"]

FLOCK_LIST    = ["OFF", "FCC", "CE", "GB", "430", "438"]

SCANRESUME_LIST = ["TIME: Resume after 5 seconds",
                   "SLOW: Resume slower after signal disappears",
                   "FAST: Resume faster after signal disappears",
                   "SEARCH: Stop scanning after receiving a signal",
                   "MONITOR"]

WELCOME_LIST = ["NONE", "FW MOD", "MESSAGE"]

RTE_LIST = ["OFF",
            "1*100ms", "2*100ms", "3*100ms", "4*100ms", "5*100ms",
            "6*100ms", "7*100ms", "8*100ms", "9*100ms", "10*100ms",
            "11*100ms", "12*100ms", "13*100ms", "14*100ms", "15*100ms",
            "16*100ms", "17*100ms", "18*100ms", "19*100ms", "20*100ms"]

FMMIN  = 76.0
FMMAX  = 108.0
CTMAX  = 255.0
UCODMAX = 3000.0

EMPTY_MEM = [0,0,0,0,0,0,0,0,0,0,0xEE,0xEE,0xEE,0xEE,0xEE,0,
             0,0,0,0,0,0,0,0,0,0,0   ,0   ,0   ,0   ,0   ,0]

# ============================================================
# Funzioni di utilitÃ 
# ============================================================

def min_max_def(value, min_val, max_val, default):
    """returns value if in bounds or default otherwise"""
    if min_val is not None and value < min_val:
        return default
    if max_val is not None and value > max_val:
        return default
    return value


def refresh_group_list(memobj):
    for i in range(16):
        name = str(memobj.list_name[i].name).strip("\x20\x00\xff")
        GROUP_LIST[i] = name if name else ("ALL" if i == 0 else "")


#--------------------------------------------------------------------------------
# nibble â†’ ascii (per codici selettive)
def hexasc(data):
    res = data
    if res <= 9:
        return chr(res+48)
    elif data == 0xA: return "A"
    elif data == 0xB: return "B"
    elif data == 0xC: return "C"
    elif data == 0xD: return "D"
    elif data == 0xF: return "F"
    else:             return " "


#--------------------------------------------------------------------------------
# ascii â†’ nibble (per codici selettive)
def ascdec(data):
    if   data == "0": return 0
    elif data == "1": return 1
    elif data == "2": return 2
    elif data == "3": return 3
    elif data == "4": return 4
    elif data == "5": return 5
    elif data == "6": return 6
    elif data == "7": return 7
    elif data == "8": return 8
    elif data == "9": return 9
    elif data == "A": return 10
    elif data == "B": return 11
    elif data == "C": return 12
    elif data == "D": return 13
    elif data == "F": return 15
    else:             return 14


#--------------------------------------------------------------------------------
# obfuscation XOR (protocollo K5)
def xorarr(data: bytes):
    tbl = [22, 108, 20, 230, 46, 145, 13, 64, 33, 53, 213, 64, 19, 3, 233, 128]
    x = b""
    r = 0
    for byte in data:
        x += bytes([byte ^ tbl[r]])
        r = (r+1) % len(tbl)
    return x


#--------------------------------------------------------------------------------
def calculate_crc16_xmodem(data: bytes):
    poly = 0x1021
    crc  = 0x0
    for byte in data:
        crc = crc ^ (byte << 8)
        for i in range(8):
            crc = crc << 1
            if (crc & 0x10000):
                crc = (crc ^ poly) & 0xFFFF
    return crc & 0xFFFF


#--------------------------------------------------------------------------------
def _send_command(serport, data: bytes):
    """Invia un comando alla radio UV-K5"""
    LOG.debug("Sending command (unobfuscated) len=0x%4.4x:\n%s" %
              (len(data), util.hexprint(data)))

    crc     = calculate_crc16_xmodem(data)
    data2   = data + struct.pack("<H", crc)
    command = struct.pack(">HBB", 0xabcd, len(data), 0) + \
              xorarr(data2) + struct.pack(">H", 0xdcba)

    if DEBUG_SHOW_OBFUSCATED_COMMANDS:
        LOG.debug("Sending command (obfuscated):\n%s" % util.hexprint(command))
    try:
        result = serport.write(command)
    except Exception:
        raise errors.RadioError("Error writing data to radio")
    return result


#--------------------------------------------------------------------------------
def _receive_reply(serport):
    header = serport.read(4)
    if len(header) != 4:
        LOG.warning("Header short read: [%s] len=%i" % (util.hexprint(header), len(header)))
        raise errors.RadioError("Header short read")
    if header[0] != 0xAB or header[1] != 0xCD or header[3] != 0x00:
        LOG.warning("Bad response header: %s len=%i" % (util.hexprint(header), len(header)))
        raise errors.RadioError("Bad response header")

    cmd = serport.read(int(header[2]))
    if len(cmd) != int(header[2]):
        LOG.warning("Body short read: [%s] len=%i" % (util.hexprint(cmd), len(cmd)))
        raise errors.RadioError("Command body short read")

    footer = serport.read(4)
    if len(footer) != 4:
        LOG.warning("Footer short read: [%s] len=%i" % (util.hexprint(footer), len(footer)))
        raise errors.RadioError("Footer short read")
    if footer[2] != 0xDC or footer[3] != 0xBA:
        LOG.warning("Bad response footer: %s len=%i" % (util.hexprint(footer), len(footer)))
        raise errors.RadioError("Bad response footer")

    if DEBUG_SHOW_OBFUSCATED_COMMANDS:
        LOG.debug("Received reply (obfuscated) len=0x%4.4x:\n%s" % (len(cmd), util.hexprint(cmd)))

    cmd2 = xorarr(cmd)
    LOG.debug("Received reply (unobfuscated) len=0x%4.4x:\n%s" % (len(cmd2), util.hexprint(cmd2)))
    return cmd2


#--------------------------------------------------------------------------------
def _getstring(data: bytes, begin, maxlen):
    end = min(begin + maxlen, len(data))
    chars = []
    for item in data[begin:end]:
        if isinstance(item, int):
            val = item
        else:
            text = str(item or "")
            if len(text) != 1:
                break
            val = ord(text)
        if val < 0x20 or val > 0x7E:
            break
        chars.append(chr(val))
    return "".join(chars)


#--------------------------------------------------------------------------------
def _sayhello(serport):
    hellopacket = b"\x14\x05\x04\x00\x6a\x39\x57\x64"
    tries = 5
    while True:
        LOG.debug("Sending hello packet")
        _send_command(serport, hellopacket)
        o = _receive_reply(serport)
        if (o):
            break
        tries -= 1
        if tries == 0:
            LOG.warning("Failed to initialise radio")
            raise errors.RadioError("Failed to initialize radio")
    firmware = _getstring(o, 4, 16)
    LOG.info("Found firmware: %s" % firmware)
    return firmware


#--------------------------------------------------------------------------------
def _readmem(serport, offset, length):
    LOG.debug("Sending readmem offset=0x%4.4x len=0x%4.4x" % (offset, length))
    readmem = b"\x1b\x05\x08\x00" + \
              struct.pack("<HBB", offset, length, 0) + \
              b"\x6a\x39\x57\x64"
    _send_command(serport, readmem)
    o = _receive_reply(serport)
    if DEBUG_SHOW_MEMORY_ACTIONS:
        LOG.debug("readmem Received data len=0x%4.4x:\n%s" % (len(o), util.hexprint(o)))
    return o[8:]


#--------------------------------------------------------------------------------
def _config_write_menu_bank(serport, bank):
    if bank < 0 or bank > 15:
        return False
    payload = b"\x62\x05\x05\x00" + bytes((1, 1, 0, 1, bank))
    _send_command(serport, payload)
    o = _receive_reply(serport)
    return bool(o and len(o) >= 9 and o[0] == 0x63 and o[1] == 0x05 and o[4] == 1 and o[5] == 1 and o[6] == 0 and o[7] == 0)


#--------------------------------------------------------------------------------
def _writemem(serport, data, offset):
    LOG.debug("Sending writemem offset=0x%4.4x len=0x%4.4x" % (offset, len(data)))
    if DEBUG_SHOW_MEMORY_ACTIONS:
        LOG.debug("writemem sent data offset=0x%4.4x len=0x%4.4x:\n%s" %
                  (offset, len(data), util.hexprint(data)))
    dlen     = len(data)
    writemem = b"\x1d\x05" + \
               struct.pack("<BBHBB", dlen+8, 0, offset, dlen, 1) + \
               b"\x6a\x39\x57\x64" + data
    _send_command(serport, writemem)
    o = _receive_reply(serport)
    LOG.debug("writemem Received data: %s len=%i" % (util.hexprint(o), len(o)))
    if (o[0] == 0x1e and
            o[4] == (offset & 0xff) and
            o[5] == (offset >> 8) & 0xff):
        return True
    else:
        LOG.warning("Bad data from writemem")
        raise errors.RadioError("Bad response to writemem")


#--------------------------------------------------------------------------------
# Scrittura differenziale: legge il blocco dalla flash e scrive SOLO se diverso.
#
# Ottimizzazione critica per l'upload CHIRP sul firmware IJV V3:
#   Il firmware PY32F071 esegue il sector erase (~300ms) + page program (~80ms)
#   della flash SPI PY25Q16 in modo bloccante (WaitWIP polling) per ogni settore
#   4KB che viene modificato. Scrivere blocchi identici causa erase inutili,
#   aumentando il tempo di upload e la pressione sul buffer UART (38400 baud).
#   Con questa funzione, se l'utente non ha modificato una zona di memoria,
#   quella zona non viene scritta â†’ zero erase inutili â†’ upload molto piÃ¹ veloce.
#
# Effetto pratico:
#   - Upload solo canali modificati: le Fasi 1 e 2 (settings) vengono saltate
#     quasi completamente se l'utente non ha modificato le impostazioni.
#   - Upload solo settings: la Fase 3 (canali) viene saltata se i canali non
#     sono stati modificati.
#--------------------------------------------------------------------------------
def _writemem_if_changed(serport, data, offset):
    dlen    = len(data)
    current = _readmem(serport, offset, dlen)   # legge il blocco attuale dalla flash
    if current and bytes(current) == bytes(data):
        LOG.debug("writemem skip (unchanged) offset=0x%4.4x" % offset)
        return True                              # identico â†’ nessuna scrittura, nessun erase
    return _writemem(serport, data, offset)      # diverso â†’ scrivi normalmente


#--------------------------------------------------------------------------------
def _resetradio(serport):
    resetpacket = b"\xdd\x05\x00\x00"
    _send_command(serport, resetpacket)


#--------------------------------------------------------------------------------
# Lettura EEPROM dalla radio
# Download: 0x0000-0x5FFF (settings + banco 0 corrente: canali, gruppi, nome banco)
#--------------------------------------------------------------------------------
def do_download(radio):
    serport = radio.pipe
    serport.timeout = 0.5
    status = chirp_common.Status()
    status.cur = 0
    status.max = MEM_SIZE
    status.msg = "Downloading from radio"
    radio.status_fn(status)

    eeprom = b""
    f = _sayhello(serport)
    if f:
        radio.FIRMWARE_VERSION = f
    else:
        raise errors.RadioError('Unable to determine firmware version')

    # Risincronizza il banco canali del layer compatibilita' con il valore
    # persistito nelle impostazioni radio prima di leggere 0x2000-0x5FFF.
    try:
        flag2 = _readmem(serport, 0x0008, 8)
        if flag2 and len(flag2) >= 4:
            _config_write_menu_bank(serport, flag2[3] if flag2[3] <= 15 else 0)
    except Exception as e:
        LOG.warning("Unable to resync memory bank before download: %s", e)

    addr = 0
    while addr < MEM_SIZE:
        o = _readmem(serport, addr, MEM_BLOCK)
        status.cur = addr
        radio.status_fn(status)
        if o and len(o) == MEM_BLOCK:
            eeprom += o
            addr   += MEM_BLOCK
        else:
            raise errors.RadioError("Memory download incomplete")

    return memmap.MemoryMapBytes(eeprom)


#--------------------------------------------------------------------------------
# Scrittura EEPROM sulla radio
# Upload in tre fasi:
#   1. Impostazioni VFO    0x0000 - 0x0050
#   2. Impostazioni utente 0x0140 - 0x2000  (include calibrazione)
#   3. Banco 0             0x2000 - 0x6000  (500 canali + gruppi + nome banco)
#--------------------------------------------------------------------------------
def do_upload(radio):
    serport = radio.pipe
    serport.timeout = 5.0  # 5s: margine abbondante per PY25Q16 sector erase (~400ms) + program (~80ms)
                           # Il fw usa write-back cache: flush solo al cambio settore (max ~500ms reale)
    status = chirp_common.Status()
    status.cur = 0
    status.max = END_MEM
    status.msg = "Uploading VFO Setting to radio"
    radio.status_fn(status)

    # Pulisce buffer RX da eventuali byte residui della sessione di download precedente.
    # Senza questo reset, bytes spuri nel buffer possono corrompere il parsing della risposta hello.
    serport.reset_input_buffer()

    f = _sayhello(serport)
    if f:
        radio.FIRMWARE_VERSION = f
    else:
        return False

    # Applica il banco persistito nel mmap prima di scrivere l'area canali.
    try:
        image_bank = radio.get_mmap()[0x0008 + 3]
        _config_write_menu_bank(serport, image_bank if image_bank <= 15 else 0)
    except Exception as e:
        LOG.warning("Unable to resync memory bank before upload: %s", e)

    # --- Fase 1: scrittura impostazioni VFO (0x0000-0x004F)
    # Scrittura differenziale: skip dei blocchi identici â†’ zero erase inutili.
    addr = 0
    while addr < PROG_SIZE_V:
        o = radio.get_mmap()[addr:addr+MEM_BLOCK]
        _writemem_if_changed(serport, o, addr)
        status.cur = addr
        radio.status_fn(status)
        if o:
            addr += MEM_BLOCK
        else:
            raise errors.RadioError("VFO upload incomplete. If the error persists, run RESET ALL on the radio and try again.")

    # --- Fase 2: scrittura impostazioni utente (0x0140-0x1DFF, esclusa calibrazione)
    # NOTA: la calibrazione (0x1E00-0x1FFF) non viene mai scritta da CHIRP â€”
    #       contiene dati RF fisici specifici del singolo esemplare hardware.
    # Scrittura differenziale: skip dei blocchi identici â†’ zero erase inutili.
    status.msg = "Uploading User Settings to radio"
    addr = PROG_SIZE_U
    while addr < PROG_SIZE_S:
        o = radio.get_mmap()[addr:addr+MEM_BLOCK]
        _writemem_if_changed(serport, o, addr)
        status.cur = addr
        radio.status_fn(status)
        if o:
            addr += MEM_BLOCK
        else:
            raise errors.RadioError("User settings upload incomplete. If the error persists, run RESET ALL on the radio and try again.")

    # --- Fase 3: scrittura banco 0 corrente (0x2000-0x5FFF)
    # Scrittura differenziale: skip dei blocchi invariati â†’ upload veloce
    # se solo pochi canali sono stati modificati rispetto al download precedente.
    status.msg = "Uploading Channels to radio"
    addr = START_MEM
    while addr < END_MEM:
        o = radio.get_mmap()[addr:addr+MEM_BLOCK]
        _writemem_if_changed(serport, o, addr)
        status.cur = addr
        radio.status_fn(status)
        if o:
            addr += MEM_BLOCK
        else:
            raise errors.RadioError("Channel upload incomplete. If the error persists, run RESET ALL on the radio and try again.")

    status.msg = "Upload OK. If the radio shows incorrect data, run MENU -> RESET ALL on the radio."
    _resetradio(serport)
    return True


#--------------------------------------------------------------------------------
def _find_band(hz):
    mhz = hz / 1000000.0
    for a in BANDS:
        if mhz >= BANDS[a][0] and mhz <= BANDS[a][1]:
            return a
    return False


################################################################################################################################
################################################################################################################################

@directory.register
class UVK5Radio(chirp_common.CloneModeRadio):
    """Quansheng UV-K5 â€” IJV Firmware V3.71 Unified (Settings + 512 Ch)"""
    VENDOR   = "Quansheng"
    MODEL    = "UV-K5"
    VARIANT  = "IJV_V4_0 - 500 Ch + Group Names"   # layout coerente con RadioManager
    BAUD_RATE = 38400
    NEEDS_COMPAT_SERIAL = False
    FIRMWARE_VERSION    = ""
    _expanded_limits    = True

#--------------------------------------------------------------------------------
    def get_features(self):
        rf = chirp_common.RadioFeatures()
        rf.has_bank              = False
        rf.has_rx_dtcs           = True
        rf.has_ctone             = True
        rf.has_settings          = True
        rf.has_comment           = False
        rf.valid_dtcs_codes      = DTCS_CODES
        rf.valid_name_length     = 10
        rf.valid_power_levels    = UVK5_POWER_LEVELS
        rf.valid_special_chans   = []
        rf.valid_duplexes        = ["", "-", "+", "off"]
        rf.valid_tuning_steps    = STEPS
        rf.valid_tmodes          = ["", "Tone", "TSQL", "DTCS", "Cross"]
        rf.valid_cross_modes     = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
                                    "->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
        rf.valid_characters      = chirp_common.CHARSET_ASCII
        rf.valid_modes           = ["FM", "AM", "USB", "CW", "WFM"]
        rf.valid_skips           = ["", "S"]
        rf._expanded_limits      = True
        rf.memory_bounds         = (1, CHAN_MAX)   # 500 canali
        rf.valid_bands = []
        for a in BANDS:
            rf.valid_bands.append(
                (int(BANDS[a][0]*1000000),
                 int(BANDS[a][1]*1000000)))
        return rf

#--------------------------------------------------------------------------------
    def sync_in(self):
        self._mmap = do_download(self)
        self.process_mmap()

#--------------------------------------------------------------------------------
    def sync_out(self):
        do_upload(self)

#--------------------------------------------------------------------------------
    def process_mmap(self):
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)

#--------------------------------------------------------------------------------
    def get_raw_memory(self, number):
        if not 1 <= number <= CHAN_MAX:
            return ""
        return repr(self._memobj.channel[number-1])

#-------------------------------------------------------------------------------- VALIDAZIONE FREQUENZA
    def validate_memory(self, mem):
        msgs = super().validate_memory(mem)
        if mem.duplex == 'off':
            return msgs
        if mem.duplex == '-':
            txfreq = mem.freq - mem.offset
        elif mem.duplex == '+':
            txfreq = mem.freq + mem.offset
        else:
            txfreq = mem.freq
        band = _find_band(txfreq)
        if band is False:
            msg = "Transmit frequency %.4f MHz is not supported by this radio" % (txfreq/1000000.0)
            msgs.append(chirp_common.ValidationError(msg))
        band = _find_band(mem.freq)
        if band is False:
            msg = "The frequency %.4f MHz is not supported by this radio" % (mem.freq/1000000.0)
            msgs.append(chirp_common.ValidationError(msg))
        return msgs

#-------------------------------------------------------------------------------- IMPOSTA TONI
    def _set_tone(self, mem, _mem):
        ((txmode, txtone, txpol),
         (rxmode, rxtone, rxpol)) = chirp_common.split_tone_encode(mem)

        if txmode == "Tone":
            txtoval = CTCSS_TONES.index(txtone)
            txmoval = 0b01
        elif txmode == "DTCS":
            txmoval = txpol == "R" and 0b11 or 0b10
            txtoval = DTCS_CODES.index(txtone)
        else:
            txmoval = 0
            txtoval = 0

        if rxmode == "Tone":
            rxtoval = CTCSS_TONES.index(rxtone)
            rxmoval = 0b01
        elif rxmode == "DTCS":
            rxmoval = rxpol == "R" and 0b11 or 0b10
            rxtoval = DTCS_CODES.index(rxtone)
        else:
            rxmoval = 0
            rxtoval = 0

        _mem.rx_codetype = rxmoval
        _mem.tx_codetype = txmoval
        _mem.rxcode      = rxtoval
        _mem.txcode      = txtoval

#-------------------------------------------------------------------------------- LEGGI TONI
    def _get_tone(self, mem, _mem):
        rxtype  = _mem.rx_codetype
        txtype  = _mem.tx_codetype
        rx_tmode = TMODES[rxtype]
        tx_tmode = TMODES[txtype]
        rx_tone = tx_tone = None

        if tx_tmode == "Tone":
            if _mem.txcode < len(CTCSS_TONES):
                tx_tone = CTCSS_TONES[_mem.txcode]
            else:
                tx_tone  = 0
                tx_tmode = ""
        elif tx_tmode == "DTCS":
            if _mem.txcode < len(DTCS_CODES):
                tx_tone = DTCS_CODES[_mem.txcode]
            else:
                tx_tone  = 0
                tx_tmode = ""

        if rx_tmode == "Tone":
            if _mem.rxcode < len(CTCSS_TONES):
                rx_tone = CTCSS_TONES[_mem.rxcode]
            else:
                rx_tone  = 0
                rx_tmode = ""
        elif rx_tmode == "DTCS":
            if _mem.rxcode < len(DTCS_CODES):
                rx_tone = DTCS_CODES[_mem.rxcode]
            else:
                rx_tone  = 0
                rx_tmode = ""

        tx_pol = txtype == 0x03 and "R" or "N"
        rx_pol = rxtype == 0x03 and "R" or "N"
        chirp_common.split_tone_decode(mem, (tx_tmode, tx_tone, tx_pol), (rx_tmode, rx_tone, rx_pol))


################################################################################################################################
#                                                                                                 L E T T U R A   M E M O R I E
################################################################################################################################

#--------------------------------------------------------------------------------
    def get_memory(self, number2):
        _mem = self._memobj
        refresh_group_list(_mem)

        mem = chirp_common.Memory()

        if isinstance(number2, str):
            number = SPECIALS[number2]
            mem.extd_number = number2
        else:
            number = number2 - 1

        if number < 0 or number >= CHAN_MAX:
            mem.number = number + 1
            mem.empty = True
            mem.immutable = ["name", "scanlists", "freq", "offset", "duplex", "mode", "tmode", "rtone", "ctone", "dtcs", "rx_dtcs", "cross_mode", "power"]
            return mem

        mem.number = number + 1
        _mem = self._memobj.channel[number]
        tmpcomment = ""

        # canale vuoto?
        is_empty = False
        if (_mem.freq == 0xffffffff) or (_mem.freq == 0) or (_mem.band == 0xF):
            is_empty = True

        if is_empty:
            mem.empty = True
            mem.power = UVK5_POWER_LEVELS[2]
            mem.extra = RadioSettingGroup("Extra", "extra")
            rs = RadioSetting("bandwidth", "Bandwidth", RadioSettingValueList(BANDWIDTH_LIST, BANDWIDTH_LIST[0]))
            mem.extra.append(rs)
            rs = RadioSetting("frev", "FreqRev", RadioSettingValueBoolean(False))
            mem.extra.append(rs)
            rs = RadioSetting("pttid", "PTTID", RadioSettingValueList(PTTID_LIST, PTTID_LIST[0]))
            mem.extra.append(rs)
            rs = RadioSetting("agcmode", _("AGC mode"), RadioSettingValueList(AGC_MODE, AGC_MODE[0]))
            mem.extra.append(rs)
            rs = RadioSetting("compander", _("Compander"), RadioSettingValueList(COMPANDER_LIST, COMPANDER_LIST[0]))
            mem.extra.append(rs)
            rs = RadioSetting("scrambler", _("Scrambler"), RadioSettingValueBoolean(False))
            mem.extra.append(rs)
            rs = RadioSetting("squelch", _("Squelch"), RadioSettingValueList(SQUELCH_LIST, SQUELCH_LIST[1]))
            mem.extra.append(rs)
            rs = RadioSetting("writeprot", _("Write Protect"), RadioSettingValueBoolean(False))
            mem.extra.append(rs)
            rs = RadioSetting("txlock", _("TX Lock"), RadioSettingValueBoolean(False))
            mem.extra.append(rs)
            rs = RadioSetting("group", "Group", RadioSettingValueList(GROUP_LIST, GROUP_LIST[0]))
            mem.extra.append(rs)
            rs = RadioSetting("busylock", "Busy Lock", RadioSettingValueBoolean(False))
            mem.extra.append(rs)
            return mem

        if number > (CHAN_MAX-1):
            mem.immutable = ["name", "scanlists"]
        else:
            _mem2 = self._memobj.channel[number]
            for char in _mem2.name:
                if str(char) == "\xFF" or str(char) == "\x00":
                    break
                mem.name += str(char)
            mem.name = mem.name.strip()

        # frequenza e offset
        mem.freq   = int(_mem.freq) * 10
        mem.offset = int(_mem.offset) * 10

        if mem.offset == 0:
            mem.duplex = ''
        else:
            if _mem.shift == OFFSET_MINUS:
                if _mem.freq == _mem.offset:
                    mem.duplex = 'off'
                    mem.offset = 0
                else:
                    mem.duplex = '-'
            elif _mem.shift == OFFSET_PLUS:
                mem.duplex = '+'
            else:
                mem.duplex = ''

        # toni
        self._get_tone(mem, _mem)

        # modulazione
        if _mem.modulation < len(MODULATION_LIST):
            mem.mode = MODULATION_LIST[_mem.modulation]
        else:
            mem.mode = "FM"

        # step
        tstep = _mem.step
        if tstep < len(STEPS):
            mem.tuning_step = STEPS[tstep]
        else:
            mem.tuning_step = 0.02

        # scan skip
        if _mem.enablescan < len(SKIP_VALUES):
            mem.skip = SKIP_VALUES[_mem.enablescan]
        else:
            mem.skip = ""

        # potenza TX
        if _mem.txpower == POWER_HIGH:
            mem.power = UVK5_POWER_LEVELS[2]
        elif _mem.txpower == POWER_MEDIUM:
            mem.power = UVK5_POWER_LEVELS[1]
        else:
            mem.power = UVK5_POWER_LEVELS[0]

        if (_mem.freq == 0xffffffff) or (_mem.freq == 0):
            mem.empty = True
        else:
            mem.empty = False

        mem.extra = RadioSettingGroup("Extra", "extra")

        # Bandwidth
        bwidth = _mem.bw
        if bwidth >= len(BANDWIDTH_LIST):
            bwidth = 0
        rs = RadioSetting("bandwidth", "Bandwidth", RadioSettingValueList(BANDWIDTH_LIST, BANDWIDTH_LIST[bwidth]))
        mem.extra.append(rs)
        tmpcomment += "bandwidth:" + BANDWIDTH_LIST[bwidth] + " "

        # Gruppo
        group = _mem.group
        if group >= len(GROUP_LIST):
            group = 0
        rs = RadioSetting("group", "Group", RadioSettingValueList(GROUP_LIST, GROUP_LIST[group]))
        mem.extra.append(rs)
        tmpcomment += GROUP_LIST[group] + " "

        # Freq reverse
        is_frev = bool(_mem.reverse > 0)
        rs = RadioSetting("frev", "FreqRev", RadioSettingValueBoolean(is_frev))
        mem.extra.append(rs)
        tmpcomment += "FreqReverse:" + (is_frev and "ON" or "OFF") + " "

        # PTTID
        pttid = _mem.ptt_id
        if pttid >= len(PTTID_LIST):
            pttid = 0
        rs = RadioSetting("pttid", "PTTID", RadioSettingValueList(PTTID_LIST, PTTID_LIST[pttid]))
        mem.extra.append(rs)
        tmpcomment += "PTTid:" + PTTID_LIST[pttid] + " "

        # Codici selettive
        codesel = hexasc(_mem.code_sel0) + hexasc(_mem.code_sel1) + \
                  hexasc(_mem.code_sel2) + hexasc(_mem.code_sel3) + \
                  hexasc(_mem.code_sel4) + hexasc(_mem.code_sel5) + \
                  hexasc(_mem.code_sel6) + hexasc(_mem.code_sel7) + \
                  hexasc(_mem.code_sel8) + hexasc(_mem.code_sel9)
        rs = RadioSetting("codesel", "Code PTTID", RadioSettingValueString(0, 10, codesel))
        mem.extra.append(rs)
        tmpcomment += "PTTid Codes:" + codesel + " "

        # Digital Code
        enc = _mem.digcode if _mem.digcode < len(DIGITAL_CODE_LIST) else 0
        rs = RadioSetting("DIGCode", _("DIGCode"), RadioSettingValueList(DIGITAL_CODE_LIST, DIGITAL_CODE_LIST[enc]))
        mem.extra.append(rs)
        tmpcomment += "DIGCode:" + DIGITAL_CODE_LIST[enc] + " "

        # AGC mode
        enc = _mem.agcmode if _mem.agcmode < len(AGC_MODE) else 0
        rs = RadioSetting("agcmode", _("AGC mode"), RadioSettingValueList(AGC_MODE, AGC_MODE[enc]))
        mem.extra.append(rs)
        tmpcomment += "AGC Mode:" + AGC_MODE[enc] + " "

        # Compander
        comp = _mem.compander
        if comp >= len(COMPANDER_LIST):
            comp = 0
        rs = RadioSetting("compander", _("Compander"), RadioSettingValueList(COMPANDER_LIST, COMPANDER_LIST[comp]))
        mem.extra.append(rs)
        tmpcomment += "Compander:" + COMPANDER_LIST[comp] + " "

        # Scrambler (bool per canali)
        scr = bool(_mem.scrambler > 0)
        rs = RadioSetting("scrambler", _("Scrambler"), RadioSettingValueBoolean(scr))
        mem.extra.append(rs)
        tmpcomment += "Scrambler:" + (scr and "ON" or "OFF") + " "

        # Squelch
        sql = _mem.squelch if _mem.squelch < len(SQUELCH_LIST) else 1
        rs = RadioSetting("squelch", _("Squelch"), RadioSettingValueList(SQUELCH_LIST, SQUELCH_LIST[sql]))
        mem.extra.append(rs)
        tmpcomment += SQUELCH_LIST[sql] + " "

        # Busy Lock
        bl = bool(_mem.busylock > 0)
        rs = RadioSetting("busylock", "Busy Lock", RadioSettingValueBoolean(bl))
        mem.extra.append(rs)
        tmpcomment += "Busy Lock:" + (bl and "ON" or "OFF") + " "

        # Write Protect
        wp = bool(_mem.writeprot > 0)
        rs = RadioSetting("writeprot", _("Write Protect"), RadioSettingValueBoolean(wp))
        mem.extra.append(rs)
        tmpcomment += "Write Protect:" + (wp and "ON" or "OFF") + " "

        # TX Lock
        wp = bool(_mem.txlock > 0)
        rs = RadioSetting("txlock", _("TX Lock"), RadioSettingValueBoolean(wp))
        mem.extra.append(rs)
        tmpcomment += "TX Lock:" + (wp and "ON" or "OFF") + " "

        return mem


################################################################################################################################
#                                                                                         S A L V A T A G G I O   M E M O R I E
################################################################################################################################

#--------------------------------------------------------------------------------
    def set_memory(self, mem):
        number = mem.number - 1

        if number < 0 or number >= CHAN_MAX:
            return mem

        _mem = self._memobj.channel[number]

        # canale prima vuoto?
        if _mem.get_raw(asbytes=False)[0] == "\xff":
            _mem.set_raw("\x00" * 32)
            for i in range(10):
                setattr(_mem, "code_sel%d" % i if i < 10 else "code_sel%s" % chr(55+i), 14)
            _mem.code_sel0 = _mem.code_sel1 = _mem.code_sel2 = _mem.code_sel3 = 14
            _mem.code_sel4 = _mem.code_sel5 = _mem.code_sel6 = _mem.code_sel7 = 14
            _mem.code_sel8 = _mem.code_sel9 = 14

        # banda
        _mem.band = _find_band(mem.freq)

        # modulazione
        if mem.mode in MODULATION_LIST:
            _mem.modulation = MODULATION_LIST.index(mem.mode)
        else:
            _mem.modulation = 0

        # frequenza / offset
        _mem.freq   = mem.freq / 10
        _mem.offset = mem.offset / 10

        if mem.duplex == "":
            _mem.offset = 0
            _mem.shift  = 0
        elif mem.duplex == '-':
            _mem.shift = OFFSET_MINUS
        elif mem.duplex == '+':
            _mem.shift = OFFSET_PLUS
        elif mem.duplex == 'off':
            _mem.shift  = OFFSET_MINUS
            _mem.offset = _mem.freq

        # nome
        _mem.name = mem.name.ljust(10)

        # toni
        self._set_tone(mem, _mem)

        # step
        if mem.tuning_step in STEPS:
            _mem.step = STEPS.index(mem.tuning_step)
        else:
            _mem.step = 0

        # potenza TX
        if str(mem.power) == str(UVK5_POWER_LEVELS[2]):
            _mem.txpower = POWER_HIGH
        elif str(mem.power) == str(UVK5_POWER_LEVELS[1]):
            _mem.txpower = POWER_MEDIUM
        else:
            _mem.txpower = POWER_LOW

        # scan skip
        if mem.skip in SKIP_VALUES:
            _mem.enablescan = SKIP_VALUES.index(mem.skip)
        else:
            _mem.enablescan = 0

        # extra
        for setting in mem.extra:
            sname  = setting.get_name()
            svalue = setting.value.get_value()

            if sname == "bandwidth":
                _mem.bw = BANDWIDTH_LIST.index(svalue)
            if sname == "pttid":
                _mem.ptt_id = PTTID_LIST.index(svalue)
            if sname == "frev":
                _mem.reverse = svalue and 1 or 0
            if sname == "DIGCode":
                _mem.digcode = DIGITAL_CODE_LIST.index(svalue)
            if sname == "agcmode":
                _mem.agcmode = AGC_MODE.index(svalue)
            if sname == "compander":
                _mem.compander = COMPANDER_LIST.index(svalue)
            if sname == "scrambler":
                _mem.scrambler = svalue and 1 or 0
            if sname == "group":
                _mem.group = GROUP_LIST.index(svalue)
            if sname == "squelch":
                _mem.squelch = SQUELCH_LIST.index(svalue)
            if sname == "busylock":
                _mem.busylock = svalue and 1 or 0
            if sname == "writeprot":
                _mem.writeprot = svalue and 1 or 0
            if sname == "txlock":
                _mem.txlock = svalue and 1 or 0
            if sname == "codesel":
                _mem.code_sel0 = ascdec(svalue[0])
                _mem.code_sel1 = ascdec(svalue[1])
                _mem.code_sel2 = ascdec(svalue[2])
                _mem.code_sel3 = ascdec(svalue[3])
                _mem.code_sel4 = ascdec(svalue[4])
                _mem.code_sel5 = ascdec(svalue[5])
                _mem.code_sel6 = ascdec(svalue[6])
                _mem.code_sel7 = ascdec(svalue[7])
                _mem.code_sel8 = ascdec(svalue[8])
                _mem.code_sel9 = ascdec(svalue[9])

        # canale rimasto vuoto (freq=0)?
        if _mem.freq == 0:
            _mem.set_raw("\xFF" * 32)
            _mem.code_sel0 = _mem.code_sel1 = _mem.code_sel2 = _mem.code_sel3 = 14
            _mem.code_sel4 = _mem.code_sel5 = _mem.code_sel6 = _mem.code_sel7 = 14
            _mem.code_sel8 = _mem.code_sel9 = 14

        return mem


################################################################################################################################
#                                                                                               L E T T U R A   S E T T I N G S
################################################################################################################################

#--------------------------------------------------------------------------------
    def get_settings(self):
        _mem = self._memobj
        refresh_group_list(_mem)

        basic       = RadioSettingGroup("basic",        "Basic Settings")
        vfoch       = RadioSettingGroup("vfoch",        "VFO / Channel Mode")
        agc         = RadioSettingGroup("agc",          "RF Gain Settings")
        keya        = RadioSettingGroup("keya",         "Programmable keys")
        dtmf        = RadioSettingGroup("dtmf",         "DTMF/5Tone Settings")
        dtmfc       = RadioSettingGroup("dtmfc",        "PHONE BOOK")
        lstn        = RadioSettingGroup("lstn",         "Memory Group")
        preset      = RadioSettingGroup("preset",       "Preset List")
        expert      = RadioSettingGroup("expert",       "Expert Settings")
        calibration = RadioSettingGroup("calibration",  _("***Calibration,Don't touch if you don't know what to do*** "))
        roinfo      = RadioSettingGroup("roinfo",       _("Driver information"))

        top = RadioSettings(basic, vfoch, agc, keya, dtmf, dtmfc, lstn, preset, expert, calibration, roinfo)

        # helper function â€” etichetta decorativa
        def append_label(radio_setting, label, descr=""):
            if not hasattr(append_label, 'idx'):
                append_label.idx = 0
            val = RadioSettingValueString(len(descr), len(descr), descr)
            val.set_mutable(False)
            rs = RadioSetting("label" + str(append_label.idx), label, val)
            append_label.idx += 1
            radio_setting.append(rs)

        #******************************* TASTI PROGRAMMABILI
        append_label(keya, "_" * 30 + " Programmable Key  " + "_" * 274, "_" * 300)

        tmpval = int(_mem.key1_shortpress_action)
        if tmpval >= len(KEYACTIONS_LIST): tmpval = 0
        rs = RadioSetting("key1_shortpress_action", "Side key 1 short press",
                          RadioSettingValueList(KEYACTIONS_LIST, KEYACTIONS_LIST[tmpval]))
        keya.append(rs)

        tmpval = int(_mem.key1_longpress_action)
        if tmpval >= len(KEYACTIONS_LIST): tmpval = 0
        rs = RadioSetting("key1_longpress_action", "Side key 1 long press",
                          RadioSettingValueList(KEYACTIONS_LIST, KEYACTIONS_LIST[tmpval]))
        keya.append(rs)

        tmpval = int(_mem.key2_shortpress_action)
        if tmpval >= len(KEYACTIONS_LIST): tmpval = 0
        rs = RadioSetting("key2_shortpress_action", "Side key 2 short press",
                          RadioSettingValueList(KEYACTIONS_LIST, KEYACTIONS_LIST[tmpval]))
        keya.append(rs)

        tmpval = int(_mem.key2_longpress_action)
        if tmpval >= len(KEYACTIONS_LIST): tmpval = 0
        rs = RadioSetting("key2_longpress_action", "Side key 2 long press",
                          RadioSettingValueList(KEYACTIONS_LIST, KEYACTIONS_LIST[tmpval]))
        keya.append(rs)

        append_label(keya, " " * 40 + "|     |                          ")
        append_label(keya, " " * 40 + "|     |                          ")
        append_label(keya, " " * 40 + "|     |                          ")
        append_label(keya, " " * 40 + "|     |                          ")
        append_label(keya, " " * 40 + "|     |___________ |        |")
        append_label(keya, " " * 40 + "|      ____________      /")
        append_label(keya, " " * 39 + "||     |                     |    |")
        append_label(keya, " " * 30 + " PTT  |" + "|     |                     |    |")
        append_label(keya, " " * 39 + " |     |____________|    |")
        append_label(keya, " " * 21 + "Side key 1 |" + "|          /  /  /  /          |")
        append_label(keya, " " * 21 + "Side key 2 |" + "|          /  /  /  /          |")
        append_label(keya, " " * 41 + "|                                |")
        append_label(keya, " " * 41 + "|                                |")
        append_label(keya, " " * 41 + "|                                |")
        append_label(keya, " " * 41 + "|_________________  |")

        #******************************* DTMF
        append_label(dtmf, "_" * 30 + " DTMF Setting  " + "_" * 274, "_" * 300)

        tmpval = _mem.dtmf_settings.auto_reset_time
        if tmpval > 61 or tmpval < 4: tmpval = 4
        rs = RadioSetting("dtmf_auto_reset_time", "DTMF Auto reset time (s)",
                          RadioSettingValueInteger(4, 61, tmpval))
        dtmf.append(rs)

        tmpval = int(_mem.dtmf_settings.preload_time)
        if tmpval > 100 or tmpval < 3: tmpval = 30
        tmpval *= 10
        rs = RadioSetting("dtmf_preload_time", "DTMF Pre-load time (ms)",
                          RadioSettingValueInteger(30, 1000, tmpval, 10))
        dtmf.append(rs)

        tmpval = int(_mem.dtmf_settings.hash_persist_time)
        if tmpval > 100 or tmpval < 3: tmpval = 30
        tmpval *= 10
        rs = RadioSetting("dtmf_hash_persist_time", "DTMF #/* persist time (ms)",
                          RadioSettingValueInteger(30, 1000, tmpval, 10))
        dtmf.append(rs)

        tmpval = int(_mem.dtmf_settings.code_persist_time)
        if tmpval > 100 or tmpval < 3: tmpval = 30
        tmpval *= 10
        rs = RadioSetting("dtmf_code_persist_time", "DTMF Code persist time (ms)",
                          RadioSettingValueInteger(30, 1000, tmpval, 10))
        dtmf.append(rs)

        tmpval = int(_mem.dtmf_settings.code_interval_time)
        if tmpval > 100 or tmpval < 3: tmpval = 30
        tmpval *= 10
        rs = RadioSetting("dtmf_code_interval_time", "DTMF Code interval time (ms)",
                          RadioSettingValueInteger(30, 1000, tmpval, 10))
        dtmf.append(rs)

        append_label(dtmf, "_" * 10 + " DTMF/5Tone Setting  (1-8 chars 0-9 ABCDEF)" + "_" * 274, "_" * 300)

        tmpval = str(_mem.dtmf_settings_numbers.dtmf_local_code).upper().strip("\x00\xff\x20")
        for i in tmpval:
            if i not in DTMF_CHARS_ID:
                tmpval = "123"; break
        val = RadioSettingValueString(1, 8, tmpval)
        val.set_charset(DTMF_CHARS_ID)
        rs = RadioSetting("dtmf_dtmf_local_code", "DTMF/5Tone OWN ID (used also as ID REGA) ", val)
        dtmf.append(rs)

        tmpval = str(_mem.dtmf_settings_numbers.dtmf_up_code).upper().strip("\x00\xff\x20")
        for i in tmpval:
            if i not in DTMF_CHARS_UPDOWN and i != "":
                tmpval = "456"; break
        val = RadioSettingValueString(1, 8, tmpval)
        val.set_charset(DTMF_CHARS_UPDOWN)
        rs = RadioSetting("dtmf_dtmf_up_code", "DTMF/5tone Up code ***(valid only in VFO Mode)", val)
        dtmf.append(rs)

        tmpval = str(_mem.dtmf_settings_numbers.dtmf_down_code).upper().strip("\x00\xff\x20")
        for i in tmpval:
            if i not in DTMF_CHARS_UPDOWN:
                tmpval = "789"; break
        val = RadioSettingValueString(1, 8, tmpval)
        val.set_charset(DTMF_CHARS_UPDOWN)
        rs = RadioSetting("dtmf_dtmf_down_code", "DTMF/5Tone Down code ***(valid only in VFO Mode)", val)
        dtmf.append(rs)

        tmppr = bool(_mem.dtmf_settings.side_tone > 0)
        rs = RadioSetting("dtmf_side_tone", "DTMF/5Tone Sidetone", RadioSettingValueBoolean(tmppr))
        dtmf.append(rs)

        tmpdlive = _mem.dtmf_live
        if tmpdlive >= len(DLIVE_LIST): tmpdlive = 0
        rs = RadioSetting("dtmf_live", "DTMF/5Tone Live ", RadioSettingValueList(DLIVE_LIST, DLIVE_LIST[tmpdlive]))
        dtmf.append(rs)

        tmpval = str(_mem.dtmf_settings.separate_code)
        if tmpval not in DTMF_CODE_CHARS: tmpval = '*'
        val = RadioSettingValueString(1, 1, tmpval)
        val.set_charset(DTMF_CODE_CHARS)
        rs = RadioSetting("dtmf_separate_code", "DTMF/5Tone Separation Code", val)
        dtmf.append(rs)

        tmpval = str(_mem.dtmf_settings.group_call_code)
        if tmpval not in DTMF_CODE_CHARS: tmpval = '#'
        val = RadioSettingValueString(1, 1, tmpval)
        val.set_charset(DTMF_CODE_CHARS)
        rs = RadioSetting("group_call_code", "DTMF/5Tone Group Call Code", val)
        dtmf.append(rs)

        tmpval = _mem.dtmf_settings.decode_response
        if tmpval >= len(DTMF_DECODE_RESPONSE_LIST): tmpval = 0
        rs = RadioSetting("dtmf_decode_response", "DTMF/5Tone Decode Response",
                          RadioSettingValueList(DTMF_DECODE_RESPONSE_LIST, DTMF_DECODE_RESPONSE_LIST[tmpval]))
        dtmf.append(rs)

        tmpval = int(_mem.dtmf_settings.first_code_persist_time)
        if tmpval > 300: tmpval = 0
        tmpval *= 10
        rs = RadioSetting("dtmf_first_code_persist_time",
                          "DTMF/5Tone First code persist time (ms)",
                          RadioSettingValueInteger(0, 300, tmpval, 10))
        dtmf.append(rs)

        #******************************* PHONE BOOK
        val = RadioSettingValueString(0, 80, "All DTMF/5Tone Contacts are 8 codes (valid: 0-9 * # ABCD), or an empty string")
        val.set_mutable(False)
        rs = RadioSetting("dtmf_descr1", "PHONE GROUP", val)
        dtmfc.append(rs)

        for i in range(1, 17):
            append_label(dtmfc, "_" * 30 + " PHONE GROUP #" + str(i))
            varname    = "DTMF_"    + str(i)
            varnumname = "DTMFNUM_" + str(i)
            cntn       = str(_mem.dtmfcontact[i-1].name).strip("\x20\x00\xff")
            cntnum     = str(_mem.dtmfcontact[i-1].number).strip("\x20\x00\xff")
            val = RadioSettingValueString(0, 8, cntn)
            rs  = RadioSetting(varname, "Contact " + str(i) + " name", val)
            dtmfc.append(rs)
            val = RadioSettingValueString(0, 8, cntnum)
            val.set_charset(DTMF_CHARS)
            rs = RadioSetting(varnumname, "Contact " + str(i) + " number", val)
            dtmfc.append(rs)

        #******************************* MEMORY GROUP (LIST NAME)
        append_label(lstn, "_" * 30 + " Memory Group LIST NAME " + "_" * 274, "_" * 300)

        val = RadioSettingValueString(0, 80, "List Name")
        val.set_mutable(False)
        rs = RadioSetting("list_descr1", "Memory Group ", val)
        lstn.append(rs)

        for i in range(0, 16):
            varlist  = "List Name_" + str(i)
            vardescrl = "Memory Group " + str(i) + " "
            cntnl    = GROUP_LIST[i]
            val      = RadioSettingValueString(0, 8, cntnl)
            rs       = RadioSetting(varlist, vardescrl, val)
            lstn.append(rs)

        #******************************* AGC
        append_label(agc, "_" * 30 + " RF Gain Band Setting " + "_" * 274, "_" * 300)
        val = RadioSettingValueString(0, 80, "RF Gain Value (dBM) (** READ ONLY **)")
        val.set_mutable(False)
        rs = RadioSetting("agc_descr", "RF Gain VFO #", val)
        agc.append(rs)

        for i in range(1, 8):
            nagc  = "agc_" + str(i)
            dagc  = "RF Gain VFO " + str(i) + " "
            vagc  = _mem.agc[i-1].val
            vagcd = AGC_CORR[i-1] + int(AGC_LIST[vagc])
            temp  = RadioSettingValueString(len(str(vagcd)), len(str(vagcd)), str(vagcd))
            temp.set_mutable(False)
            rs = RadioSetting(nagc, dagc, temp)
            agc.append(rs)

        #******************************* PRESET
        for i in range(1, 13):
            append_label(preset, "_" * 30 + " PRESET " + str(i) + "_" * 274, "_" * 300)

            varlist   = "Preset_Name_" + str(i)
            cntnl     = str(_mem.preset[i-1].name).strip("\x20\x00\xff")
            val       = RadioSettingValueString(0, 8, cntnl)
            rs        = RadioSetting(varlist, "Preset Name " + str(i), val)
            preset.append(rs)

            freqlow = "Low_Range_" + str(i)
            flow = _mem.preset[i-1].freq_low / 100000.0
            if flow > 1300.00000:
                rs = RadioSetting(freqlow, "Low Range ", RadioSettingValueString(0, 10, "1300.00000"))
            else:
                rs = RadioSetting(freqlow, "Low Range ", RadioSettingValueString(0, 10, str(flow)))
            preset.append(rs)

            frequp = "Up_Range_" + str(i)
            fup = _mem.preset[i-1].freq_up / 100000.0
            if fup > 1300.00000:
                rs = RadioSetting(frequp, "Up Range ", RadioSettingValueString(0, 10, "1300.00000"))
            else:
                rs = RadioSetting(frequp, "Up Range ", RadioSettingValueString(0, 10, str(fup)))
            preset.append(rs)

            vstep = _mem.preset[i-1].step
            if vstep >= len(STEP_LIST): vstep = 10
            rs = RadioSetting("Step_" + str(i), "Step ", RadioSettingValueList(STEP_LIST, STEP_LIST[vstep]))
            preset.append(rs)

            vtxpower = _mem.preset[i-1].txpower
            if vtxpower >= len(TXPOWER_LIST): vtxpower = 0
            rs = RadioSetting("TX Power_" + str(i), "TX Power ", RadioSettingValueList(TXPOWER_LIST, TXPOWER_LIST[vtxpower]))
            preset.append(rs)

            vbw = _mem.preset[i-1].bw
            if vbw >= len(BANDWIDTH_LIST): vbw = 0
            rs = RadioSetting("Bw_" + str(i), "BW ", RadioSettingValueList(BANDWIDTH_LIST, BANDWIDTH_LIST[vbw]))
            preset.append(rs)

            vagc = _mem.preset[i-1].agcmode
            if vagc >= len(AGC_MODE): vagc = 0
            rs = RadioSetting("AGC_" + str(i), "AGC ", RadioSettingValueList(AGC_MODE, AGC_MODE[vagc]))
            preset.append(rs)

            vmod = _mem.preset[i-1].modulation
            if vmod >= len(MODULATION_LIST): vmod = 0
            rs = RadioSetting("Mode_" + str(i), "Mode ", RadioSettingValueList(MODULATION_LIST, MODULATION_LIST[vmod]))
            preset.append(rs)

            vsql = _mem.preset[i-1].squelch if _mem.preset[i-1].squelch < len(SQUELCH_LIST) else 1
            rs = RadioSetting("Sql_" + str(i), "Squelch ", RadioSettingValueList(SQUELCH_LIST, SQUELCH_LIST[vsql]))
            preset.append(rs)

        #******************************* SETTAGGI DI BASE
        append_label(basic, "_" * 30 + " Display settings " + "_" * 274, "_" * 300)

        tmpback = _mem.backlight_auto_mode
        if tmpback >= len(BACKLIGHT_LIST): tmpback = 0
        rs = RadioSetting("backlight_auto_mode", "BackLightTime",
                          RadioSettingValueList(BACKLIGHT_LIST, BACKLIGHT_LIST[tmpback]))
        basic.append(rs)

        rs = RadioSetting("bl_mode", "BLmode (TX/RX)", RadioSettingValueBoolean(bool(_mem.bl_mode > 0)))
        basic.append(rs)

        rs = RadioSetting("back_type", "Display Inverted", RadioSettingValueBoolean(bool(_mem.back_type > 0)))
        basic.append(rs)

        rs = RadioSetting("micbar", "MicBar", RadioSettingValueBoolean(bool(_mem.micbar > 0)))
        basic.append(rs)

        tmpdispmode = _mem.power_on_dispmode
        if tmpdispmode >= len(WELCOME_LIST): tmpdispmode = 0
        rs = RadioSetting("welcome_mode", "PONMSG",
                          RadioSettingValueList(WELCOME_LIST, WELCOME_LIST[tmpdispmode]))
        basic.append(rs)

        rs = RadioSetting("power_on_beep", "PonBep", RadioSettingValueBoolean(bool(_mem.power_on_beep > 0)))
        basic.append(rs)

        append_label(basic, "_" * 30 + " Audio settings " + "_" * 274, "_" * 300)

        rs = RadioSetting("beep_control", "Beep control", RadioSettingValueBoolean(bool(_mem.beep_control > 0)))
        basic.append(rs)

        tmpmicgain = _mem.mic_gain
        if tmpmicgain >= len(MICGAIN_LIST): tmpmicgain = MICGAIN_LIST.index("+12.0dB")
        rs = RadioSetting("mic_gain", "Mic Gain", RadioSettingValueList(MICGAIN_LIST, MICGAIN_LIST[tmpmicgain]))
        basic.append(rs)

        append_label(basic, "_" * 30 + " Key Lock settings " + "_" * 274, "_" * 300)

        rs = RadioSetting("key_lock", "Keypad Lock", RadioSettingValueBoolean(bool(_mem.key_lock > 0)))
        basic.append(rs)

        rs = RadioSetting("auto_keypad_lock", "Auto keypad lock", RadioSettingValueBoolean(bool(_mem.auto_keypad_lock > 0)))
        basic.append(rs)

        append_label(basic, "_" * 30 + " RTX settings " + "_" * 274, "_" * 300)

        tmpvfo = _mem.vfomode
        if tmpvfo >= len(VFOMODE_LIST): tmpvfo = 0
        rs = RadioSetting("vfomode", "VFO mode ", RadioSettingValueList(VFOMODE_LIST, VFOMODE_LIST[tmpvfo]))
        basic.append(rs)

        tmpbandstx = _mem.bands_tx
        if tmpbandstx >= len(BANDS_TX_LIST): tmpbandstx = 0
        rs = RadioSetting("bands_tx", "Bands TX", RadioSettingValueList(BANDS_TX_LIST, BANDS_TX_LIST[tmpbandstx]))
        basic.append(rs)

        append_label(basic, "_" * 30 + " Beacon/CQ Call settings " + "_" * 274, "_" * 300)

        tmpbeacon = _mem.beacon
        if tmpbeacon >= len(BEACON_LIST): tmpbeacon = BEACON_LIST.index("OFF")
        rs = RadioSetting("beacon", "Beacon/CQ Call", RadioSettingValueList(BEACON_LIST, BEACON_LIST[tmpbeacon]))
        basic.append(rs)

        qrz_label = str(_mem.qrz_label).rstrip("\x20\x00\xff") + "\x00"
        qrz_label = _getstring(qrz_label.encode('ascii', errors='ignore'), 0, 8)
        rs = RadioSetting("qrz_label", _("QRA (8 characters)"), RadioSettingValueString(0, 8, qrz_label))
        basic.append(rs)

        logo1 = str(_mem.logo_line1).strip("\x20\x00\xff") + "\x00"
        logo1 = _getstring(logo1.encode('ascii', errors='ignore'), 0, 16)
        rs = RadioSetting("logo1", _("Logo string 1 (16 characters)"), RadioSettingValueString(0, 16, logo1))
        basic.append(rs)

        logo2 = str(_mem.logo_line2).strip("\x20\x00\xff") + "\x00"
        logo2 = _getstring(logo2.encode('ascii', errors='ignore'), 0, 16)
        rs = RadioSetting("logo2", _("Logo string 2 (16 characters)"), RadioSettingValueString(0, 16, logo2))
        basic.append(rs)

        append_label(basic, "_" * 30 + " Other settings " + "_" * 274, "_" * 300)

        tmptot = _mem.max_talk_time
        if tmptot >= len(TALKTIME_LIST): tmptot = TALKTIME_LIST.index("3min")
        rs = RadioSetting("max_talk_time", "Max talk time (Tx TOT)",
                          RadioSettingValueList(TALKTIME_LIST, TALKTIME_LIST[tmptot]))
        basic.append(rs)

        tmpscanres = _mem.scan_resume_mode
        if tmpscanres >= len(SCANRESUME_LIST): tmpscanres = 0
        rs = RadioSetting("scan_resume_mode", "Scan resume mode (Sc REV)",
                          RadioSettingValueList(SCANRESUME_LIST, SCANRESUME_LIST[tmpscanres]))
        basic.append(rs)

        #******************************* VFO / CHANNEL MODE
        append_label(vfoch, "_" * 300 + "_" * 274, "_" * 300)

        tmpchdispmode = _mem.channel_display_mode
        if tmpchdispmode >= len(CHANNELDISP_LIST): tmpchdispmode = 0
        rs = RadioSetting("channel_display_mode", "Channel Display mode",
                          RadioSettingValueList(CHANNELDISP_LIST, CHANNELDISP_LIST[tmpchdispmode]))
        vfoch.append(rs)

        tmpc = _mem.call_channel + 1
        if tmpc > CHAN_MAX: tmpc = 1
        rs = RadioSetting("call_channel", "Call channel", RadioSettingValueInteger(1, CHAN_MAX, tmpc))
        vfoch.append(rs)

        #******************************* EXPERT SETTINGS
        append_label(expert, "_" * 30 + " Expert settings " + "_" * 274, "_" * 300)

        rs = RadioSetting("tail_note_elimination", "TX Squelch Tail Elimination",
                          RadioSettingValueBoolean(bool(_mem.tail_note_elimination > 0)))
        expert.append(rs)

        tmprte = _mem.repeater_tail_elimination
        if tmprte >= len(RTE_LIST): tmprte = 0
        rs = RadioSetting("repeater_tail_elimination", "RX Squelch Tail Elimination",
                          RadioSettingValueList(RTE_LIST, RTE_LIST[tmprte]))
        expert.append(rs)

        rs = RadioSetting("tx_enable", "TX enable", RadioSettingValueBoolean(bool(_mem.tx_enable > 0)))
        expert.append(rs)

        rs = RadioSetting("vfo_lock", "VFO hidden", RadioSettingValueBoolean(bool(_mem.vfo_lock > 0)))
        expert.append(rs)

        custom_tone = _mem.custom_tone / 10.0
        if custom_tone > CTMAX:
            rs = RadioSetting("custom_tone", "CTCSS Custom Tone (0 ~ 255 Hz)",
                              RadioSettingValueString(0, 5, "255.0"))
        else:
            rs = RadioSetting("custom_tone", "CTCSS Custom Tone (0 ~ 255 Hz)",
                              RadioSettingValueString(0, 5, str(custom_tone)))
        expert.append(rs)

        append_label(expert, " " * 300, " " * 300)

        if _mem.afc > 7: _mem.afc = 7
        rs = RadioSetting("afc", "AFC (0~7)", RadioSettingValueInteger(0, 7, _mem.afc))
        expert.append(rs)

        if _mem.tx_dev > 9: _mem.tx_dev = 9
        rs = RadioSetting("tx_dev", "Tx Deviation(0=Standard, 9=Max deviation)",
                          RadioSettingValueInteger(0, 9, _mem.tx_dev))
        expert.append(rs)

        rs = RadioSetting("savedirect", "AutoSave ", RadioSettingValueBoolean(bool(_mem.savedirect > 0)))
        expert.append(rs)

        append_label(expert, "_" * 30 + " SATCOM " + "_" * 274, "_" * 300)

        sat_freq = _mem.sat_freq / 100000.0
        if sat_freq > 290.00000 or sat_freq < 210.00000:
            rs = RadioSetting("sat_freq",
                              "Sat Switch Frequency (range 210 ~ 290.00000 MHz)",
                              RadioSettingValueString(0, 9, "280.00000"))
        else:
            rs = RadioSetting("sat_freq",
                              "Sat Switch Frequency (range 210 ~ 290.00000 MHz)",
                              RadioSettingValueString(0, 9, str(sat_freq)))
        expert.append(rs)

        append_label(expert, " " * 300, " " * 300)
        append_label(expert, "_" * 30 + " UP CONVERTER " + "_" * 274, "_" * 300)

        tmpupconv = _mem.upconv
        if tmpupconv >= len(UPCONV_LIST): tmpupconv = UPCONV_LIST.index("OFF")
        rs = RadioSetting("upconv", "UP Converter", RadioSettingValueList(UPCONV_LIST, UPCONV_LIST[tmpupconv]))
        expert.append(rs)

        custom_upconv = _mem.custom_upconv / 100000.0
        if custom_upconv > 999.99999:
            rs = RadioSetting("custom_upconv",
                              "Custom Upconverter Frequency (max 999.99999 MHz)",
                              RadioSettingValueString(0, 9, "999.99999"))
        else:
            rs = RadioSetting("custom_upconv",
                              "Custom Upconverter Frequency (max 999.99999 MHz)",
                              RadioSettingValueString(0, 9, str(custom_upconv)))
        expert.append(rs)

        #******************************* CALIBRATION
        radio_setting_group = RadioSettingGroup("other0_calibration", "BattCal")
        calibration.append(radio_setting_group)
        append_label(radio_setting_group, " " * 300, " " * 300)
        append_label(radio_setting_group, "_" * 30 + " BATTERY CALIBRATION " + "_" * 274, "_" * 300)

        for idx, (name, label, scale) in enumerate([
            ("batt_cal0", "Battery voltage offset", 100.0),
            ("batt_cal1", "Threshold full",         10.0),
            ("batt_cal2", "Threshold 4 bar",        10.0),
            ("batt_cal3", "Threshold 3 bar",        10.0),
            ("batt_cal4", "Threshold 2 bar",        10.0),
            ("batt_cal5", "Threshold 1 bar",        10.0),
            ("batt_cal6", "Threshold empty",        10.0),
        ]):
            v = getattr(_mem, name) / scale
            radio_setting = RadioSetting(name, label, RadioSettingValueString(0, 4, str(v)))
            radio_setting_group.append(radio_setting)

        radio_setting_group = RadioSettingGroup("other_calibration", "FrqCal")
        calibration.append(radio_setting_group)
        name     = "cal.xtalFreq"
        temp_val = min_max_def(_mem.get_path(name), -50, 50, 0)
        val      = RadioSettingValueInteger(-50, 50, temp_val)
        radio_setting = RadioSetting(name, "Xtal frequency correction ,value -50 to +50", val)
        radio_setting_group.append(radio_setting)

        radio_setting_group = RadioSettingGroup("tx_power_calibration", "TxpCal")
        calibration.append(radio_setting_group)
        for bnd in range(0, 7):
            band_group = RadioSettingSubGroup('txpower_band_%i' % bnd,
                                              'Band %i' % (bnd+1) + (BANDSTX_CAL[bnd]))
            powers = {"low": "Low", "mid": "Medium", "hi": "High"}
            radio_setting_group.append(band_group)
            for pwr, pwrn in powers.items():
                bounds   = ["lower", "center", "upper"]
                subgroup = RadioSettingSubGroup('txpower_band_%i_%s' % (bnd, pwr), pwrn)
                band_group.append(subgroup)
                for bound in bounds:
                    name     = f"cal.txp[{bnd}].{pwr}.{bound}"
                    tempval  = min_max_def(_mem.get_path(name), 0, 255, 0)
                    val      = RadioSettingValueInteger(0, 255, tempval)
                    radio_setting = RadioSetting(name, bound.capitalize(), val)
                    subgroup.append(radio_setting)

        radio_setting_group = RadioSettingGroup("squelch_calibration", "Squelch")
        calibration.append(radio_setting_group)
        bands = {"sqlBand1_3": "Frequency Band 1-3 (13 ~ 174 MHz)",
                 "sqlBand4_7": "Frequency Band 4-7 (174 ~ 1300 MHz)"}
        for bnd, bndn in bands.items():
            band_group_range = RadioSettingSubGroup(bnd, bndn)
            radio_setting_group.append(band_group_range)
            for sql in range(1, 10):
                band_group = RadioSettingSubGroup('%s_%i' % (bnd, sql), "Squelch %i" % sql)
                band_group_range.append(band_group)
                for sqparam, sqlab in [
                    ("openGlitchThr", "Glitch threshold open"),
                    ("openNoiseThr",  "Noise threshold open"),
                    ("openRssiThr",   "RSSI threshold open"),
                ]:
                    name    = "cal.%s.%s[%i]" % (bnd, sqparam, sql)
                    maxval  = 127 if "Noise" in sqlab else 255
                    tempval = min_max_def(_mem.get_path(name), 0, maxval, 0)
                    val     = RadioSettingValueInteger(0, maxval, tempval)
                    radio_setting = RadioSetting(name, sqlab, val)
                    band_group.append(radio_setting)

        radio_setting_group = RadioSettingGroup("reserved", "reserved")
        calibration.append(radio_setting_group)

        #******************************* INFO
        if self.FIRMWARE_VERSION == "":
            firmware = "To get the firmware version please download the image from the radio first"
        else:
            firmware = self.FIRMWARE_VERSION
        val = RadioSettingValueString(0, 128, firmware)
        val.set_mutable(False)
        rs = RadioSetting("fw_ver", "Firmware Version", val)
        roinfo.append(rs)

        val = RadioSettingValueBoolean(self._expanded_limits)
        rs  = RadioSetting("nolimits", "Limits disabled for modified firmware", val)
        rs.set_warning(_(
            'This should only be enabled if you are using modified firmware '
            'that supports wider frequency coverage. Enabling this will cause '
            'CHIRP not to enforce OEM restrictions and may lead to undefined '
            'or unregulated behavior. Use at your own risk!'),
            safe_value=False)
        roinfo.append(rs)

        return top


################################################################################################################################
#                                                                                        S A L V A T A G G I O  S E T T I N G S
################################################################################################################################

#--------------------------------------------------------------------------------
    def set_settings(self, settings):
        _mem = self._memobj

        for element in settings:
            if not isinstance(element, RadioSetting):
                self.set_settings(element)
                continue

            if element.get_name() == "bl_mode":
                _mem.bl_mode = element.value and 1 or 0
            if element.get_name() == "back_type":
                _mem.back_type = element.value and 1 or 0
            if element.get_name() == "signal_meter":
                _mem.signal_meter = element.value and 1 or 0
            if element.get_name() == "beep_control":
                _mem.beep_control = element.value and 1 or 0
            if element.get_name() == "power_on_beep":
                _mem.power_on_beep = element.value and 1 or 0
            if element.get_name() == "micbar":
                _mem.micbar = element.value and 1 or 0
            if element.get_name() == "tail_note_elimination":
                _mem.tail_note_elimination = element.value and 1 or 0
            if element.get_name() == "key_lock":
                _mem.key_lock = element.value and 1 or 0
            if element.get_name() == "auto_keypad_lock":
                _mem.auto_keypad_lock = element.value and 1 or 0
            if element.get_name() == "tx_enable":
                _mem.tx_enable = element.value and 1 or 0
            if element.get_name() == "vfo_lock":
                _mem.vfo_lock = element.value and 1 or 0
            if element.get_name() == "savedirect":
                _mem.savedirect = element.value and 1 or 0
            if element.get_name() == "satcom":
                _mem.satcom = element.value and 1 or 0

            if element.get_name() == "call_channel":
                _mem.call_channel = int(element.value) - 1
            if element.get_name() == "max_talk_time":
                _mem.max_talk_time = TALKTIME_LIST.index(str(element.value))
            if element.get_name() == "beacon":
                _mem.beacon = BEACON_LIST.index(str(element.value))
            if element.get_name() == "afc":
                _mem.afc = int(element.value)
            if element.get_name() == "tx_dev":
                _mem.tx_dev = int(element.value)
            if element.get_name() == "mic_gain":
                _mem.mic_gain = MICGAIN_LIST.index(str(element.value))
            if element.get_name() == "channel_display_mode":
                _mem.channel_display_mode = CHANNELDISP_LIST.index(str(element.value))
            if element.get_name() == "backlight_auto_mode":
                _mem.backlight_auto_mode = BACKLIGHT_LIST.index(str(element.value))
            if element.get_name() == "welcome_mode":
                _mem.power_on_dispmode = WELCOME_LIST.index(str(element.value))
            if element.get_name() == "repeater_tail_elimination":
                _mem.repeater_tail_elimination = RTE_LIST.index(str(element.value))
            if element.get_name() == "vfomode":
                _mem.vfomode = VFOMODE_LIST.index(str(element.value))
            if element.get_name() == "bands_tx":
                _mem.bands_tx = BANDS_TX_LIST.index(str(element.value))
            if element.get_name() == "battery_save":
                pass  # commentato nel firmware
            if element.get_name() == "scan_resume_mode":
                _mem.scan_resume_mode = SCANRESUME_LIST.index(str(element.value))
            if element.get_name() == "qrz_label":
                _mem.qrz_label = element.value
            if element.get_name() == "logo1":
                _mem.logo_line1 = element.value
            if element.get_name() == "logo2":
                _mem.logo_line2 = element.value
            if element.get_name() == "upconv":
                _mem.upconv = UPCONV_LIST.index(str(element.value))

            if element.get_name() == "custom_upconv":
                val = str(element.value).strip()
                try:    val2 = round(float(val) * 100000.0)
                except: val2 = 0
                if val2 > 999.99999 * 100000.0: val2 = 0
                _mem.custom_upconv = val2

            if element.get_name() == "custom_tone":
                val = str(element.value).strip()
                try:    val2 = round(float(val) * 10)
                except: val2 = 0x09F6
                if val2 > CTMAX * 10: val2 = 0x09F6
                _mem.custom_tone = val2

            if element.get_name() == "sat_freq":
                val = str(element.value).strip()
                try:    val2 = round(float(val) * 100000.0)
                except: val2 = 0x1AB3F00
                if val2 > 290.00000 * 100000.0 or val2 < 210.00000 * 100000.0: val2 = 0x1AB3F00
                _mem.sat_freq = val2

            # Battery calibration
            for name, scale, default in [
                ("batt_cal0", 100, 0xc8), ("batt_cal1", 10, 0x50),
                ("batt_cal2", 10, 0x4e),  ("batt_cal3", 10, 0x4b),
                ("batt_cal4", 10, 0x48),  ("batt_cal5", 10, 0x44),
                ("batt_cal6", 10, 0x41),
            ]:
                if element.get_name() == name:
                    val = str(element.value).strip()
                    try:    val2 = round(float(val) * scale)
                    except: val2 = default
                    if val2 > 9 * scale: val2 = default
                    setattr(_mem, name, val2)

            # DTMF settings
            if element.get_name() == "dtmf_side_tone":
                _mem.dtmf_settings.side_tone = element.value and 1 or 0
            if element.get_name() == "dtmf_separate_code":
                _mem.dtmf_settings.separate_code = str(element.value)
            if element.get_name() == "group_call_code":
                _mem.dtmf_settings.group_call_code = str(element.value)
            if element.get_name() == "dtmf_decode_response":
                _mem.dtmf_settings.decode_response = DTMF_DECODE_RESPONSE_LIST.index(str(element.value))
            if element.get_name() == "dtmf_auto_reset_time":
                _mem.dtmf_settings.auto_reset_time = int(element.value)
            if element.get_name() == "dtmf_preload_time":
                _mem.dtmf_settings.preload_time = int(int(element.value) / 10)
            if element.get_name() == "dtmf_first_code_persist_time":
                _mem.dtmf_settings.first_code_persist_time = int(int(element.value) / 10)
            if element.get_name() == "dtmf_hash_persist_time":
                _mem.dtmf_settings.hash_persist_time = int(int(element.value) / 10)
            if element.get_name() == "dtmf_code_persist_time":
                _mem.dtmf_settings.code_persist_time = int(int(element.value) / 10)
            if element.get_name() == "dtmf_code_interval_time":
                _mem.dtmf_settings.code_interval_time = int(int(element.value) / 10)
            if element.get_name() == "dtmf_live":
                _mem.dtmf_live = list(DLIVE_LIST).index(str(element.value))
            if element.get_name() == "dtmf_dtmf_local_code":
                k = str(element.value).rstrip("\x20\xff\x00") + "\x00" * 8
                _mem.dtmf_settings_numbers.dtmf_local_code = k[0:8]
            if element.get_name() == "dtmf_dtmf_up_code":
                k = str(element.value).strip("\x20\xff\x00") + "\x00" * 8
                _mem.dtmf_settings_numbers.dtmf_up_code = k[0:8]
            if element.get_name() == "dtmf_dtmf_down_code":
                k = str(element.value).rstrip("\x20\xff\x00") + "\x00" * 8
                _mem.dtmf_settings_numbers.dtmf_down_code = k[0:8]

            # Contatti DTMF (Phone Book)
            for i in range(1, 17):
                if element.get_name() == "DTMF_" + str(i):
                    _mem.dtmfcontact[i-1].name = str(element.value)
                if element.get_name() == "DTMFNUM_" + str(i):
                    _mem.dtmfcontact[i-1].number = str(element.value)

            # Nomi gruppi (List Name)
            for i in range(0, 16):
                if element.get_name() == "List Name_" + str(i):
                    _mem.list_name[i].name = element.value

            # Tasti programmabili
            if element.get_name() == "key1_shortpress_action":
                _mem.key1_shortpress_action = KEYACTIONS_LIST.index(str(element.value))
            if element.get_name() == "key1_longpress_action":
                _mem.key1_longpress_action = KEYACTIONS_LIST.index(str(element.value))
            if element.get_name() == "key2_shortpress_action":
                _mem.key2_shortpress_action = KEYACTIONS_LIST.index(str(element.value))
            if element.get_name() == "key2_longpress_action":
                _mem.key2_longpress_action = KEYACTIONS_LIST.index(str(element.value))

            # nolimits
            if element.get_name() == "nolimits":
                self._expanded_limits = bool(element.value)

            # Preset (loop su 12 preset)
            for i in range(1, 13):
                if element.get_name() == "Preset_Name_" + str(i):
                    _mem.preset[i-1].name = str(element.value)

                if element.get_name() == "Low_Range_" + str(i):
                    val = str(element.value).strip()
                    try:    val2 = round(float(val) * 100000.0)
                    except: val2 = 0
                    if val2 > 1300.00000 * 100000.0: val2 = 0
                    _mem.preset[i-1].freq_low = val2

                if element.get_name() == "Up_Range_" + str(i):
                    val = str(element.value).strip()
                    try:    val2 = round(float(val) * 100000.0)
                    except: val2 = 0
                    if val2 > 1300.00000 * 100000.0: val2 = 0
                    _mem.preset[i-1].freq_up = val2

                if element.get_name() == "Step_" + str(i):
                    _mem.preset[i-1].step = STEP_LIST.index(str(element.value))
                if element.get_name() == "TX Power_" + str(i):
                    _mem.preset[i-1].txpower = TXPOWER_LIST.index(str(element.value))
                if element.get_name() == "Bw_" + str(i):
                    _mem.preset[i-1].bw = BANDWIDTH_LIST.index(str(element.value))
                if element.get_name() == "AGC_" + str(i):
                    _mem.preset[i-1].agcmode = AGC_MODE.index(str(element.value))
                if element.get_name() == "Mode_" + str(i):
                    _mem.preset[i-1].modulation = MODULATION_LIST.index(str(element.value))
                if element.get_name() == "Sql_" + str(i):
                    _mem.preset[i-1].squelch = SQUELCH_LIST.index(str(element.value))

