-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Sensor API #23
Add Sensor API #23
Conversation
d1bc954
to
147f82a
Compare
This is good to merge, @vsonnier @guruofquality can you take a quick glance? Things can be improved and generalized later, particularly I'm not happy with the locale hoops to jump for |
Hello @zuckschwerdt @guruofquality, I tried the
Putting traces, it seems neither On a side note, indeed double SoapyPlutoSDR::get_sensor_value(struct iio_channel *chn) const
{
double val = 0.0;
char buf[32];
std::stringstream val_as_string;
if (iio_channel_find_attr(chn, "input")) {
if (iio_channel_attr_read(chn, "input", buf, sizeof(buf)) > 0) {
val_as_string << buf;
val_as_string >> val;
}
}
else {
if (iio_channel_attr_read(chn, "raw", buf, sizeof(buf)) > 0) {
val_as_string << buf;
val_as_string >> val;
}
}
if (iio_channel_find_attr(chn, "offset")) {
if (iio_channel_attr_read(chn, "offset", buf, sizeof(buf)) > 0) {
std::stringstream offset_as_string;
double offset = 0.0;
offset_as_string << buf;
offset_as_string >> offset;
val += offset;
}
}
if (iio_channel_find_attr(chn, "scale")) {
if (iio_channel_attr_read(chn, "scale", buf, sizeof(buf)) > 0) {
std::stringstream scale_as_string;
double scale = 1.0;
scale_as_string << buf;
scale_as_string >> scale;
val *= scale;
}
}
return val / 1000.0;
}
(with #include <iostream>) Also: std::string SoapyPlutoSDR::id_to_unit(const std::string& id) const
{
static std::map<std::string, std::string> id_to_unit_table = {
{ "current", "A" },
{ "power", "W" },
{ "temp", "C" },
{ "voltage", "V" }
};
auto it_match = id_to_unit_table.find(id);
if (it_match != id_to_unit_table.end()) {
return it_match->second;
}
return std::string();
}
(with #include <map>) C++11 constructor initializers FTW. Not tested, since the related functions are not called but that's the idea. |
Thanks! Will do. SoapySDRUtil needs a patch to show the values. I've pushed a preliminary commit to SoapySDR/feat-sensors From what I've read using streams instead of strtod is noticably slower and I'm not sure what the locale on the stream is set to? |
I've tested this branch. However, it doesn't compile because #include <chrono>
#include <thread>
...
std::this_thread::sleep_for(std::chrono::seconds(1)); At that point, I was able to run the intended command, which leads me to really understand std::string SoapyPlutoSDR::id_to_unit(const std::string& id) const
{
static std::map<std::string, std::string> id_to_unit_table = {
{ "current", "A" },
{ "power", "W" },
{ "temp", "C" },
{ "voltage", "V" }
};
for (auto it_match : id_to_unit_table) {
//if the id starts with a known prefix, retreive its unit.
if (id.substr(0, it_match.first.size()) == it_match.first) {
return it_match.second;
}
}
return std::string();
}
( [/comment nazi]Of the importance of comments in the code...[comment nazi/]) Finally I got:
Well perfomance is not an issue here, clarity is. And for about the locale, don't touch it especially for simple things as converting strings to numbers. In any case both PR have to be merged "together" else this module-only modification is of small use by itself. Edit: SoapySDRUtil Looks Good To Me. |
@zuckschwerdt this PR looks good, please merge when you feel its ready. Also, is feat-sensors ready, can you make another PR for that one? |
I like neither I'm not confident about the locale though. libiio will always return numbers according to "C" locale. Is the stream guaranteed to read in "C" locale even if there is a different (system) locale set? (floating point numbers aren't that simple when you can't be sure what the decimal point character is.) |
Hello Christian, The main issue of the old C In C++ you can on the other hand have local locales (pun intended) proper to the stream you are using. If you want to force the "C" locale equivalent apparently you can set it that way: val_as_string.imbue(std::locale::classic()); |
Agreed. The question now though, do we need to call |
Ah, ok. Yes we need that call to be sure. |
That is looking fine to me. |
No description provided.