Dawn Framework 1.0
Universal data acquisition framework for embedded systems
encoder.cxx
1// dawn/src/porting/nuttx/encoder.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/porting/encoder.hxx"
7
8#include <errno.h>
9#include <fcntl.h>
10#include <nuttx/sensors/qencoder.h>
11#include <sys/ioctl.h>
12#include <unistd.h>
13
14#include "dawn/debug.hxx"
15
16int encoder_open(const char *path)
17{
18 int fd = open(path, O_RDWR);
19 DAWNINFO("ENC: open %s %d\n", path, fd);
20 if (fd < 0)
21 {
22 DAWNERR("Failed to open encoder file %s (error %d)\n", path, fd);
23 return -EIO;
24 }
25
26 return fd;
27}
28
29void encoder_close(int fd)
30{
31 if (fd >= 0)
32 {
33 close(fd);
34 }
35}
36
37int encoder_read_position(int fd, int32_t *pos)
38{
39 int ret = ioctl(fd, QEIOC_POSITION, reinterpret_cast<unsigned long>(pos));
40 if (ret < 0)
41 {
42 DAWNERR("QEIOC_POSITION failed %d\n", -errno);
43 return -errno;
44 }
45
46 return OK;
47}
48
49int encoder_read_index(int fd, dawn::porting::encoder_index_s *index)
50{
51 struct qe_index_s idx;
52 int ret;
53
54 ret = ioctl(fd, QEIOC_GETINDEX, reinterpret_cast<unsigned long>(&idx));
55 if (ret < 0)
56 {
57 DAWNERR("QEIOC_GETINDEX failed %d\n", -errno);
58 return -errno;
59 }
60
61 index->qenc_pos = idx.qenc_pos;
62 index->indx_pos = idx.indx_pos;
63 index->indx_cnt = idx.indx_cnt;
64
65 return OK;
66}
67
68int encoder_reset(int fd)
69{
70 int ret = ioctl(fd, QEIOC_RESET, 0);
71 if (ret < 0)
72 {
73 DAWNERR("QEIOC_RESET failed %d\n", -errno);
74 return -errno;
75 }
76
77 return OK;
78}
79
80int encoder_set_posmax(int fd, uint32_t posmax)
81{
82 int ret = ioctl(fd, QEIOC_SETPOSMAX, static_cast<unsigned long>(posmax));
83 if (ret < 0)
84 {
85 DAWNERR("QEIOC_SETPOSMAX failed %d\n", -errno);
86 return -errno;
87 }
88
89 return OK;
90}
Quadrature encoder index sample.
Definition encoder.hxx:22