00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef __GLOVE_DEVICES_IMPL_CGIU_H_
00028 #define __GLOVE_DEVICES_IMPL_CGIU_H_
00029
00030 #include "../../config.hpp"
00031
00032 #include <boost/function.hpp>
00033 #include <boost/thread.hpp>
00034 #include <boost/asio.hpp>
00035 #include <boost/cstdint.hpp>
00036 #include <boost/interprocess/sync/interprocess_mutex.hpp>
00037 #include <boost/interprocess/sync/interprocess_semaphore.hpp>
00038
00039 #include <vector>
00040 #include <iostream>
00041 #include <string>
00042
00043 #include "../CyberGlove.hpp"
00044
00045
00046
00047
00048 namespace glove {
00049 namespace devices {
00050 namespace impl {
00051
00055 typedef boost::function<void(const std::vector<boost::uint8_t>&)> StreamHandler;
00056
00063 API void printByteVector(const std::vector<boost::uint8_t> v, std::ostream& os = std::cout);
00070 API void printByteVector(const boost::uint8_t* const data, const std::size_t len, std::ostream& os =
00071 std::cout);
00077 API void printSensorMask(unsigned int mask, std::ostream& os = std::cout);
00078
00082 #define BUFFER_SIZE 256
00083
00090 class API CGIU {
00091
00098 class SerialPortBuffer {
00099 boost::interprocess::interprocess_semaphore mutex;
00100
00101 public:
00105 std::vector<boost::uint8_t> data;
00106
00110 SerialPortBuffer() :
00111 mutex(1) {
00112 mutex.wait();
00113 }
00114
00118 void notify() {
00119
00120 mutex.post();
00121 }
00122
00126 void wait() {
00127 mutex.wait();
00128 }
00129 };
00130 friend struct SerialBuffer;
00131
00133 boost::asio::io_service ioService;
00135 boost::asio::serial_port* serialPort;
00137 std::string port;
00139 boost::thread* ioThread;
00140
00142 boost::interprocess::interprocess_semaphore mutex;
00144 SerialPortBuffer serialPortBuffer;
00146 boost::array<boost::uint8_t, BUFFER_SIZE> readBuffer;
00147
00151 void startReading();
00157 void readComplete(const boost::system::error_code& error, std::size_t numBytesRead);
00158
00167 void writeCommand(const std::string& cmd, const boost::uint8_t parameters[],
00168 const std::size_t numParameters);
00173 void skipToCommandHeader(const std::string& cmd);
00181 std::vector<boost::uint8_t> extractCommand(const std::string& cmd, const std::size_t cmdLength,
00182 const bool skipHeader = false);
00191 std::vector<boost::uint8_t> readCommand(const std::string& cmd, const std::size_t sizeHint,
00192 const boost::uint8_t delimiter, const bool skipHeader = false);
00193
00195 static const unsigned int timeout;
00196
00198 static const std::string INVALID_CMD;
00200 static const std::string SAMPLING_ERROR;
00202 static const std::string GLOVE_ERROR;
00203
00211 bool isValidResponse(const std::string& cmd, const std::vector<boost::uint8_t>& response,
00212 const std::size_t minimalResponseLength = 0);
00219 bool isValidSensorRecord(const std::vector<boost::uint8_t>& record, std::string* validationStr =
00220 NULL);
00221
00223 bool isStreaming_;
00225 boost::thread* streamThread;
00230 void processRecordStream(StreamHandler handler);
00231
00232 protected:
00233
00245 std::vector<boost::uint8_t>
00246 command(const std::string& cmd, std::size_t minimalResponseLength = 0);
00260 std::vector<boost::uint8_t> command(const std::string& cmd, const boost::uint8_t parameters[],
00261 const std::size_t numParameters, std::size_t minimalResponseLength = 0);
00262
00272 void set(const std::string& cmd, const bool b = true);
00283 bool getBool(const std::string& cmd);
00294 boost::uint8_t getByte(const std::string& cmd);
00307 unsigned int getBytes(const std::string& cmd, const unsigned short numBytes);
00317 std::string getString(const std::string& cmd);
00318
00319 public:
00323 CGIU();
00327 virtual ~CGIU();
00328
00335 void connect(const std::string& port);
00340 bool isConnected() const;
00345 void disconnect();
00352 const std::string& getPort();
00353
00358 void requestGloveStatus(const bool b = true);
00369 bool gloveStatusRequested();
00370
00380 void requestQuantizedValues(const bool b = true);
00390 bool quantizedValuesRequested();
00391
00392 #ifdef UNTESTED
00393 void requestTimestamp(const bool b = true);
00394 bool timestampRequested();
00395
00396 void setCalibration();
00397 void setSensorOffset();
00398 void setSensorGain();
00399
00400 void setActuation(std::vector<std::pair<CyberGlove::Finger, boost::uint8_t> > amplitudes);
00401 void setBaudRate();
00402 void setSampleRate();
00403
00404 void setParameters(int p);
00405 #endif
00406
00415 unsigned int getParameters();
00416
00426 std::vector<boost::uint8_t> getSensorValues();
00434 void streamSensorValues(void(*f)(const std::vector<boost::uint8_t>&));
00442 void streamSensorValues(StreamHandler h);
00448 void stopStreaming();
00453 bool isStreaming() const;
00454
00455 #ifdef UNTESTED
00456 unsigned int hardwareSensorMask();
00457 #endif
00458
00502 unsigned int softwareSensorMask();
00511 void softwareSensorMask(unsigned int mask);
00512
00521 int numSensors();
00530 bool isRightHanded();
00539 bool isLeftHanded();
00540
00541 #ifdef UNTESTED
00542 void enableNoiseFilter(const bool b = true);
00543 bool isNoiseFilterEnabled();
00544 #endif
00545
00555 void enableSwitch(const bool b = true);
00564 bool isSwitchEnabled();
00565
00572 void cancel();
00579 void restart();
00586 void reinitialize();
00587
00596 std::string usage();
00606 std::string info();
00615 std::pair<int, int> version();
00616
00625 virtual std::string toString();
00626
00627 };
00628
00629 }
00630 }
00631 }
00632
00633 #endif