Skip to content

Commit

Permalink
dev: add ws1608 bsp support
Browse files Browse the repository at this point in the history
wwhai committed Jun 21, 2023
1 parent c529a01 commit ff3e2e2
Showing 11 changed files with 239 additions and 9 deletions.
3 changes: 3 additions & 0 deletions appstack/lua_runtime.go
Original file line number Diff line number Diff line change
@@ -178,6 +178,9 @@ func LoadAppLib(app *typex.Application, e typex.RuleX) {
// 树莓派4B
addAppLib(app, e, "raspi4b", "GPIOGet", rulexlib.RASPI4_GPIOGet(e))
addAppLib(app, e, "raspi4b", "GPIOSet", rulexlib.RASPI4_GPIOSet(e))
// 玩客云WS1508
addAppLib(app, e, "ws1608", "GPIOGet", rulexlib.WKYWS1608_GPIOGet(e))
addAppLib(app, e, "ws1608", "GPIOSet", rulexlib.WKYWS1608_GPIOSet(e))
//------------------------------------------------------------------------
// 校验数据
//------------------------------------------------------------------------
81 changes: 81 additions & 0 deletions bspsupport/amlogic_s805_adda_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package archsupport

/*
*
* Linux 特定实现
*
*/

import (
"fmt"
"log"
"os"
"os/exec"
)

/*
玩客云WS1608有一个RGB LED,其系统内部已经直接支持进设备树:
R -> /sys/class/leds/onecloud\:red\:alive/brightness
G -> /sys/class/leds/onecloud\:green\:alive/brightness
B -> /sys/class/leds/onecloud\:blue\:alive/brightness
*/
const (
AmlogicWKYS805_R string = "red"
AmlogicWKYS805_G string = "green"
AmlogicWKYS805_B string = "blue"
)

func init() {
env := os.Getenv("ARCHSUPPORT")
if env == "WKYS805" {
_AmlogicWKYS805_RGBAllInit()
}
}

/*
explain:init all gpio
*/
func _AmlogicWKYS805_RGBAllInit() int {
_AmlogicWKYS805_RGBInit(AmlogicWKYS805_R, 0)
_AmlogicWKYS805_RGBInit(AmlogicWKYS805_G, 0)
_AmlogicWKYS805_RGBInit(AmlogicWKYS805_B, 0)
// 返回值无用
return 1
}

func _AmlogicWKYS805_RGBInit(pin string, value int) {
AmlogicWKYS805_RGBSet(pin, value)
}

func AmlogicWKYS805_RGBSet(pin string, value int) (bool, error) {
cmd := fmt.Sprintf("echo %d > /sys/class/leds/onecloud\\:%s\\:alive/brightness", value, pin)
_, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
log.Println("[AmlogicWKYS805_RGBSet] error", err)
return false, err
}
v, e := AmlogicWKYS805_RGBGet(pin)
if e != nil {
return false, e
}
return v == value, nil
}

func AmlogicWKYS805_RGBGet(pin string) (int, error) {
cmd := fmt.Sprintf("cat /sys/class/leds/onecloud\\:%s\\:alive/brightness", pin)
Value, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
return -1, err
}
if len(Value) < 1 {
return -1, errInvalidLen
}
if Value[0] == '0' {
return 0, nil
}
if Value[0] == '1' {
return 1, nil
}
return -1, errInvalidValue
}
28 changes: 28 additions & 0 deletions bspsupport/amlogic_s805_adda_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package archsupport

import "fmt"

/*
*
* Linux 特定实现
*
*/

func init() {
fmt.Println("WKYS805 RGB GPIO not support Windows")
}

/*
*
* Windows下我们不实现 但是留下接口防止未来扩展的时候改代码
*
*/

func AmlogicWKYS805_RGBSet(pin string, value int) (bool, error) {
return false, errArchNotSupport
}

func AmlogicWKYS805_RGBGet(pin string) (int, error) {

return -1, errArchNotSupport
}
5 changes: 2 additions & 3 deletions bspsupport/eekit_adda_linux.go
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ package archsupport
*/

import (
"errors"
"fmt"
"log"
"os"
@@ -112,13 +111,13 @@ func EEKIT_GPIOGet(pin int) (int, error) {
return -1, err
}
if len(Value) < 1 {
return -1, errors.New("invalid length")
return -1, errInvalidLen
}
if Value[0] == '0' {
return 0, nil
}
if Value[0] == '1' {
return 1, nil
}
return -1, errors.New("invalid value")
return -1, errInvalidValue
}
10 changes: 10 additions & 0 deletions bspsupport/error_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package archsupport

import (
"errors"
"runtime"
)

var errInvalidLen = errors.New("invalid length")
var errInvalidValue = errors.New("invalid value")
var errArchNotSupport = errors.New("not support current OS:" + runtime.GOOS)
5 changes: 2 additions & 3 deletions bspsupport/raspberry4b_adda_linux.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package archsupport

import (
"errors"
"fmt"
"log"
"os"
@@ -92,13 +91,13 @@ func RASPI4_GPIOGet(pin int) (int, error) {
return -1, err
}
if len(Value) < 1 {
return -1, errors.New("invalid length")
return -1, errInvalidLen
}
if Value[0] == '0' {
return 0, nil
}
if Value[0] == '1' {
return 1, nil
}
return -1, errors.New("invalid value")
return -1, errInvalidValue
}
33 changes: 30 additions & 3 deletions bspsupport/readme.md
Original file line number Diff line number Diff line change
@@ -22,10 +22,11 @@ ARCHSUPPORT=EEKITH3 rulex run

## 支持硬件列表

| 硬件名 | 环境参数 | 示例 |
| --------------- | -------- | --------------------------------- |
| 硬件名 | 环境参数 | 示例 |
| --------------- | -------- | ------------------------------- |
| EEKITH3版本网关 | EEKITH3 | `ARCHSUPPORT=EEKITH3 rulex run` |
| 树树莓派4B、4B+ | RPI4 | `ARCHSUPPORT=RPI4B rulex run` |
| 树莓派4B、4B+ | RPI4 | `ARCHSUPPORT=RPI4B rulex run` |
| 玩客云S805 | WKYS805 | `ARCHSUPPORT=WKYS805 rulex run` |

> 警告: 这些属于板级高级功能,和硬件架构以及外设有关,默认关闭。 如果你自己需要定制,最好针对自己的硬件进行跨平台适配, 如果没有指定平台,可能会导致预料之外的结果。
@@ -52,3 +53,29 @@ ARCHSUPPORT=EEKITH3 rulex run
| 参数名 | 类型 | 说明 |
| ------ | ---- | -------- |
| Pin | int | GPIO引脚 |

## 示例脚本
1. 玩客云WS1608
```lua
function Main(arg)
while true do
ws1608:GPIOSet("red", 1)
applib:Sleep(2000)
ws1608:GPIOSet("red", 0)
applib:Sleep(2000)
end
end

```
2. EEKIT 网关
```lua
function Main(arg)
while true do
eekit:GPIOSet(6, 1)
applib:Sleep(2000)
eekit:GPIOSet(7, 0)
applib:Sleep(2000)
end
end

```
3 changes: 3 additions & 0 deletions engine/lua_runtime.go
Original file line number Diff line number Diff line change
@@ -105,6 +105,9 @@ func LoadBuildInLuaLib(e typex.RuleX, r *typex.Rule) {
// 树莓派4B
r.AddLib(e, "raspi4b", "GPIOGet", rulexlib.RASPI4_GPIOGet(e))
r.AddLib(e, "raspi4b", "GPIOSet", rulexlib.RASPI4_GPIOSet(e))
// 玩客云WS1508
r.AddLib(e, "ws1608", "GPIOGet", rulexlib.WKYWS1608_GPIOGet(e))
r.AddLib(e, "ws1608", "GPIOSet", rulexlib.WKYWS1608_GPIOSet(e))
//------------------------------------------------------------------------
// AI BASE
//------------------------------------------------------------------------
56 changes: 56 additions & 0 deletions rulexlib/amlogic_s805_ws1608_lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package rulexlib

import (
lua "github.com/hootrhino/gopher-lua"
"github.com/hootrhino/rulex/typex"
"github.com/hootrhino/rulex/vendor3rd"
"github.com/plgd-dev/kit/v2/strings"
)

/*
*
* 读GPIO, lua的函数调用应该是这样: ws1608:GPIOGet(pin) -> v,error
*
*/
func WKYWS1608_GPIOGet(rx typex.RuleX) func(*lua.LState) int {
return func(l *lua.LState) int {
pin := l.ToString(2)
if !strings.SliceContains([]string{"red", "green", "blue"}, pin) {
l.Push(lua.LNumber(0))
l.Push(lua.LNil)
return 1
}
v, e := vendor3rd.AmlogicWKYS805_RGBGet(pin)
if e != nil {
l.Push(lua.LNil)
l.Push(lua.LString(e.Error()))
} else {
l.Push(lua.LNumber(v))
l.Push(lua.LNil)
}
return 2
}
}

/*
*
* 写GPIO, lua的函数调用应该是这样: ws1608:GPIOSet(pin, v) -> error
*
*/
func WKYWS1608_GPIOSet(rx typex.RuleX) func(*lua.LState) int {
return func(l *lua.LState) int {
pin := l.ToString(2)
value := l.ToNumber(3)
if !strings.SliceContains([]string{"red", "green", "blue"}, pin) {
l.Push(lua.LNil)
return 1
}
_, e := vendor3rd.AmlogicWKYS805_RGBSet((pin), int(value))
if e != nil {
l.Push(lua.LString(e.Error()))
} else {
l.Push(lua.LNil)
}
return 1
}
}
10 changes: 10 additions & 0 deletions test/apps/ws1608-rgb_demo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- 应用名称 "ws1608:GPIO Test"

function Main(arg)
while true do
ws1608:GPIOSet("red", 1)
applib:Sleep(2000)
ws1608:GPIOSet("red", 0)
applib:Sleep(2000)
end
end
14 changes: 14 additions & 0 deletions vendor3rd/amlogic_s805_wky_ws1608_rgb_support.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package vendor3rd

import archsupport "github.com/hootrhino/rulex/bspsupport"

// R string = "red"
// G string = "green"
// B string = "blue"
func AmlogicWKYS805_RGBSet(pin string, value int) (bool, error) {
return archsupport.AmlogicWKYS805_RGBSet(pin, value)
}

func AmlogicWKYS805_RGBGet(pin string) (int, error) {
return archsupport.AmlogicWKYS805_RGBGet(pin)
}

0 comments on commit ff3e2e2

Please sign in to comment.