
from . import USBISS

class SPI(USBISS):
    """SPI operating mode of USBISS
    """

    self.mode = None
 

    def __init__(self, port, spi_mode=None, freq=None):

        # Execute baseclass __init__
        super(SPI, self).__init__(port)

        # Select the SPI mode of USB-ISS's SPI operating mode
        try:
            # Emulate spidev.SpiDev.mode through self.mode.
            # self.spi_mode do not corresponds to spidev.SpiDev.mode.
            # self.mode is set with a value from a lookup table.
            # critical because external applications may inspect this value.
            # Serves as a check on value of spi_mode which should be between
            # 0 and 3.
            lookup_table = [0, 2, 1, 3]
            self.mode = lookup_table[spi_mode]

            # Add signal for SPI switch
            spi_mode = 0x90 + spi_mode
        except:
            error = ("The value of spi_mode, %s, is not between 0 and 3" %
                     (spi_mode))
            raise ValueError(error)

        # Select frequency of USB-ISS's SPI operating mode
        try:
            sck_divisor = self.iss_spi_divisor(freq)
        except:
            raise TypeError("Missing argument for frequency for SPI mode")

        # Configure USB-ISS
        self.set_iss_mode([spi_mode, sck_divisor])

    def xfer(self, data):
        """Spidev function for transferring bytes to port
        TODO: enforce rule that up to 63 bytes of data can be sent.
        TODO: enforce rule that there is no gaps in data bytes (what define a gap?)
        """
        self.serial.write(bytearray([0x61] + data))
        response = self.serial.read(1 + len(data))
        if len(response) != 0:
            status = response[0]
            if status == 0:
                raise RuntimeError('USB-ISS: Transmission Error')

            decoded = [struct.unpack('B', response[i + 1: i + 2])[0] for i in range(0, len(data))]
            return decoded
        else:
            raise RuntimeError('USB-ISS: Transmission Error: No bytes received!')

    def iss_spi_divisor(self, sck):
        """Calculate a divisor from input SPI clock speed
        """
        _divisor = (6000000 / sck) - 1
        divisor = int(_divisor)

        if divisor != _divisor:
            raise ValueError('Non-integral SCK divisor.')

        if not 1 <= divisor < 256:
            error = "The value of sck_divisor, %s, is not between 0 and 255" % (divisor)
            raise ValueError(error)
        return divisor

@@ -84,87 +84,207 @@
 %22%22%0A%0A
+%0A
     
-self.mode = None%0A %0A%0A    def __init__(self, port, spi_mode=None, freq=None):
+SPI_MODE = 0x90%0A    SPI_CMD = 0x61%0A%0A%0A    def __init__(self, port, mode=0, max_speed_hz=3000000):%0A%0A        self._mode = mode%0A        self._max_speed_hz = max_speed_hz%0A        self.sck_divisor = 1
 %0A%0A  
@@ -1696,36 +1696,31 @@
 elf.
-serial.write(bytearray(%5B0x61
+iss_write(%5Bself.SPI_CMD
 %5D + 
@@ -1720,25 +1720,24 @@
 CMD%5D + data)
-)
 %0A        res
@@ -1753,15 +1753,12 @@
 elf.
-serial.
+iss_
 read
@@ -1948,121 +1948,36 @@
     
-decoded = %5Bstruct.unpack('B', response%5Bi + 1: i + 2%5D)%5B0%5D for i in range(0, len(data))%5D%0A            return decoded
+return self.decode(response)
 %0A   

Use iss_write/iss_read and decode from baseclass as well as storing command bytes and default SPI configuration in variables.