pulse_encoder.c
3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
/*
* Copyright (c) 2006-2021, 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_pulse_encoder_init(struct rt_device *dev)
{
struct rt_pulse_encoder_device *pulse_encoder;
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
if (pulse_encoder->ops->init)
{
return pulse_encoder->ops->init(pulse_encoder);
}
else
{
return -RT_ENOSYS;
}
}
static rt_err_t rt_pulse_encoder_open(struct rt_device *dev, rt_uint16_t oflag)
{
struct rt_pulse_encoder_device *pulse_encoder;
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
if (pulse_encoder->ops->control)
{
return pulse_encoder->ops->control(pulse_encoder, PULSE_ENCODER_CMD_ENABLE, RT_NULL);
}
else
{
return -RT_ENOSYS;
}
}
static rt_err_t rt_pulse_encoder_close(struct rt_device *dev)
{
struct rt_pulse_encoder_device *pulse_encoder;
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
if (pulse_encoder->ops->control)
{
return pulse_encoder->ops->control(pulse_encoder, PULSE_ENCODER_CMD_DISABLE, RT_NULL);
}
else
{
return -RT_ENOSYS;
}
}
static rt_size_t rt_pulse_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
{
struct rt_pulse_encoder_device *pulse_encoder;
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
if (pulse_encoder->ops->get_count)
{
*(rt_int32_t *)buffer = pulse_encoder->ops->get_count(pulse_encoder);
}
return 1;
}
static rt_err_t rt_pulse_encoder_control(struct rt_device *dev, int cmd, void *args)
{
rt_err_t result;
struct rt_pulse_encoder_device *pulse_encoder;
result = RT_EOK;
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
switch (cmd)
{
case PULSE_ENCODER_CMD_CLEAR_COUNT:
result = pulse_encoder->ops->clear_count(pulse_encoder);
break;
case PULSE_ENCODER_CMD_GET_TYPE:
*(enum rt_pulse_encoder_type *)args = pulse_encoder->type;
break;
case PULSE_ENCODER_CMD_ENABLE:
case PULSE_ENCODER_CMD_DISABLE:
result = pulse_encoder->ops->control(pulse_encoder, cmd, args);
break;
default:
result = -RT_ENOSYS;
break;
}
return result;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops pulse_encoder_ops =
{
rt_pulse_encoder_init,
rt_pulse_encoder_open,
rt_pulse_encoder_close,
rt_pulse_encoder_read,
RT_NULL,
rt_pulse_encoder_control
};
#endif
rt_err_t rt_device_pulse_encoder_register(struct rt_pulse_encoder_device *pulse_encoder, const char *name, void *user_data)
{
struct rt_device *device;
RT_ASSERT(pulse_encoder != RT_NULL);
RT_ASSERT(pulse_encoder->ops != RT_NULL);
device = &(pulse_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 = &pulse_encoder_ops;
#else
device->init = rt_pulse_encoder_init;
device->open = rt_pulse_encoder_open;
device->close = rt_pulse_encoder_close;
device->read = rt_pulse_encoder_read;
device->write = RT_NULL;
device->control = rt_pulse_encoder_control;
#endif
device->user_data = user_data;
return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE);
}