예제 #1
0
    def writeToReadFrom(self, write, read, numBytes, data):

        dataPacket = ""
        writeOnlyFlag = "0"

        if write:
            if numBytes > 0:
                for i in range(numBytes):
                    dataPacket += "{:02x}".format(data[i])
            else:
                dataPacket = "0"
        else:
            # read only, keep writing the same value
            for i in range(numBytes):
                dataPacket += "{:02x}".format(data)

        if not read:
            writeOnlyFlag = "1"

        self.usb.sendCommand(
            "SPI" + str(self.spiIndex) + " WHR " + writeOnlyFlag + " " + str(numBytes) + " " + dataPacket
        )
        result = self.usb.readResponse()

        if not read:
            if not result.startswith("-OK"):
                raise DeviceError(f'Error Binho responded with {result}, not the expected "-OK"')

            return bytearray()

        if not result.startswith("-SPI0 RXD "):
            raise DeviceError(f'Error Binho responded with {result}, not the expected "-SPI0 RXD ..."')

        return bytearray.fromhex(result[9:])
예제 #2
0
    def exchangeBytes(self,
                      oneWireCmd,
                      bytesToWrite=None,
                      bytesToRead=0,
                      oneWireIndex=0):
        """
        Issues one of the following commands to the one wire bus:
            None (this is the default)
            Reset then Select ROM (requires a one wire address in the address buffer)
            Reset then Skip ROM
        Then writes bytesToWrite (max 1024) out to the bus
        Then reads bytesToRead (max 1024) from the bus
        :param oneWireCmd: 1-Wire command to send out on the bus
        :type oneWireCmd: oneWireCmd
        :param bytesToWrite: bytearray of bytes to write out to the 1-Wire bus after the 1-Wire Cmd
        :type bytesToWrite: bytearray
        :param bytesToRead: Number of bytes to read after writeing bytes
        :type bytesToRead: int
        :param oneWireIndex: The 1-Wire index, 0 on the binho nova
        :type oneWireIndex: int
        :return: A bytearray of bytes that were read out
        :rtype: bytearray
        """

        if len(bytesToWrite) > 1024:
            raise CapabilityError(
                "WHR command can only write 1024 bytes at a time!")

        if bytesToRead > 1024:
            raise CapabilityError(
                "WHR command can only read 1024 bytes at a time!")

        self.usb.sendCommand(f"1WIRE{oneWireIndex} "
                             f"WHR {oneWireCmd} "
                             f"{bytesToRead} "
                             f"{len(bytesToWrite)} "
                             f'{"".join(f"{b:02x}" for b in bytesToWrite)}')

        result = self.usb.readResponse()

        if bytesToRead == 0:
            if not result.startswith("-OK"):
                raise DeviceError(
                    f'Error Binho responded with {result}, not the expected "-OK"'
                )

            return bytearray()

        if not result.startswith("-1WIRE0 RXD "):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-1WIRE0 RXD ..."'
            )

        return bytearray.fromhex(result[12:])
예제 #3
0
    def value(self, value: Any) -> None:
        command = f"IO{self.io_number} VALUE {value}"
        self.usb.sendCommand(command)
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(f'Binho responded to command "{command}" with {result}, not the expected "-OK".')
예제 #4
0
    def checkDeviceSuccess(cls, ret_str):
        if ret_str == "-OK":
            return True
        if ret_str == "-NG":
            return False

        raise DeviceError(f"Invalid command response: {ret_str}")
예제 #5
0
    def pwm_frequency(self, freq: int) -> None:
        command = f"IO{self.io_number} PWMFREQ {freq}"
        self.usb.sendCommand(command)
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(f'Binho responded to command "{command}" with {result}, not the expected "-OK".')
예제 #6
0
    def writeByte(self, data, oneWireIndex=0, powered=True):
        """
        Write a byte over 1-Wire
        :param data: Data byte to write
        :type data: int
        :param oneWireIndex: The 1-Wire index, 0 on the binho nova
        :type oneWireIndex: int
        :param powered: Leave 1-Wire power on after this command finsihes
        :type powered: bool
        :raises DeviceError: if the command is unsuccessful
        :return: None
        :rtype: None
        """

        if not 0 <= data <= 255:
            raise CapabilityError(
                f"Data byte must be in range 0-255, not {data}")

        if powered:
            self.usb.sendCommand(f"1WIRE{oneWireIndex} WRITE {data} POWER")
        else:
            self.usb.sendCommand(f"1WIRE{oneWireIndex} WRITE {data}")

        if not self.usb.checkDeviceSuccess(self.usb.readResponse()):
            raise DeviceError("Error executing 1-Wire Write received NAK")
예제 #7
0
    def interrupt_source(self, int_mode: str) -> None:
        command = f"IO{self.io_number} INT {int_mode}"
        self.usb.sendCommand(command)
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(f'Binho responded to command "{command}" with {result}, not the expected "-OK".')
예제 #8
0
    def ping(self):
        self.usb.sendCommand("+PING")
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #9
0
    def setLEDColor(self, color):
        self.usb.sendCommand("+LED " + color)
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #10
0
    def toggle(self, duration):
        command = f"IO{self.io_number} TOGGLE {duration}"
        self.usb.sendCommand(command)
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(
                f'Binho responded to command "{command}" with {result}, not the expected "-OK".'
            )
예제 #11
0
    def operationMode(self, mode):
        self.usb.sendCommand("+MODE " + str(self.coreIndex) + " " + mode)
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #12
0
    def numericalBase(self, base):
        self.usb.sendCommand("+BASE " + str(base))
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #13
0
    def bitOrder(self, order):

        self.usb.sendCommand("SPI" + str(self.spiIndex) + " ORDER " + order)
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #14
0
    def mode(self, mode):

        self.usb.sendCommand("SPI" + str(self.spiIndex) + " MODE " + str(mode))
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #15
0
    def addressBits(self):

        self.usb.sendCommand("I2C" + str(self.i2cIndex) + " ADDR ?")
        result = self.usb.readResponse()

        if not result.startswith("-I2C" + str(self.i2cIndex) + " ADDR"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-I2C'
                + str(self.i2cIndex) + ' CLK"')

        if "8BIT" in result:
            return 8
        if "7BIT" in result:
            return 7

        raise DeviceError(
            f'Error Binho responded with {result}, not the expected "-I2C' +
            str(self.i2cIndex) + ' CLK"')
예제 #16
0
    def bitsPerTransfer(self, bits):

        self.usb.sendCommand("SPI" + str(self.spiIndex) + " TXBITS " + str(bits))
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #17
0
    def clockFrequency(self, clock):

        self.usb.sendCommand("SPI" + str(self.spiIndex) + " CLK " + str(clock))
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #18
0
    def start(self, address):

        self.usb.sendCommand("I2C" + str(self.i2cIndex) + " START " +
                             str(address))
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-OK"')
        return True
예제 #19
0
    def setLEDRGB(self, red, green, blue):
        self.usb.sendCommand("+LED " + str(red) + " " + str(green) + " " +
                             str(blue))
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #20
0
    def resetToBtldr(self, fail_silent=False):

        self.usb.sendCommand("+BTLDR")
        result = self.usb.readResponse()

        if not result.startswith("-OK") and not fail_silent:
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #21
0
    def deviceID(self):

        self.usb.sendCommand("+ID")
        result = self.usb.readResponse()

        if not result.startswith("-ID"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-ID"')

        return result[4:]
예제 #22
0
    def end(self, suppressError=False):

        self.usb.sendCommand("SPI" + str(self.spiIndex) + " END")
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            if not suppressError:
                raise DeviceError(f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #23
0
    def numericalBase(self):
        self.usb.sendCommand("+BASE ?")
        result = self.usb.readResponse()

        if not result.startswith("-BASE"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-BASE"'
            )

        return result[6:]
예제 #24
0
    def setPeripheralRegisterValueI2C(self, register, value):

        print("I2C" + str(self.i2cIndex) + " SLAVE REG " + str(register) +
              " " + str(hex(value)))
        self.usb.sendCommand("I2C" + str(self.i2cIndex) + " SLAVE REG " +
                             str(register) + " " + str(hex(value)))
        result = self.usb.readResponse()

        if not result.startswith("-OK"):
            if register in ('PTR', 'ptr'):
                raise DeviceError(
                    f'Error Binho responded with {result}, not the expected "-OK". This could be caused'
                    +
                    ' by setting the pointer register to an index beyond the number of configured '
                    + 'registers.')
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-OK"')

        return True
예제 #25
0
    def clockFrequency(self):

        self.usb.sendCommand("SPI" + str(self.spiIndex) + " CLK ?")
        result = self.usb.readResponse()

        if not result.startswith("-SPI" + str(self.spiIndex) + " CLK"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-SPI' + str(self.spiIndex) + ' CLK"'
            )

        return int(result[10:])
예제 #26
0
    def bitsPerTransfer(self):

        self.usb.sendCommand("SPI" + str(self.spiIndex) + " TXBITS ?")
        result = self.usb.readResponse()

        if not result.startswith("-SPI" + str(self.spiIndex) + " TXBITS"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-SPI' + str(self.spiIndex) + ' TXBITS"'
            )

        return int(result[13:])
예제 #27
0
    def transfer(self, data):

        self.usb.sendCommand("SPI" + str(self.spiIndex) + " TXRX " + str(data))
        result = self.usb.readResponse()

        if not result.startswith("-SPI" + str(self.spiIndex) + " RXD"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-SPI' + str(self.spiIndex) + ' RXD"'
            )

        return bytearray.fromhex(result[9:])
예제 #28
0
    def mode(self):

        self.usb.sendCommand("SPI" + str(self.spiIndex) + " MODE ?")
        result = self.usb.readResponse()

        if not result.startswith("-SPI" + str(self.spiIndex) + " MODE"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-SPI' + str(self.spiIndex) + ' MODE"'
            )

        return int(result[11:])
예제 #29
0
    def getRegisterBankI2C(self):

        self.usb.sendCommand("I2C" + str(self.i2cIndex) + " SLAVE BANK " + "?")
        result = self.usb.readResponse()

        if not result.startswith("-I2C" + str(self.i2cIndex) + " SLAVE BANK "):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-I2C'
                + str(self.i2cIndex) + ' SLAVE BANK"')

        return bytearray.fromhex(result[17:])
예제 #30
0
    def bitOrder(self):

        self.usb.sendCommand("SPI" + str(self.spiIndex) + " ORDER ?")
        result = self.usb.readResponse()

        if not result.startswith("-SPI" + str(self.spiIndex) + " ORDER"):
            raise DeviceError(
                f'Error Binho responded with {result}, not the expected "-SPI' + str(self.spiIndex) + ' ORDER"'
            )

        return result[12:]