forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
239 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |