!170 PikaStdDevice_v1.11.7_1668967321013

Merge pull request !170 from pikabot/PikaStdDevice_v1.11.7_1668967321013
This commit is contained in:
李昂 2022-11-20 18:04:10 +00:00 committed by Gitee
commit 9dc9426c6f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 85 additions and 48 deletions

View File

@ -9,6 +9,7 @@ void PikaStdDevice_GPIO_init(PikaObj* self) {
obj_setInt(self, "isOn", 0); obj_setInt(self, "isOn", 0);
obj_setStr(self, "pull", "none"); obj_setStr(self, "pull", "none");
obj_setInt(self, "id", -999); obj_setInt(self, "id", -999);
obj_setPtr(self, "pika_dev", NULL);
} }
int PikaStdDevice_GPIO_getId(PikaObj* self) { int PikaStdDevice_GPIO_getId(PikaObj* self) {
@ -76,38 +77,70 @@ void PikaStdDevice_GPIO_setPull(PikaObj* self, char* pull) {
} }
} }
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;
}
void PikaStdDevice_GPIO_setPin(PikaObj* self, char* pinName) { void PikaStdDevice_GPIO_setPin(PikaObj* self, char* pinName) {
obj_setStr(self, "pin", pinName); obj_setStr(self, "pin", pinName);
} }
void PikaStdDevice_GPIO_platformDisable(PikaObj* self) { void PikaStdDevice_GPIO_platformDisable(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR(); pika_dev* dev = _get_dev(self);
pika_hal_ioctl(dev, PIKA_IOCTL_DISABLE);
} }
void PikaStdDevice_GPIO_platformEnable(PikaObj* self) { void PikaStdDevice_GPIO_platformEnable(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR(); pika_dev* dev = _get_dev(self);
pika_hal_ioctl(dev, PIKA_IOCTL_ENABLE);
} }
void PikaStdDevice_GPIO_platformLow(PikaObj* self) { void PikaStdDevice_GPIO_platformLow(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR(); pika_dev* dev = _get_dev(self);
uint32_t val = 0;
pika_hal_write(dev, &val, sizeof(val));
} }
void PikaStdDevice_GPIO_platformHigh(PikaObj* self) { void PikaStdDevice_GPIO_platformHigh(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR(); pika_dev* dev = _get_dev(self);
uint32_t val = 1;
pika_hal_write(dev, &val, sizeof(val));
} }
void PikaStdDevice_GPIO_platformSetMode(PikaObj* self) { void PikaStdDevice_GPIO_platformSetMode(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR(); char* mode = obj_getStr(self, "mode");
} pika_GPIO_config cfg = {0};
if(strEqu(mode, "in")){
void PikaStdDevice_GPIO_platformOff(PikaObj* self) { cfg.dir = PIKA_GPIO_DIR_IN;
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR(); }else{
} cfg.dir = PIKA_GPIO_DIR_OUT;
}
void PikaStdDevice_GPIO_platformOn(PikaObj* self) { pika_dev* dev = _get_dev(self);
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR(); char* pull = obj_getStr(self, "pull");
if (strEqu(pull, "up")){
cfg.pull = PIKA_GPIO_PULL_UP;
}
if (strEqu(pull, "down")){
cfg.pull = PIKA_GPIO_PULL_DOWN;
}
if (strEqu(pull, "none")) {
cfg.pull = PIKA_GPIO_PULL_NONE;
}
pika_hal_ioctl(dev, PIKA_IOCTL_CONFIG, &cfg);
} }
void PikaStdDevice_GPIO_platformRead(PikaObj* self) { void PikaStdDevice_GPIO_platformRead(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR(); pika_dev* dev = _get_dev(self);
uint32_t val = 0;
pika_hal_read(dev, &val, sizeof(val));
obj_setInt(self, "readBuff", val);
} }

View File

@ -4,7 +4,7 @@
#define PIKA_HAL_TABLE_FILE_API #define PIKA_HAL_TABLE_FILE_API
#include "pika_hal_table.h" #include "pika_hal_table.h"
static const pika_dev_impl pika_dev_impl_list[_PIKA_DEV_TYPE_MAX] = { static const pika_dev_impl pika_dev_impl_list[] = {
#define PIKA_HAL_TABLE_IMPL #define PIKA_HAL_TABLE_IMPL
#include "pika_hal_table.h" #include "pika_hal_table.h"
}; };
@ -18,10 +18,12 @@ static pika_dev_impl* _pika_dev_get_impl(pika_dev_type type) {
pika_dev* pika_hal_open(pika_dev_type dev_type, char* name) { pika_dev* pika_hal_open(pika_dev_type dev_type, char* name) {
if (dev_type >= _PIKA_DEV_TYPE_MAX) { if (dev_type >= _PIKA_DEV_TYPE_MAX) {
__platform_printf("Error: dev_type invalied.\r\n");
return NULL; return NULL;
} }
pika_dev_impl* impl = _pika_dev_get_impl(dev_type); pika_dev_impl* impl = _pika_dev_get_impl(dev_type);
if (impl->open == NULL) { if (impl->open == NULL) {
__platform_printf("Error: dev_open_impl not implamented.\r\n");
return NULL; return NULL;
} }
pika_dev* dev = (pika_dev*)pikaMalloc(sizeof(pika_dev)); pika_dev* dev = (pika_dev*)pikaMalloc(sizeof(pika_dev));
@ -29,8 +31,9 @@ pika_dev* pika_hal_open(pika_dev_type dev_type, char* name) {
return NULL; return NULL;
} }
dev->type = dev_type; dev->type = dev_type;
if (impl->open(dev) != 0) { if (impl->open(dev, name) != 0) {
pikaFree(dev, sizeof(pika_dev)); pikaFree(dev, sizeof(pika_dev));
__platform_printf("Error: dev_open failed.\r\n");
return NULL; return NULL;
} }
return dev; return dev;
@ -49,7 +52,7 @@ int pika_hal_close(pika_dev* dev) {
return ret; return ret;
} }
int pika_hal_read(pika_dev* dev, uint8_t* buf, size_t len) { int pika_hal_read(pika_dev* dev, void* buf, size_t len) {
if (dev == NULL) { if (dev == NULL) {
return -1; return -1;
} }
@ -60,7 +63,7 @@ int pika_hal_read(pika_dev* dev, uint8_t* buf, size_t len) {
return impl->read(dev, buf, len); return impl->read(dev, buf, len);
} }
int pika_hal_write(pika_dev* dev, uint8_t* buf, size_t len) { int pika_hal_write(pika_dev* dev, void* buf, size_t len) {
if (dev == NULL) { if (dev == NULL) {
return -1; return -1;
} }
@ -71,20 +74,20 @@ int pika_hal_write(pika_dev* dev, uint8_t* buf, size_t len) {
return impl->write(dev, buf, len); return impl->write(dev, buf, len);
} }
static const int _pika_hal_cmd_arg_cnt[_PIKA_DEV_CMD_MAX] = { static const int _pika_hal_cmd_arg_cnt[_PIKA_IOCTL_MAX] = {
[PIKA_DEV_CMD_ENABLE] = 0, [PIKA_IOCTL_ENABLE] = 0,
[PIKA_DEV_CMD_DISABLE] = 0, [PIKA_IOCTL_DISABLE] = 0,
[PIKA_DEV_CMD_CONFIG] = 1, [PIKA_IOCTL_CONFIG] = 1,
}; };
static int _pika_hal_get_arg_cnt(pika_dev_cmd cmd) { static int _pika_hal_get_arg_cnt(pika_ioctl_cmd cmd) {
if (cmd >= _PIKA_DEV_CMD_MAX) { if (cmd >= _PIKA_IOCTL_MAX) {
return -1; return -1;
} }
return _pika_hal_cmd_arg_cnt[cmd]; return _pika_hal_cmd_arg_cnt[cmd];
} }
int pika_hal_ioctl(pika_dev* dev, pika_dev_cmd cmd, ...) { int pika_hal_ioctl(pika_dev* dev, pika_ioctl_cmd cmd, ...) {
if (dev == NULL) { if (dev == NULL) {
return -1; return -1;
} }

View File

@ -4,8 +4,8 @@
#include <stdint.h> #include <stdint.h>
/* /*
* pika_dev_hal is a C HAL lib for PikaScript modules. * pika_hal is a C Device HAL lib for PikaScript modules.
* the api of pika_dev_hal is similar to the api of posix file API. * the API of pika_hal is similar to the posix file API.
*/ */
typedef enum pika_dev_type { typedef enum pika_dev_type {
@ -14,12 +14,12 @@ typedef enum pika_dev_type {
_PIKA_DEV_TYPE_MAX, _PIKA_DEV_TYPE_MAX,
} pika_dev_type; } pika_dev_type;
typedef enum pika_dev_cmd { typedef enum pika_ioctl_cmd {
PIKA_DEV_CMD_ENABLE, PIKA_IOCTL_ENABLE,
PIKA_DEV_CMD_DISABLE, PIKA_IOCTL_DISABLE,
PIKA_DEV_CMD_CONFIG, PIKA_IOCTL_CONFIG,
_PIKA_DEV_CMD_MAX, _PIKA_IOCTL_MAX,
} pika_dev_cmd; } pika_ioctl_cmd;
typedef enum pika_gpio_dir { typedef enum pika_gpio_dir {
PIKA_GPIO_DIR_IN, PIKA_GPIO_DIR_IN,
@ -60,18 +60,18 @@ typedef struct pika_dev {
} pika_dev; } pika_dev;
typedef struct pika_dev_impl { typedef struct pika_dev_impl {
int (*open)(pika_dev* dev); int (*open)(pika_dev* dev, char* name);
int (*close)(pika_dev* dev); int (*close)(pika_dev* dev);
int (*read)(pika_dev* dev, uint8_t* buf, size_t count); int (*read)(pika_dev* dev, void* buf, size_t count);
int (*write)(pika_dev* dev, uint8_t* buf, size_t count); int (*write)(pika_dev* dev, void* buf, size_t count);
int (*ioctl)(pika_dev* dev, pika_dev_cmd cmd, uintptr_t cfg); int (*ioctl)(pika_dev* dev, pika_ioctl_cmd cmd, uintptr_t cfg);
} pika_dev_impl; } pika_dev_impl;
/* posix file like API */ /* posix file like API */
pika_dev* pika_hal_open(pika_dev_type dev_type, char* name); pika_dev* pika_hal_open(pika_dev_type dev_type, char* name);
int pika_hal_close(pika_dev* dev); int pika_hal_close(pika_dev* dev);
int pika_hal_read(pika_dev* dev, uint8_t* buf, size_t len); int pika_hal_read(pika_dev* dev, void* buf, size_t len);
int pika_hal_write(pika_dev* dev, uint8_t* buf, size_t len); int pika_hal_write(pika_dev* dev, void* buf, size_t len);
int PIKA_HAL_TABLE_FILE_API(pika_dev* dev, pika_dev_cmd cmd, ...); int pika_hal_ioctl(pika_dev* dev, pika_ioctl_cmd cmd, ...);
#endif #endif

View File

@ -1,7 +1,7 @@
#undef pika_hal_table_add #undef pika_hal_table_add
#if defined(PIKA_HAL_TABLE_FILE_API) #if defined(PIKA_HAL_TABLE_FILE_API)
#define pika_hal_table_add(dev_name) \ #define pika_hal_table_add(dev_name) \
PIKA_WEAK int pika_##dev_name##_open(pika_dev* dev) { \ PIKA_WEAK int pika_##dev_name##_open(pika_dev* dev, char* name) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \ WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \ return -1; \
} \ } \
@ -9,12 +9,12 @@
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \ WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \ return -1; \
} \ } \
PIKA_WEAK int pika_##dev_name##_read(pika_dev* dev, uint8_t* buf, \ PIKA_WEAK int pika_##dev_name##_read(pika_dev* dev, void* buf, \
size_t len) { \ size_t len) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \ WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \ return -1; \
} \ } \
PIKA_WEAK int pika_##dev_name##_write(pika_dev* dev, uint8_t* buf, \ PIKA_WEAK int pika_##dev_name##_write(pika_dev* dev, void* buf, \
size_t len) { \ size_t len) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \ WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \ return -1; \
@ -32,17 +32,17 @@
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \ WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \ return -1; \
} \ } \
int pika_##dev_name##_ioctl(pika_dev* dev, pika_dev_cmd cmd, \ int pika_##dev_name##_ioctl(pika_dev* dev, pika_ioctl_cmd cmd, \
uintptr_t arg) { \ uintptr_t arg) { \
if (NULL == dev) { \ if (NULL == dev) { \
return -1; \ return -1; \
} \ } \
switch (cmd) { \ switch (cmd) { \
case PIKA_DEV_CMD_ENABLE: \ case PIKA_IOCTL_ENABLE: \
return pika_##dev_name##_ioctl_enable(dev); \ return pika_##dev_name##_ioctl_enable(dev); \
case PIKA_DEV_CMD_DISABLE: \ case PIKA_IOCTL_DISABLE: \
return pika_##dev_name##_ioctl_disable(dev); \ return pika_##dev_name##_ioctl_disable(dev); \
case PIKA_DEV_CMD_CONFIG: \ case PIKA_IOCTL_CONFIG: \
return pika_##dev_name##_ioctl_config( \ return pika_##dev_name##_ioctl_config( \
dev, (pika_##dev_name##_config*)arg); \ dev, (pika_##dev_name##_config*)arg); \
default: \ default: \

View File

@ -164,7 +164,8 @@ releases = [
"v1.10.3 11088f6b4ba3d5b545fe4bfdc8c27ab4fbe01936", "v1.10.3 11088f6b4ba3d5b545fe4bfdc8c27ab4fbe01936",
"v1.10.4 f09ea25237cd35a27fd071198ff771c8b4a088ad", "v1.10.4 f09ea25237cd35a27fd071198ff771c8b4a088ad",
"v1.11.0 f8b529a956da57d8623247bea594e65469cac1c5", "v1.11.0 f8b529a956da57d8623247bea594e65469cac1c5",
"v1.11.2 95642575a3e17e4c52e28cdbfbef900e02db61e0" "v1.11.2 95642575a3e17e4c52e28cdbfbef900e02db61e0",
"v1.11.7 b7112610bb29109fd7268bf7162004561d73daa1"
] ]
[[packages]] [[packages]]