[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
/* 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
}

View File

@ -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);