Advertisement
j0h

sensorData.cpp

j0h
Mar 10th, 2023 (edited)
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cmath>
  5. #include <unistd.h>
  6.  
  7. using namespace std;
  8.  int num=0;
  9.  int val=0;
  10.  double sum=0;
  11.  double avg=0.0;
  12.  double stDev=0.0;
  13.  double err=0.0;
  14.  ifstream myFile_Handler;
  15. int ReadSensor(string FileName);
  16. int main(){
  17. string FileX="/sys/bus/iio/devices/iio:device2/in_accel_x_raw";
  18. string FileY="/sys/bus/iio/devices/iio:device2/in_accel_y_raw";
  19. string FileZ="/sys/bus/iio/devices/iio:device2/in_accel_z_raw";
  20. for (;;){
  21.     cout << " X: " << ReadSensor(FileX) << " Y: " << ReadSensor(FileY) << " Z: "<< ReadSensor(FileZ) << endl;
  22.     sleep(1);
  23.     cout << "\x1b[A" << "\33[2K\r" << std::flush;
  24. }
  25.     return 0;
  26. }
  27.  
  28.  
  29.  int ReadSensor(string FileName){
  30.          int val;
  31.          ifstream myFile_Handler;
  32.     string myLine;
  33.     myFile_Handler.open(FileName);
  34. // /sys/bus/iio/devices/iio\:device2/in_accel_x_raw
  35.     if(myFile_Handler.is_open())    {
  36.         while(getline(myFile_Handler, myLine)){
  37.                val = std::stoi(myLine);
  38.         }
  39.     myFile_Handler.close();
  40.     }    else    {
  41.         cout << "Unable to open the file!";
  42.     }
  43.  
  44.   return val;
  45.      }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement