Complete options get set

This commit is contained in:
John Zhao 2018-04-10 13:58:37 +08:00
parent 490185a577
commit 8c0605255b
4 changed files with 98 additions and 17 deletions

View File

@ -42,6 +42,23 @@ int main(int argc, char *argv[]) {
} }
} }
/*
{ // auto-exposure
device->SetOptionValue(Option::EXPOSURE_MODE, 0);
device->SetOptionValue(Option::MAX_GAIN, 40); // [0.48]
device->SetOptionValue(Option::MAX_EXPOSURE_TIME, 120); // [0,240]
device->SetOptionValue(Option::DESIRED_BRIGHTNESS, 200); // [0,255]
}
*/
/*
{ // manual-exposure
device->SetOptionValue(Option::EXPOSURE_MODE, 1);
device->SetOptionValue(Option::GAIN, 20); // [0.48]
device->SetOptionValue(Option::BRIGHTNESS, 20); // [0,240]
device->SetOptionValue(Option::CONTRAST, 20); // [0,255]
}
device->SetOptionValue(Option::IR_CONTROL, 80);
*/
device->LogOptionInfos(); device->LogOptionInfos();
std::size_t left_count = 0; std::size_t left_count = 0;

View File

@ -58,7 +58,7 @@ bool Device::Supports(const Option &option) const {
const std::vector<StreamRequest> &Device::GetStreamRequests( const std::vector<StreamRequest> &Device::GetStreamRequests(
const Capabilities &capability) const { const Capabilities &capability) const {
if (!Supports(capability)) { if (!Supports(capability)) {
LOG(FATAL) << "Unsupported capability: " << to_string(capability); LOG(FATAL) << "Unsupported capability: " << capability;
} }
try { try {
auto &&cap_requests = stream_requests_map.at(model_); auto &&cap_requests = stream_requests_map.at(model_);
@ -132,15 +132,27 @@ ImuExtrinsics Device::GetImuExtrinsics() const {
} }
OptionInfo Device::GetOptionInfo(const Option &option) const { OptionInfo Device::GetOptionInfo(const Option &option) const {
if (!Supports(option)) {
LOG(WARNING) << "Unsupported option: " << option;
return {0, 0, 0};
}
auto &&info = channels_->GetControlInfo(option); auto &&info = channels_->GetControlInfo(option);
return {info.min, info.max, info.def}; return {info.min, info.max, info.def};
} }
std::int32_t Device::GetOptionValue(const Option &option) const { std::int32_t Device::GetOptionValue(const Option &option) const {
if (!Supports(option)) {
LOG(WARNING) << "Unsupported option: " << option;
return -1;
}
return channels_->GetControlValue(option); return channels_->GetControlValue(option);
} }
void Device::SetOptionValue(const Option &option, std::int32_t value) { void Device::SetOptionValue(const Option &option, std::int32_t value) {
if (!Supports(option)) {
LOG(WARNING) << "Unsupported option: " << option;
return;
}
channels_->SetControlValue(option, value); channels_->SetControlValue(option, value);
} }
@ -151,7 +163,7 @@ void Device::LogOptionInfos() const {
void Device::SetStreamCallback( void Device::SetStreamCallback(
const Stream &stream, stream_callback_t callback) { const Stream &stream, stream_callback_t callback) {
if (!Supports(stream)) { if (!Supports(stream)) {
LOG(WARNING) << "Unsupported stream: " << to_string(stream); LOG(WARNING) << "Unsupported stream: " << stream;
return; return;
} }
if (callback) { if (callback) {

View File

@ -43,7 +43,22 @@ int XuCamCtrlId(Option option) {
Channels::Channels(std::shared_ptr<uvc::device> device) : device_(device) { Channels::Channels(std::shared_ptr<uvc::device> device) : device_(device) {
VLOG(2) << __func__; VLOG(2) << __func__;
UpdateControlInfos();
}
Channels::~Channels() {
VLOG(2) << __func__;
}
void Channels::LogControlInfos() const {
for (auto &&it = control_infos_.begin(); it != control_infos_.end(); it++) {
LOG(INFO) << it->first << ": min=" << it->second.min
<< ", max=" << it->second.max << ", def=" << it->second.def
<< ", cur=" << GetControlValue(it->first);
}
}
void Channels::UpdateControlInfos() {
for (auto &&option : std::vector<Option>{Option::GAIN, Option::BRIGHTNESS, for (auto &&option : std::vector<Option>{Option::GAIN, Option::BRIGHTNESS,
Option::CONTRAST}) { Option::CONTRAST}) {
control_infos_[option] = PuControlInfo(option); control_infos_[option] = PuControlInfo(option);
@ -65,22 +80,11 @@ Channels::Channels(std::shared_ptr<uvc::device> device) : device_(device) {
} }
} }
Channels::~Channels() {
VLOG(2) << __func__;
}
void Channels::LogControlInfos() const {
for (auto &&it = control_infos_.begin(); it != control_infos_.end(); it++) {
LOG(INFO) << it->first << ": min=" << it->second.min
<< ", max=" << it->second.max << ", def=" << it->second.def
<< ", cur=" << GetControlValue(it->first);
}
}
Channels::control_info_t Channels::GetControlInfo(const Option &option) const { Channels::control_info_t Channels::GetControlInfo(const Option &option) const {
try { try {
return control_infos_.at(option); return control_infos_.at(option);
} catch (const std::out_of_range &e) { } catch (const std::out_of_range &e) {
LOG(WARNING) << "Get control info of " << option << " failed";
return {0, 0, 0}; return {0, 0, 0};
} }
} }
@ -117,9 +121,44 @@ std::int32_t Channels::GetControlValue(const Option &option) const {
} }
void Channels::SetControlValue(const Option &option, std::int32_t value) { void Channels::SetControlValue(const Option &option, std::int32_t value) {
// TODO(JohnZhao) auto in_range = [this, &option, &value]() {
UNUSED(option) auto &&info = GetControlInfo(option);
UNUSED(value) if (value < info.min || value > info.max) {
LOG(WARNING) << option << " set value out of range, " << value
<< " not in [" << info.min << "," << info.max << "]";
return false;
}
return true;
};
switch (option) {
case Option::GAIN:
case Option::BRIGHTNESS:
case Option::CONTRAST: {
if (!in_range())
break;
if (!PuControlQuery(option, uvc::PU_QUERY_SET, &value)) {
LOG(WARNING) << option << " set value failed";
}
} break;
case Option::FRAME_RATE:
case Option::IMU_FREQUENCY:
case Option::EXPOSURE_MODE:
case Option::MAX_GAIN:
case Option::MAX_EXPOSURE_TIME:
case Option::DESIRED_BRIGHTNESS:
case Option::IR_CONTROL:
case Option::HDR_MODE: {
if (!in_range())
break;
XuCamCtrlSet(option, value);
} break;
case Option::ZERO_DRIFT_CALIBRATION:
case Option::ERASE_CHIP:
LOG(WARNING) << option << " set value useless";
break;
default:
LOG(FATAL) << "Unsupported option " << option;
}
} }
bool Channels::PuControlRange( bool Channels::PuControlRange(
@ -169,6 +208,16 @@ std::int32_t Channels::XuCamCtrlGet(Option option) const {
} }
} }
void Channels::XuCamCtrlSet(Option option, std::int32_t value) const {
int id = XuCamCtrlId(option);
std::uint8_t data[3] = {static_cast<std::uint8_t>(id & 0xFF),
static_cast<std::uint8_t>((value >> 8) & 0xFF),
static_cast<std::uint8_t>(value & 0xFF)};
if (!XuCamCtrlQuery(uvc::XU_QUERY_SET, 3, data)) {
LOG(WARNING) << "Set control value of " << option << " failed";
}
}
Channels::control_info_t Channels::PuControlInfo(Option option) const { Channels::control_info_t Channels::PuControlInfo(Option option) const {
int32_t min = 0, max = 0, def = 0; int32_t min = 0, max = 0, def = 0;
if (!PuControlRange(option, &min, &max, &def)) { if (!PuControlRange(option, &min, &max, &def)) {

View File

@ -39,6 +39,8 @@ class Channels {
void LogControlInfos() const; void LogControlInfos() const;
void UpdateControlInfos();
control_info_t GetControlInfo(const Option &option) const; control_info_t GetControlInfo(const Option &option) const;
std::int32_t GetControlValue(const Option &option) const; std::int32_t GetControlValue(const Option &option) const;
void SetControlValue(const Option &option, std::int32_t value); void SetControlValue(const Option &option, std::int32_t value);
@ -57,6 +59,7 @@ class Channels {
bool XuCamCtrlQuery(uvc::xu_query query, uint16_t size, uint8_t *data) const; bool XuCamCtrlQuery(uvc::xu_query query, uint16_t size, uint8_t *data) const;
std::int32_t XuCamCtrlGet(Option option) const; std::int32_t XuCamCtrlGet(Option option) const;
void XuCamCtrlSet(Option option, std::int32_t value) const;
control_info_t PuControlInfo(Option option) const; control_info_t PuControlInfo(Option option) const;
control_info_t XuControlInfo(Option option) const; control_info_t XuControlInfo(Option option) const;