[chirp_devel] [PATCH 2 of 7] [ic2820] New banks support

Dan Smith
Tue Feb 7 20:08:28 PST 2012


# HG changeset patch
# User Dan Smith <dsmith at danplanet.com>
# Date 1328673680 28800
# Node ID 74c27f2df96137b1b07289a7bcdd31cf424e1dfd
# Parent  fa6b1f430f74f991f945a62dfb59e56d3ac6e4e7
[ic2820] New banks support

diff -r fa6b1f430f74 -r 74c27f2df961 chirp/ic2820.py
--- a/chirp/ic2820.py	Tue Feb 07 19:29:00 2012 -0800
+++ b/chirp/ic2820.py	Tue Feb 07 20:01:20 2012 -0800
@@ -84,6 +84,112 @@
 
 MEM_LOC_SIZE = 48
 
+class IC2820Bank(chirp_common.NamedBank):
+    def set_name(self, name):
+        _banks = self._model._radio._memobj.bank_names
+        _banks[self.index].name = str(name).ljust(8)[:8]
+
+class IC2820BankModel(chirp_common.BankModel,
+                      chirp_common.BankIndexInterface):
+    def get_num_banks(self):
+        return 26 # A-Z
+
+    def get_banks(self):
+        banks = []
+        _banks = self._radio._memobj.bank_names
+
+        for i in range(0, 26):
+            bank = IC2820Bank(self,
+                              chr(ord("A") + i),
+                              str(_banks[i].name).rstrip())
+            bank.index = i
+            banks.append(bank)
+
+        return banks
+
+    def add_memory_to_bank(self, memory, bank):
+        if memory.number >= 500:
+            raise Exception("Special memories cannot be in banks")
+
+        _bank = self._radio._memobj.bank_info[memory.number]
+        if bank:
+            _bank.bank = bank.index
+            _bank.index = self.get_next_bank_index(bank)
+        else:
+            self._remove_memory_from_bank(memory, None)
+
+    def remove_memory_from_bank(self, memory, bank):
+        if memory.number >= 500:
+            raise Exception("Special memories cannot be in banks")
+
+        _bank = self._radio._memobj.bank_info[memory.number]
+        if bank and _bank.bank != bank.index:
+            raise Exception("Memory %i is not in bank %s. Cannot remove" % \
+                                (memory.number, bank))
+
+        _bank.bank = 0xFF
+        _bank.index = 0xFF
+
+    def get_bank_memories(self, bank):
+        memories = []
+        for i in range(0, 500):
+            _bank = self._radio._memobj.bank_info[i]
+            if _bank.bank == bank.index:
+                memories.append(self._radio.get_memory(i))
+        return memories
+
+    def get_memory_banks(self, memory):
+        if memory.number >= 500:
+            raise Exception("Special memories cannoy be in banks")
+
+        _bank = self._radio._memobj.bank_info[memory.number]
+        if _bank.bank == 0xFF:
+            return []
+        else:
+            return [self.get_banks()[_bank.bank]]
+
+    def get_index_bounds(self):
+        return (0, 99)
+
+    def get_memory_index(self, memory, bank):
+        if memory.number >= 500:
+            raise Exception("Special memories cannot be in banks")
+
+        _bank = self._radio._memobj.bank_info[memory.number]
+        if _bank.bank != bank.index:
+            raise Exception("Memory %i is not in bank %s" % (memory.number,
+                                                             bank))
+
+        return _bank.index
+
+    def set_memory_index(self, memory, bank, index):
+        if memory.number >= 500:
+            raise Exception("Special memories cannoy be in banks")
+
+        if index not in range(*self.get_index_bounds()):
+            raise Exception("Invalid index")
+
+        _bank = self._radio._memobj.bank_info[memory.number]
+        if _bank.bank != bank.index:
+            raise Exception("Memory %i is not in bank %s" % (memory.number,
+                                                             bank))
+
+        _bank.index = index
+
+    def get_next_bank_index(self, bank):
+        indexes = []
+        for i in range(0, 500):
+            _bank = self._radio._memobj.bank_info[i]
+            if _bank.bank == bank.index and _bank.index >= 0:
+                indexes.append(_bank.index)
+
+        for i in range(0, 256):
+            if i not in indexes:
+                return i
+
+        raise errors.RadioError("Out of slots in this bank")
+
+
 class IC2820Radio(icf.IcomCloneModeRadio, chirp_common.IcomDstarSupport):
     VENDOR = "Icom"
     MODEL = "IC-2820H"
@@ -107,6 +213,7 @@
     def get_features(self):
         rf = chirp_common.RadioFeatures()
         rf.has_bank_index = True
+        rf.has_bank_names = True
         rf.requires_call_lists = False
         rf.memory_bounds = (0, 499)
         rf.valid_modes = list(MODES)
@@ -119,18 +226,6 @@
         rf.valid_name_length = 8
         return rf
 
-    def get_available_bank_index(self, bank):
-        indexes = []
-        for mem in self._memories.values():
-            if mem.bank == bank and mem.bank_index >= 0:
-                indexes.append(mem.bank_index)
-
-        for i in range(0, 256):
-            if i not in indexes:
-                return i
-
-        raise errors.RadioError("Out of slots in this bank")
-
     def _get_special(self):
         special = {"C0" : 500 + 20,
                    "C1" : 500 + 21}
@@ -149,9 +244,14 @@
     def process_mmap(self):
         self._memobj = bitwise.parse(mem_format, self._mmap)
 
+    def _resolve_memory_number(self, number):
+        if isinstance(number, str):
+            return self._get_special()[number]
+        else:
+            return number
+
     def get_memory(self, number):
-        if isinstance(number, str):
-            number = self._get_special()[number]
+        number = self._resolve_memory_number(number)
 
         bitpos = (1 << (number % 8))
         bytepos = number / 8
@@ -171,13 +271,6 @@
 
         mem.number = number
         if number < 500:
-            _bank = self._memobj.bank_info[number]
-            mem.bank = _bank.bank
-            mem.bank_index = _bank.index
-            if mem.bank == 0xFF:
-                mem.bank = None
-                mem.bank_index = -1
-
             _skip = self._memobj.skip_flags[bytepos]
             _pskip = self._memobj.pskip_flags[bytepos]
             if _skip & bitpos:
@@ -223,14 +316,6 @@
         was_empty = _used & bitpos
 
         if mem.number < 500:
-            _bank = self._memobj.bank_info[mem.number]
-            if mem.bank:
-                _bank.bank = mem.bank
-                _bank.index = mem.bank_index
-            else:
-                _bank.bank = 0xFF
-                _bank.index = 0xFF
-
             skip = self._memobj.skip_flags[bytepos]
             pskip = self._memobj.pskip_flags[bytepos]
             if mem.skip == "S":
@@ -271,19 +356,8 @@
     def get_raw_memory(self, number):
         return repr(self._memobj.memory[number])
     
-    def get_banks(self):
-        _banks = self._memobj.bank_names
-
-        banks = []
-        for i in range(0, 26):
-            banks.append(str(_banks[i].name).rstrip())
-
-        return banks
-
-    def set_banks(self, banks):
-        _banks = self._memobj.bank_names
-        for i in range(0, 26):
-            _banks[i].name = str(banks[i]).ljust(8)[:8]
+    def get_bank_model(self):
+        return IC2820BankModel(self)
 
     def get_urcall_list(self):
         _calls = self._memobj.urcall



More information about the chirp_devel mailing list