delcom_util.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 /* Simple utility to control delcom USB light from command line
00036  *
00037  *
00038  */
00039 
00040 #include <string.h>
00041 #include <stdio.h>
00042 #include <getopt.h>
00043 #include <stdbool.h>
00044 #include <string>
00045 
00046 #include  <wgtest_status_indicator/delcom_usb_light.h>
00047 
00048 using namespace wgtest_status_indicator;
00049 
00050 std::string boolToString(bool val)
00051 {
00052   return val ? "True" : "False";
00053 }
00054 
00055 int main (int argc, char **argv)
00056 {
00057     // First, grab the user's options.
00058     static int orange_flag;
00059     static int get_flag;
00060     static int green_flag;
00061     static int help_flag;
00062     static int off_flag;
00063     static int red_flag;
00064 
00065     int c;
00066     while (1) 
00067     {
00068       static struct option long_options[] = {
00069         // These options set a flag.
00070         {"orange",  no_argument,  &orange_flag,  1},
00071         {"get",   no_argument,  &get_flag,   1},
00072         {"green", no_argument,  &green_flag, 1},
00073         {"help",  no_argument,  &help_flag,  1},
00074         {"off",   no_argument,  &off_flag,   1},
00075         {"red",   no_argument,  &red_flag,   1},
00076       };
00077       
00078       int option_index = 0;
00079       c = getopt_long(argc, argv, "", long_options, &option_index);
00080       
00081       if (c == -1)
00082         break;
00083       
00084       switch (c) 
00085       {
00086       case 0:
00087         if (long_options[option_index].flag != 0)
00088           break;
00089       }
00090     }
00091     
00092     // Print usage if the user didn't supply a valid option or asked for help.
00093     if ((! orange_flag && ! red_flag && ! green_flag && ! off_flag && ! get_flag)
00094         || help_flag) 
00095     {
00096       printf(
00097              "Usage: %s [ --orange | --red | --green | --off | --get ]\n",
00098              argv[0]
00099              );
00100       
00101       exit(1);
00102     }
00103     
00104     DelcomUSBLight *light = new DelcomUSBLight;
00105     if (!light->isOpen())
00106     {
00107       fprintf(stderr, "Unable to open USB light!");
00108       return 1;
00109     }
00110     
00111     // Make command
00112     USBLightCommand cmd;
00113 
00114     if (get_flag)
00115     {
00116       // Get status
00117       if (!light->getStatus(cmd))
00118         return 1;
00119 
00120       if (cmd.off)
00121         fprintf(stdout, "Light status: OFF\n");
00122       else
00123       {
00124         fprintf(stdout, "Green: %s. Orange: %s. Red: %s.\n", 
00125                 boolToString(cmd.green).c_str(),
00126                 boolToString(cmd.orange).c_str(), 
00127                 boolToString(cmd.red).c_str());
00128       }
00129     }
00130     else
00131     {
00132       // Turn light on/off
00133       cmd.green = green_flag;
00134       cmd.orange = orange_flag;
00135       cmd.off = off_flag;
00136       cmd.red = red_flag;
00137       
00138       if (!light->sendCommand(cmd))
00139         return 1;
00140     }
00141 
00142     return 0;
00143 }


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