libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
rb_file.c
Go to the documentation of this file.
1 
19 #include "unistd.h"
20 #include "time.h"
21 #include "stdio.h"
22 #include "libpruio/pruio.h"
23 
25 int main(int argc, char **argv)
26 {
27  const uint32 tSamp = 123401;
28  const uint32 tmr = 20000;
29  const uint32 NoStep = 3;
30  const uint32 NoFile = 2;
31  const char *NamFil = "output.%u";
32  struct timespec mSec;
33  mSec.tv_nsec = 1000000;
34  pruIo *io = pruio_new(PRUIO_DEF_ACTIVE, 0, 0, 0);
35  if (io->Errr){
36  printf("constructor failed (%s)\n", io->Errr); return 1;}
37 
38  do {
39  if (pruio_adc_setStep(io, 9, 0, 0, 0, 0)){ // step 9, AIN-0
40  printf("step 9 configuration failed: (%s)\n", io->Errr); break;}
41  if (pruio_adc_setStep(io,10, 1, 0, 0, 0)){ // step 10, AIN-1
42  printf("step 10 configuration failed: (%s)\n", io->Errr); break;}
43  if (pruio_adc_setStep(io,11, 2, 0, 0, 0)){ // step 11, AIN-2
44  printf("step 11 configuration failed: (%s)\n", io->Errr); break;}
45 
46  uint32 mask = 7 << 9;
47  uint32 tInd = tSamp * NoStep;
48  uint32 half = ((io->ESize >> 2) / NoStep) * NoStep;
49 
50  if (half > tInd){ half = tInd;} // adapt size for small files
51  uint32 samp = (half << 1) / NoStep;
52 
53  if (pruio_config(io, samp, mask, tmr, 0)){ // configure driver
54  printf("config failed (%s)\n", io->Errr); break;}
55 
56  if (pruio_rb_start(io)){
57  printf("rb_start failed (%s)\n", io->Errr); break;}
58 
59  uint16 *p0 = io->Adc->Value;
60  uint16 *p1 = p0 + half;
61  uint32 n;
62  char fName[20];
63  for(n = 0; n < NoFile; n++){
64  sprintf(fName, NamFil, n);
65  printf("Creating file %s\n", fName);
66  FILE *oFile = fopen(fName, "wb");
67  uint32 i = 0;
68  while(i < tInd){
69  i += half;
70  if(i > tInd){ // fetch the rest(maybe no complete chunk)
71  uint32 rest = tInd + half - i;
72  uint32 iEnd = p1 >= p0 ? rest : rest + half;
73  while(io->DRam[0] < iEnd) nanosleep(&mSec, NULL);
74  printf(" writing samples %u-%u\n", tInd -rest, tInd-1);
75  fwrite(p0, sizeof(uint16), rest, oFile);
76  uint16 *swap = p0;
77  p0 = p1;
78  p1 = swap;
79  break;
80  }
81  if(p1 > p0) while(io->DRam[0] < half) nanosleep(&mSec, NULL);
82  else while(io->DRam[0] > half) nanosleep(&mSec, NULL);
83  printf(" writing samples %u-%u\n", i-half, i-1);
84  fwrite(p0, sizeof(uint16), half, oFile);
85  uint16 *swap = p0;
86  p0 = p1;
87  p1 = swap;
88  }
89  fclose(oFile);
90  printf("Finished file %s\n", fName);
91  }
92  } while(0);
94  return 0;
95 }
VAR io
create new driver UDT
Definition: 1.bas:18
UInt32 n
The counter for test cycles.
Definition: performance.bas:90
unsigned short uint16
16 bit unsigned integer data type.
Definition: pruio.h:40
unsigned int uint32
32 bit unsigned integer data type.
Definition: pruio.h:41
char * pruio_config(pruIo *Io, uint32 Samp, uint32 Mask, uint32 Tmr, uint16 Mds)
Wrapper function for PruIo::config().
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for the constructor PruIo::PruIo().
char * pruio_adc_setStep(pruIo *Io, uint8 Stp, uint8 ChN, uint8 Av, uint8 SaD, uint32 OpD)
Wrapper function for AdcUdt::setStep().
char * pruio_rb_start(pruIo *Io)
Wrapper function for PruIo::rb_start().
void pruio_destroy(pruIo *Io)
Wrapper function for the destructor PruIo::~PruIo().
@ PRUIO_DEF_ACTIVE
Activate all subsystems.
Definition: pruio.h:531
const ANY_PTR NULL
The NULL pointer.
const tmr
The sampling rate in ns (20000 -> 50 kHz).
Definition: rb_file.bas:22
const NoStep
The number of active steps (must match setStep calls and mask).
Definition: rb_file.bas:23
VAR iEnd
The last byte of the rest.
Definition: rb_file.bas:67
VAR mask
The active steps (9 to 11).
Definition: rb_file.bas:40
VAR rest
The rest of the buffer (in bytes)
Definition: rb_file.bas:66
VAR samp
The number of samples (per step).
Definition: rb_file.bas:45
VAR i
Start index.
Definition: rb_file.bas:62
const tSamp
The number of samples in the files (per step).
Definition: rb_file.bas:21
VAR half
The maximum index of the half ring buffer.
Definition: rb_file.bas:42
VAR tInd
The maximum total index.
Definition: rb_file.bas:41
VAR p0
A pointer to the start of the ring buffer.
Definition: rb_file.bas:53
const NamFil
The output file names.
Definition: rb_file.bas:25
VAR p1
A pointer to the middle of the ring buffer.
Definition: rb_file.bas:54
const NoFile
The number of files to write.
Definition: rb_file.bas:24
int main(int argc, char **argv)
The main function.
Definition: rb_file.c:25
Wrapper structure for PruIo.
Definition: pruio.h:550