Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

adxl-simple.c

Go to the documentation of this file.
00001 
00013 #include <sys/types.h>
00014 
00015 #include <assert.h>
00016 #include <stdlib.h>
00017 #include <stdio.h>
00018 #include <math.h>
00019 #include <unistd.h>
00020 #include <stdint.h>
00021 #include <string.h>
00022 #include <signal.h>
00023 #include <time.h>
00024 #include <getopt.h>
00025 
00026 #include <sys/termios.h>
00027 #include <fcntl.h>
00028 #include <stdbool.h>
00029 
00030 #include "flog.h"
00031 #include "nsds_util.h"
00032 #include "adxl_util.h"
00033 
00034 // ---------------------------------------------------------------------
00035 // Globals
00036 
00037 
00038 // ------------------------------------------------------------------------
00045 void adxl_do_work(const char *port)
00046 {
00047     char          me[] = "adxl_do_work";
00048 //    uint32_t      idx = 0;
00049     uint32_t      data_idx = 0;
00050     char          *data_buf = NULL;
00051     int           bufsize;
00052     int           rc;
00053     int           comm_fd = -1;
00054     adxl_reading_t data;
00055   
00056     
00057     comm_fd = adxl_open(port);
00058     if(comm_fd <= 0)
00059     {
00060         flog_usr(FLOG_ERROR, FL_ERR_UNKNOWN, me,
00061                  "Unable to open ADXL connection");
00062         return;
00063     }
00064     
00065     // Figure space required
00066     bufsize = (sizeof(char) * NUM_ADXL_CHANNELS * (DATUM_LEN + 4));
00067 
00068     // Allocate send buffer
00069     data_buf = (char *) malloc(bufsize);
00070     if(data_buf == NULL)
00071     {
00072         flog_usr(FLOG_ERROR, FL_ERR_NO_MEMORY, me,
00073                  "Malloc failure on %d bytes!", bufsize);
00074 
00075         return;
00076     }
00077 
00078     flog_usr(FLOG_NOTICE, 0, me, "Main loop running");
00079     
00080     // Run until port closed, error, control-c or done streaming
00081     while(!control_break)
00082     {
00083         sleep(1);
00084 
00085         // read a value from the daq, blocking read
00086         rc = adxl_read(comm_fd, &data);
00087         if(rc != 0)
00088         {
00089             flog_usr(FLOG_ERROR, FL_ERR_UNKNOWN, me,
00090                      "Error %d reading from ADXL", rc);
00091             break;
00092         }
00093 
00094         sprintf(data_buf, " X: %6.5f Y: %6.5f", 
00095                 data.G[0], data.G[1]);
00096 
00097         // Track ttl xmitted bytes
00098         data_idx++;
00099             
00100         // DDT, dump to screen
00101         flog_usr(FLOG_DEBUG, 0, me, data_buf);
00102     }
00103     
00104     flog_usr(FLOG_NOTICE, 0, me,
00105              "Exiting, %d points or so",  data_idx);
00106 
00107     free(data_buf);
00108     adxl_close(comm_fd);
00109     
00110     return;
00111 }
00112 
00113 // ---------------------------------------------------------------------
00127 int main(int argc, char *argv[])
00128 {
00129     char          me[] = "main";
00130     int           help_flag = 0;
00131     int           idx, rc;
00132     char          *port = NULL;
00133     
00134     struct option long_options[] = 
00135         {
00136             {"help", no_argument, &help_flag, 'h'},
00137             {"port", required_argument, 0, 'p'},
00138             {0, 0, 0, 0}
00139         };
00140     
00141     // Set logging options
00142     flog_set_report(FLOG_L3BUG, FLOG_QUIET, FLOG_QUIET);
00143     flog_set_style(0x2019, 0x0038, 0x6008);    
00144 
00145     // Parse command line with getopt_long
00146     while(1)
00147     {
00148         rc = getopt_long(argc, argv, "p:r:h",
00149                          long_options, &idx);
00150         if(rc == -1)
00151             break;
00152 
00153         switch(rc)
00154         {
00155         case 0:
00156             /* If this option set a flag, do nothing else now. */
00157             break;
00158 
00159         case '?':
00160             /* getopt_long already printed an error message. */
00161             break;
00162 
00163         case 'h':
00164             help_flag = 1;
00165             break;
00166 
00167         case 'p':
00168             port = optarg;
00169             flog_usr(FLOG_NOTICE, 0, me, "Setting port to '%s'", port);
00170             break;
00171 
00172         default:
00173             flog_usr(FLOG_ERROR, FL_ERR_SYSTEM, me,
00174                      "Unreachable case in getopt_long parse");
00175             exit(1);
00176         }
00177     }
00178 
00179     if(help_flag != 0)
00180     {
00181         print_args(long_options);
00182         return(1);
00183     }
00184     
00185     flog_usr(FLOG_NOTICE, 0, me,
00186              "Compiled for %d data channels", NUM_ADXL_CHANNELS);
00187     
00188     // Signal handler
00189     flog_usr(FLOG_NOTICE, 0, me, "Installing signal handler");
00190     control_break = false;
00191     signal(SIGINT, adxl_sighandler);
00192     signal(SIGPIPE, adxl_sighandler);
00193 
00194     // Call main routine
00195     adxl_do_work(port);
00196 
00197     // Bail
00198     flog_usr(FLOG_NOTICE, 0, me, "Done");
00199     return(0);
00200 }

Generated on Fri May 2 15:13:15 2003 for ADXL202-NEESgrid driver by doxygen1.3