From e5dc60e3cfc5551c18308dd4d2dad1069c6e5f2f Mon Sep 17 00:00:00 2001 From: tangweikang Date: Sun, 11 Aug 2019 15:34:51 +0800 Subject: [PATCH] [components][drivers] update encoder driven framework --- components/drivers/include/drivers/encoder.h | 32 +++++++---------- components/drivers/misc/encoder.c | 37 ++++++++++---------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/components/drivers/include/drivers/encoder.h b/components/drivers/include/drivers/encoder.h index c6c3e4d70..57cb47e6f 100644 --- a/components/drivers/include/drivers/encoder.h +++ b/components/drivers/include/drivers/encoder.h @@ -19,20 +19,18 @@ extern "C" { #endif /* encoder control command */ -typedef enum -{ - ENCODER_INFO_GET = 0x01, /* get a encoder feature information */ - ENCODER_SWITCH_ON, /* switch on encoder */ - ENCODER_SWITCH_OFF, /* switch off encoder */ - ENCODER_COUNT_CLEAR, /* clear encoder count */ -} rt_encoder_ctrl_t; +#define ENCODER_CMD_GET_TYPE (128 + 0) /* get a encoder type information */ +#define ENCODER_CMD_ENABLE (128 + 1) /* enable encoder */ +#define ENCODER_CMD_DISABLE (128 + 2) /* disable encoder */ +#define ENCODER_CMD_CLEAR_COUNT (128 + 3) /* clear encoder count */ /* 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 */ -} rt_encoder_type_t; +}; 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); }; -/* Encoder feature information */ -struct rt_encoder_info -{ - rt_encoder_type_t type; /* the type of encoder */ -}; - -typedef struct rt_encoder_device +struct rt_encoder_device { struct rt_device parent; const struct rt_encoder_ops *ops; - const struct rt_encoder_info *info; -} rt_encoder_t; + enum rt_encoder_type type; +}; -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 } diff --git a/components/drivers/misc/encoder.c b/components/drivers/misc/encoder.c index d0b2ef952..ec799bd5f 100644 --- a/components/drivers/misc/encoder.c +++ b/components/drivers/misc/encoder.c @@ -13,9 +13,9 @@ 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) { 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) { - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; 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 { @@ -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) { - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; 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 { @@ -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) { - 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) { *(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) { rt_err_t result; - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; result = RT_EOK; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; switch (cmd) { - case ENCODER_INFO_GET: - *(struct rt_encoder_info *)args = *encoder->info; + case ENCODER_CMD_GET_TYPE: + *(enum rt_encoder_type *)args = encoder->type; break; - case ENCODER_SWITCH_ON: - case ENCODER_SWITCH_OFF: - case ENCODER_COUNT_CLEAR: + case ENCODER_CMD_ENABLE: + case ENCODER_CMD_DISABLE: + case ENCODER_CMD_CLEAR_COUNT: result = encoder->ops->control(encoder, cmd, args); break; default: @@ -105,13 +105,12 @@ const static struct rt_device_ops encoder_ops = }; #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; RT_ASSERT(encoder != RT_NULL); RT_ASSERT(encoder->ops != RT_NULL); - RT_ASSERT(encoder->info != RT_NULL); device = &(encoder->parent); -- GitLab