Exemplo n.º 1
0
def check_range(from_index, to_index):
    """Check to see if the specified indexes are in range.

    :param int from_index: The starting index.
    :param int to_index: The ending index.
    :raises: ArgumentNullException if either param is None.
    :raises: IllegalArgumentException if either param is not an int.
    :raises: IndexError if either param is less than zero.
    """
    if from_index is None:
        raise ArgumentNullException("from_index cannot be None.")

    if to_index is None:
        raise ArgumentNullException("to_index cannot be None.")

    if not isinstance(from_index, int):
        raise IllegalArgumentException("from_index MUST be an int.")

    if not isinstance(to_index, int):
        raise IllegalArgumentException("to_index MUST be an int.")

    if from_index < 0:
        raise IndexError("from_index cannot be less than zero.")

    if to_index < 0:
        raise IndexError("to_index cannot be less than zero.")

    if from_index > to_index:
        raise IndexError("from_index cannot be greater than to_index.")
Exemplo n.º 2
0
    def write_bytes(self, address, buf):
        """Write a list of bytes to the specified device address.

        Currently, RPi drivers do not allow writing more than 3 bytes at a
        time. As such, if any list of greater than 3 bytes is provided, an
        exception is thrown.

        :param int address: The address of the target device.
        :param list buf: A list of bytes to write to the bus.
        :raises: raspy.object_disposed_exception.ObjectDisposedException if
        this instance has been disposed.

        :raises: raspy.invalid_operation_exception.InvalidOperationException if
        a connection to the I2C bus has not yet been opened.

        :raises: raspy.illegal_argument_exception.IllegalArgumentException if
        the buffer contains more than 3 bytes or if the specified buffer
        parameter is not a list.

        :raises: raspy.io.io_exception.IOException if an error occurs while
        writing the buffer contents to the I2C bus or if only a partial
        write succeeds.
        """
        if self.is_disposed:
            raise ObjectDisposedException("I2CBus")

        if not self.__isOpen:
            raise InvalidOperationException("No open connection to write to.")

        if isinstance(buf, list):
            if len(buf) > 3:
                # TODO we only do this to keep parity with the JS and C#
                # ports. They each have their own underlying native
                # implementations. SMBus2 itself is capable of writing
                # much more than 3 bytes. We should change this as soon
                # as we can get the other ports to support more.
                msg = "Cannot write more than 3 bytes at a time."
                raise IllegalArgumentException(msg)
        else:
            msg = "The specified buf param value is not a list."
            raise IllegalArgumentException(msg)

        try:
            trans = i2c_msg.write(address, buf)
            self.__bus.i2c_rdwr(trans)
        except OSError or IOError:
            msg = "Error writing to address '" + str(address)
            msg += "': I2C transaction failed."
            raise IOException(msg)
Exemplo n.º 3
0
    def get_terminal_config(self, channel=None):
        """Get the terminal configuration for the specified channel.

        :param DeviceControlChannel channel: The channel to get the terminal
        configuration for.
        :raises: raspy.object_disposed_exception.ObjectDisposedException if
        this instance has been disposed.
        :raises: raspy.argument_null_exception.ArgumentNullException if the
        'channel' param is None.
        :raises: raspy.io.io_exception.IOException if an I/O error occurred.
        The specified address is inaccessible or the I2C transaction failed.
        """
        if self.is_disposed:
            return ObjectDisposedException("MCPDeviceController")

        if channel is None:
            raise ArgumentNullException("'channel' param cannot be None.")

        if not isinstance(channel, device_control_channel.DeviceControlChannel):
            msg = "'channel' param must be of type DeviceControlChannel."
            raise IllegalArgumentException(msg)

        # Read the current config.
        tcon = self._read(channel.term_control_address)

        # Build result
        chan_enabled = (tcon & channel.hardware_config_ctrl_bit) > 0
        pin_a_enabled = (tcon & channel.term_a_connection_ctrl_bit) > 0
        pin_w_enabled = (tcon & channel.wiper_connection_ctrl_bit) > 0
        pin_b_enabled = (tcon & channel.term_b_connection_ctrl_bit) > 0
        return DeviceControllerTermConfig(channel, chan_enabled,
                                          pin_a_enabled, pin_w_enabled,
                                          pin_b_enabled)
Exemplo n.º 4
0
    def bs_and(self, b_set):
        """Perform a logical AND of this BitSet with the specified BitSet.

        This BitSet is modified so that each bit in it has the value True if
        (and only if) it both initially had the value True and the
        corresponding bit in the specified BitSet also had the value True.

        :param BitSet b_set: A BitSet. Raises ArgumentNullException if None.
        :raises: ArgumentNullException if bs is None.
        :raises: IllegalArgumentException if bs is not a BitSet.
        """
        if b_set is None:
            raise ArgumentNullException("param 'b_set' cannot be None.")

        if self == b_set:
            return

        if not isinstance(b_set, BitSet):
            raise IllegalArgumentException("param 'b_set' must be a BitSet.")

        while self.__words_in_use > b_set.get_words_in_use():
            self.__words_in_use -= 1
            self.__bits[self.__words_in_use] = 0

        for i in range(0, self.__words_in_use - 1):
            self.__bits[i] &= b_set.get_bits()[i]

        self.recalculate_words_in_use()
        self._check_invariants()
Exemplo n.º 5
0
    def decrease(self, steps=0):
        """Decrease the wiper's value by the specified number of steps.

        It is not an error if the wiper hits or already hit the lower
        boundary (0). In such situations, the wiper sticks to the lower
        boundary or doesn't change.

        :param int steps: The number of steps to decrease by. If not
        specified or zero, then defaults to 1.
        :raises: raspy.io.io_exception.IOException if communication with the
        device failed.
        """
        if self.__currentValue == 0:
            return

        if steps < 0:
            msg = "Only positive integer values are permitted."
            raise IllegalArgumentException(msg)

        if self.non_volatile_mode != microchip_pot_non_volatile_mode.VOLATILE_ONLY:
            msg = "Decrease is only permitted on volatile-only wipers."
            raise InvalidOperationException(msg)

        # Check bounds:
        actual_steps = steps
        if steps > self.__currentValue:
            actual_steps = self.__currentValue

        new_val = self.__currentValue - actual_steps
        if new_val == 0 or steps > 5:
            self.__currentValue = new_val
        else:
            chan = device_control_channel.value_of(self.__channel)
            self.__controller.decrease(chan, actual_steps)
            self.__currentValue = new_val
Exemplo n.º 6
0
    def increase(self, channel=None, steps=0):
        """Increment the volatile wiper for the given number of steps.

        :param DeviceControlChannel channel: The device channel the wiper
        is on.
        :param int steps: The number of steps.
        :raises: raspy.object_disposed_exception.ObjectDisposedException if
        this instance has been disposed.
        :raises: raspy.argument_null_exception.ArgumentNullException if the
        'channel' param is None.
        :raises: raspy.illegal_argument_exception.IllegalArgumentException if
        param 'channel' is not of type DeviceControlChannel.
        :raises: raspy.io.io_exception.IOException if an I/O error occurred.
        The specified address is inaccessible or the I2C transaction failed.
        """
        if self.is_disposed:
            return ObjectDisposedException("MCPDeviceController")

        if channel is None:
            raise ArgumentNullException("'channel' param cannot be None.")

        if not isinstance(channel, device_control_channel.DeviceControlChannel):
            msg = "'channel' param must be of type DeviceControlChannel."
            raise IllegalArgumentException(msg)

        # Increase only works on volatile-wiper.
        if steps is None:
            steps = 0

        mem_addr = channel.volatile_mem_address
        self._increase_or_decrease(mem_addr, True, steps)
Exemplo n.º 7
0
    def __init__(self, mag, deg_per_sec_factor=None):
        """Initialize a new instance of AxisGyroscope.

        :param raspy.components.gyroscopes.mutli_axis_gyro.MultiAxisGyro mag: The
        multi-axis gyro to read from.
        :param float deg_per_sec_factor: The degrees-per-second factor value.
        """
        Gyro.__init__(self)

        if mag is None:
            raise ArgumentNullException("'mag' param cannot be None.")

        if not isinstance(mag, MultiAxisGyro):
            msg = "'mag' param must be an instance of "
            msg += "raspy.components.gyroscopes.multi_axis_gyro.MultiAxisGyro."
            raise IllegalArgumentException(msg)

        self.__multiAxisGyro = mag
        self.__trigger = gyro_trigger_mode.READ_NOT_TRIGGERED
        self.__value = 0.0
        self.__offset = 0.0
        self.__angle = 0.0
        self.__degPerSecondFactor = 0
        self.__factorSet = False

        if deg_per_sec_factor:
            if isinstance(deg_per_sec_factor, float):
                self.__degPerSecondFactor = deg_per_sec_factor
                self.__factorSet = True
Exemplo n.º 8
0
def pad_center(data, char, length):
    """Pad the center of the specified string.

    Adds the specified padding character(s) to the center of the specified
    string.

    :param str data: The string to pad.
    :param str char: The character or string to pad the center of the
    specified string with. If null or empty string, DEFAULT_PAD_CHAR will be
    used instead.
    :param int length: The number of characters or instances of string to pad
    the center with.
    :returns: The padded version of the string.
    :rtype: str
    :raises raspy.illegal_argument_exception.IllegalArgumentException: if data
    param is null or not a string.
    """
    if is_null_or_empty(data):
        raise IllegalArgumentException("data param must be a string.")

    if is_null_or_empty(char):
        char = DEFAULT_PAD_CHAR

    if len(data) > 2 and length > 0:
        first_half = data[:len(data) / 2]
        second_half = data[len(data) / 2:len(data)]
        padding_char = create(char, length)
        string_buf = first_half + padding_char + second_half
        return string_buf

    return data
Exemplo n.º 9
0
    def __init__(self, provider):
        """Initialize a new instance of raspy.lcd.lcd_module.LcdModule class.

        :param raspy.lcd.lcd_transfer_provider.LcdTransferProvider provider:
        The transfer provider to use to send data and commands to the display.
        :raises: raspy.illegal_argument_exception.IllegalArgumentException if
        the provider is None or not an instance of LcdTransferProvider.
        """
        super(Disposable, self).__init__()
        if provider is None or not isinstance(provider, LcdTransferProvider):
            msg = "'provider' param bust be an LcdTransferProvider"
            raise IllegalArgumentException(msg)

        self.__rowOffsets = [0x00, 0x40, 0x14, 0x54]
        self.__showCursor = True
        self.__blinkCursor = True
        self.__visible = True
        self.__backlight = True
        self.__numLines = 0
        self.__numColumns = 0
        self.__sendQueue = list()
        self.__ready = False
        # self.__autoScroll = False  # TODO implement this?
        # self._currLine = 0        # TODO implement this?
        self.__provider = provider
        if self.__provider.is_four_bit_mode:
            self.__displayFunction = (function_set_flags.FOUR_BIT_MODE
                                      | function_set_flags.ONE_LINE
                                      | function_set_flags.FIVE_BY_EIGHT_DOTS)
        else:
            self.__displayFunction = (function_set_flags.EIGHT_BIT_MODE
                                      | function_set_flags.ONE_LINE
                                      | function_set_flags.FIVE_BY_EIGHT_DOTS)
Exemplo n.º 10
0
    def __init__(self, bits):
        """Initialize a new instance of BitSet.

        :param int, list bits: The initial size of the BitSet -or- a list of
        words bits (words) to compose this instance from.
        :raises: IllegalArgumentException if 'bits' param is not a valid type
        (must be an int or a list of bits).
        :raises: IndexError if 'bits' is an int but is less than zero.
        """
        self.__bits = list()
        self.__words_in_use = 0
        self.__size_is_sticky = False
        self.name = "BitSet"

        # Main constructor logic.
        if bits is None:
            self.__bits = [None] * BITS_PER_WORD
        else:
            if isinstance(bits, int):
                if bits < 0:
                    raise IndexError("'bits' param must not be negative.")

                self.__bits = [None] * word_index(bits - 1) + 1
                self.__size_is_sticky = True
            elif isinstance(bits, list):
                self.__bits = bits
                self.__words_in_use = len(self.__bits)
                self._check_invariants()
            else:
                raise IllegalArgumentException(
                    "param 'bits' must be an int or a list")
Exemplo n.º 11
0
    def bs_or(self, b_set):
        """Perform a logical OR of this BitSet with the specified BitSet.

        This BitSet is modified so that a bit in it has the value True if
        (and only if) it either already had the value True or the
        corresponding bit in the specified BitSet has the value True.

        :param BitSet b_set: A BitSet.
        :raises: ArgumentNullException if bs is None.
        :raises: IllegalArgumentException if bs is not a BitSet.
        """
        if b_set is None:
            raise ArgumentNullException("param 'b_set' cannot be None.")

        if self == b_set:
            return

        if not isinstance(b_set, BitSet):
            raise IllegalArgumentException("param 'b_set' must be a BitSet.")

        words_in_common = min(self.__words_in_use, b_set.get_words_in_use())
        if self.__words_in_use < b_set.get_words_in_use():
            self._ensure_capacity(b_set.get_words_in_use())
            self.__words_in_use = b_set.get_words_in_use()

        for i in range(0, words_in_common - 1):
            self.__bits[i] |= b_set.get_bits()[i]

        if words_in_common < b_set.get_words_in_use():
            self.__bits = self.__bits[0:self.__words_in_use - words_in_common]

        self._check_invariants()
Exemplo n.º 12
0
    def set_value(self, channel=None, value=0, non_vol=False):
        """Set the wiper's value in the device.

        :param DeviceControlChannel channel: The device channel the wiper
        is on.
        :param int value: The wiper's value.
        :param bool non_vol: Set True to write to non-volatile memory, or
        False to write to volatile memory.
        :raises: raspy.object_disposed_exception.ObjectDisposedException if
        this instance has been disposed.
        :raises: raspy.argument_null_exception.ArgumentNullException if the
        'channel' param is None.
        :raises: raspy.illegal_argument_exception.IllegalArgumentException if
        param 'channel' is not of type DeviceControlChannel - or - if 'value'
        param is a negative number.
        :raises: raspy.io.io_exception.IOException if an I/O error occurred.
        The specified address is inaccessible or the I2C transaction failed.
        """
        if self.is_disposed:
            return ObjectDisposedException("MCPDeviceController")

        if channel is None:
            raise ArgumentNullException("'channel' param cannot be None.")

        if not isinstance(channel, device_control_channel.DeviceControlChannel):
            msg = "'channel' param must be of type DeviceControlChannel."
            raise IllegalArgumentException(msg)

        if value < 0:
            msg = "Only positive integer values are permitted. Got value: '"
            msg += str(value) + "' for writing to channel '"
            msg += channel.name
            raise IllegalArgumentException(msg)

        # Choose proper mem address.
        if non_vol is None:
            non_vol = False

        mem_addr = channel.volatile_mem_address
        if non_vol:
            mem_addr = channel.non_volatile_mem_address

        # Write value to device.
        self._write(mem_addr, value)
Exemplo n.º 13
0
    def output_file(self, out_file):
        """Set the output file the image will be captured to.

        :param FileInfo out_file: The output file.
        :raises: IllegalArgumentException if out_file is not of type FileInfo.
        """
        if type(out_file) != FileInfo:
            msg = "out_file must be of type raspy.io.file_info.FileInfo"
            raise IllegalArgumentException(msg)

        self.__outputFile = out_file
Exemplo n.º 14
0
    def image_quality(self, quality):
        """Set the image quality.

        :param int quality: The image quality level.
        :raises: IllegalArgumentException if the quality level is not a value
        between QUALITY_MIN and QUALITY_MAX.
        """
        if quality < QUALITY_MIN or quality > QUALITY_MAX:
            msg = "Quality value out of range."
            raise IllegalArgumentException(msg)

        self.__quality = quality
Exemplo n.º 15
0
    def image_size(self, img_size):
        """Set the size of the image.

        :param raspy.size.Size img_size: The image size.
        :raises: IllegalArgumentException if img_size is not of type
        `raspy.size.Size`.
        """
        if type(img_size) != size.Size:
            msg = "img_size property must be of type raspy.size.Size"
            raise IllegalArgumentException(msg)

        self.__size = img_size
Exemplo n.º 16
0
    def set_bit_raw_value(self, index, bit):
        """Set the raw bit value at the specified index.

        Avoid using this method whenever possible. Instead, use either set()
        or set_from_to() so as to preserve invariants.

        :param int index: The index at which to set the specified bit.
        :param int, bool bit: Set True or 1 to set the bit, or False or 0
        to clear the bit.
        :raises: IllegalArgumentException if index is not an int -or-
        bit parameter is not an int or a bool.
        :raises: IndexError if index parameter is less than zero.
        """
        if not isinstance(index, int):
            raise IllegalArgumentException("index must be a valid integer.")

        if index < 0 or index > len(self.__bits) - 1:
            msg = "index must be greater than zero and less than or "
            msg += "equal to the last index in the bit set."
            raise IndexError(msg)

        if isinstance(bit, int):
            if bit < 0:
                bit = 0

            if bit > 1:
                bit = 1
        elif isinstance(bit, bool):
            if bit:
                bit = 1
            else:
                bit = 0
        else:
            msg = "bit must be a number (0 or 1) or bool."
            raise IllegalArgumentException(msg)

        self.__bits[index] = bit
Exemplo n.º 17
0
    def __init__(self,
                 device=None,
                 pin_a0=False,
                 pin_a1=False,
                 pin_a2=False,
                 channel=microchip_pot_channel.NONE,
                 non_vol_mode=microchip_pot_non_volatile_mode.
                 VOLATILE_AND_NON_VOLATILE,
                 init_non_vol_wiper_val=0):
        """Initialize a new instance of MicrochipPotentiometer.

        :param raspy.io.i2c.i2c_interface.I2CInterface device: The I2C bus
        device this instance is connected to.
        :param bool pin_a0: Set True if device's address pin A0 is high.
        :param bool pin_a1: Set True if device's address pin A1 is high.
        :param bool pin_a2: Set True if device's address pin A2 is high.
        :param int channel: The potentiometer channel.
        :param int non_vol_mode: The non-volatility mode.
        :param int init_non_vol_wiper_val: The initial value to set.
        :raises: raspy.argument_null_exception.ArgumentNullException if
        'device' param is None.
        :raises: raspy.illegal_argument_exception.IllegalArgumentException if
        the specified channel is not supported by the device.
        :raises: raspy.io.io_exception.IOException if unable to open the
        I2C bus.
        """
        Potentiometer.__init__(self)
        if device is None:
            msg = "Param 'device' cannot be None."
            raise ArgumentNullException(msg)

        if not self.is_channel_supported(channel):
            msg = "Specified channel not supported by device."
            raise IllegalArgumentException(msg)

        self.__emitter = EventEmitter()
        self.__channel = channel
        self.__currentValue = 0
        self.__nonVolMode = non_vol_mode
        device_addr = MicrochipPotentiometer._build_i2c_address(
            pin_a0, pin_a1, pin_a2)
        self.__controller = mcp_device_controller.MCPDeviceController(
            device, device_addr)
        self.__emitter.on(
            EVENT_WIPER_ACTION,
            lambda wiper_evt: self._on_wiper_action_event(wiper_evt))
        self._initialize(init_non_vol_wiper_val)
Exemplo n.º 18
0
    def set(self, index):
        """Set the bit at the specified index True.

        :param int index: The index at which to set the bit.
        :raises: IllegalArgumentException if index is not an int.
        :raises: IndexError if index is less than zero.
        """
        if not isinstance(index, int):
            raise IllegalArgumentException("index must be a valid integer.")

        if index < 0:
            raise IndexError("index cannot be less than zero.")

        offset = word_index(index)
        self._expand_to(offset)
        self.__bits[offset] |= (1 << index)  # Restore invariants.
        self._check_invariants()
Exemplo n.º 19
0
    def set_terminal_config(self, config=None):
        """Set the device's terminal configuration.

        :param DeviceControllerTermConfig config: The configuration to set.
        :raises: raspy.object_disposed_exception.ObjectDisposedException if
        this instance has been disposed.
        :raises: raspy.argument_null_exception.ArgumentNullException if the
        'config' param is None.
        :raises: raspy.io.io_exception.IOException if an I/O error occurred.
        The specified address is inaccessible or the I2C transaction failed.
        """
        if self.is_disposed:
            return ObjectDisposedException("MCPDeviceController")

        if config is None:
            raise ArgumentNullException("'config' param cannot be None.")

        if not isinstance(config, DeviceControllerTermConfig):
            msg = "'config' param must be of type DeviceControllerTermConfig."
            raise IllegalArgumentException(msg)

        chan = config.channel
        if chan is None:
            msg = "A configuration with a null channel is not permitted."
            raise ArgumentNullException(msg)

        # Read current config.
        mem_addr = config.channel.term_control_address
        tcon = self._read(mem_addr)

        # Modify config.
        ctrl_bit = chan.hardware_config_ctrl_bit
        tcon = self._set_bit(tcon, ctrl_bit, config.channel_enabled)

        ctrl_bit = chan.term_a_connection_ctrl_bit
        tcon = self._set_bit(tcon, ctrl_bit, config.pin_a_enabled)

        ctrl_bit = chan.wiper_connection_ctrl_bit
        tcon = self._set_bit(tcon, ctrl_bit, config.pin_w_enabled)

        ctrl_bit = chan.term_b_connection_ctrl_bit
        tcon = self._set_bit(tcon, ctrl_bit, config.pin_b_enabled)

        # Write new config to device.
        self._write(mem_addr, tcon)
Exemplo n.º 20
0
    def flip(self, index):
        """Set the bit at the specified index to the compliment of the value.

        :param int index: The index of the bit to flip.
        :raises: IllegalArgumentException if index is not an int.
        :raises: IndexError if index is less than zero.
        """
        if not isinstance(index, int):
            raise IllegalArgumentException("index must be a valid integer.")

        if index < 0:
            raise IndexError("index cannot be less than zero.")

        offset = word_index(index)
        self._expand_to(offset)
        self.__bits[offset] ^= 1 << index
        self.recalculate_words_in_use()
        self._check_invariants()
Exemplo n.º 21
0
    def get(self, index):
        """Get the value of the bit at the specified index.

        :param int index: The index at which to get the bit value.
        :returns: True if the requested bit is set.
        :rtype: bool
        :raises: IllegalArgumentException if index is not an int.
        :raises: IndexError if index is less than zero.
        """
        if isinstance(index, int):
            raise IllegalArgumentException("index must be a valied int.")

        if index < 0:
            raise IndexError("index cannot be less than zero.")

        self._check_invariants()
        offset = word_index(index)
        return (offset < self.__words_in_use
                and (self.__bits[index] & (1 << index)) != 0)
Exemplo n.º 22
0
def number_of_trailing_zeros(num):
    """Get the number or trailing zeros in the specified number.

    :param int num: The number value to inspect.
    :returns: The number of trailing zeros.
    :rtype: int
    :raises: IllegalArgumentException if the 'num' param is not a number.
    """
    if num is None:
        return 0

    if not isinstance(num, int):
        raise IllegalArgumentException("param 'num' must be an int.")

    mask = 1
    result = 64
    for i in range(0, 63):
        mask <<= 1
        if (num & mask) != 0:
            result = i
            break
    return result
Exemplo n.º 23
0
    def __init__(self, dcc=None, chan_enabled=False, pin_a_enabled=False,
                 pin_w_enabled=False, pin_b_enabled=False):
        """Initialize a new instance of DeviceControllerTermConfig.

        :param device_control_channel.DeviceControlChannel dcc: The device
        control channel.
        :param bool chan_enabled: Set True to enable the channel.
        :param bool pin_a_enabled: Set True to enable pin A.
        :param bool pin_w_enabled: Set True to enable pin W.
        :param bool pin_b_enabled: Set True to enable pin B.
        :raises: raspy.illegal_argument_exception.IllegalArgumentException if
        param dcc is not an instance of DeviceControlChannel.
        """
        instance = isinstance(dcc, device_control_channel.DeviceControlChannel)
        if dcc is not None and not instance:
            msg = "'dcc' param must be object of type DeviceControlChannel"
            raise IllegalArgumentException(msg)
        self.__channel = dcc
        self.__channelEnabled = chan_enabled
        self.__pinAEnabled = pin_a_enabled
        self.__pinWEnabled = pin_w_enabled
        self.__pinBEnabled = pin_b_enabled
Exemplo n.º 24
0
    def value_of(words):
        """Get a new BitSet containing all the bits in the specified list.

        :param list words: The list of bits to construct a BitSet from. If
        None, then this function will return None.
        :returns: A new BitSet containing the specified list of bits.
        :rtype: BitSet
        :raises: IllegalArgumentException if not a list.
        """
        if words is None:
            return None

        if not isinstance(words, list):
            raise IllegalArgumentException(
                "param 'words' must be an array of bits.")

        num = len(words)
        while num > 0 and words[num - 1] == 0:
            num -= 1

        words_copy = list(words)
        return BitSet(words_copy)
Exemplo n.º 25
0
    def increase(self, steps=0):
        """Increase the wiper's value bye the specified number of steps.

        It is not an error if the wiper hits or already hit the upper
        boundary. In such situations, the wiper sticks to the upper boundary
        or doesn't change.
        :param int steps: How many steps to increase. If not specified or
        zero, then defaults to 1. If the current value is equal to the max
        value, then nothing happens. If steps is less than zero, then an
        exception is thrown.
        :raises: raspy.io.io_exception.IOException if communication with the
        device failed.
        """
        max_val = self.max_value
        if self.__currentValue == max_val:
            return

        if steps < 0:
            msg = "Only positive integer values are permitted."
            raise IllegalArgumentException(msg)

        vol_only = microchip_pot_non_volatile_mode.VOLATILE_ONLY
        if self.non_volatile_mode != vol_only:
            msg = "Increase is only permitted for volatile-only wipers."
            raise InvalidOperationException(msg)

        # Check bounds.
        actual_steps = steps
        if (steps + self.__currentValue) > max_val:
            actual_steps = max_val - self.__currentValue

        new_val = self.__currentValue + actual_steps
        if new_val == max_val or steps > 5:
            self.__currentValue = new_val
        else:
            chan = device_control_channel.value_of(self.__channel)
            self.__controller.increase(chan, actual_steps)
            self.__currentValue = new_val
Exemplo n.º 26
0
    def __init__(self, provider, rows=0, columns=0):
        """Initialize a new instance of LcdComponent.

        :param raspy.lcd.lcd_transfer_provider.LcdTransferProvider provider: The
        LCD transfer provider.
        :param int rows: The number of rows in the display.
        :param int columns: The number of columns in the display.
        :raises: raspy.argument_null_exception.ArgumentNullException if the
        provider is None.
        :raises: raspy.illegal_argument_exception.IllegalArgumentException if
        the specified provider is not of LcdTransferProvider type.
        """
        Lcd.__init__(self)
        if provider is None:
            raise ArgumentNullException("'provider' cannot be None.")

        if not isinstance(provider, LcdTransferProvider):
            msg = "'provider' must be of type LcdTransferProvider "
            msg += "or derivative."
            raise IllegalArgumentException(msg)

        self.__module = LcdModule(provider)
        self.__module.begin(columns, rows)
Exemplo n.º 27
0
    def __init__(self, device, bus_address=-1):
        """Initialize a new instance of MCPDeviceController.

        :param raspy.io.i2c.i2c_interface.I2CInterface device: The I2C bus
        device this instance is connected to.
        :param int bus_address: The bus address of the device.
        :raises: raspy.argument_null_exception.ArgumentNullException if param
        'device' is None.
        :raises: raspy.illegal_argument_exception.IllegalArgumentException if
        param 'device' is not of type I2CInterface.
        """
        Disposable.__init__(self)
        if device is None:
            raise ArgumentNullException("Param 'device' cannot be None.")

        if not isinstance(device, I2CInterface):
            msg = "Param 'device' must be of I2CInterface."
            raise IllegalArgumentException(msg)

        self.__busAddress = bus_address
        self.__device = device
        if not self.__device.is_open:
            self.__device.open()
Exemplo n.º 28
0
    def and_not(self, b_set):
        """Clear all bits in this BitSet matching bits in specified BitSet.

        This clears all of the bits in this BitSet whose corresponding bit is
        set in the specified BitSet.

        :param BitSet b_set: The BitSet with which to mask this instance.
        :raises: ArgumentNullException if bs is None.
        :raises: IllegalArgumentException if bs is not a BitSet.
        """
        if b_set is None:
            raise ArgumentNullException("param 'b_set' cannot be None.")

        if not isinstance(b_set, BitSet):
            raise IllegalArgumentException("param 'b_set' must be a BitSet.")

        i = min(len(self.__bits), len(b_set.get_bits()))
        while i >= 0:
            i -= 1
            self.__bits[i] &= ~b_set.get_bits()[i]

        self.recalculate_words_in_use()
        self._check_invariants()
Exemplo n.º 29
0
    def x_or(self, b_set):
        """Perform a logical XOR of this BitSet with the specified BitSet.

        This BitSet is modified so that the bits in it have the value True if
        (and only if) one of the following statements hold true:
        - The bit initially has the value True, and the corresponding bit in
        the specified BitSet has the value False.
        - The bit initially has the value False, and the corresponding bit in
        the specified BitSet has the value True.

        :param BitSet b_set: A BitSet.
        :raises: ArgumentNullException if bs is None.
        :raises: IllegalArgumentException if bs is not a BitSet.
        """
        if b_set is None:
            raise ArgumentNullException("param 'b_set' cannot be None.")

        if not isinstance(b_set, BitSet):
            raise IllegalArgumentException("param 'b_set' must be a BitSet.")

        # Calculate how many words which have in common with the other BitSet.
        words_in_common = min(self.__words_in_use, b_set.get_words_in_use())
        if self.__words_in_use < b_set.get_words_in_use():
            self._ensure_capacity(b_set.get_words_in_use())
            self.__words_in_use = b_set.get_words_in_use()

        # Perform logical XOR on words in common.
        for i in range(0, words_in_common - 1):
            self.__bits[i] ^= b_set.get_bits()[i]

        # Copy any remaining words.
        if words_in_common < b_set.get_words_in_use():
            self.__bits = self.__bits[0:b_set.get_words_in_use() -
                                      words_in_common]

        self.recalculate_words_in_use()
        self._check_invariants()
Exemplo n.º 30
0
    def terminal_configuration(self, config):
        """Set the terminal configuration.

        :param MCPTerminalConfiguration config:
        The terminal configuration.
        :raises: raspy.io.io_exception.IOException if communication with
        the device fails.
        """
        if config is None:
            raise ArgumentNullException("config value cannot be None.")

        if config.channel != self.__channel:
            msg = "Setting a configuration with a channel that is not the "
            msg += "potentiometer's channel is not supported."
            raise IllegalArgumentException(msg)

        chan = device_control_channel.value_of(self.__channel)
        chan_enable = config.is_channel_enabled
        pin_a = config.is_pin_a_enabled
        pin_w = config.is_pin_w_enabled
        pin_b = config.is_pin_b_enabled
        dev_con = DeviceControllerTermConfig(chan, chan_enable, pin_a, pin_w,
                                             pin_b)
        self.__controller.set_terminal_config(dev_con)