38 #ifndef WIN32_LEAN_AND_MEAN 39 #define WIN32_LEAN_AND_MEAN 1 50 #include <sys/types.h> 64 std::string byte_to_binary(uint8_t x)
69 for (z = 128; z > 0; z >>= 1)
71 b << (((x & z) == z) ?
"1" :
"0");
77 std::string bytes_to_binary(uint64_t x,
int bits)
81 for(currentByte = 0; currentByte*8<bits; currentByte++)
83 b = byte_to_binary((x >> currentByte*8)) +
" " + b;
86 return b.erase(0,(currentByte*8 - bits));
89 void usage(
const char *programNameP)
92 "USAGE: %s [<options>]\n",
94 fprintf(stderr,
"Where <options> are:\n");
95 fprintf(stderr,
"\t-a <ip_address> : ip address (default=10.66.171.21)\n");
96 fprintf(stderr,
"\t-s : set the calibration (default is query)\n");
97 fprintf(stderr,
"\t-l : set the left calibration \n");
98 fprintf(stderr,
"\t-r : set the right calibration \n");
99 fprintf(stderr,
"\t-e : set the right black level \n");
100 fprintf(stderr,
"\t-k : set the left black level \n");
105 bool fileExists(
const std::string& name)
108 return (0 == stat(name.c_str(), &sbuf));
117 std::string ipAddress =
"10.66.171.21";
118 std::string leftIn =
"50";
119 std::string rightIn =
"50";
120 std::string leftBlackIn =
"-61";
121 std::string rightBlackIn =
"-61";
129 bool setLeftBlack=
false;
130 bool setRightBlack=
false;
137 while(-1 != (inArgument =
getopt(argc, argvPP,
"l:r:k:e:a:sy")))
139 case 'a': ipAddress = std::string(
optarg);
break;
140 case 'l': leftIn = std::string(
optarg); setLeft=
true;
break;
141 case 'r': rightIn = std::string(
optarg); setRight=
true;
break;
142 case 'k': leftBlackIn = std::string(
optarg); setLeftBlack=
true;
break;
143 case 'e': rightBlackIn = std::string(
optarg); setRightBlack=
true;
break;
144 case 's': setCal =
true;
break;
145 default:
usage(*argvPP);
break;
151 if (setCal && (!setLeft && !setRight && !setLeftBlack && !setRightBlack)) {
152 fprintf(stderr,
"Please specify a value to set using -l, -r, -e, or -k\n");
158 fprintf(stdout,
"Attempting to establish communications with \"%s\"\n",
162 if (NULL == channelP) {
163 fprintf(stderr,
"Failed to establish communications with \"%s\"\n",
172 status = channelP->getSensorCalibration(sensorCalibration);
174 fprintf(stderr,
"failed to query sensor calibration: %s\n",
182 if (setCal && setLeft) {
183 left = atoi(leftIn.c_str());
185 if (left > 255 || left < 0)
187 fprintf(stderr,
"Left sensor gain range is 0-255\n");
192 sensorCalibration.
adc_gain[0] = left;
195 if (setCal && setRight) {
196 right = atoi(rightIn.c_str());
198 if (right > 255 || right < 0)
200 fprintf(stderr,
"Right sensor gain range is 0-255\n");
205 sensorCalibration.
adc_gain[1] = right;
208 if (setCal && setLeftBlack)
210 leftBlack = atoi(leftBlackIn.c_str());
211 sensorCalibration.
bl_offset[0] = leftBlack;
214 if (setCal && setRightBlack)
216 rightBlack = atoi(rightBlackIn.c_str());
217 sensorCalibration.
bl_offset[1] = rightBlack;
221 if (
false == setCal) {
223 fprintf(stdout,
"left sensor gain: %hu\nright sensor gain %hu\nleft sensor black level: %d\nright sensor black level %d\n",
229 fprintf(stdout,
"Setting :\nleft sensor gain: %5hu binary: %s\n" 230 "right sensor gain %5hu binary: %s\n" 231 "left sensor black level: %d binary: %s\n" 232 "right sensor black level %d binary: %s\n",
233 sensorCalibration.
adc_gain[0],byte_to_binary(sensorCalibration.
adc_gain[0]).c_str(),
234 sensorCalibration.
adc_gain[1],byte_to_binary(sensorCalibration.
adc_gain[1]).c_str(),
235 sensorCalibration.
bl_offset[0],bytes_to_binary(sensorCalibration.
bl_offset[0], 14).c_str(),
236 sensorCalibration.
bl_offset[1],bytes_to_binary(sensorCalibration.
bl_offset[1], 14).c_str());
238 status = channelP->setSensorCalibration(sensorCalibration);
240 fprintf(stderr,
"failed to set sensor calibration: %s\n",
245 fprintf(stdout,
"Sensor calibration successfully updated\n");
static const char * statusString(Status status)
static Channel * Create(const std::string &sensorAddress)
int getopt(int argc, char **argv, char *opts)
static CRL_CONSTEXPR Status Status_Ok
int main(int argc, char **argvPP)
static void Destroy(Channel *instanceP)