>
> *The ints and floats inside the loop are not a problem. Only one of each
> is allocated on the stack when the function is entered, and then they're
> assigned each time through the loop. Perfectly fine (and good practice for
> limiting the scope of variables).*


This would assume you know which compiler is being used, and you've
actually disassembled the executable to see what the compiler does.
Generally, it is a bad idea to assume you know what is happening. Because
of this ambiguity, it is considered bad form.

A while statement has no concept of scope. So main() has scope whether
inside the while statement, or not.


On Sat, Jan 2, 2016 at 3:45 AM, Rick Mann <rm...@latencyzero.com> wrote:

> What version of the kernel are you using? There's a problem with some of
> them.
>
> The ints and floats inside the loop are not a problem. Only one of each is
> allocated on the stack when the function is entered, and then they're
> assigned each time through the loop. Perfectly fine (and good practice for
> limiting the scope of variables).
>
> > On Jan 1, 2016, at 21:08 , William Hermans <yyrk...@gmail.com> wrote:
> >
> > It's been a while since I've written anything in C++, but I notice
> you're not doing any error checking on your filestream. For instance check
> out the very simply code listing I demonstrate here ( in C ).
> http://www.embeddedhobbyist.com/2015/10/beaglebone-black-adc/
> >
> > Notice that I'm checking if length is -1, and if so, moving code
> execution back up to the top of the loop. Using continue. You should be
> doing something similar( whatever filestream allows ). Also, I've not idea
> how filestream works in Linux, but if you're opening a new file descriptor
> every function call, you'll want to refactor that out. e.g. you get your
> file descriptor only once when the executable is first run, and then
> operate off that.
> >
> > Another thing that you have not really asked about but deserves mention.
> You're declaring 2 int's and two floats every program loop. You'll want to
> fix that . . .
> >
> > On Fri, Jan 1, 2016 at 9:26 PM, Sunny Sharma <sunnysharm...@gmail.com>
> wrote:
> > Hi friends,
> >
> > I am trying to voltage values from 2 analog sensors which are connected
> to pins AIN0 and AIN1 using C++. I have taken Derek Molloy's code for
> reading one voltage value and edited it to add a second pin to read values
> from. The code is as follows:
> >
> > #include<iostream>
> > #include<fstream>
> > #include<string>
> > #include<sstream>
> > #include<cmath>
> > using namespace std;
> >
> > #define LDR_PATH "/sys/bus/iio/devices/iio:device0/in_voltage"
> >
> > int readAnalog(int number){
> >
> >    stringstream ss;
> >
> >    ss << LDR_PATH << number << "_raw";
> >
> >    fstream fs;
> >
> >    fs.open(ss.str().c_str(), fstream::in);
> >
> >    fs >> number;
> >
> >    fs.close();
> >
> >    return number;
> >
> > }
> >
> > int main(int argc, char* argv[]){
> >
> >    cout << "Starting the IR distance sensor program:" << endl;
> >
> >    while(1) {
> >
> >       int value0 = readAnalog(0);
> >       int value1 = readAnalog(1);
> >       float V0 = ((float)value0 / 4096) * 1.8;
> >       float V1 = ((float)value1 / 4096) * 1.8;
> >
> >       cout << "Sensor0 is: " << V0 << " Sensor1 is: " << V1 << '\r' <<
> flush;
> >
> >       usleep(100000);
> >
> >    }
> >
> >    return 0;
> > }
> >
> > However, when I run this code, after 10-15 successful iterations, I
> receive this error message:
> >
> > terminate called after throwing an instance of 'std::ios_base::failure'
> >   what():  basic_filebuf::underflow error reading the file
> > Aborted
> >
> > I am not sure as to why I am receiving this error, or really what this
> error message even means. My guess it is some access error when trying to
> read the pin value.
> >
> > The Adafruit BBIO python library actually found a work around this issue
> in C, but I'm not sure how I can extrapolate this to C++ (see:
> https://github.com/adafruit/adafruit-beaglebone-io-python/blob/master/source/c_adc.c
> )
> >
> > If anyone has any insight or help they could offer, that would be very
> helpful!
> >
> > Thanks
> >
> > --
> > For more options, visit http://beagleboard.org/discuss
> > ---
> > You received this message because you are subscribed to the Google
> Groups "BeagleBoard" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an email to beagleboard+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> >
> >
> > --
> > For more options, visit http://beagleboard.org/discuss
> > ---
> > You received this message because you are subscribed to the Google
> Groups "BeagleBoard" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an email to beagleboard+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>
>
> --
> Rick Mann
> rm...@latencyzero.com
>
>
> --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to the Google Groups
> "BeagleBoard" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to beagleboard+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to beagleboard+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to