encoder.cpp
Go to the documentation of this file.
1 #include "phidgets_api/encoder.h"
2 
3 #include <cassert>
4 
5 namespace phidgets {
6 
7 Encoder::Encoder() : Phidget(), encoder_handle_(nullptr)
8 {
9  // create the handle
10  CPhidgetEncoder_create(&encoder_handle_);
11 
12  // pass handle to base class
13  Phidget::init((CPhidgetHandle)encoder_handle_);
14 
15  // register base class callbacks
17 
18  // register encoder data callbacks
19  CPhidgetEncoder_set_OnInputChange_Handler(encoder_handle_,
20  InputChangeHandler, this);
21  CPhidgetEncoder_set_OnPositionChange_Handler(encoder_handle_,
22  PositionChangeHandler, this);
23 }
24 
26 {
27 }
28 
30 {
31  int count;
32  int ret = CPhidgetEncoder_getInputCount(encoder_handle_, &count);
33 
34  assert(ret == EPHIDGET_OK);
35 
36  return count;
37 }
38 
39 bool Encoder::getInputState(int index)
40 {
41  int state;
42  int ret = CPhidgetEncoder_getInputState(encoder_handle_, index, &state);
43 
44  assert(ret == EPHIDGET_OK);
45 
46  return state == PTRUE;
47 }
48 
50 {
51  int count;
52  int ret = CPhidgetEncoder_getEncoderCount(encoder_handle_, &count);
53 
54  assert(ret == EPHIDGET_OK);
55 
56  return count;
57 }
58 
59 int Encoder::getPosition(int index)
60 {
61  int position;
62  int ret = CPhidgetEncoder_getPosition(encoder_handle_, index, &position);
63 
64  assert(ret == EPHIDGET_OK);
65 
66  return position;
67 }
68 
69 void Encoder::setPosition(int index, int position)
70 {
71  int ret = CPhidgetEncoder_setPosition(encoder_handle_, index, position);
72 
73  assert(ret == EPHIDGET_OK);
74 }
75 
77 {
78  int position;
79  int ret =
80  CPhidgetEncoder_getIndexPosition(encoder_handle_, index, &position);
81 
82  // Encoder does not support indexing or index has not occured
83  if (ret == EPHIDGET_UNKNOWNVAL) return 0;
84 
85  assert(ret == EPHIDGET_OK);
86 
87  return position;
88 }
89 
90 bool Encoder::getEnabled(int index)
91 {
92  int enabled;
93  int ret = CPhidgetEncoder_getEnabled(encoder_handle_, index, &enabled);
94 
95  assert(ret == EPHIDGET_OK);
96 
97  return enabled == PTRUE;
98 }
99 
100 void Encoder::setEnabled(int index, bool enabled)
101 {
102  int ret = CPhidgetEncoder_setEnabled(encoder_handle_, index,
103  enabled ? PTRUE : PFALSE);
104 
105  assert(ret == EPHIDGET_OK);
106 }
107 
108 int Encoder::InputChangeHandler(CPhidgetEncoderHandle /* phid */, void *userPtr,
109  int index, int inputState)
110 {
111  ((Encoder *)userPtr)->inputChangeHandler(index, inputState);
112  return 0;
113 }
114 
115 int Encoder::PositionChangeHandler(CPhidgetEncoderHandle /* phid */,
116  void *userPtr, int index, int time,
117  int positionChange)
118 {
119  ((Encoder *)userPtr)->positionChangeHandler(index, time, positionChange);
120  return 0;
121 }
122 
123 void Encoder::inputChangeHandler(int /* index */, int /* inputState */)
124 {
125  // This method can be overridden in a concrete subclass (e.g., ROS wrapper)
126 }
127 
128 void Encoder::positionChangeHandler(int /* index */, int /* time */,
129  int /* positionChange */)
130 {
131  // This method can be overridden in a concrete subclass (e.g., ROS wrapper)
132 }
133 
134 } // namespace phidgets
CPhidgetEncoderHandle encoder_handle_
Definition: encoder.h:60
static int InputChangeHandler(CPhidgetEncoderHandle phid, void *userPtr, int index, int inputState)
Definition: encoder.cpp:108
void registerHandlers()
Definition: phidget.cpp:17
int getInputCount()
Gets the number of digital input channels supported by this board.
Definition: encoder.cpp:29
int getIndexPosition(int index)
Gets the position of an encoder the last time an index pulse occured. An index pulse in this context ...
Definition: encoder.cpp:76
virtual void inputChangeHandler(int index, int inputState)
Definition: encoder.cpp:123
void setEnabled(int index, bool enabled)
Set the powered state of an encoder. If an encoder is not enabled, it will not be given power...
Definition: encoder.cpp:100
int getEncoderCount()
Gets the number of encoder input channels supported by this board.
Definition: encoder.cpp:49
int getPosition(int index)
Reads the current position of an encoder.
Definition: encoder.cpp:59
static int PositionChangeHandler(CPhidgetEncoderHandle phid, void *userPtr, int index, int time, int positionChange)
Definition: encoder.cpp:115
bool getInputState(int index)
Reads the current state of a digital input.
Definition: encoder.cpp:39
virtual ~Encoder()
Definition: encoder.cpp:25
void setPosition(int index, int position)
Sets the offset of an encoder such that current position is the specified value.
Definition: encoder.cpp:69
void init(CPhidgetHandle handle)
Definition: phidget.cpp:24
bool getEnabled(int index)
Checks if an encoder is powered on and receiving events.
Definition: encoder.cpp:90
virtual void positionChangeHandler(int index, int time, int positionChange)
Definition: encoder.cpp:128


phidgets_api
Author(s): Tully Foote, Ivan Dryanovski
autogenerated on Fri Apr 9 2021 02:56:02