# HG changeset patch
# User Jim Unroe <rock.unroe@gmail.com>
# Date 1637458706 18000
#      Sat Nov 20 20:38:26 2021 -0500
# Node ID cb3753ad6d1162edd182df51ba86f4411424af6b
# Parent  87039272c0404ad20a440aab1f2c75d028fd7b7d
[RB18] Prep for adding Retevis RB18

This patch cleans up some code and gets the driver ready for adding the
Retevis RB18.

#9559

diff -r 87039272c040 -r cb3753ad6d11 chirp/drivers/radtel_t18.py
--- a/chirp/drivers/radtel_t18.py	Wed Nov 17 11:34:23 2021 -0800
+++ b/chirp/drivers/radtel_t18.py	Sat Nov 20 20:38:26 2021 -0500
@@ -1,4 +1,4 @@
-# Copyright 2017 Jim Unroe <rock.unroe@gmail.com>
+# Copyright 2021 Jim Unroe <rock.unroe@gmail.com>
 #
 # 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
@@ -63,7 +63,6 @@
 """
 
 CMD_ACK = "\x06"
-BLOCK_SIZE = 0x08
 
 VOICE_LIST = ["Off", "Chinese", "English"]
 TIMEOUTTIMER_LIST = ["Off", "30 seconds", "60 seconds", "90 seconds",
@@ -77,7 +76,7 @@
 SIDEKEY2_LIST = ["Off", "Scan", "Emergency Alarm", "Display Battery"]
 
 SETTING_LISTS = {
-    "voice": VOICE_LIST,
+    "voiceprompt": VOICE_LIST,
     "timeouttimer": TIMEOUTTIMER_LIST,
     "scanmode": SCANMODE_LIST,
     "voxlevel": VOXLEVEL_LIST,
@@ -152,7 +151,7 @@
 def _t18_exit_programming_mode(radio):
     serial = radio.pipe
     try:
-        serial.write("b")
+        serial.write(radio.CMD_EXIT)
     except:
         raise errors.RadioError("Radio refused to exit programming mode")
 
@@ -189,7 +188,7 @@
     serial = radio.pipe
 
     cmd = struct.pack(">cHb", 'W', block_addr, block_size)
-    data = radio.get_mmap()[block_addr:block_addr + 8]
+    data = radio.get_mmap()[block_addr:block_addr + block_size]
 
     LOG.debug("Writing Data:")
     LOG.debug(util.hexprint(cmd + data))
@@ -215,11 +214,11 @@
     status.cur = 0
     status.max = radio._memsize
 
-    for addr in range(0, radio._memsize, BLOCK_SIZE):
-        status.cur = addr + BLOCK_SIZE
+    for addr in range(0, radio._memsize, radio.BLOCK_SIZE):
+        status.cur = addr + radio.BLOCK_SIZE
         radio.status_fn(status)
 
-        block = _t18_read_block(radio, addr, BLOCK_SIZE)
+        block = _t18_read_block(radio, addr, radio.BLOCK_SIZE)
         data += block
 
         LOG.debug("Address: %04x" % addr)
@@ -240,10 +239,10 @@
     status.max = radio._memsize
 
     for start_addr, end_addr in radio._ranges:
-        for addr in range(start_addr, end_addr, BLOCK_SIZE):
-            status.cur = addr + BLOCK_SIZE
+        for addr in range(start_addr, end_addr, radio.BLOCK_SIZE):
+            status.cur = addr + radio.BLOCK_SIZE
             radio.status_fn(status)
-            _t18_write_block(radio, addr, BLOCK_SIZE)
+            _t18_write_block(radio, addr, radio.BLOCK_SIZE)
 
     _t18_exit_programming_mode(radio)
 
@@ -264,6 +263,8 @@
     VENDOR = "Radtel"
     MODEL = "T18"
     BAUD_RATE = 9600
+    BLOCK_SIZE = 0x08
+    CMD_EXIT = "b"
 
     _magic = "1ROGRAM"
     _fingerprint = "SMP558" + "\x00\x00"
@@ -284,6 +285,8 @@
         rf.valid_skips = ["", "S"]
         rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
         rf.valid_duplexes = ["", "-", "+", "split", "off"]
+        if self.MODEL != "T18":
+            rf.valid_power_levels = self.POWER_LEVELS
         rf.can_odd_split = True
         rf.has_rx_dtcs = True
         rf.has_ctone = True
@@ -412,6 +415,9 @@
         rxtone = self._decode_tone(_mem.rxtone)
         chirp_common.split_tone_decode(mem, txtone, rxtone)
 
+        if self.MODEL != "T18":
+            mem.power = self.POWER_LEVELS[_mem.highpower]
+
         if self._frs:
             FRS_IMMUTABLE = ["freq", "duplex", "offset", "mode"]
             if mem.number >= 8 and mem.number <= 14:
@@ -469,6 +475,9 @@
         self._encode_tone(_mem.txtone, *txtone)
         self._encode_tone(_mem.rxtone, *rxtone)
 
+        if self.MODEL != "T18":
+            _mem.highpower = mem.power == self.POWER_LEVELS[1]
+
         _mem.narrow = 'N' in mem.mode
         _mem.skip = mem.skip == "S"
 
@@ -590,6 +599,9 @@
     VENDOR = "Retevis"
     MODEL = "RT22S"
 
+    POWER_LEVELS = [chirp_common.PowerLevel("Low",  watts=0.50),
+                    chirp_common.PowerLevel("High", watts=2.00)]
+
     _magic = "9COGRAM"
     _fingerprint = "SMP558" + "\x02"
     _upper = 22
