Go to the documentation of this file.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 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 #include <stdbool.h>
00037 #include <stdint.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 #include <stdio.h>
00041 #include <hid.h>
00042 
00043 #include <ros/console.h>
00044 
00045 #include <wgtest_status_indicator/delcom_usb_light.h>
00046 
00047 using namespace wgtest_status_indicator;
00048 
00049 bool DelcomUSBLight::openDevice()
00050 {
00051     
00052   static const unsigned short vendor_id  = 0x0fc5;
00053   static const unsigned short product_id = 0xb080;
00054 
00055   
00056   if (hid_dev_)
00057   {
00058     delete hid_dev_;
00059     hid_dev_ = NULL;
00060   }  
00061 
00062   HIDInterfaceMatcher matcher = { vendor_id, product_id, NULL, NULL, 0 };
00063   
00064   int iface_num = 0;
00065   hid_return ret;
00066 
00067   HIDInterface *iface;
00068 
00069   ret = hid_init();
00070   if (ret != HID_RET_SUCCESS) 
00071   {
00072     ROS_ERROR("hid_init failed with return code %x", ret);
00073     return false;
00074   }
00075   
00076   iface = hid_new_HIDInterface();
00077   if (iface == 0) 
00078   {
00079     ROS_ERROR("hid_new_HIDInterface() failed, out of memory?");
00080     return false;
00081   }
00082   
00083   ret = hid_force_open(iface, iface_num, &matcher, 3);
00084   if (ret == 12)
00085   {
00086     ROS_ERROR("hid_force_open failed with error %x. This may be a permissions issue. Check device permissions", ret);
00087     return false;
00088   }
00089   else if (ret != HID_RET_SUCCESS) 
00090   {
00091     fprintf(stderr, "hid_force_open failed with return code %x", ret);
00092     return false;
00093   }
00094 
00095   
00096   hid_dev_ = iface;
00097   return true;
00098 }
00099 
00100 bool DelcomUSBLight::sendCommand(const USBLightCommand &cmd)
00101 {
00102   if (!isOpen() && !openDevice())
00103   {
00104     ROS_ERROR("Unable to open device to send command");
00105     return false;
00106   }
00107 
00108   
00109   int path[2];
00110   path[0] = 0xff000000;
00111   path[1] = 0x00000000;
00112   
00113   
00114   uint8_t buf[8];  
00115   memset(buf, 0, sizeof(buf));
00116 
00117   buf[0] = 101;
00118   buf[1] = 12;
00119   
00120   
00121   if (cmd.green)
00122   {
00123     buf[2] = 0x01 << 0;
00124     buf[3] = 0xFF;
00125   }
00126   if (cmd.red)
00127   {
00128     buf[2] = 0x01 << 1;
00129     buf[3] = 0xFF;
00130   }
00131   if (cmd.orange)
00132   {
00133     buf[2] = 0x01 << 2;
00134     buf[3] = 0xFF;
00135   }
00136   if (cmd.off)
00137   {
00138     buf[3] = 0xFF;
00139   }
00140   
00141   hid_return ret = hid_set_feature_report(hid_dev_, path, 1, (char*)buf, 8);
00142   if (ret != HID_RET_SUCCESS) 
00143   {
00144     ROS_ERROR("hid_set_feature_report failed(). Returned %d", ret);
00145     return false;
00146   }
00147 
00148   return true;
00149 }
00150 
00151 
00152 bool DelcomUSBLight::getStatus(USBLightCommand &status)
00153 {
00154   if (!isOpen() && !openDevice())
00155   {
00156     ROS_ERROR("Unable to open device to get status");
00157     return false;
00158   }
00159 
00160   
00161   int path[2];
00162   path[0] = 0xff000000;
00163   path[1] = 0x00000000;
00164   
00165   
00166   uint8_t buf[8];  
00167   memset(buf, 0, sizeof(buf));
00168 
00169   
00170   buf[0] = 100;
00171 
00172   hid_return ret = hid_get_feature_report(hid_dev_, path, 1, (char*)buf, 8);
00173   if (ret != HID_RET_SUCCESS) 
00174   {
00175     ROS_ERROR("hid_get_feature_report failed(). Returned: %x", ret);
00176     return false;
00177   }
00178 
00179   status.green  = buf[2] == 0xFE;
00180   status.red    = buf[2] == 0xFD;
00181   status.orange = buf[2] == 0xFB;
00182   
00183   if (!status.green && !status.red && !status.orange)
00184     status.off = true;
00185 
00186   return true;
00187 }
00188 
00189 DelcomUSBLight::DelcomUSBLight() :
00190   hid_dev_(NULL)
00191 {
00192   openDevice();
00193 }
00194 
00195 DelcomUSBLight::~DelcomUSBLight()
00196 {
00197   if (hid_dev_)
00198     delete hid_dev_;  
00199 }