Re: [beagleboard] Errors reading multiple Analog Inputs from Beaglebone Black using C++

2016-01-03 Thread William Hermans
>
> *I'm not sure what you think is going on in that code. A while loop most
> certainly has a scope, in that the variables declared within the while
> block are not visible before nor after the loop. Any compiler that accepts
> the syntax (i.e. any modern C or C++ compiler) will treat that exactly the
> same as if the variables were declared at the top of the function, except
> that the name will only be visible within the loop.*


You are absolutely correct, and I do not know what the hell I was thinking.
Generally I always declare variables outside of loops, because I think it
is clearer. But what should have made it clear to me is that anything
inside of curly brackets is limited in scope to that set of brackets. You
do not even need a function or loop, etc to limit scope in this manner.




On Sun, Jan 3, 2016 at 2:06 AM, Rick Mann  wrote:

>
> > On Jan 2, 2016, at 06:57 , William Hermans  wrote:
> >
> > 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.
>
> I'm not sure what you think is going on in that code. A while loop most
> certainly has a scope, in that the variables declared within the while
> block are not visible before nor after the loop. Any compiler that accepts
> the syntax (i.e. any modern C or C++ compiler) will treat that exactly the
> same as if the variables were declared at the top of the function, except
> that the name will only be visible within the loop.
>
> --
> 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.


Re: [beagleboard] Errors reading multiple Analog Inputs from Beaglebone Black using C++

2016-01-03 Thread Rick Mann

> On Jan 2, 2016, at 06:57 , William Hermans  wrote:
> 
> 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.

I'm not sure what you think is going on in that code. A while loop most 
certainly has a scope, in that the variables declared within the while block 
are not visible before nor after the loop. Any compiler that accepts the syntax 
(i.e. any modern C or C++ compiler) will treat that exactly the same as if the 
variables were declared at the top of the function, except that the name will 
only be visible within the loop.

-- 
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.


Re: [beagleboard] Errors reading multiple Analog Inputs from Beaglebone Black using C++

2016-01-02 Thread William Hermans
>
> *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  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  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 
> 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
> > #include
> > #include
> > #include
> > #include
> > 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(10);
> >
> >}
> >
> >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

Re: [beagleboard] Errors reading multiple Analog Inputs from Beaglebone Black using C++

2016-01-02 Thread Rick Mann
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  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  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
> #include
> #include
> #include
> #include
> 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(10); 
>   
>}
>
>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.


Re: [beagleboard] Errors reading multiple Analog Inputs from Beaglebone Black using C++

2016-01-01 Thread William Hermans
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 
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
> #include
> #include
> #include
> #include
> 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(10);
>
>}
>
>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.


[beagleboard] Errors reading multiple Analog Inputs from Beaglebone Black using C++

2016-01-01 Thread Sunny Sharma
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
#include
#include
#include
#include
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(10); 
  
   }
   
   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.