access_register.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2009-2010, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
37 #include <string.h>
38 #include <cstdio>
39 #include <cstdlib>
40 #include <cstring>
41 #include <cassert>
42 #include <unistd.h>
43 
44 int sensorread(IpCamList *camera, uint8_t reg)
45 {
46  uint16_t val;
47 
48  if ( wge100ReliableSensorRead( camera, reg, &val, NULL ) != 0) {
49  fprintf(stderr, "Could not get register.\n");
50  return -1;
51  }
52 
53  return val;
54 }
55 
56 int sensorwrite(IpCamList *camera, uint8_t reg, uint16_t val)
57 {
58  if ( wge100ReliableSensorWrite( camera, reg, val, NULL ) != 0) {
59  fprintf(stderr, "Could not set register.\n");
60  return -1;
61  }
62  return 0;
63 }
64 
65 int usage(int argc, char **argv)
66 {
67  fprintf(stderr, "Usage: %s <camera_url> # Read all imager register\n", argv[0]);
68  fprintf(stderr, " %s <camera_url> <register> # Read an imager registers\n", argv[0]);
69  fprintf(stderr, " %s <camera_url> <register> <value> # Write an imager register\n", argv[0]);
70  fprintf(stderr, " %s <camera_url> stresstest <register> # Repeatedly reads imager register\n", argv[0]);
71  fprintf(stderr, " %s <camera_url> stresstest <register> <value> # Repeatedly writes value to imager register\n", argv[0]);
72  fprintf(stderr, "Notes:\n");
73  fprintf(stderr, "- All <register> and <value> values are in hexadecimal.\n");
74  fprintf(stderr, "- This tool will never reconfigure the camera's IP address to allow its use during image streaming.\n");
75  fprintf(stderr, "- Register accesses during image streaming may cause interruptions in the streaming.\n");
76  return -1;
77 }
78 
79 int main(int argc, char** argv)
80 {
81  bool stress = false;
82  bool write = false;
83 
84  char **nextarg = argv + 1;
85  char **endarg = argv;
86  while (*endarg)
87  endarg++;
88 
89  // Get the URL
90  if (nextarg == endarg || !strcmp(*nextarg, "--help")) // No arguments or --help
91  return usage(argc, argv);
92  const char *camera_url = *nextarg++;
93 
94  // Find the camera matching the URL
95  IpCamList camera;
96  const char *errmsg;
97  int outval = wge100FindByUrl(camera_url, &camera, SEC_TO_USEC(0.1), &errmsg);
98  if (outval)
99  {
100  fprintf(stderr, "Matching URL %s : %s\n", camera_url, errmsg);
101  return -1;
102  }
103 
104  if (nextarg == endarg) // Read all
105  {
106  for (int i = 0; i < 256; i++)
107  {
108  if (i % 16 == 0)
109  fprintf(stderr, "%02x: ", i);
110 
111  if (i % 16 == 8)
112  fprintf(stderr, "- ");
113 
114  int value = sensorread(&camera, i);
115  if (value == -1)
116  fprintf(stderr, "??");
117  else
118  fprintf(stderr, "%04x ", value);
119 
120  if ((i + 1) % 16 == 0)
121  fprintf(stderr, "\n");
122  }
123  return 0;
124  }
125 
126  if (!strcmp(*nextarg, "stresstest")) // This is a stress test
127  {
128  stress = true;
129  if (++nextarg == endarg)
130  return usage(argc, argv);
131  }
132  // There is at least one argument left at this point.
133 
134  uint16_t val;
135  uint8_t reg;
136 
137  sscanf(*nextarg++, "%hhx", &reg);
138 
139  if (nextarg != endarg) // It is a write.
140  {
141  sscanf(*nextarg++, "%hx", &val);
142  write = true;
143  }
144 
145  if (nextarg != endarg) // Too many arguments.
146  return usage(argc, argv);
147 
148  if (stress && !write)
149  {
150  int count = 0;
151  while (1)
152  {
153  int oldval = sensorread(&camera, reg);
154  fprintf(stderr, "Count %i reg %02x read: %04x\n", count++, reg, oldval);
155  }
156  }
157  else if (stress)
158  {
159  int count = 0;
160  uint16_t oldval = sensorread(&camera, reg);
161  while (1)
162  {
163  sensorwrite(&camera, reg, val);
164  int newval = sensorread(&camera, reg);
165  fprintf(stderr, "Count %i Reg %02x was: %04x set: %04x read: %04x\n", count++, reg, oldval, val, newval);
166  oldval = newval;
167  }
168  }
169  else
170  {
171  uint16_t oldval = sensorread(&camera, reg);
172  if (write)
173  {
174  sensorwrite(&camera, reg, val);
175  int newval = sensorread(&camera, reg);
176  fprintf(stderr, "Reg %02x was: %04x set: %04x read: %04x\n", reg, oldval, val, newval);
177  }
178  else
179  fprintf(stderr, "Reg %02x read: %04x\n", reg, oldval);
180  }
181 
182  return 0;
183 }
int sensorwrite(IpCamList *camera, uint8_t reg, uint16_t val)
int sensorread(IpCamList *camera, uint8_t reg)
int wge100ReliableSensorRead(const IpCamList *camInfo, uint8_t reg, uint16_t *data, int *retries)
Definition: wge100lib.c:1349
int wge100ReliableSensorWrite(const IpCamList *camInfo, uint8_t reg, uint16_t data, int *retries)
Definition: wge100lib.c:1252
#define SEC_TO_USEC(sec)
Definition: host_netutil.h:65
int wge100FindByUrl(const char *url, IpCamList *camera, unsigned wait_us, const char **errmsg)
Definition: wge100lib.c:94
int main(int argc, char **argv)
int usage(int argc, char **argv)


wge100_camera
Author(s): Blaise Gassend, Patrick Mihelich, Eric MacIntosh, David Palchak
autogenerated on Mon Jun 10 2019 15:44:15