delcom_usb_light.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
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     // These are for a delcom gen 2 usb light. YMMV.
00052   static const unsigned short vendor_id  = 0x0fc5;
00053   static const unsigned short product_id = 0xb080;
00054 
00055   // Clear the existing device, if any
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   // Set our device up
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   // Path to the LEDs
00109   int path[2];
00110   path[0] = 0xff000000;
00111   path[1] = 0x00000000;
00112   
00113   // Set up the packet
00114   uint8_t buf[8];  
00115   memset(buf, 0, sizeof(buf));
00116 
00117   buf[0] = 101;
00118   buf[1] = 12;
00119   
00120   // buf[2] is the LSB, buf[3] is the MSB
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   // Path to the LEDs
00161   int path[2];
00162   path[0] = 0xff000000;
00163   path[1] = 0x00000000;
00164   
00165   // Set up the packet
00166   uint8_t buf[8];  
00167   memset(buf, 0, sizeof(buf));
00168 
00169   // We are reading the feature to find out the current status.
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 }


wgtest_status_indicator
Author(s): Kevin Watts
autogenerated on Sat Dec 28 2013 17:57:04