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


phidgets_api
Author(s): Tully Foote, Ivan Dryanovski
autogenerated on Tue May 7 2019 03:19:24