Luci操作创龙(Tronlong)T113-i开发板GPIO

第一步:验证GPIO控制

shell 复制代码
# 关闭LED
echo 0 > /sys/class/leds/user-led0/brightness
# 打开LED
echo 1 > /sys/class/leds/user-led0/brightness
# 查询LED状态
cat /sys/class/leds/user-led0/brightness

第二步:创建rpcd 插件

创建 /usr/libexec/rpcd/t113_led 文件

shell 复制代码
#!/bin/sh
. /usr/share/libubox/jshn.sh

case "$1" in
    list)
        echo '{ "set": {"value": 0} }'
        ;;
    call)
        if [ "$2" = "set" ]; then
            # ubus通过标准输入传递JSON,而不是命令行参数
            # 读取标准输入中的JSON数据
            json_input=$(cat)
            
            # 如果没有读取到数据,尝试使用命令行参数(用于直接调用)
            if [ -z "$json_input" ] && [ -n "$3" ]; then
                json_input="$3"
            fi
            
            if [ -z "$json_input" ]; then
                echo '{ "error": "No JSON input provided" }'
                exit 1
            fi
            
            json_load "$json_input" || {
                echo '{ "error": "Invalid JSON" }'
                exit 1
            }
            
            json_get_var value value

            case "$value" in
                0|1|"0"|"1") : ;;
                *)
                    echo '{ "error": "value must be 0 or 1" }'
                    exit 1
                    ;;
            esac

            echo "$value" > /sys/class/leds/user-led0/brightness 2>/dev/null
            if [ $? -eq 0 ]; then
                echo '{ "status": "ok" }'
            else
                echo '{ "error": "Failed to write to LED" }'
            fi
        else
            echo '{ "error": "Method not found" }'
            exit 1
        fi
        ;;
    *)
        echo '{ "error": "Invalid command" }'
        exit 1
        ;;
esac

赋予执行权限

shell 复制代码
chmod +x /usr/libexec/rpcd/t113_led

第三步:重启rpcd服务

shell 复制代码
/etc/init.d/rpcd restart

第四步:验证ubus服务是否注册成功

shell 复制代码
ubus list | grep t113
# 应输出: t113_led

ubus call t113_led set '{"value":1}'
# 应返回: { "status": "ok" }

第五步:创建luci页面

  1. controller: /usr/lib/luci/controller/t113_led.lua
lua 复制代码
module("luci.controller.greeting", package.seeall)

function index()
    -- 使用 'view' 而非 'cbi',就是使用js来渲染
    -- entry({"admin", "system", "greeting"}, cbi("greeting"), _("Greeting"), 99)
    entry({"admin", "system", "greeting"}, view("eboy_light/greeting"), _("Greeting"), 99)
    entry({"admin", "system", "t113_led"}, view("eboy_light/t113_led"), _("T113_LED"), 98)
end
  1. view: /www/luci-static/resources/view/eboy_light/t113_led.js
js 复制代码
'use strict';
'require rpc';
'require view';
'require uci';

var callGPIOSet = rpc.declare({
    object: 't113_led',
    method: 'set',
    params: ['value'],
    expect: { '': {} }
});

return view.extend({
    render: function() {

        var btnOn = E('button', {
            'class': 'cbi-button cbi-button-apply',
            'click': function() {
                callGPIOSet(1).then(function(res) {
                    console.log(res)
                    if (res.status === 'ok') {
                        alert(_('灯已开启'));
                    } else {
                        alert(_('Error: %s').format(res.error || 'Unknown'));
                    }
                });
            }
        }, _('开灯'));

        var btnOff = E('button', {
            'class': 'cbi-button cbi-button-reset',
            'click': function() {
                callGPIOSet(0).then(function(res) {
                    if (res.status === 'ok') {
                        alert(_('灯已关闭'));
                    } else {
                        alert(_('Error: %s').format(res.error || 'Unknown'));
                    }
                });
            }
        }, _('关灯'));

        return E('div', {}, [
            E('h2', {}, _('T113 LED 控制')),
            E('p', {}, _('控制 GPIO')),
            E('div', {'style': 'margin: 10px 0;'}, [btnOn, ' ', btnOff])
        ]);
    }
});

第六步:配置权限

创建 /usr/share/rpcd/acl.d/t113_led.json

json 复制代码
{
    "t113_led": {
        "description": "T113 LED control",
        "read": {
            "ubus": {
                "t113_led": [ "list" ]
            }
        },
        "write": {
            "ubus": {
                "t113_led": [ "set" ]
            }
        }
    }
}

重启rpcd服务

shell 复制代码
/etc/init.d/rpcd restart
#or service rpcd restart
相关推荐
ShineWinsu20 小时前
对于CSS:选择器的解析
前端·css·面试·html·web·选择器·网页
cui_ruicheng2 天前
FastAPI 应用开发(五)——SQLAlchemy ORM、APIRouter 与 Alembic 数据库迁移
python·fastapi·web
代码什么用3 天前
Java Web 21-22
java·开发语言·web
墨神谕4 天前
RPC框架与HTTP REST的区别
网络协议·http·rpc
殷色玫瑰5 天前
C++ STL:map 与 set 详解——从底层红黑树到实际应用
c语言·数据结构·c++·算法·visualstudio·rpc
进化中的刀盾兽5 天前
从零实现 C++ Json-Rpc(九):从 TCP 字节流中拆出完整消息——MuduoBuffer 与 LVProtocol
c++·rpc
袁哥大话安全5 天前
巡隐WEBSHELL扫描软件
人工智能·安全·web
进化中的刀盾兽6 天前
从零实现 C++ Json-Rpc(八):补齐 detail.hpp——JSON 封装与请求 ID 生成
c++·rpc
小小龙学IT7 天前
Go 语言 gRPC(grpc-go)深度解析
rpc·架构·golang·go
acd120097 天前
RPC接口超时配置明明没问题,线上却随机超时
网络·网络协议·rpc