Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
AnalogDump.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string>
4 
5 #include "../hubolib.h"
6 
7 using namespace HuboLib;
8 using namespace std;
9 
10 /*
11 Compile and link:
12  g++ AnalogDump.cpp -L../ -lhubo -lpthread -lrt -o AnalogDump
13 Run:
14  sudo ./AnalogDump
15 Purpose:
16  Dump all analog channels into a file.
17 */
18 
19 string GetDateTime(char seperator, char timeSeperator);
20 bool AppendFile(string sFile, string sText);
21 
22 int main(int argc, char* argv[])
23 {
24  if (argc != 3)
25  {
26  printf ("Usage: \n Analogdump <sample rate [ms]> <filename>\n example: Analogdump 1000 test.csv\n");
27  return 0;
28  }
29 
30  long logSpeed = 1000;
31  if (sscanf (argv[1], "%ld", &logSpeed) != 1)
32  {
33  printf ("Logspeed could not be determined!\n");
34  return 0;
35  }
36 
37  char buffer [128];
38  if (sscanf (argv[2], "%s", buffer) != 1)
39  {
40  printf ("File name could not be determined!\n");
41  return 0;
42  }
43  string sFile = buffer;
44 
45  // Initialize the library once in your program.
46  if (!Initialize())
47  {
48  printf ("Error: Initialize\n");
49  return 1;
50  }
51 
52  // Define all channels to be read (set to 1) from the ADC.
53  unsigned short overSampling[MAX_MCP3x08_CHANNELS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
54  Set_MCP3x08_Oversampling (overSampling);
55 
56  // Wait as long until we have valid values in the buffer.
58 
59  int channel = 0;
60  unsigned long adcCount;
61  double volt;
62  string sLine = "";
63 
64  while (1)
65  {
66  sLine = GetDateTime(' ', ':');
67  sLine += " ";
68 
69  for (int channel=0; channel<MAX_MCP3x08_CHANNELS; channel++)
70  {
71  // Read channel and output its value.
72  if (Get_AI_Channel (channel, adcCount, volt))
73  {
74  sprintf (buffer, "%04ld ", adcCount);
75  }
76  else
77  {
78  sprintf (buffer, " ");
79  }
80  sLine += buffer;
81  }
82  sLine +="\n";
83  printf ("%s", sLine.c_str());
84  AppendFile(sFile, sLine);
85 
86  usleep(1000L * logSpeed);
87  }
88 
89  // Free library resources.
90  Uninitialize();
91 
92  return 0;
93 }
94 
95 string GetDateTime(char seperator, char timeSeperator)
96 {
97  time_t t = time(0); // get time now
98  struct tm *pNow = localtime (&t);
99  char buffer[128];
100  sprintf (buffer, "%02d.%02d.%d%c%02d%c%02d%c%02d", pNow->tm_mday, (pNow->tm_mon + 1), (pNow->tm_year + 1900), seperator, pNow->tm_hour, timeSeperator, pNow->tm_min, timeSeperator, pNow->tm_sec);
101  // printf ("Time = %s\n", buffer);
102  return string(buffer);
103 }
104 
105 bool AppendFile(string sFile, string sText)
106 {
107  FILE* pFile = fopen (sFile.c_str(), "a+");
108  if (!pFile)
109  return false;
110  bool bResult = fputs (sText.c_str(), pFile) > 0;
111  fflush (pFile);
112  fclose(pFile);
113  return bResult;
114 }
115 
bool Wait_For_MCP3x08_Buffered_Values()
Waits until the input buffer of the MCP3x08 are initialized from the hardware.
#define MAX_MCP3x08_CHANNELS
Definition: hubolib.h:39
bool Initialize()
Initializes the library.
bool AppendFile(string sFile, string sText)
Definition: AnalogDump.cpp:105
bool Get_AI_Channel(int channel, unsigned long &count, double &volt)
Get the buffered and oversampled data from the MCP3x08.
bool Set_MCP3x08_Oversampling(unsigned short overSampling[MAX_MCP3x08_CHANNELS])
Specifies the ADC channels to be sampled as well as the number they get oversampled.
void Uninitialize()
Releases any resources bound to the library.
string GetDateTime(char seperator, char timeSeperator)
Definition: AnalogDump.cpp:95
int main(int argc, char *argv[])
Definition: AnalogDump.cpp:22