2021-10-01 20:00:15 +08:00
|
|
|
#include "PikaStdDevice_GPIO.h"
|
2022-02-26 14:40:56 +08:00
|
|
|
#include "BaseObj.h"
|
2022-11-20 22:16:41 +08:00
|
|
|
#include "pika_hal.h"
|
2021-10-01 20:00:15 +08:00
|
|
|
|
|
|
|
void PikaStdDevice_GPIO_init(PikaObj* self) {
|
|
|
|
obj_setInt(self, "isEnable", 0);
|
2022-03-19 09:49:19 +08:00
|
|
|
obj_setStr(self, "pin", "none");
|
2021-10-01 20:00:15 +08:00
|
|
|
obj_setStr(self, "mode", "out");
|
|
|
|
obj_setInt(self, "isOn", 0);
|
2021-10-25 20:16:08 +08:00
|
|
|
obj_setStr(self, "pull", "none");
|
2022-03-19 09:49:19 +08:00
|
|
|
obj_setInt(self, "id", -999);
|
2022-11-21 02:03:01 +08:00
|
|
|
obj_setPtr(self, "pika_dev", NULL);
|
2022-03-19 09:49:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int PikaStdDevice_GPIO_getId(PikaObj* self) {
|
|
|
|
return obj_getInt(self, "id");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PikaStdDevice_GPIO_setId(PikaObj* self, int id) {
|
|
|
|
obj_setInt(self, "id", id);
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-10 23:02:40 +08:00
|
|
|
void PikaStdDevice_GPIO___init__(PikaObj* self) {
|
|
|
|
PikaStdDevice_GPIO_init(self);
|
|
|
|
}
|
|
|
|
|
2021-10-01 20:00:15 +08:00
|
|
|
void PikaStdDevice_GPIO_disable(PikaObj* self) {
|
|
|
|
obj_setInt(self, "isEnable", 0);
|
2022-03-19 09:53:48 +08:00
|
|
|
obj_runNativeMethod(self, "platformDisable", NULL);
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PikaStdDevice_GPIO_enable(PikaObj* self) {
|
|
|
|
obj_setInt(self, "isEnable", 1);
|
2022-03-19 09:53:48 +08:00
|
|
|
obj_runNativeMethod(self, "platformEnable", NULL);
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* PikaStdDevice_GPIO_getMode(PikaObj* self) {
|
|
|
|
return obj_getStr(self, "mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
char* PikaStdDevice_GPIO_getPin(PikaObj* self) {
|
|
|
|
return obj_getStr(self, "pin");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PikaStdDevice_GPIO_low(PikaObj* self) {
|
|
|
|
obj_setInt(self, "isOn", 0);
|
2022-03-19 09:53:48 +08:00
|
|
|
obj_runNativeMethod(self, "platformLow", NULL);
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PikaStdDevice_GPIO_high(PikaObj* self) {
|
|
|
|
obj_setInt(self, "isOn", 1);
|
2022-03-19 09:53:48 +08:00
|
|
|
obj_runNativeMethod(self, "platformHigh", NULL);
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
|
|
|
|
2022-02-26 14:40:56 +08:00
|
|
|
int PikaStdDevice_GPIO_read(PikaObj* self) {
|
2022-03-19 09:53:48 +08:00
|
|
|
obj_runNativeMethod(self, "platformRead", NULL);
|
2021-10-25 20:16:08 +08:00
|
|
|
return obj_getInt(self, "readBuff");
|
|
|
|
}
|
|
|
|
|
2021-10-01 20:00:15 +08:00
|
|
|
void PikaStdDevice_GPIO_setMode(PikaObj* self, char* mode) {
|
2022-02-26 14:40:56 +08:00
|
|
|
if (strEqu(mode, "out") || strEqu(mode, "in")) {
|
2021-10-24 21:26:22 +08:00
|
|
|
obj_setStr(self, "mode", mode);
|
2022-03-19 09:53:48 +08:00
|
|
|
obj_runNativeMethod(self, "platformSetMode", NULL);
|
2022-02-26 14:40:56 +08:00
|
|
|
} else {
|
2021-10-24 21:26:22 +08:00
|
|
|
obj_setErrorCode(self, 1);
|
|
|
|
obj_setSysOut(self, "[error] GPIO mode should be 'out' or 'in'.");
|
|
|
|
}
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
|
|
|
|
2022-02-26 14:40:56 +08:00
|
|
|
void PikaStdDevice_GPIO_setPull(PikaObj* self, char* pull) {
|
|
|
|
if (strEqu(pull, "up") || strEqu(pull, "down") || strEqu(pull, "none")) {
|
2021-10-25 20:16:08 +08:00
|
|
|
obj_setStr(self, "pull", pull);
|
2022-02-26 14:40:56 +08:00
|
|
|
} else {
|
2021-10-25 20:16:08 +08:00
|
|
|
obj_setErrorCode(self, 1);
|
2022-02-26 14:40:56 +08:00
|
|
|
obj_setSysOut(self,
|
|
|
|
"[error] GPIO pull should be 'up', 'down' or 'none'.");
|
2021-10-25 20:16:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:24:00 +08:00
|
|
|
static pika_dev* _get_dev(PikaObj* self){
|
|
|
|
pika_dev* dev = obj_getPtr(self, "pika_dev");
|
|
|
|
if(NULL != dev){
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
dev = pika_hal_open(PIKA_HAL_GPIO, obj_getStr(self, "pin"));
|
|
|
|
if(NULL == dev){
|
|
|
|
__platform_printf("Error: open GPIO '%s' failed.\r\n", obj_getStr(self, "pin"));
|
|
|
|
}
|
|
|
|
obj_setPtr(self, "pika_dev", dev);
|
|
|
|
return dev;
|
2022-11-21 02:03:01 +08:00
|
|
|
}
|
|
|
|
|
2022-02-26 14:40:56 +08:00
|
|
|
void PikaStdDevice_GPIO_setPin(PikaObj* self, char* pinName) {
|
2021-10-01 20:00:15 +08:00
|
|
|
obj_setStr(self, "pin", pinName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PikaStdDevice_GPIO_platformDisable(PikaObj* self) {
|
2022-12-07 18:24:00 +08:00
|
|
|
pika_dev* dev = _get_dev(self);
|
|
|
|
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PikaStdDevice_GPIO_platformEnable(PikaObj* self) {
|
2022-12-07 18:24:00 +08:00
|
|
|
pika_dev* dev = _get_dev(self);
|
|
|
|
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
2022-08-16 11:05:19 +08:00
|
|
|
|
2021-10-01 20:00:15 +08:00
|
|
|
void PikaStdDevice_GPIO_platformLow(PikaObj* self) {
|
2022-12-07 18:24:00 +08:00
|
|
|
pika_dev* dev = _get_dev(self);
|
|
|
|
uint32_t val = 0;
|
|
|
|
pika_hal_write(dev, &val, sizeof(val));
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PikaStdDevice_GPIO_platformHigh(PikaObj* self) {
|
2022-12-07 18:24:00 +08:00
|
|
|
pika_dev* dev = _get_dev(self);
|
|
|
|
uint32_t val = 1;
|
|
|
|
pika_hal_write(dev, &val, sizeof(val));
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
|
|
|
|
2021-11-14 19:40:06 +08:00
|
|
|
void PikaStdDevice_GPIO_platformSetMode(PikaObj* self) {
|
2022-11-21 02:03:01 +08:00
|
|
|
char* mode = obj_getStr(self, "mode");
|
2022-11-21 14:10:05 +08:00
|
|
|
pika_hal_GPIO_config cfg = {0};
|
2022-12-07 18:24:00 +08:00
|
|
|
if(strEqu(mode, "in")){
|
2022-11-21 14:10:05 +08:00
|
|
|
cfg.dir = PIKA_HAL_GPIO_DIR_IN;
|
2022-12-07 18:24:00 +08:00
|
|
|
}else{
|
2022-11-21 14:10:05 +08:00
|
|
|
cfg.dir = PIKA_HAL_GPIO_DIR_OUT;
|
2022-11-21 02:03:01 +08:00
|
|
|
}
|
|
|
|
pika_dev* dev = _get_dev(self);
|
|
|
|
char* pull = obj_getStr(self, "pull");
|
2022-12-07 18:24:00 +08:00
|
|
|
if (strEqu(pull, "up")){
|
2022-11-21 14:10:05 +08:00
|
|
|
cfg.pull = PIKA_HAL_GPIO_PULL_UP;
|
2022-11-21 02:03:01 +08:00
|
|
|
}
|
2022-12-07 18:24:00 +08:00
|
|
|
if (strEqu(pull, "down")){
|
2022-11-21 14:10:05 +08:00
|
|
|
cfg.pull = PIKA_HAL_GPIO_PULL_DOWN;
|
2022-11-21 02:03:01 +08:00
|
|
|
}
|
|
|
|
if (strEqu(pull, "none")) {
|
2022-11-21 14:10:05 +08:00
|
|
|
cfg.pull = PIKA_HAL_GPIO_PULL_NONE;
|
2022-11-21 02:03:01 +08:00
|
|
|
}
|
2022-11-21 14:10:05 +08:00
|
|
|
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg);
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
2021-10-25 20:16:08 +08:00
|
|
|
|
2022-02-26 14:40:56 +08:00
|
|
|
void PikaStdDevice_GPIO_platformRead(PikaObj* self) {
|
2022-11-21 02:03:01 +08:00
|
|
|
pika_dev* dev = _get_dev(self);
|
|
|
|
uint32_t val = 0;
|
|
|
|
pika_hal_read(dev, &val, sizeof(val));
|
|
|
|
obj_setInt(self, "readBuff", val);
|
2022-03-19 09:49:19 +08:00
|
|
|
}
|