Пример #1
0
    def pwm(self, val):
        """Get the PWM (pulse-width modulation) value.

        :param int val: The PWM value.
        :raises: raspy.invalid_operation_exception.InvalidOperationException
        if setting a PWM value on a pin that is not configured as PWM.
        """
        if self.mode == pin_mode.PWM:
            err_msg = "Cannot set PWM value on a pin not configured for PWM."
            raise InvalidOperationException(err_msg)

        if val < 0:
            val = 0

        if val > 1023:
            val = 1023

        if self.__pwm != val:
            self.__pwm = val
            if not self.__isPWM:
                cmd = "gpio mode " + str(self.inner_pin) + " pwm"
                exec_utils.execute_command(cmd)

            cmd = "gpio pwm " + str(self.inner_pin) + " " + str(self.__pwm)
            exec_utils.execute_command(cmd)
Пример #2
0
def get_cpu_info(target):
    """Get information about the CPU.

    Returns the value from the specified target field.

    :param string target: The target attribute to the value of.
    :returns: The value of the specified CPU attribute.
    :rtype: string
    :raises: raspy.invalid_operation_exception.InvalidOperationException if
    the specified target is invalid (unknown).
    """
    global __cpuInfo
    if __cpuInfo is None:
        __cpuInfo = []
        result = exec_utils.execute_command("cat /proc/cpuinfo")
        if result is not None:
            # noinspection PyTypeChecker
            for key, val in enumerate(result):
                line = val
                parts = line.split(":")
                if len(parts) >= 2:
                    tpart0 = string_utils.trim(parts[0])
                    tpart1 = string_utils.trim(parts[1])
                    if (not string_utils.is_null_or_empty(tpart0)
                            and not string_utils.is_null_or_empty(tpart1)):
                        # noinspection PyTypeChecker
                        __cpuInfo[tpart0] = tpart1

    if target in __cpuInfo:
        # noinspection PyTypeChecker
        return __cpuInfo[target]

    raise InvalidOperationException("Invalid target: " + target)
Пример #3
0
def get_voltage(id):
    """Get the voltage.

    :param string id: The ID of the voltage type to get (core, sdram_c, etc).
    :returns: The voltage.
    :rtype: int, long
    :raises: raspy.invalid_operation_exception.InvalidOperationException if
    invalid command ("measure_volts") or response.
    """
    cmd = "/opt/vc/bin/vcgencmd measure_volts " + id
    result = exec_utils.execute_command(cmd)
    if result is not None:
        val = -1
        separators = ["\\\[", "\\\=", "\\\V", "\\\]"]
        # noinspection PyTypeChecker
        for i, line in enumerate(result):
            parts = re.split('|'.join(map(re.escape, separators)), line)
            try:
                val = float(parts[1])
            except ValueError:
                val = -1

        return val

    raise InvalidOperationException("Invalid command or response.")
Пример #4
0
    def dispose(self):
        """Dispose managed resources.

        Performs application-defined tasks associated with freeing, releasing,
        or resetting resources.
        """
        if gpio.Gpio.is_disposed:
            return

        self.__unexport_pin(self.inner_pin)
        if self.__isPWM:
            cmd = "gpio unexport " + str(self.inner_pin.value)
            exec_utils.execute_command(cmd)

        self.write(pin_state.LOW)
        gpio.Gpio.dispose(self)
Пример #5
0
def get_clock_frequency(target):
    """Get the clock frequency for the specified target.

    :param string target: The target clock to get the frequency of.
    :returns: The clock frequency, if successful; Otherwise, -1.
    :rtype: int, long
    """
    if string_utils.is_null_or_empty(target):
        return -1

    target = string_utils.trim(target)
    val = -1
    cmd = "/opt/vc/bin/vcgencmd measure_clock " + target
    result = exec_utils.execute_command(cmd)
    if result is not None:
        # noinspection PyTypeChecker
        for i, line in enumerate(result):
            parts = line.split("=", 2)
            try:
                temp = float(string_utils.trim(parts[1]))
            except ValueError:
                temp = -1

            if temp != -1:
                val = temp
                break
    return val
Пример #6
0
def get_read_elf_tag(tag):
    """Obtain a specific tag value from the ELF.

    This method will obtain a specified tag value from the ELF info in the
    '/proc/self/exe' program (this method is used to help determine the
    HARD-FLOAT / SOFT-FLOAT ABI of the system).

    :param string tag: The tag to get the value of.
    :returns: The ABI tag value.
    :rtype: string
    """
    tag_val = string_utils.EMPTY
    try:
        cmd = "/usr/bin/readelf -A /proc/self/exe"
        result = exec_utils.execute_command(cmd)
        if result is not None:
            # noinspection PyTypeChecker
            for i, line in enumerate(result):
                part = string_utils.trim(line)
                if (string_utils.starts_with(part, tag)
                        and string_utils.contains(part, ":")):
                    line_parts = part.split(":", 2)
                    if len(line_parts) > 1:
                        tag_val = string_utils.trim(line_parts[1])

                    break
    except Exception:
        pass

    return tag_val
Пример #7
0
def get_cpu_temperature():
    """Get the CPU temperature.

    :returns: The CPU temperature.
    :rtype: int, long
    :raises: raspy.invalid_operation_exception.InvalidOperationException if
    invalid command ("measure_temp") or response.
    """
    # CPU temperature is in the form:
    # pi@mypi$ /opt/vc/bin/vcgencmd measure_temp
    # temp=42.3'C
    # Support for this was added around firmware version 3357xx per info
    # at http://www.raspberrypi.org/phpBB3/viewtopic.php?p=169909#p169909
    result = exec_utils.execute_command("/opt/vc/bin/vcgencmd measure_temp")
    if result is not None:
        val = -1
        separators = ["\\\[", "\\\=", "\\\]", "\\\'"]
        # noinspection PyTypeChecker
        for i, line in enumerate(result):
            parts = re.split('|'.join(map(re.escape, separators)), line)
            try:
                val = float(parts[1])
            except ValueError:
                val = -1

        return val

    raise InvalidOperationException("Invalid command or response.")
Пример #8
0
def get_ip_address():
    """Get the IP address of the local system's hostname.

    This only works if the hostname can be resolved.

    :returns: The IP address.
    :rtype: string
    """
    return exec_utils.execute_command("hostname --ip-address")[0]
Пример #9
0
def get_fqdn():
    """Get the fully-qualified domain name of the local host.

    :returns: The fully-qualified domain name (FQDN).
    :rtype: string
    """
    result = exec_utils.execute_command("hostname -f")
    if result is not None:
        return result[0]

    return get_hostname()
Пример #10
0
def test_execute_command():
    """Test execute_command method."""
    cmd = 'echo "Hello World!"'
    text = "Hello"
    if platform.system() == "Windows":
        cmd = "echo Hello World!"

    newline = '\n'
    result = exec_utils.execute_command(cmd)
    # noinspection PyTypeChecker
    output_str = newline.join(result)
    print("DEBUG: " + output_str)
    assert string_utils.contains(output_str, text)
Пример #11
0
def get_name_servers():
    """Get an array of all available name servers.

    :returns: The name servers.
    :rtype: array
    """
    result = []
    lines = exec_utils.execute_command(
        "cat /etc/resolv.conf | grep nameserver")
    # noinspection PyTypeChecker
    for key, val in enumerate(lines):
        result.append(val[len("nameserver") + 1:])

    return result
Пример #12
0
def get_all_fqdns():
    """Get all FQDNs of the machine.

    This enumerates all the configured network addresses on all configured
    network interfaces, and translates them to DNS domain names. Addresses
    that cannot be translated (ie. because they do not have an appropriate
    reverse DNS entry) are skipped. Note that different addresses may resolve
    to the same name, therefore the return value may contain duplicate entries.
    Do not make any assumptions about the order of the items in the array.

    :returns: The FQDNs.
    :rtype: array
    """
    names = exec_utils.execute_command("hostname --all-fqdns")[0]
    return names.split()
Пример #13
0
def get_os_firmware_date():
    """Get the OS firmware date.

    :returns: The OS firmware date.
    :rtype: string
    :raises: raspy.invalid_operation_exception.InvalidOperationException if
    an unexpected response is received.
    """
    results = exec_utils.execute_command("/opt/vc/bin/vcgencmd version")
    val = string_utils.EMPTY
    if results is not None:
        # noinspection PyTypeChecker
        for key, value in enumerate(results):
            # We are intentionally only getting the first line.
            val = value
            break

    if not string_utils.is_null_or_empty(val):
        return val

    raise InvalidOperationException("Invalid command or response.")
Пример #14
0
def get_bash_version_info():
    """Get the BaSH version info.

    This method is used to help determine the HARD_FLOAT / SOFT_FLOAT ABI of
    the system.

    :returns: The BaSH version info.
    :rtype: string
    """
    ver = string_utils.EMPTY
    try:
        result = exec_utils.execute_command("bash --version")
        # noinspection PyTypeChecker
        for i, line in enumerate(result):
            if not string_utils.is_null_or_empty(line):
                ver = line  # Return only the first line.
                break
    except Exception:
        pass

    return ver
Пример #15
0
def get_codec_enabled(codec):
    """Get whether or not the specified codec is enabled.

    :param codec: The codec to get.
    :type codec: string
    :returns: true if the specified codec is enabled; Otherwise, false.
    :rtype: boolean
    :raises: raspy.InvalidOperationException if invalid command
    ("codec_enabled") or response.
    """
    enabled = False
    cmd = "/opt/vc/bin/vcgencmd codec_enabled " + codec
    result = exec_utils.execute_command(cmd)
    if result is not None:
        # noinspection PyTypeChecker
        for i, line in enumerate(result):
            parts = line.split("=", 2)
            if string_utils.trim(parts[1]).upper() == "ENABLED":
                enabled = True
                break
    return enabled
Пример #16
0
def get_memory():
    """Get the system memory info.

    :returns: The memory info.
    :rtype: array
    """
    values = []
    result = exec_utils.execute_command("free -b")
    if result is not None:
        val = 0
        # noinspection PyTypeChecker
        for i, line in enumerate(result):
            if string_utils.starts_with(line, "Mem:"):
                parts = line.split()
                for j, part in enumerate(parts):
                    line_part = string_utils.trim(part)
                    if (not string_utils.is_null_or_empty(line_part)
                            and line.upper() == "Mem:".upper()):
                        values.append(float(val))

    return values
Пример #17
0
def get_os_firmware_build():
    """Get the OS firmware build.

    :returns: the OS firmware build.
    :rtype: string
    :raises: raspy.invalid_operation_exception.InvalidOperationException if
    an unexpected response is received.
    """
    results = exec_utils.execute_command("/opt/vc/bin/vcgencmd version")
    val = string_utils.EMPTY
    if results is not None:
        # noinspection PyTypeChecker
        for key, val in enumerate(results):
            line = val
            if string_utils.starts_with(line, "version "):
                val = line
                break

    if not string_utils.is_null_or_empty(val):
        return val[8:]

    raise InvalidOperationException("Invalid command or response")