encoder.c 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-08-08     balanceTWK   the first version
 */

#include <rtthread.h>
#include <rtdevice.h>

static rt_err_t rt_encoder_init(struct rt_device *dev)
{
16
    struct rt_encoder_device *encoder;
17

18
    encoder = (struct rt_encoder_device *)dev;
19 20 21 22 23 24 25 26 27 28 29 30
    if (encoder->ops->init)
    {
        return encoder->ops->init(encoder);
    }
    else
    {
        return -RT_ENOSYS;
    }
}

static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag)
{
31
    struct rt_encoder_device *encoder;
32

33
    encoder = (struct rt_encoder_device *)dev;
34 35
    if (encoder->ops->control)
    {
36
        return encoder->ops->control(encoder, ENCODER_CMD_ENABLE, RT_NULL);
37 38 39 40 41 42 43 44 45
    }
    else
    {
        return -RT_ENOSYS;
    }
}

static rt_err_t rt_encoder_close(struct rt_device *dev)
{
46
    struct rt_encoder_device *encoder;
47

48
    encoder = (struct rt_encoder_device *)dev;
49 50
    if (encoder->ops->control)
    {
51
        return encoder->ops->control(encoder, ENCODER_CMD_DISABLE, RT_NULL);
52 53 54 55 56 57 58 59 60
    }
    else
    {
        return -RT_ENOSYS;
    }
}

static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
{
61
    struct rt_encoder_device *encoder;
62

63
    encoder = (struct rt_encoder_device *)dev;
64 65 66 67 68 69 70 71 72 73
    if (encoder->ops->get_count)
    {
        *(rt_int32_t *)buffer = encoder->ops->get_count(encoder);
    }
    return 1;
}

static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args)
{
    rt_err_t result;
74
    struct rt_encoder_device *encoder;
75 76

    result = RT_EOK;
77
    encoder = (struct rt_encoder_device *)dev;
78 79
    switch (cmd)
    {
80 81 82
    case ENCODER_CMD_CLEAR_COUNT:
        result = encoder->ops->clear_count(encoder);
        break;
83 84
    case ENCODER_CMD_GET_TYPE:
        *(enum rt_encoder_type *)args = encoder->type;
85
        break;
86 87
    case ENCODER_CMD_ENABLE:
    case ENCODER_CMD_DISABLE:
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        result = encoder->ops->control(encoder, cmd, args);
        break;
    default:
        result = -RT_ENOSYS;
        break;
    }

    return result;
}

#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops encoder_ops =
{
    rt_encoder_init,
    rt_encoder_open,
    rt_encoder_close,
    rt_encoder_read,
    RT_NULL,
    rt_encoder_control
};
#endif

110
rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
{
    struct rt_device *device;

    RT_ASSERT(encoder != RT_NULL);
    RT_ASSERT(encoder->ops != RT_NULL);

    device = &(encoder->parent);

    device->type        = RT_Device_Class_Miscellaneous;
    device->rx_indicate = RT_NULL;
    device->tx_complete = RT_NULL;

#ifdef RT_USING_DEVICE_OPS
    device->ops         = &encoder_ops;
#else
    device->init        = rt_encoder_init;
    device->open        = rt_encoder_open;
    device->close       = rt_encoder_close;
    device->read        = rt_encoder_read;
    device->write       = RT_NULL;
    device->control     = rt_encoder_control;
#endif
    device->user_data   = user_data;

    return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE);
}