예제 #1
0
int gpio_set_value(uint8_t gpio_pin, uint8_t value)
{
    uint8_t dir;

    if (!check_pin(gpio_pin))
        return -1;

    if (!is_gpio_exported(gpio_pin))
        return 0;

    if (gpio_get_direction(gpio_pin, &dir) < 0)
        return -1;

    if (dir == GPIO_INPUT) {
        fprintf(stderr, "gpio: Cannot set value to an input.\n");
        return -1;
    }

    /* Only write "0" or "1" to /sys/class/gpio/gpioN/value */
    return write_int_gpio_file(gpio_pin, "value", value == 0 ? 0 : 1);
}
예제 #2
0
static int lua_gpio_index(lua_State *L) {
    gpio_t *gpio;
    const char *field;

    if (!lua_isstring(L, 2))
        return lua_gpio_error(L, GPIO_ERROR_ARG, 0, "Error: unknown method or property");

    field = lua_tostring(L, 2);

    /* Look up method in metatable */
    lua_getmetatable(L, 1);
    lua_getfield(L, -1, field);
    if (!lua_isnil(L, -1))
        return 1;

    gpio = luaL_checkudata(L, 1, "periphery.GPIO");

    if (strcmp(field, "fd") == 0) {
        lua_pushinteger(L, gpio_fd(gpio));
        return 1;
    } else if (strcmp(field, "pin") == 0) {
        lua_pushunsigned(L, gpio_pin(gpio));
        return 1;
    } else if (strcmp(field, "supports_interrupts") == 0) {
        bool supported;
        int ret;

        if ((ret = gpio_supports_interrupts(gpio, &supported)) < 0)
            return lua_gpio_error(L, ret, gpio_errno(gpio), "Error: %s", gpio_errmsg(gpio));

        lua_pushboolean(L, supported);
        return 1;
    } else if (strcmp(field, "direction") == 0) {
        gpio_direction_t direction;
        int ret;

        if ((ret = gpio_get_direction(gpio, &direction)) < 0)
            return lua_gpio_error(L, ret, gpio_errno(gpio), "Error: %s", gpio_errmsg(gpio));

        switch (direction) {
            case GPIO_DIR_IN: lua_pushstring(L, "in"); break;
            case GPIO_DIR_OUT: lua_pushstring(L, "out"); break;
            default: lua_pushstring(L, "unknown"); break;
        }
        return 1;
    } else if (strcmp(field, "edge") == 0) {
        gpio_edge_t edge;
        int ret;

        if ((ret = gpio_get_edge(gpio, &edge)) < 0)
            return lua_gpio_error(L, ret, gpio_errno(gpio), "Error: %s", gpio_errmsg(gpio));

        switch (edge) {
            case GPIO_EDGE_NONE: lua_pushstring(L, "none"); break;
            case GPIO_EDGE_RISING: lua_pushstring(L, "rising"); break;
            case GPIO_EDGE_FALLING: lua_pushstring(L, "falling"); break;
            case GPIO_EDGE_BOTH: lua_pushstring(L, "both"); break;
            default: lua_pushstring(L, "unknown"); break;
        }
        return 1;
    }

    return lua_gpio_error(L, GPIO_ERROR_ARG, 0, "Error: unknown property");
}
예제 #3
0
GpioPin::pin_direction_t GpioPin::getDirection() const
{
    return (gpio_get_direction(m_bank, m_pin) == GPIO_GDIR_INPUT) ? kInput : kOutput;
}