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.
- Loading branch information
Showing
14 changed files
with
221 additions
and
18 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
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,98 @@ | ||
package archsupport | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os/exec" | ||
) | ||
|
||
/* | ||
* | ||
* 树莓派文档可以参考这里:https://www.raspberrypi.com/documentation/computers/raspberry-pi.html | ||
* | ||
*/ | ||
|
||
const ( | ||
//----------------------------------------------- | ||
// 注意: 该定义使用的是树莓派GPIO的【物理编号】 | ||
//----------------------------------------------- | ||
// 4个输出 | ||
raspi_DO1 string = "11" // GPIO.0 | ||
raspi_DO2 string = "12" // GPIO.1 | ||
raspi_DO3 string = "13" // GPIO.2 | ||
raspi_DO4 string = "15" // GPIO.3 | ||
|
||
// 4个输入 | ||
raspi_DI1 string = "16" // GPIO.4 | ||
raspi_DI2 string = "18" // GPIO.5 | ||
raspi_DI3 string = "22" // GPIO.6 | ||
raspi_DI4 string = "29" // GPIO.21 | ||
) | ||
|
||
const ( | ||
raspi_Out string = "out" | ||
raspi_In string = "in" | ||
) | ||
|
||
func init() { | ||
_RASPI4B_GPIOAllInit() | ||
} | ||
func _RASPI4B_GPIOAllInit() { | ||
// 初始化输入 | ||
_RASPI4B_GPIOInit(raspi_DO1, raspi_Out) | ||
_RASPI4B_GPIOInit(raspi_DO2, raspi_Out) | ||
_RASPI4B_GPIOInit(raspi_DO3, raspi_Out) | ||
_RASPI4B_GPIOInit(raspi_DO4, raspi_Out) | ||
// 初始化输出 | ||
_RASPI4B_GPIOInit(raspi_DI1, raspi_In) | ||
_RASPI4B_GPIOInit(raspi_DI2, raspi_In) | ||
_RASPI4B_GPIOInit(raspi_DI3, raspi_In) | ||
_RASPI4B_GPIOInit(raspi_DI4, raspi_In) | ||
} | ||
|
||
func _RASPI4B_GPIOInit(Pin string, EnDir string) { | ||
//gpio export | ||
cmd := fmt.Sprintf("echo %s > /sys/class/gpio/export", Pin) | ||
_, err := exec.Command("sh", "-c", cmd).Output() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
// gpio set direction | ||
cmd = fmt.Sprintf("echo %s > /sys/class/gpio/gpio%s/direction", EnDir, Pin) | ||
_, err = exec.Command("sh", "-c", cmd).Output() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
} | ||
|
||
func RASPI4_GPIOSet(pin, value int) (bool, error) { | ||
cmd := fmt.Sprintf("echo %d > /sys/class/gpio/gpio%d/value", value, pin) | ||
_, err := exec.Command("sh", "-c", cmd).Output() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
v, e := RASPI4_GPIOGet(pin) | ||
if e != nil { | ||
return false, e | ||
} | ||
return v == value, nil | ||
} | ||
|
||
func RASPI4_GPIOGet(pin int) (int, error) { | ||
cmd := fmt.Sprintf("cat /sys/class/gpio/gpio%d/value", pin) | ||
Value, err := exec.Command("sh", "-c", cmd).Output() | ||
if err != nil { | ||
return -1, err | ||
} | ||
if len(Value) < 1 { | ||
return -1, errors.New("invalid length") | ||
} | ||
if Value[0] == '0' { | ||
return 0, nil | ||
} | ||
if Value[0] == '1' { | ||
return 1, nil | ||
} | ||
return -1, errors.New("invalid value") | ||
} |
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 archsupport | ||
|
||
/* | ||
* | ||
* Windows下我们不实现 但是留下接口防止未来扩展的时候改代码 | ||
* | ||
*/ | ||
|
||
func RASPI4_GPIOSet(pin, value int) (bool, error) { | ||
return false, nil | ||
} | ||
func RASPI4_GPIOGet(pin int) (int, error) { | ||
return 0, nil | ||
} |
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 |
---|---|---|
@@ -1,2 +1,21 @@ | ||
# 跨平台 | ||
如果有不同操作系统上的实现库,建议统一放置此处。可参考下面的hello文件里面的程序。 | ||
这里放置一些对特定硬件的支持库,一般指的是定制化网关产品。如果有不同操作系统上的实现库,建议统一放置此处。可参考下面的hello文件里面的程序。 | ||
|
||
## 当前兼容 | ||
### EEKIT 网关 | ||
EEKIT 是 RULEX 团队的默认硬件,操作系统为`64位OpenWrt、Armbian`, CPU 架构为`64位全志H3`。EEKIT 网关的lua标准库命名空间为`eekit`。 | ||
### 树莓派4B+ | ||
除此之外,还对`树莓派4B`的 GPIO 做了支持。树莓派的lua标准库命名空间为`raspberry`。 | ||
|
||
## 环境变量 | ||
如果要启用硬件特性,需要在启动的时候加入 `ARCHSUPPORT` 环境变量来指定运行的版本, 例如要在 EEKIT-H3网关上运行: | ||
```sh | ||
ARCHSUPPORT=EEKITH3 rulex run | ||
``` | ||
## 支持硬件列表 | ||
| 硬件名 | 环境参数 | 示例 | | ||
| --------------- | -------- | ------------------------------- | | ||
| EEKITH3版本网关 | EEKITH3 | `ARCHSUPPORT=EEKITH3 rulex run` | | ||
| 树树莓派4B、4B+ | RPI4 | `ARCHSUPPORT=RPI4B rulex run` | | ||
|
||
> 警告: 默认为 EEKITH3, 如果你自己需要定制,最好针对自己的硬件进行跨平台适配, 如果没有指定平台,可能会导致预料之外的结果。 |
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,46 @@ | ||
package rulexlib | ||
|
||
import ( | ||
"github.com/i4de/rulex/typex" | ||
"github.com/i4de/rulex/vendor3rd" | ||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
/* | ||
* | ||
* 读GPIO, lua的函数调用应该是这样: eekit:GPIOGet(pin) -> v,error | ||
* | ||
*/ | ||
func RASPI4_GPIOGet(rx typex.RuleX) func(*lua.LState) int { | ||
return func(l *lua.LState) int { | ||
pin := l.ToNumber(2) | ||
v, e := vendor3rd.RASPI4_GPIOGet(int(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的函数调用应该是这样: eekit:GPIOSet(pin, v) -> error | ||
* | ||
*/ | ||
func RASPI4_GPIOSet(rx typex.RuleX) func(*lua.LState) int { | ||
return func(l *lua.LState) int { | ||
pin := l.ToNumber(2) | ||
value := l.ToNumber(3) | ||
_, e := vendor3rd.RASPI4_GPIOSet(int(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
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,18 @@ | ||
package vendor3rd | ||
|
||
import ( | ||
"github.com/i4de/rulex/archsupport" | ||
) | ||
|
||
/* | ||
* | ||
* 跨平台支持 | ||
* | ||
*/ | ||
|
||
func RASPI4_GPIOSet(pin, value int) (bool, error) { | ||
return archsupport.RASPI4_GPIOSet(pin, value) | ||
} | ||
func RASPI4_GPIOGet(pin int) (int, error) { | ||
return archsupport.RASPI4_GPIOGet(pin) | ||
} |