[components][drivers] update encoder driven framework

This commit is contained in:
tangweikang 2019-08-11 15:34:51 +08:00
parent cc565da5a1
commit e5dc60e3cf
2 changed files with 30 additions and 39 deletions

View File

@ -19,20 +19,18 @@ extern "C" {
#endif #endif
/* encoder control command */ /* encoder control command */
typedef enum #define ENCODER_CMD_GET_TYPE (128 + 0) /* get a encoder type information */
{ #define ENCODER_CMD_ENABLE (128 + 1) /* enable encoder */
ENCODER_INFO_GET = 0x01, /* get a encoder feature information */ #define ENCODER_CMD_DISABLE (128 + 2) /* disable encoder */
ENCODER_SWITCH_ON, /* switch on encoder */ #define ENCODER_CMD_CLEAR_COUNT (128 + 3) /* clear encoder count */
ENCODER_SWITCH_OFF, /* switch off encoder */
ENCODER_COUNT_CLEAR, /* clear encoder count */
} rt_encoder_ctrl_t;
/* encoder type */ /* encoder type */
typedef enum enum rt_encoder_type
{ {
SINGLE_PHASE_ENCODER = 0x01, /* single phase encoder */ UNKNOWN_ENCODER_TYPE = 0x00, /* Unknown encoder type */
SINGLE_PHASE_ENCODER, /* single phase encoder */
AB_PHASE_ENCODER /* two phase encoder */ AB_PHASE_ENCODER /* two phase encoder */
} rt_encoder_type_t; };
struct rt_encoder_device; struct rt_encoder_device;
@ -43,20 +41,14 @@ struct rt_encoder_ops
rt_err_t (*control)(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args); rt_err_t (*control)(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args);
}; };
/* Encoder feature information */ struct rt_encoder_device
struct rt_encoder_info
{
rt_encoder_type_t type; /* the type of encoder */
};
typedef struct rt_encoder_device
{ {
struct rt_device parent; struct rt_device parent;
const struct rt_encoder_ops *ops; const struct rt_encoder_ops *ops;
const struct rt_encoder_info *info; enum rt_encoder_type type;
} rt_encoder_t; };
rt_err_t rt_device_encoder_register(rt_encoder_t *encoder, const char *name, void *user_data); rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -13,9 +13,9 @@
static rt_err_t rt_encoder_init(struct rt_device *dev) static rt_err_t rt_encoder_init(struct rt_device *dev)
{ {
rt_encoder_t *encoder; struct rt_encoder_device *encoder;
encoder = (rt_encoder_t *)dev; encoder = (struct rt_encoder_device *)dev;
if (encoder->ops->init) if (encoder->ops->init)
{ {
return encoder->ops->init(encoder); return encoder->ops->init(encoder);
@ -28,12 +28,12 @@ static rt_err_t rt_encoder_init(struct rt_device *dev)
static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag) static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag)
{ {
rt_encoder_t *encoder; struct rt_encoder_device *encoder;
encoder = (rt_encoder_t *)dev; encoder = (struct rt_encoder_device *)dev;
if (encoder->ops->control) if (encoder->ops->control)
{ {
return encoder->ops->control(encoder, ENCODER_SWITCH_ON, RT_NULL); return encoder->ops->control(encoder, ENCODER_CMD_ENABLE, RT_NULL);
} }
else else
{ {
@ -43,12 +43,12 @@ static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag)
static rt_err_t rt_encoder_close(struct rt_device *dev) static rt_err_t rt_encoder_close(struct rt_device *dev)
{ {
rt_encoder_t *encoder; struct rt_encoder_device *encoder;
encoder = (rt_encoder_t *)dev; encoder = (struct rt_encoder_device *)dev;
if (encoder->ops->control) if (encoder->ops->control)
{ {
return encoder->ops->control(encoder, ENCODER_SWITCH_OFF, RT_NULL); return encoder->ops->control(encoder, ENCODER_CMD_DISABLE, RT_NULL);
} }
else else
{ {
@ -58,9 +58,9 @@ static rt_err_t rt_encoder_close(struct rt_device *dev)
static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size) static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
{ {
rt_encoder_t *encoder; struct rt_encoder_device *encoder;
encoder = (rt_encoder_t *)dev; encoder = (struct rt_encoder_device *)dev;
if (encoder->ops->get_count) if (encoder->ops->get_count)
{ {
*(rt_int32_t *)buffer = encoder->ops->get_count(encoder); *(rt_int32_t *)buffer = encoder->ops->get_count(encoder);
@ -71,18 +71,18 @@ static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buff
static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args) static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args)
{ {
rt_err_t result; rt_err_t result;
rt_encoder_t *encoder; struct rt_encoder_device *encoder;
result = RT_EOK; result = RT_EOK;
encoder = (rt_encoder_t *)dev; encoder = (struct rt_encoder_device *)dev;
switch (cmd) switch (cmd)
{ {
case ENCODER_INFO_GET: case ENCODER_CMD_GET_TYPE:
*(struct rt_encoder_info *)args = *encoder->info; *(enum rt_encoder_type *)args = encoder->type;
break; break;
case ENCODER_SWITCH_ON: case ENCODER_CMD_ENABLE:
case ENCODER_SWITCH_OFF: case ENCODER_CMD_DISABLE:
case ENCODER_COUNT_CLEAR: case ENCODER_CMD_CLEAR_COUNT:
result = encoder->ops->control(encoder, cmd, args); result = encoder->ops->control(encoder, cmd, args);
break; break;
default: default:
@ -105,13 +105,12 @@ const static struct rt_device_ops encoder_ops =
}; };
#endif #endif
rt_err_t rt_device_encoder_register(rt_encoder_t *encoder, const char *name, void *user_data) rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data)
{ {
struct rt_device *device; struct rt_device *device;
RT_ASSERT(encoder != RT_NULL); RT_ASSERT(encoder != RT_NULL);
RT_ASSERT(encoder->ops != RT_NULL); RT_ASSERT(encoder->ops != RT_NULL);
RT_ASSERT(encoder->info != RT_NULL);
device = &(encoder->parent); device = &(encoder->parent);