source: trunk/phidget_ik/include/phidget_ik/phidget_ik.h @ 49

Last change on this file since 49 was 3, checked in by wcaarls, 12 years ago

Published phidget_ik at revision 840

File size: 7.2 KB
Line 
1/**
2 * \file phidget_ik.h
3 * \brief PhidgetInterfaceKit interface class header file
4 *
5 * Based on the TextLCD class by Tully Foote.
6 * Most member documentation was copied from the Phidget API manual.
7 *
8 * \author Wouter Caarls <w.caarls@tudelft.nl>
9 */
10
11#include <phidgetspp.hh>
12
13/// Class for interfacing with the PhidgetInterfaceKit
14/** If you wish to use the InterfaceKit API's callback functions,
15 *  you should subclass this and override the Handler functions:
16 *  attachHandler(), inputChangeHandler(), outputChangeHandler() and sensorChangeHandler().
17 */
18class PhidgetIK : public Phidget
19{
20  public:
21    /// The constructor for the PhidgetInterfaceKit.
22    /** Create a new PhidgetIK interface class */
23        PhidgetIK() :
24          Phidget((CPhidgetHandle*)&ik_handle_),
25          ik_handle_(0)
26        {
27      last_error_ = CPhidgetInterfaceKit_create(&ik_handle_);
28     
29      if (!last_error_)
30      {
31        CPhidget_set_OnAttach_Handler((CPhidgetHandle)ik_handle_, PhidgetIK::attachDelegate, this);
32        CPhidgetInterfaceKit_set_OnInputChange_Handler(ik_handle_, PhidgetIK::inputChangeDelegate, this);
33        CPhidgetInterfaceKit_set_OnOutputChange_Handler(ik_handle_, PhidgetIK::outputChangeDelegate, this);
34        last_error_ = CPhidgetInterfaceKit_set_OnSensorChange_Handler(ik_handle_, PhidgetIK::sensorChangeDelegate, this);
35      }
36        }
37       
38    /// Initialize and connect to a device.
39        /** This will connect to any or a specific PhidgetInterfaceKit
40        @param serial_number The serial number to which to connect (-1 for any) */
41    int init(int serial_number)
42    {
43      return (last_error_ = open(serial_number));
44    }
45   
46    /// Gets the number of digital inputs supported by this board.
47    int getInputCount()
48    {
49      int count=-1;
50     
51      last_error_ = CPhidgetInterfaceKit_getInputCount(ik_handle_, &count);
52     
53      return count;
54    }
55   
56    /// Gets the state of a digital input.
57    int getInputState(int index)
58    {
59      int state=-1;
60     
61      last_error_ = CPhidgetInterfaceKit_getInputState(ik_handle_, index, &state);
62     
63      return state;
64    }
65   
66    /// Gets the number of digital outputs supported by this board.
67    int getOutputCount()
68    {
69      int count=-1;
70     
71      last_error_ = CPhidgetInterfaceKit_getOutputCount(ik_handle_, &count);
72     
73      return count;
74    }
75   
76    /// Gets the state of a digital output.
77    int getOutputState(int index)
78    {
79      int state=-1;
80     
81      last_error_ = CPhidgetInterfaceKit_getOutputState(ik_handle_, index, &state);
82     
83      return state;
84    }
85   
86    /// Sets the state of a digital output.
87    int setOutputState(int index, int state)
88    {
89      return (last_error_ = CPhidgetInterfaceKit_setOutputState(ik_handle_, index, state));
90    }
91
92    /// Gets the number of sensor (analog) inputs supported by this board.
93    int getSensorCount()
94    {
95      int count=-1;
96     
97      last_error_ = CPhidgetInterfaceKit_getSensorCount(ik_handle_, &count);
98     
99      return count;
100    }
101   
102    /// Gets a sensor value (0-1000).
103    int getSensorValue(int index)
104    {
105      int value=-1;
106     
107      last_error_ = CPhidgetInterfaceKit_getSensorValue(ik_handle_, index, &value);
108     
109      return value;
110    }
111   
112    /// Gets a sensor raw value (12-bit).
113    int getSensorRawValue(int index)
114    {
115      int value=-1;
116     
117      last_error_ = CPhidgetInterfaceKit_getSensorRawValue(ik_handle_, index, &value);
118     
119      return value;
120    }
121
122    /// Gets a sensor change trigger.
123    int getSensorChangeTrigger(int index)
124    {
125      int trigger=-1;
126     
127      last_error_ = CPhidgetInterfaceKit_getSensorChangeTrigger(ik_handle_, index, &trigger);
128     
129      return trigger;
130    }
131   
132    /// Sets a sensor change trigger.
133    int setSensorChangeTrigger(int index, int trigger)
134    {
135      return (last_error_ = CPhidgetInterfaceKit_setSensorChangeTrigger(ik_handle_, index, trigger));
136    }
137   
138    /// Gets the ratiometric state for this board.
139    int getRatiometric()
140    {
141      int ratiometric=-1;
142     
143      last_error_ = CPhidgetInterfaceKit_getRatiometric(ik_handle_, &ratiometric);
144     
145      return ratiometric;
146    }
147   
148    /// Sets the ratiometric state for this board.
149    int setRatiometric(int ratiometric)
150    {
151      return (last_error_ = CPhidgetInterfaceKit_setRatiometric(ik_handle_, ratiometric));
152    }
153   
154    /// Gets the Data Rate for an analog input.
155    int getDataRate(int index)
156    {
157      int datarate=-1;
158     
159      last_error_ = CPhidgetInterfaceKit_getDataRate(ik_handle_, index, &datarate);
160     
161      return datarate;
162    }
163   
164    /// Sets the Data Rate (in ms) for an analog input.
165    int setDataRate(int index, int datarate)
166    {
167      return (last_error_ = CPhidgetInterfaceKit_setDataRate(ik_handle_, index, datarate));
168    }
169   
170    /// Gets the maximum supported data rate (in ms) for an analog input.
171    int getDataRateMax(int index)
172    {
173      int max=-1;
174     
175      last_error_ = CPhidgetInterfaceKit_getDataRateMax(ik_handle_, index, &max);
176     
177      return max;
178    }
179   
180    /// Gets the minimum supported data rate (in ms) for an analog input.
181    int getDataRateMin(int index)
182    {
183      int min=-1;
184     
185      last_error_ = CPhidgetInterfaceKit_getDataRateMin(ik_handle_, index, &min);
186     
187      return min;
188    }
189   
190    /// Returns the last error generated by a CPhidget function.
191    int getLastError()
192    {
193      return last_error_;
194    }
195
196  protected:
197    /// Storage for the CPhidget handle.
198    CPhidgetInterfaceKitHandle ik_handle_;
199   
200    /// Last error generated by a CPhidget function.
201    int last_error_;
202   
203    /// This is called when a PhidgetInterfaceKit is attached
204    /** For overriding. */
205    virtual int attachHandler()
206    {
207      return 0;
208    };
209
210    /// This is called when a digital input changes.
211    /** For overriding. */
212    virtual int inputChangeHandler(int index, int inputState)
213    {
214      return 0;
215    }
216   
217    /// This is called when a digital output changes.
218    /** For overriding. */
219    virtual int outputChangeHandler(int index, int outputState)
220    {
221      return 0;
222    }
223   
224    /// This is called when a sensor value changes by more then the change trigger.
225    /** For overriding. */
226    virtual int sensorChangeHandler(int index, int sensorValue)
227    {
228      return 0;
229    }
230   
231  private:
232    /// Delegate for calling attachHandler method.
233    static int attachDelegate(CPhidgetHandle phid, void *userptr)
234    {
235      return ((PhidgetIK*)userptr)->attachHandler();
236    };
237
238    /// Delegate for calling inputChangeHandler method.
239    static int inputChangeDelegate(CPhidgetInterfaceKitHandle phid, void *userPtr, int index, int inputState)
240    {
241      return ((PhidgetIK*)userPtr)->inputChangeHandler(index, inputState);
242    }
243   
244    /// Delegate for calling outputChangeHandler method.
245    static int outputChangeDelegate(CPhidgetInterfaceKitHandle phid, void *userPtr, int index, int outputState)
246    {
247      return ((PhidgetIK*)userPtr)->outputChangeHandler(index, outputState);
248    }
249   
250    /// Delegate for calling sensorChangeHandler method.
251    static int sensorChangeDelegate(CPhidgetInterfaceKitHandle phid, void *userPtr, int index, int sensorValue)
252    {
253      return ((PhidgetIK*)userPtr)->sensorChangeHandler(index, sensorValue);
254    }
255};
Note: See TracBrowser for help on using the repository browser.