#!/usr/bin/env python3
"""Python binding of gpio wrapper of LetMeCreate library."""

import ctypes

_LIB = ctypes.CDLL('libletmecreate_core.so')

# GPIO pin number
MIKROBUS_1_AN = 22
MIKROBUS_1_RST = 23
MIKROBUS_1_PWM = 73
MIKROBUS_1_INT = 21
MIKROBUS_2_AN = 25
MIKROBUS_2_RST = 27
MIKROBUS_2_PWM = 74
MIKROBUS_2_INT = 24
GPIO_14 = 14
GPIO_21 = 21
GPIO_22 = 22
GPIO_23 = 23
GPIO_24 = 24
GPIO_25 = 25
GPIO_27 = 27
GPIO_31 = 31
GPIO_73 = 73
GPIO_74 = 74
GPIO_75 = 75
GPIO_88 = 88
GPIO_89 = 89
GPIO_72 = 72
GPIO_80 = 80
GPIO_81 = 81
GPIO_82 = 82
GPIO_83 = 83
GPIO_84 = 84
GPIO_85 = 85

# GPIO direction
GPIO_OUTPUT = 0
GPIO_INPUT = 1


def init(gpio_pin):
    """Initialise a GPIO.

    Export the GPIO and configure it as an input.

    Note: An exception is thrown if the gpio cannot be initialised.
    """
    ret = _LIB.gpio_init(gpio_pin)
    if ret < 0:
        raise Exception("gpio init failed")


def set_direction(gpio_pin, direction):
    """Configure GPIO as an input or an output.

    gpio_pin: Index of the GPIO.

    direction: must be 0 (GPIO_OUTPUT) or 1 (GPIO_INPUT).

    Note: An exception is thrown if it fails to set the direction of a GPIO.
    """
    ret = _LIB.gpio_set_direction(gpio_pin, direction)
    if ret < 0:
        raise Exception("gpio set direction failed")


def get_direction(gpio_pin):
    """Returns the direction of a GPIO (0 or 1).

    gpio_pin: Index of the GPIO.

    Note: An exception is thrown if it fails to get the current direction of a
    GPIO.
    """
    direction = ctypes.c_uint8(0)
    ret = _LIB.gpio_get_direction(gpio_pin, ctypes.byref(direction))
    if ret < 0:
        raise Exception("gpio get direction failed")
    return direction.value


def set_value(gpio_pin, value):
    """Sets the GPIO as high or low.

    The GPIO must be configured as an output before calling this function.

    gpio_pin: Index of the GPIO.

    value: 0 for low, any other positive integer for high.

    Note: An exception is thrown if it fails to set the value of a GPIO.
    """
    ret = _LIB.gpio_set_value(gpio_pin, value)
    if ret < 0:
        raise Exception("gpio set value failed")


def get_value(gpio_pin):
    """Reads the current value of a GPIO (0: low, 1: high)

    gpio_pin: Index of the GPIO.

    Note: An exception is thrown if it fails to read the value of a GPIO.
    """
    value = ctypes.c_uint8(0)
    ret = _LIB.gpio_get_value(gpio_pin, ctypes.byref(value))
    if ret < 0:
        raise Exception("gpio get direction failed")
    return value.value


def release(gpio_pin):
    """Release a GPIO.

    gpio_pin: Index of the GPIO.

    Note: An exception is thrown if it fails to release the GPIO.
    """
    ret = _LIB.gpio_release(gpio_pin)
    if ret < 0:
        raise Exception("gpio get direction failed")

@@ -139,16 +139,94 @@
 e.so')%0A%0A
+# Pin type%0ATYPE_AN = 0%0ATYPE_RST = 1%0ATYPE_PWM = 2%0ATYPE_INT = 3%0ATYPE_COUNT = 4%0A%0A
 # GPIO p
@@ -971,24 +971,377 @@
  failed%22)%0A%0A%0A
+def get_pin(mikrobus_index, pin_type):%0A    %22%22%22Returns GPIO index of a pin on provided mikrobus%0A%0A    Note: An exception is thrown if the gpio cannot be found.%0A    %22%22%22%0A    pin = ctypes.c_uint8(0)%0A    ret = _LIB.gpio_get_pin(mikrobus_index, pin_type, ctypes.byref(pin))%0A    if ret %3C 0:%0A        raise Exception(%22gpio get pin failed%22)%0A    return pin.value%0A%0A%0A
 def set_dire

Add get_pin function