Revision b3e6d144 utils/dc-ctl/dc-ctl.cpp

b/utils/dc-ctl/dc-ctl.cpp
51 51
    }
52 52
}
53 53
            
54
unsigned int getFeatureValue(dc1394feature_t feature, dc1394camera_t * camera)
55
{
56
    unsigned int value;
57
    dc1394error_t error = dc1394_feature_get_value(camera, feature, &value);
58
    if (error != DC1394_SUCCESS)
59
        throw std::runtime_error("libdc1394 error: could not get value");
60
    return value;
61
}
62

  
63

  
64
/// pass by reference because we're expecting multiple values, alternately could return a vector
65
std::string getWhiteBalance(dc1394camera_t * camera)
66
{
67
    unsigned int valueBU, valueRV;
68
    dc1394error_t error = dc1394_feature_whitebalance_get_value(camera, &valueBU, &valueRV);
69
    if (error != DC1394_SUCCESS)
70
        throw std::runtime_error("libdc1394 error: could not get value");
71
    std::ostringstream result;
72
    result << valueBU << "," << valueRV;
73
    return result.str();
74
}
54 75

  
55
void saveSettings(const std::string &filename, dc1394camera_t * /*camera*/)
76

  
77
void saveSettings(const std::string &filename, dc1394camera_t * camera)
56 78
{
57 79
    std::cout << "Saving settings to " << filename << std::endl;
80
    /// FIXME: find reference on how to safely, idiomatically open and write to a file
81
    std::ofstream fout;
82
    try 
83
    {
84
        fout.open(filename.c_str());
85
        fout << "camera=" << std::hex << camera->guid << "\n" << std::dec;
86
        fout << "brightness=" << getFeatureValue(DC1394_FEATURE_BRIGHTNESS, camera) << "\n";
87
        fout << "auto-exposure=" << getFeatureValue(DC1394_FEATURE_EXPOSURE, camera) << "\n";
88
        fout << "sharpness=" << getFeatureValue(DC1394_FEATURE_SHARPNESS, camera) << "\n";
89
        fout << "whitebalance=" << getWhiteBalance(camera) << "\n";
90
        fout << "saturation=" << getFeatureValue(DC1394_FEATURE_SATURATION, camera) << "\n";
91
        fout << "gamma=" << getFeatureValue(DC1394_FEATURE_GAMMA, camera) << "\n";
92
        fout << "shutter-time=" << getFeatureValue(DC1394_FEATURE_SHUTTER, camera) << "\n";
93
        fout << "gain=" << getFeatureValue(DC1394_FEATURE_GAIN, camera) << "\n";
94
        fout.close();
95
    }
96
    catch (const std::exception& e)  // catch it here so that we close file
97
    {
98
        std::cerr << "error: " << e.what() << "\n";
99
    }
100
    fout.close();
58 101
}
59 102

  
60 103
std::string featureHelp(const dc1394featureset_t &features, dc1394feature_t feature)
......
168 211
        // using strings so that value can be "auto"
169 212
        desc.add_options()
170 213
            ("help,h", "produce help message")
171
            ("camera,c", po::value<unsigned>()->default_value(0), "camera number to use")
214
            ("camera,c", po::value<string>()->default_value("0"), "guid of camera number to use (0 is first camera on bus)")
172 215
            ("brightness,b", po::value<string>(), featureHelp(features, DC1394_FEATURE_BRIGHTNESS).c_str())
173 216
            ("auto-exposure,e", po::value<string>(), featureHelp(features, DC1394_FEATURE_EXPOSURE).c_str())
174 217
            ("sharpness,s", po::value<string>(), featureHelp(features, DC1394_FEATURE_SHARPNESS).c_str())
......
192 235
            cleanup(dc1394, camera, cameras);
193 236
            return 0;
194 237
        }
195
        
238

  
196 239
        if(vm.count("save"))
197 240
        {
198 241
            saveSettings(vm["save"].as<string>(), camera);
......
206 249
            store(parse_config_file(configFile, desc), vm);
207 250
        }
208 251

  
209
        if (vm["camera"].as<unsigned>() != 0) // using non-default camera id
252
        if (vm["camera"].as<string>() != "0") // using non-default camera id
210 253
        {
211
            if (vm["camera"].as<unsigned>() < cameras->num)
212
            {
213
                camera = dc1394_camera_new_unit(dc1394, cameras->ids[vm["camera"].as<unsigned>()].guid,
214
                        cameras->ids[vm["camera"].as<unsigned>()].unit);
215

  
216
                camerr = dc1394_feature_get_all(camera, &features);
217
                if (camerr != DC1394_SUCCESS)
218
                    throw std::runtime_error("libdc1394 error: this should be more verbose");
219
            }
220
            else
254
            // find right guid
255
            bool matchedGUID = false;
256
            for (unsigned i = 0; i < cameras->num and not matchedGUID; ++i)
221 257
            {
222
                std::cerr << "Invalid camera-number " << vm["camera"].as<unsigned>() << std::endl;
223
                return 1;
258
                std::stringstream GUIDInHex;
259
                GUIDInHex << std::hex << cameras->ids[i].guid;
260
                if (GUIDInHex.str() == vm["camera"].as<string>())
261
                {
262
                    camera = dc1394_camera_new_unit(dc1394, cameras->ids[i].guid,
263
                            cameras->ids[i].unit);
264
                    matchedGUID = true;
265
                }
224 266
            }
267
            if (not matchedGUID)
268
                throw std::runtime_error("could not find camera with guid " + 
269
                        lexical_cast<string>(vm["camera"].as<string>()));
270

  
271

  
272
            camerr = dc1394_feature_get_all(camera, &features);
273
            if (camerr != DC1394_SUCCESS)
274
                throw std::runtime_error("libdc1394 error: this should be more verbose");
225 275
        }
226 276

  
227 277
        if (vm["list-features"].as<bool>())

Also available in: Unified diff