<div dir="ltr">I fixed all of the items for which I received feedback, and also added the auto-identification of the image file.<div>anything else that needs to be modified?  I would like to have this included in the main branch, as it should be ready to be used by a wider audience.</div><div><br></div><div>thanks</div><div>Dave KK6DF</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Apr 27, 2015 at 11:40 PM, David Fannin <span dir="ltr">&lt;<a href="mailto:dfannin@sushisoft.com" target="_blank">dfannin@sushisoft.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"># HG changeset patch<br>
# User David Fannin &lt;<a href="mailto:dfannin@sushisoft.com">dfannin@sushisoft.com</a>&gt;<br>
# Date 1430203203 25200<br>
#      Mon Apr 27 23:40:03 2015 -0700<br>
# Node ID 237bb7557e557c689aa8d1863c40f2388b596974<br>
# Parent  31a7494c324a347634c30cc3c9bded2ce83eeb21<br>
[Th9000] Adding devices file  for new radio TYT TH9000D models  #1035<br>
<br>
diff -r 31a7494c324a -r 237bb7557e55 chirp/drivers/th9000.py<br>
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000<br>
+++ b/chirp/drivers/th9000.py   Mon Apr 27 23:40:03 2015 -0700<br>
@@ -0,0 +1,839 @@<br>
+# Copyright 2015 David Fannin KK6DF  &lt;<a href="mailto:kk6df@arrl.org">kk6df@arrl.org</a>&gt;<br>
+#<br>
+# This program is free software: you can redistribute it and/or modify<br>
+# it under the terms of the GNU General Public License as published by<br>
+# the Free Software Foundation, either version 2 of the License, or<br>
+# (at your option) any later version.<br>
+#<br>
+# This program is distributed in the hope that it will be useful,<br>
+# but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br>
+# GNU General Public License for more details.<br>
+#<br>
+# You should have received a copy of the GNU General Public License<br>
+# along with this program.  If not, see &lt;<a href="http://www.gnu.org/licenses/" target="_blank">http://www.gnu.org/licenses/</a>&gt;.<br>
+<br>
+import os<br>
+import struct<br>
+import time<br>
+import logging<br>
+<br>
+from chirp import bitwise<br>
+from chirp import chirp_common<br>
+from chirp import directory<br>
+from chirp import errors<br>
+from chirp import memmap<br>
+from chirp import util<br>
+from chirp.settings import RadioSettingGroup, RadioSetting, RadioSettings, \<br>
+    RadioSettingValueList, RadioSettingValueString, RadioSettingValueBoolean, \<br>
+    RadioSettingValueInteger, RadioSettingValueString, \<br>
+    RadioSettingValueFloat, InvalidValueError<br>
+<br>
+LOG = logging.getLogger(__name__)<br>
+<br>
+#<br>
+#  Chirp Driver for TYT TH-9000D (models: 2M (144 Mhz), 1.25M (220 Mhz)  and 70cm (440 Mhz)  radios)<br>
+#<br>
+#  Version 1.0<br>
+#<br>
+#  Note: Features not working:<br>
+#         - DCS , Cross Signaling<br>
+#         - Skip channels<br>
+#<br>
+# Global Parameters<br>
+#<br>
+MMAPSIZE = 16384<br>
+TONES = [62.5] + list(chirp_common.TONES)<br>
+TMODES =  [&#39;&#39;,&#39;Tone&#39;,&#39;DTCS&#39;]<br>
+DUPLEXES = [&#39;&#39;,&#39;err&#39;,&#39;-&#39;,&#39;+&#39;] # index 2 not used<br>
+MODES = [&#39;WFM&#39;,&#39;FM&#39;,&#39;NFM&#39;]  #  25k, 20k,15k bw<br>
+TUNING_STEPS=[ 5.0, 6.25, 8.33, 10.0, 12.5, 15.0, 20.0, 25.0, 30.0, 50.0 ] # index 0-9<br>
+POWER_LEVELS=[chirp_common.PowerLevel(&quot;High&quot;, watts=65),<br>
+              chirp_common.PowerLevel(&quot;Mid&quot;, watts=25),<br>
+              chirp_common.PowerLevel(&quot;Low&quot;, watts=10)]<br>
+<br>
+CROSS_MODES = chirp_common.CROSS_MODES<br>
+<br>
+APO_LIST = [ &quot;Off&quot;,&quot;30 min&quot;,&quot;1 hr&quot;,&quot;2 hrs&quot; ]<br>
+BGCOLOR_LIST = [&quot;Blue&quot;,&quot;Orange&quot;,&quot;Purple&quot;]<br>
+BGBRIGHT_LIST = [&quot;%s&quot; % x for x in range(1,32)]<br>
+SQUELCH_LIST = [&quot;Off&quot;] + [&quot;Level %s&quot; % x for x in range(1,20)]<br>
+TIMEOUT_LIST = [&quot;Off&quot;] + [&quot;%s min&quot; % x for x in range(1,30)]<br>
+TXPWR_LIST = [&quot;60W&quot;,&quot;25W&quot;]  # maximum power for Hi setting<br>
+TBSTFREQ_LIST = [&quot;1750Hz&quot;,&quot;2100Hz&quot;,&quot;1000Hz&quot;,&quot;1450Hz&quot;]<br>
+BEEP_LIST = [&quot;Off&quot;,&quot;On&quot;]<br>
+<br>
+SETTING_LISTS = {<br>
+        &quot;auto_power_off&quot;: APO_LIST,<br>
+        &quot;bg_color&quot;      : BGCOLOR_LIST,<br>
+        &quot;bg_brightness&quot; : BGBRIGHT_LIST,<br>
+        &quot;squelch&quot;       : SQUELCH_LIST,<br>
+        &quot;timeout_timer&quot; : TIMEOUT_LIST,<br>
+        &quot;choose_tx_power&quot;: TXPWR_LIST,<br>
+        &quot;tbst_freq&quot;     : TBSTFREQ_LIST,<br>
+        &quot;voice_prompt&quot;  : BEEP_LIST<br>
+}<br>
+<br>
+MEM_FORMAT = &quot;&quot;&quot;<br>
+#seekto 0x0000;<br>
+struct {<br>
+   u8 unknown0000[16];<br>
+   char idhdr[16];<br>
+   u8 unknown0001[16];<br>
+} fidhdr;<br>
+&quot;&quot;&quot;<br>
+#Overall Memory Map:<br>
+#<br>
+#    Memory Map (Range 0x0100-3FF0, step 0x10):<br>
+#<br>
+#        Field                   Start  End  Size<br>
+#                                (hex)  (hex) (hex)<br>
+#<br>
+#        1 Channel Set Flag        0100  011F   20<br>
+#        2 Channel Skip Flag       0120  013F   20<br>
+#        3 Blank/Unknown           0140  01EF   B0<br>
+#        4 Unknown                 01F0  01FF   10<br>
+#        5 TX/RX Range             0200  020F   10<br>
+#        6 Bootup Passwd           0210  021F   10<br>
+#        7 Options, Radio          0220  023F   20<br>
+#        8 Unknown                 0240  019F<br>
+#            8B Startup Label      03E0  03E7   07<br>
+#        9 Channel Bank            2000  38FF 1900<br>
+#             Channel 000          2000  201F   20<br>
+#             Channel 001          2020  202F   20<br>
+#             ...<br>
+#             Channel 199          38E0  38FF   20<br>
+#        10 Blank/Unknown          3900  3FFF  6FF  14592  16383    1792<br>
+#            Total Map Size           16128 (2^8 = 16384)<br>
+#<br>
+#  TH9000/220  memory map<br>
+#  section: 1 and 2:  Channel Set/Skip Flags<br>
+#<br>
+#    Channel Set (starts 0x100) : Channel Set  bit is value 0 if a memory location in the channel bank is active.<br>
+#    Channel Skip (starts 0x120): Channel Skip bit is value 0 if a memory location in the channel bank is active.<br>
+#<br>
+#    Both flag maps are a total 24 bytes in length, aligned on 32 byte records.<br>
+#    bit = 0 channel set/no skip,  1 is channel not set/skip<br>
+#<br>
+#    to index a channel:<br>
+#        cbyte = channel / 8 ;<br>
+#        cbit  = channel % 8 ;<br>
+#        setflag  = csetflag[cbyte].c[cbit] ;<br>
+#        skipflag = cskipflag[cbyte].c[cbit] ;<br>
+#<br>
+#    channel range is 0-199, range is 32 bytes (last 7 unknown)<br>
+#<br>
+MEM_FORMAT = MEM_FORMAT + &quot;&quot;&quot;<br>
+#seekto 0x0100;<br>
+struct {<br>
+   bit c[8];<br>
+} csetflag[32];<br>
+<br>
+struct {<br>
+   u8 unknown0100[7];<br>
+} ropt0100;<br>
+<br>
+#seekto 0x0120;<br>
+struct {<br>
+   bit c[8];<br>
+} cskipflag[32];<br>
+<br>
+struct {<br>
+   u8 unknown0120[7];<br>
+} ropt0120;<br>
+&quot;&quot;&quot;<br>
+#  TH9000  memory map<br>
+#  section: 5  TX/RX Range<br>
+#     used to set the TX/RX range of the radio (e.g.  222-228Mhz for 220 meter)<br>
+#     possible to set range for tx/rx<br>
+#<br>
+MEM_FORMAT = MEM_FORMAT + &quot;&quot;&quot;<br>
+#seekto 0x0200;<br>
+struct {<br>
+    bbcd txrangelow[4];<br>
+    bbcd txrangehi[4];<br>
+    bbcd rxrangelow[4];<br>
+    bbcd rxrangehi[4];<br>
+} freqrange;<br>
+&quot;&quot;&quot;<br>
+# TH9000  memory map<br>
+# section: 6  bootup_passwd<br>
+#    used to set bootup passwd (see boot_passwd checkbox option)<br>
+#<br>
+#  options - bootup password<br>
+#<br>
+#  bytes:bit   type                 description<br>
+#  ---------------------------------------------------------------------------<br>
+#  6         u8 bootup_passwd[6]     bootup passwd, 6 chars, numberic chars 30-39 , see boot_passwd checkbox to set<br>
+#  10        u8 unknown;<br>
+#<br>
+<br>
+MEM_FORMAT = MEM_FORMAT + &quot;&quot;&quot;<br>
+#seekto 0x0210;<br>
+struct {<br>
+   u8 bootup_passwd[6];<br>
+   u8 unknown2010[10];<br>
+} ropt0210;<br>
+&quot;&quot;&quot;<br>
+#  TH9000/220  memory map<br>
+#  section: 7  Radio Options<br>
+#        used to set a number of radio options<br>
+#<br>
+#  bytes:bit   type                 description<br>
+#  ---------------------------------------------------------------------------<br>
+#  1         u8 display_mode     display mode, range 0-2, 0=freq,1=channel,2=name (selecting name affects vfo_mr)<br>
+#  1         u8 vfo_mr;          vfo_mr , 0=vfo, mr=1<br>
+#  1         u8 unknown;<br>
+#  1         u8 squelch;         squelch level, range 0-19, hex for menu<br>
+#  1         u8 unknown[2];<br>
+#  1         u8 channel_lock;    if display_mode[channel] selected, then lock=1,no lock =0<br>
+#  1         u8 unknown;<br>
+#  1         u8 bg_brightness ;  background brightness, range 0-21, hex, menu index<br>
+#  1         u8 unknown;<br>
+#  1         u8 bg_color ;       bg color, menu index,  blue 0 , orange 1, purple 2<br>
+#  1         u8 tbst_freq ;      tbst freq , menu 0 = 1750Hz, 1=2100 , 2=1000 , 3=1450hz<br>
+#  1         u8 timeout_timer;   timeout timer, hex, value = minutes, 0= no timeout<br>
+#  1         u8 unknown;<br>
+#  1         u8 auto_power_off;   auto power off, range 0-3, off,30min, 1hr, 2hr, hex menu index<br>
+#  1         u8 voice_prompt;     voice prompt, value 0,1 , Beep ON = 1, Beep Off = 2<br>
+#<br>
+# description of function setup options, starting at 0x0230<br>
+#<br>
+#  bytes:bit   type                 description<br>
+#  ---------------------------------------------------------------------------<br>
+#  1         u8  // 0<br>
+#   :4       unknown:6<br>
+#   :1       elim_sql_tail:1   eliminate squelsh tail when no ctcss checkbox (1=checked)<br>
+#   :1       sql_key_function  &quot;squelch off&quot; 1 , &quot;squelch momentary off&quot; 0 , menu index<br>
+#  2         u8 unknown[2] /1-2<br>
+#  1         u8 // 3<br>
+#   :4       unknown:4<br>
+#   :1       inhibit_init_ops:1 //bit 5<br>
+#   :1       unknownD:1<br>
+#   :1       inhibit_setup_bg_chk:1 //bit 7<br>
+#   :1       unknown:1<br>
+#  1         u8 tail_elim_type    menu , (off=0,120=1,180=2),  // 4<br>
+#  1         u8 choose_tx_power    menu , (60w=0,25w=1) // 5<br>
+#  2         u8 unknown[2]; // 6-7<br>
+#  1         u8 bootup_passwd_flag  checkbox 1=on, 0=off // 8<br>
+#  7         u8 unknown[7]; // 9-F<br>
+#<br>
+MEM_FORMAT = MEM_FORMAT + &quot;&quot;&quot;<br>
+#seekto 0x0220;<br>
+struct {<br>
+   u8 display_mode;<br>
+   u8 vfo_mr;<br>
+   u8 unknown0220A;<br>
+   u8 squelch;<br>
+   u8 unknown0220B[2];<br>
+   u8 channel_lock;<br>
+   u8 unknown0220C;<br>
+   u8 bg_brightness;<br>
+   u8 unknown0220D;<br>
+   u8 bg_color;<br>
+   u8 tbst_freq;<br>
+   u8 timeout_timer;<br>
+   u8 unknown0220E;<br>
+   u8 auto_power_off;<br>
+   u8 voice_prompt;<br>
+   u8 unknown0230A:6,<br>
+      elim_sql_tail:1,<br>
+      sql_key_function:1;<br>
+   u8 unknown0230B[2];<br>
+   u8 unknown0230C:4,<br>
+      inhibit_init_ops:1,<br>
+      unknown0230D:1,<br>
+      inhibit_setup_bg_chk:1,<br>
+      unknown0230E:1;<br>
+   u8 tail_elim_type;<br>
+   u8 choose_tx_power;<br>
+   u8 unknown0230F[2];<br>
+   u8 bootup_passwd_flag;<br>
+   u8 unknown0230G[7];<br>
+} settings;<br>
+&quot;&quot;&quot;<br>
+#  TH9000  memory map<br>
+#  section: 8B  Startup Label<br>
+#<br>
+#  bytes:bit   type                 description<br>
+#  ---------------------------------------------------------------------------<br>
+#  7     char start_label[7]    label displayed at startup (usually your call sign)<br>
+#<br>
+MEM_FORMAT = MEM_FORMAT + &quot;&quot;&quot;<br>
+#seekto 0x03E0;<br>
+struct {<br>
+    char startname[7];<br>
+} slabel;<br>
+&quot;&quot;&quot;<br>
+#  TH9000/220  memory map<br>
+#  section: 9  Channel Bank<br>
+#         description of channel bank (200 channels , range 0-199)<br>
+#         Each 32 Byte (0x20 hex)  record:<br>
+#  bytes:bit   type                 description<br>
+#  ---------------------------------------------------------------------------<br>
+#  4         bbcd freq[4]        receive frequency in packed binary coded decimal<br>
+#  4         bbcd offset[4]      transmit offset in packed binary coded decimal (note: plus/minus direction set by &#39;duplex&#39; field)<br>
+#  1         u8<br>
+#   :4       unknown:4<br>
+#   :4       tuning_step:4         tuning step, menu index value from 0-9<br>
+#            5,6.25,8.33,10,12.5,15,20,25,30,50<br>
+#  1         u8<br>
+#   :4       unknown:4          not yet decoded, used for DCS coding?<br>
+#   :2       channel_width:2     channel spacing, menu index value from 0-3<br>
+#            25,20,12.5<br>
+#   :1       reverse:1           reverse flag, 0=off, 1=on (reverses tx and rx freqs)<br>
+#   :1       txoff:1             transmitt off flag, 0=transmit , 1=do not transmit<br>
+#  1         u8<br>
+#   :1       talkaround:1        talkaround flag, 0=off, 1=on (bypasses repeater)<br>
+#   :1       compander:1         compander flag, 0=off, 1=on (turns on/off voice compander option)<br>
+#   :2       unknown:2<br>
+#   :2       power:2             tx power setting, value range 0-2, 0=hi,1=med,2=lo<br>
+#   :2       duplex:2            duplex settings, 0=simplex,2= minus(-) offset, 3= plus (+) offset (see offset field)<br>
+#<br>
+#  1         u8<br>
+#   :4       unknown:4<br>
+#   :2       rxtmode:2           rx tone mode, value range 0-2, 0=none, 1=CTCSS, 2=DCS  (ctcss tone in field rxtone)<br>
+#   :2       txtmode:2           tx tone mode, value range 0-2, 0=none, 1=CTCSS, 3=DCS  (ctcss tone in field txtone)<br>
+#  1         u8<br>
+#   :2       unknown:2<br>
+#   :6       txtone:6            tx ctcss tone, menu index<br>
+#  1         u8<br>
+#   :2       unknown:2<br>
+#   :6       rxtone:6            rx ctcss tone, menu index<br>
+#  1         u8 txcode           ?, not used for ctcss<br>
+#  1         u8 rxcode           ?, not used for ctcss<br>
+#  3         u8 unknown[3]<br>
+#  7         char name[7]        7 byte char string for channel name<br>
+#  1         u8<br>
+#   :6       unknown:6,<br>
+#   :2       busychannellockout:2 busy channel lockout option , 0=off, 1=repeater, 2=busy  (lock out tx if channel busy)<br>
+#  4         u8 unknownI[4];<br>
+#  1         u8<br>
+#   :7       unknown:7<br>
+#   :1       scrambler:1         scrambler flag, 0=off, 1=on (turns on tyt scrambler option)<br>
+#<br>
+MEM_FORMAT = MEM_FORMAT + &quot;&quot;&quot;<br>
+#seekto 0x2000;<br>
+struct {<br>
+  bbcd freq[4];<br>
+  bbcd offset[4];<br>
+  u8 unknown2000A:4,<br>
+     tuning_step:4;<br>
+  u8 unknown2000B:4,<br>
+     channel_width:2,<br>
+     reverse:1,<br>
+     txoff:1;<br>
+  u8 talkaround:1,<br>
+     compander:1,<br>
+     unknown2000C:2,<br>
+     power:2,<br>
+     duplex:2;<br>
+  u8 unknown2000D:4,<br>
+     rxtmode:2,<br>
+     txtmode:2;<br>
+  u8 unknown2000E:2,<br>
+     txtone:6;<br>
+  u8 unknown2000F:2,<br>
+     rxtone:6;<br>
+  u8 txcode;<br>
+  u8 rxcode;<br>
+  u8 unknown2000G[3];<br>
+  char name[7];<br>
+  u8 unknown2000H:6,<br>
+     busychannellockout:2;<br>
+  u8 unknown2000I[4];<br>
+  u8 unknown2000J:7,<br>
+     scrambler:1;<br>
+} memory[200] ;<br>
+&quot;&quot;&quot;<br>
+<br>
+def _echo_write(radio, data):<br>
+    try:<br>
+        radio.pipe.write(data)<br>
+        radio.pipe.read(len(data))<br>
+    except Exception, e:<br>
+        LOG.error(&quot;Error writing to radio: %s&quot; % e)<br>
+        raise errors.RadioError(&quot;Unable to write to radio&quot;)<br>
+<br>
+<br>
+def _checksum(data):<br>
+    cs = 0<br>
+    for byte in data:<br>
+        cs += ord(byte)<br>
+    return cs % 256<br>
+<br>
+def _read(radio, length):<br>
+    try:<br>
+        data = radio.pipe.read(length)<br>
+    except Exception, e:<br>
+        LOG.error( &quot;Error reading from radio: %s&quot; % e)<br>
+        raise errors.RadioError(&quot;Unable to read from radio&quot;)<br>
+<br>
+    if len(data) != length:<br>
+        LOG.error( &quot;Short read from radio (%i, expected %i)&quot; % (len(data),<br>
+                                                           length))<br>
+        LOG.debug(util.hexprint(data))<br>
+        raise errors.RadioError(&quot;Short read from radio&quot;)<br>
+    return data<br>
+<br>
+<br>
+<br>
+def _ident(radio):<br>
+    radio.pipe.setTimeout(1)<br>
+    _echo_write(radio,&quot;PROGRAM&quot;)<br>
+    response = radio.pipe.read(3)<br>
+    if response != &quot;QX\06&quot;:<br>
+        LOG.debug( &quot;Response was :\n%s&quot; % util.hexprint(response))<br>
+        raise errors.RadioError(&quot;Unsupported model&quot;)<br>
+    _echo_write(radio, &quot;\x02&quot;)<br>
+    response = radio.pipe.read(16)<br>
+    LOG.debug(util.hexprint(response))<br>
+    if response[1:8] != &quot;TH-9000&quot;:<br>
+        LOG.error( &quot;Looking  for:\n%s&quot; % util.hexprint(&quot;TH-9000&quot;))<br>
+        LOG.error( &quot;Response was:\n%s&quot; % util.hexprint(response))<br>
+        raise errors.RadioError(&quot;Unsupported model&quot;)<br>
+<br>
+def _send(radio, cmd, addr, length, data=None):<br>
+    frame = struct.pack(&quot;&gt;cHb&quot;, cmd, addr, length)<br>
+    if data:<br>
+        frame += data<br>
+        frame += chr(_checksum(frame[1:]))<br>
+        frame += &quot;\x06&quot;<br>
+    _echo_write(radio, frame)<br>
+    LOG.debug(&quot;Sent:\n%s&quot; % util.hexprint(frame))<br>
+    if data:<br>
+        result = radio.pipe.read(1)<br>
+        if result != &quot;\x06&quot;:<br>
+            LOG.debug( &quot;Ack was: %s&quot; % repr(result))<br>
+            raise errors.RadioError(&quot;Radio did not accept block at %04x&quot; % addr)<br>
+        return<br>
+    result = _read(radio, length + 6)<br>
+    LOG.debug(&quot;Got:\n%s&quot; % util.hexprint(result))<br>
+    header = result[0:4]<br>
+    data = result[4:-2]<br>
+    ack = result[-1]<br>
+    if ack != &quot;\x06&quot;:<br>
+        LOG.debug(&quot;Ack was: %s&quot; % repr(ack))<br>
+        raise errors.RadioError(&quot;Radio NAK&#39;d block at %04x&quot; % addr)<br>
+    _cmd, _addr, _length = struct.unpack(&quot;&gt;cHb&quot;, header)<br>
+    if _addr != addr or _length != _length:<br>
+        LOG.debug( &quot;Expected/Received:&quot;)<br>
+        LOG.debug(&quot; Length: %02x/%02x&quot; % (length, _length))<br>
+        LOG.debug( &quot; Addr: %04x/%04x&quot; % (addr, _addr))<br>
+        raise errors.RadioError(&quot;Radio send unexpected block&quot;)<br>
+    cs = _checksum(result[1:-2])<br>
+    if cs != ord(result[-2]):<br>
+        LOG.debug( &quot;Calculated: %02x&quot; % cs)<br>
+        LOG.debug( &quot;Actual:     %02x&quot; % ord(result[-2]))<br>
+        raise errors.RadioError(&quot;Block at 0x%04x failed checksum&quot; % addr)<br>
+    return data<br>
+<br>
+<br>
+def _finish(radio):<br>
+    endframe = &quot;\x45\x4E\x44&quot;<br>
+    _echo_write(radio, endframe)<br>
+    result = radio.pipe.read(1)<br>
+    if result != &quot;\x06&quot;:<br>
+        LOG.error( &quot;Got:\n%s&quot; % util.hexprint(result))<br>
+        raise errors.RadioError(&quot;Radio did not finish cleanly&quot;)<br>
+<br>
+def do_download(radio):<br>
+<br>
+    _ident(radio)<br>
+<br>
+    _memobj = None<br>
+    data = &quot;&quot;<br>
+<br>
+    for start,end in radio._ranges:<br>
+        for addr in range(start,end,0x10):<br>
+            block = _send(radio,&#39;R&#39;,addr,0x10)<br>
+            data += block<br>
+            status = chirp_common.Status()<br>
+            status.cur = len(data)<br>
+            status.max = end<br>
+            status.msg = &quot;Downloading from radio&quot;<br>
+            radio.status_fn(status)<br>
+<br>
+    _finish(radio)<br>
+<br>
+    return memmap.MemoryMap(data)<br>
+<br>
+def do_upload(radio):<br>
+<br>
+    _ident(radio)<br>
+<br>
+    for start,end in radio._ranges:<br>
+        for addr in range(start,end,0x10):<br>
+            if addr &lt; 0x0100:<br>
+                continue<br>
+            block = radio._mmap[addr:addr+0x10]<br>
+            _send(radio,&#39;W&#39;,addr,len(block),block)<br>
+            status = chirp_common.Status()<br>
+            status.cur = addr<br>
+            status.max = end<br>
+            status.msg = &quot;Uploading to Radio&quot;<br>
+            radio.status_fn(status)<br>
+<br>
+    _finish(radio)<br>
+<br>
+<br>
+<br>
+#<br>
+# The base class, extended for use with other models<br>
+#<br>
+@directory.register<br>
+class Th9000Radio(chirp_common.CloneModeRadio,<br>
+                  chirp_common.ExperimentalRadio):<br>
+    &quot;&quot;&quot;TYT TH-9000&quot;&quot;&quot;<br>
+    VENDOR = &quot;TYT&quot;<br>
+    MODEL = &quot;TH9000 Base&quot;<br>
+    BAUD_RATE = 9600<br>
+    valid_freq = [(900000000, 999000000)]<br>
+<br>
+<br>
+    _memsize = MMAPSIZE<br>
+    _ranges = [(0x0000,0x4000)]<br>
+<br>
+    @classmethod<br>
+    def get_prompts(cls):<br>
+        rp = chirp_common.RadioPrompts()<br>
+        rp.experimental = (&quot;The TYT TH-9000 driver is an beta version.&quot;<br>
+                           &quot;Proceed with Caution and backup your data&quot;)<br>
+        return rp<br>
+<br>
+    def get_features(self):<br>
+        rf = chirp_common.RadioFeatures()<br>
+        rf.has_settings = True<br>
+        rf.has_bank = False<br>
+        rf.has_cross = True<br>
+        rf.has_tuning_step = False<br>
+        rf.has_rx_dtcs = True<br>
+        rf.valid_skips = [&quot;&quot;,&quot;S&quot;]<br>
+        rf.memory_bounds = (0, 199)<br>
+        rf.valid_name_length = 7<br>
+        rf.valid_characters = chirp_common.CHARSET_UPPER_NUMERIC + &quot;-&quot;<br>
+        rf.valid_modes = MODES<br>
+        rf.valid_tmodes = chirp_common.TONE_MODES<br>
+        rf.valid_cross_modes = CROSS_MODES<br>
+        rf.valid_power_levels = POWER_LEVELS<br>
+        rf.valid_dtcs_codes = chirp_common.ALL_DTCS_CODES<br>
+        rf.valid_bands = self.valid_freq<br>
+        return rf<br>
+<br>
+    # Do a download of the radio from the serial port<br>
+    def sync_in(self):<br>
+        self._mmap = do_download(self)<br>
+        self.process_mmap()<br>
+<br>
+    # Do an upload of the radio to the serial port<br>
+    def sync_out(self):<br>
+        do_upload(self)<br>
+<br>
+    def process_mmap(self):<br>
+        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)<br>
+<br>
+<br>
+    # Return a raw representation of the memory object, which<br>
+    # is very helpful for development<br>
+    def get_raw_memory(self, number):<br>
+        return repr(self._memobj.memory[number])<br>
+<br>
+    # not working yet<br>
+    def _get_dcs_index(self, _mem,which):<br>
+        base = getattr(_mem, &#39;%scode&#39; % which)<br>
+        extra = getattr(_mem, &#39;%sdcsextra&#39; % which)<br>
+        return (int(extra) &lt;&lt; 8) | int(base)<br>
+<br>
+    def _set_dcs_index(self, _mem, which, index):<br>
+        base = getattr(_mem, &#39;%scode&#39; % which)<br>
+        extra = getattr(_mem, &#39;%sdcsextra&#39; % which)<br>
+        base.set_value(index &amp; 0xFF)<br>
+        extra.set_value(index &gt;&gt; 8)<br>
+<br>
+<br>
+    # Extract a high-level memory object from the low-level memory map<br>
+    # This is called to populate a memory in the UI<br>
+    def get_memory(self, number):<br>
+        # Get a low-level memory object mapped to the image<br>
+        _mem = self._memobj.memory[number]<br>
+<br>
+        # get flag info<br>
+        cbyte = number / 8 ;<br>
+        cbit =  7 - (number % 8) ;<br>
+        setflag = self._memobj.csetflag[cbyte].c[cbit];<br>
+        skipflag = self._memobj.cskipflag[cbyte].c[cbit];<br>
+<br>
+        mem = chirp_common.Memory()<br>
+<br>
+        mem.number = number  # Set the memory number<br>
+<br>
+        if setflag == 1:<br>
+            mem.empty = True<br>
+            return mem<br>
+<br>
+        mem.freq = int(_mem.freq) * 100<br>
+        mem.offset = int(_mem.offset) * 100<br>
+        <a href="http://mem.name" target="_blank">mem.name</a> = str(_<a href="http://mem.name" target="_blank">mem.name</a>).rstrip() # Set the alpha tag<br>
+        mem.duplex = DUPLEXES[_mem.duplex]<br>
+        mem.mode = MODES[_mem.channel_width]<br>
+        mem.power = POWER_LEVELS[_mem.power]<br>
+<br>
+        rxtone = txtone = None<br>
+<br>
+<br>
+        rxmode = TMODES[_mem.rxtmode]<br>
+        txmode = TMODES[_mem.txtmode]<br>
+<br>
+<br>
+        rxpol = txpol =  &quot;&quot;<br>
+<br>
+        # doesn&#39;t work<br>
+        if rxmode == &quot;Tone&quot;:<br>
+            rxpol = &quot;&quot;<br>
+            rxtone = TONES[_mem.rxtone]<br>
+        elif rxmode == &quot;DTCS&quot;:<br>
+            rxpol = &quot;N&quot;<br>
+            rxtone = chirp_common.ALL_DTCS_CODES[self._get_dcs_index(_mem,&#39;rx&#39;)]<br>
+<br>
+        if txmode == &quot;Tone&quot;:<br>
+            txpol = &quot;&quot;<br>
+            txtone = TONES[_mem.txtone]<br>
+        elif txmode == &quot;DTCS&quot;:<br>
+            txpol = &quot;N&quot;<br>
+            txtone = chirp_common.ALL_DTCS_CODES[self._get_dcs_index(_mem,&#39;tx&#39;)]<br>
+<br>
+<br>
+        chirp_common.split_tone_decode(mem,<br>
+                                       (txmode, txtone, txpol),<br>
+                                       (rxmode, rxtone, rxpol))<br>
+<br>
+        mem.skip = &quot;S&quot; if skipflag == 1 else &quot;&quot;<br>
+<br>
+<br>
+        # We&#39;ll consider any blank (i.e. 0MHz frequency) to be empty<br>
+        if mem.freq == 0:<br>
+            mem.empty = True<br>
+<br>
+        return mem<br>
+<br>
+    # Store details about a high-level memory to the memory map<br>
+    # This is called when a user edits a memory in the UI<br>
+    def set_memory(self, mem):<br>
+        # Get a low-level memory object mapped to the image<br>
+<br>
+        _mem = self._memobj.memory[mem.number]<br>
+<br>
+        cbyte = mem.number / 8<br>
+        cbit =  7 - (mem.number % 8)<br>
+<br>
+        if mem.empty:<br>
+            self._memobj.csetflag[cbyte].c[cbit] = 1<br>
+            self._memobj.cskipflag[cbyte].c[cbit] = 1<br>
+            return<br>
+<br>
+        self._memobj.csetflag[cbyte].c[cbit] =  0<br>
+        self._memobj.cskipflag[cbyte].c[cbit]  =  1 if (mem.skip == &quot;S&quot;) else 0<br>
+<br>
+        _mem.set_raw(&quot;\x00&quot; * 32)<br>
+<br>
+        _mem.freq = mem.freq / 100         # Convert to low-level frequency<br>
+        _mem.offset = mem.offset / 100         # Convert to low-level frequency<br>
+<br>
+        _<a href="http://mem.name" target="_blank">mem.name</a> = mem.name.ljust(7)[:7]  # Store the alpha tag<br>
+        _mem.duplex = DUPLEXES.index(mem.duplex)<br>
+<br>
+<br>
+        try:<br>
+            _mem.channel_width = MODES.index(mem.mode)<br>
+        except ValueError:<br>
+            _mem.channel_width = 0<br>
+<br>
+        ((txmode, txtone, txpol),<br>
+         (rxmode, rxtone, rxpol)) = chirp_common.split_tone_encode(mem)<br>
+<br>
+        _mem.txtmode = TMODES.index(txmode)<br>
+        _mem.rxtmode = TMODES.index(rxmode)<br>
+<br>
+        if txmode == &quot;Tone&quot;:<br>
+            _mem.txtone = TONES.index(txtone)<br>
+        elif txmode == &quot;DTCS&quot;:<br>
+            self._set_dcs_index(_mem,&#39;tx&#39;,chirp_common.ALL_DTCS_CODES.index(txtone))<br>
+<br>
+        if rxmode == &quot;Tone&quot;:<br>
+            _mem.rxtone = TONES.index(rxtone)<br>
+        elif rxmode == &quot;DTCS&quot;:<br>
+            self._set_dcs_index(_mem, &#39;rx&#39;, chirp_common.ALL_DTCS_CODES.index(rxtone))<br>
+<br>
+        #_mem.txinv = txpol == &quot;N&quot;<br>
+        #_mem.rxinv = rxpol == &quot;N&quot;<br>
+<br>
+<br>
+        if mem.power:<br>
+            _mem.power = POWER_LEVELS.index(mem.power)<br>
+        else:<br>
+            _mem.power = 0<br>
+<br>
+    def _get_settings(self):<br>
+        _settings = self._memobj.settings<br>
+        _freqrange = self._memobj.freqrange<br>
+        _slabel = self._memobj.slabel<br>
+<br>
+        basic = RadioSettingGroup(&quot;basic&quot;,&quot;Global Settings&quot;)<br>
+        freqrange = RadioSettingGroup(&quot;freqrange&quot;,&quot;Frequency Ranges&quot;)<br>
+        top = RadioSettingGroup(&quot;top&quot;,&quot;All Settings&quot;,basic,freqrange)<br>
+        settings = RadioSettings(top)<br>
+<br>
+        def _filter(name):<br>
+            filtered = &quot;&quot;<br>
+            for char in str(name):<br>
+                if char in chirp_common.CHARSET_ASCII:<br>
+                    filtered += char<br>
+                else:<br>
+                    filtered += &quot;&quot;<br>
+            return filtered<br>
+<br>
+        val = RadioSettingValueString(0,7,_filter(_slabel.startname))<br>
+        rs = RadioSetting(&quot;startname&quot;,&quot;Startup Label&quot;,val)<br>
+        basic.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;bg_color&quot;,&quot;LCD Color&quot;,<br>
+                           RadioSettingValueList(BGCOLOR_LIST, BGCOLOR_LIST[_settings.bg_color]))<br>
+        basic.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;bg_brightness&quot;,&quot;LCD Brightness&quot;,<br>
+                           RadioSettingValueList(BGBRIGHT_LIST, BGBRIGHT_LIST[_settings.bg_brightness]))<br>
+        basic.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;squelch&quot;,&quot;Squelch Level&quot;,<br>
+                           RadioSettingValueList(SQUELCH_LIST, SQUELCH_LIST[_settings.squelch]))<br>
+        basic.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;timeout_timer&quot;,&quot;Timeout Timer (TOT)&quot;,<br>
+                           RadioSettingValueList(TIMEOUT_LIST, TIMEOUT_LIST[_settings.timeout_timer]))<br>
+        basic.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;auto_power_off&quot;,&quot;Auto Power Off (APO)&quot;,<br>
+                           RadioSettingValueList(APO_LIST, APO_LIST[_settings.auto_power_off]))<br>
+        basic.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;voice_prompt&quot;,&quot;Beep Prompt&quot;,<br>
+                           RadioSettingValueList(BEEP_LIST, BEEP_LIST[_settings.voice_prompt]))<br>
+        basic.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;tbst_freq&quot;,&quot;Tone Burst Frequency&quot;,<br>
+                           RadioSettingValueList(TBSTFREQ_LIST, TBSTFREQ_LIST[_settings.tbst_freq]))<br>
+        basic.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;choose_tx_power&quot;,&quot;Max Level of TX Power&quot;,<br>
+                           RadioSettingValueList(TXPWR_LIST, TXPWR_LIST[_settings.choose_tx_power]))<br>
+        basic.append(rs)<br>
+<br>
+        (flow,fhigh)  = self.valid_freq[0]<br>
+        flow  /= 1000<br>
+        fhigh /= 1000<br>
+        fmidrange = (fhigh- flow)/2<br>
+<br>
+        rs = RadioSetting(&quot;txrangelow&quot;,&quot;TX Freq, Lower Limit (khz)&quot;, RadioSettingValueInteger(flow,<br>
+            flow + fmidrange,<br>
+            int(_freqrange.txrangelow)/10))<br>
+        freqrange.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;txrangehi&quot;,&quot;TX Freq, Upper Limit (khz)&quot;, RadioSettingValueInteger(fhigh-fmidrange,<br>
+            fhigh,<br>
+            int(_freqrange.txrangehi)/10))<br>
+        freqrange.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;rxrangelow&quot;,&quot;RX Freq, Lower Limit (khz)&quot;, RadioSettingValueInteger(flow,<br>
+            flow+fmidrange,<br>
+            int(_freqrange.rxrangelow)/10))<br>
+        freqrange.append(rs)<br>
+<br>
+        rs = RadioSetting(&quot;rxrangehi&quot;,&quot;RX Freq, Upper Limit (khz)&quot;, RadioSettingValueInteger(fhigh-fmidrange,<br>
+            fhigh,<br>
+            int(_freqrange.rxrangehi)/10))<br>
+        freqrange.append(rs)<br>
+<br>
+        return settings<br>
+<br>
+    def get_settings(self):<br>
+        try:<br>
+            return self._get_settings()<br>
+        except:<br>
+            import traceback<br>
+            LOG.error( &quot;failed to parse settings&quot;)<br>
+            traceback.print_exc()<br>
+            return None<br>
+<br>
+    def set_settings(self,settings):<br>
+        _settings = self._memobj.settings<br>
+        for element in settings:<br>
+            if not isinstance(element,RadioSetting):<br>
+                self.set_settings(element)<br>
+                continue<br>
+            else:<br>
+                try:<br>
+                    name = element.get_name()<br>
+<br>
+                    if  name in [&quot;txrangelow&quot;,&quot;txrangehi&quot;,&quot;rxrangelow&quot;,&quot;rxrangehi&quot;]:<br>
+                        LOG.debug( &quot;setting %s = %s&quot; % (name,int(element.value)*10))<br>
+                        setattr(self._memobj.freqrange,name,int(element.value)*10)<br>
+                        continue<br>
+<br>
+                    if name in [&quot;startname&quot;]:<br>
+                        LOG.debug( &quot;setting %s = %s&quot; % (name, element.value))<br>
+                        setattr(self._memobj.slabel,name,element.value)<br>
+                        continue<br>
+<br>
+                    obj = _settings<br>
+                    setting = element.get_name()<br>
+<br>
+                    if element.has_apply_callback():<br>
+                        LOG.debug( &quot;using apply callback&quot;)<br>
+                        element.run_apply_callback()<br>
+                    else:<br>
+                        LOG.debug( &quot;Setting %s = %s&quot; % (setting, element.value))<br>
+                        setattr(obj, setting, element.value)<br>
+                except Exception, e:<br>
+                    LOG.debug( element.get_name())<br>
+                    raise<br>
+<br>
+    @classmethod<br>
+    def match_model(cls, filedata, filename):<br>
+        if  MMAPSIZE == len(filedata):<br>
+           (flow,fhigh)  = cls.valid_freq[0]<br>
+           flow  /= 1000000<br>
+           fhigh /= 1000000<br>
+<br>
+           txmin=ord(filedata[0x200])*100 + (ord(filedata[0x201])&gt;&gt;4)*10 + ord(filedata[0x201])%16<br>
+           txmax=ord(filedata[0x204])*100 + (ord(filedata[0x205])&gt;&gt;4)*10 + ord(filedata[0x205])%16<br>
+           rxmin=ord(filedata[0x208])*100 + (ord(filedata[0x209])&gt;&gt;4)*10 + ord(filedata[0x209])%16<br>
+           rxmax=ord(filedata[0x20C])*100 + (ord(filedata[0x20D])&gt;&gt;4)*10 + ord(filedata[0x20D])%16<br>
+<br>
+           if ( rxmin &gt;= flow and rxmax &lt;= fhigh and txmin &gt;= flow and txmax &lt;= fhigh ):<br>
+                return True<br>
+<br>
+        return False<br>
+<br>
+@directory.register<br>
+class Th9000220Radio(Th9000Radio):<br>
+    &quot;&quot;&quot;TYT TH-9000 220&quot;&quot;&quot;<br>
+    VENDOR = &quot;TYT&quot;<br>
+    MODEL = &quot;TH9000_220&quot;<br>
+    BAUD_RATE = 9600<br>
+    valid_freq = [(220000000, 260000000)]<br>
+<br>
+@directory.register<br>
+class Th9000144Radio(Th9000220Radio):<br>
+    &quot;&quot;&quot;TYT TH-9000 144&quot;&quot;&quot;<br>
+    VENDOR = &quot;TYT&quot;<br>
+    MODEL = &quot;TH9000_144&quot;<br>
+    BAUD_RATE = 9600<br>
+    valid_freq = [(136000000, 174000000)]<br>
+<br>
+@directory.register<br>
+class Th9000440Radio(Th9000220Radio):<br>
+    &quot;&quot;&quot;TYT TH-9000 440&quot;&quot;&quot;<br>
+    VENDOR = &quot;TYT&quot;<br>
+    MODEL = &quot;TH9000_440&quot;<br>
+    BAUD_RATE = 9600<br>
+    valid_freq = [(400000000, 490000000)]<br>
</blockquote></div><br></div>