access_register.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2009-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 #include "wge100_camera/wge100lib.h"
00036 #include "wge100_camera/host_netutil.h"
00037 #include <string.h>
00038 #include <cstdio>
00039 #include <cstdlib>
00040 #include <cstring>
00041 #include <cassert>
00042 #include <unistd.h>
00043   
00044 int sensorread(IpCamList *camera, uint8_t reg)
00045 {
00046   uint16_t val;
00047 
00048   if ( wge100ReliableSensorRead( camera, reg, &val, NULL ) != 0) {
00049     fprintf(stderr, "Could not get register.\n");
00050     return -1;
00051   }
00052 
00053   return val;
00054 }
00055 
00056 int sensorwrite(IpCamList *camera, uint8_t reg, uint16_t val)
00057 {
00058   if ( wge100ReliableSensorWrite( camera, reg, val, NULL ) != 0) {
00059     fprintf(stderr, "Could not set register.\n");
00060     return -1;
00061   }
00062   return 0;
00063 }
00064 
00065 int usage(int argc, char **argv)
00066 {  
00067   fprintf(stderr, "Usage: %s <camera_url>                                  # Read all imager register\n", argv[0]);
00068   fprintf(stderr, "       %s <camera_url> <register>                       # Read an imager registers\n", argv[0]);
00069   fprintf(stderr, "       %s <camera_url> <register> <value>               # Write an imager register\n", argv[0]);
00070   fprintf(stderr, "       %s <camera_url> stresstest <register>            # Repeatedly reads imager register\n", argv[0]);
00071   fprintf(stderr, "       %s <camera_url> stresstest <register> <value>    # Repeatedly writes value to imager register\n", argv[0]);
00072   fprintf(stderr, "Notes:\n");
00073   fprintf(stderr, "- All <register> and <value> values are in hexadecimal.\n");
00074   fprintf(stderr, "- This tool will never reconfigure the camera's IP address to allow its use during image streaming.\n");
00075   fprintf(stderr, "- Register accesses during image streaming may cause interruptions in the streaming.\n");
00076   return -1;
00077 }
00078 
00079 int main(int argc, char** argv)
00080 {
00081   bool stress = false;
00082   bool write = false;
00083 
00084   char **nextarg = argv + 1;
00085   char **endarg = argv;
00086   while (*endarg)
00087     endarg++;
00088 
00089   // Get the URL
00090   if (nextarg == endarg || !strcmp(*nextarg, "--help")) // No arguments or --help
00091     return usage(argc, argv);
00092   const char *camera_url = *nextarg++;
00093 
00094   // Find the camera matching the URL
00095   IpCamList camera;
00096   const char *errmsg;
00097   int outval = wge100FindByUrl(camera_url, &camera, SEC_TO_USEC(0.1), &errmsg);
00098   if (outval)
00099   {
00100     fprintf(stderr, "Matching URL %s : %s\n", camera_url, errmsg);
00101     return -1;
00102   }
00103 
00104   if (nextarg == endarg) // Read all
00105   {
00106     for (int i = 0; i < 256; i++)
00107     {
00108       if (i % 16 == 0)
00109         fprintf(stderr, "%02x:  ", i); 
00110       
00111       if (i % 16 == 8)
00112         fprintf(stderr, "- "); 
00113         
00114       int value = sensorread(&camera, i);
00115       if (value == -1)
00116         fprintf(stderr, "??");
00117       else
00118         fprintf(stderr, "%04x ", value);
00119       
00120       if ((i + 1) % 16 == 0)
00121         fprintf(stderr, "\n"); 
00122     }
00123     return 0;
00124   }
00125 
00126   if (!strcmp(*nextarg, "stresstest")) // This is a stress test
00127   {
00128     stress = true;
00129     if (++nextarg == endarg)
00130       return usage(argc, argv);
00131   }
00132   // There is at least one argument left at this point.
00133   
00134   uint16_t val;
00135   uint8_t reg;
00136   
00137   sscanf(*nextarg++, "%hhx", &reg);
00138 
00139   if (nextarg != endarg) // It is a write.
00140   {
00141     sscanf(*nextarg++, "%hx", &val);
00142     write = true;
00143   }
00144                      
00145   if (nextarg != endarg) // Too many arguments.
00146     return usage(argc, argv);
00147 
00148   if (stress && !write)
00149   {
00150     int count = 0;
00151     while (1)
00152     {
00153       int oldval = sensorread(&camera, reg);
00154       fprintf(stderr, "Count %i reg %02x read: %04x\n", count++, reg, oldval);
00155     }
00156   }
00157   else if (stress)
00158   {
00159     int count = 0;
00160     uint16_t oldval = sensorread(&camera, reg);
00161     while (1)
00162     {
00163       sensorwrite(&camera, reg, val);
00164       int newval = sensorread(&camera, reg);
00165       fprintf(stderr, "Count %i Reg %02x was: %04x set: %04x read: %04x\n", count++, reg, oldval, val, newval);
00166       oldval = newval;
00167     }
00168   }
00169   else
00170   {
00171     uint16_t oldval = sensorread(&camera, reg);
00172     if (write)
00173     {
00174       sensorwrite(&camera, reg, val);
00175       int newval = sensorread(&camera, reg);
00176       fprintf(stderr, "Reg %02x was: %04x set: %04x read: %04x\n", reg, oldval, val, newval);
00177     }
00178     else
00179       fprintf(stderr, "Reg %02x read: %04x\n", reg, oldval);
00180   }
00181 
00182   return 0;
00183 }


wge100_camera
Author(s): Blaise Gassend, Patrick Mihelich, Eric MacIntosh, David Palchak
autogenerated on Mon Oct 6 2014 08:59:04