libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
rb_file.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
16 
17 from __future__ import print_function
18 from libpruio import *
19 import time
20 
21 
22 libc = CDLL("libc.so.6")
23 
24 
25 tSamp = 123401
26 
27 tmr = 20000
28 
29 NoStep = 3
30 
31 NoFile = 2
32 
33 NamFil = "output.%u"
34 
35 
36 io = pruio_new(PRUIO_DEF_ACTIVE, 0, 0, 0)
37 try:
38 
39  IO = io.contents
40  if IO.Errr: raise AssertionError("pruio_new failed (%s)" % IO.Errr)
41 
42  if pruio_adc_setStep(io, 9, 0, 0, 0, 0): # step 9, AIN-0
43  raise AssertionError("step 9 configuration failed: (%s)" % IO.Errr)
44  if pruio_adc_setStep(io,10, 1, 0, 0, 0): # step 9, AIN-1
45  raise AssertionError("step 10 configuration failed: (%s)" % IO.Errr)
46  if pruio_adc_setStep(io,11, 2, 0, 0, 0): # step 9, AIN-2
47  raise AssertionError("step 11 configuration failed: (%s)" % IO.Errr)
48 
49 
50  mask = 0b111 << 9
51 
52  tInd = tSamp * NoStep
53 
54  half = ((IO.ESize >> 2) // NoStep) * NoStep
55 
56  if half > tInd: half = tInd # adapt size for small files
57 
58  samp = (half << 1) // NoStep
59 
60  if pruio_config(io, samp, mask, tmr, 0): # upload settings, prepare MM/RB mode
61  raise AssertionError("config failed (%s)" % IO.Errr)
62 
63  if pruio_rb_start(io): # start sampling
64  raise AssertionError("rb_start failed (%s)" % IO.Errr)
65 
66 
67  p0 = IO.Adc.contents.Value
68 
69  p1 = cast(byref(p0.contents, (half << 1)), POINTER(c_ushort))
70  for n in range(0, NoFile):
71 
72  fName = NamFil % n
73  print("Creating file %s" % fName)
74 
75  oFile = libc.fopen(fName, "wb")
76 
77  i = 0
78  while i < tInd:
79  i += half
80  if i > tInd: # fetch the rest(maybe no complete chunk)
81 
82  rest = tInd + half - i
83 
84  iEnd = rest if p1 >= p0 else rest + half
85  while IO.DRam[0] < iEnd: time.sleep(0.001)
86  print(" writing samples %u-%u" % (tInd -rest, tInd-1))
87  libc.fwrite(p0, sizeof(UInt16), rest, oFile) # write data
88  p0, p1 = p1, p0 # swap buffer pointers
89  break
90  if p1 > p0:
91  while IO.DRam[0] < half: time.sleep(0.001) # wait for completion
92  else:
93  while IO.DRam[0] > half: time.sleep(0.001) # wait for completion
94  print(" writing samples %u-%u" % (i-half, i-1))
95  libc.fwrite(p0, sizeof(UInt16), half, oFile) # write bin data
96  p0, p1 = p1, p0 # swap buffer pointers
97  libc.fclose(oFile)
98  print("Finished file %s" % fName)
99 finally:
100  pruio_destroy(io)
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().