libpruio  0.6.6
Fast and easy Digital/Analog Input/Output for Beaglebones
pwm_cap.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 ## \file
3 # \brief Example: PWM output and CAP input.
4 #
5 # This file contains an example on how to measure the frequency and duty
6 # cycle of a pulse train with a eCAP module input. The program sets
7 # another pin as eHRPWM output to generate a pulse width modulated signal
8 # as source for the measurement. The output can be changed by some keys,
9 # the frequency and duty cycle of the input is shown continuously in the
10 # terminal output. Find a functional description in section \ref
11 # sSecExaPwmCap.
12 #
13 # Licence: GPLv3, Copyright 2017-\Year by \Mail
14 #
15 # Run by: `python pwm_cap.py`
16 #
17 # \since 0.6.0
18 
19 import curses
20 from libpruio import *
21 
22 ## The main function
23 def pwm_cap(stdscr):
24  ## Clear and refresh the screen for a blank canvas
25  stdscr.clear()
26  stdscr.nodelay(1)
27 
28  ## Header pin for pwm output
29  POUT = P9_21
30  #POUT = P8_07 # alternate header pin for pwm output
31  ## Header pin for cap input
32  P_IN = P9_42
33 
34  ## Create a ctypes pointer to the pruio structure
35  io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0)
36  try:
37  IO = io.contents # the pointer dereferencing, using contents member
38  if IO.Errr: raise AssertionError("pruio_new failed (%s)" % IO.Errr)
39  if pruio_cap_config(io, P_IN, 2.): # configure input pin
40  raise AssertionError("failed setting input @P_IN (%s)" % IO.Errr)
41  f1 = c_float(0.) # Variable for calculated frequency
42  d1 = c_float(0.) # Variable for calculated duty cycle
43  f0 = c_float(31250) # The required frequency
44  d0 = c_float(.5) # The required duty cycle
45  if pruio_pwm_setValue(io, POUT, f0, d0): # configure output pin
46  raise AssertionError("failed setting output @POUT (%s)" % IO.Errr)
47  # pin config passed, now transfer local settings to PRU and
48  if pruio_config(io, 1, 0x1FE, 0, 4): # start IO mode
49  raise AssertionError("config failed (%s)" % IO.Errr)
50  c = -1 # variable for keyboard character
51  form = "Frequency: %10.2f , Duty: %2.8f" # common format variable
52  stdscr.addstr(0,0, "--> " + form % (f0.value, d0.value)); # show initial demand
53  while True:
54  if pruio_cap_Value(io, P_IN, byref(f1), byref(d1)): # get current input
55  raise AssertionError("failed reading input @P_IN (%s)" % IO.Errr)
56  stdscr.addstr(1,4, form % (f1.value, d1.value)) # and show
57  c = stdscr.getch()
58  if c != -1: # react on user keystrokes
59  if c == ord('-'): f0.value = 0.5
60  elif c == ord('+'): f0.value = 1000000.
61  elif c == ord('m'):
62  if f0.value > 5.5: f0.value -= 5.
63  else: f0.value = .5
64  elif c == ord('p'):
65  if f0.value < 999995.: f0.value += 5.
66  else: f0.value = 1000000.
67  elif c == ord('*'):
68  if f0.value < 500000.: f0.value *= 2
69  else: f0.value = 1000000.
70  elif c == ord('/'):
71  if f0.value > 1.: f0.value /= 2
72  else: f0.value = .5
73  elif c == ord('0'): d0.value = 0.0
74  elif c == ord('1'): d0.value = 0.1
75  elif c == ord('2'): d0.value = 0.2
76  elif c == ord('3'): d0.value = 0.3
77  elif c == ord('4'): d0.value = 0.4
78  elif c == ord('5'): d0.value = 0.5
79  elif c == ord('6'): d0.value = 0.6
80  elif c == ord('7'): d0.value = 0.7
81  elif c == ord('8'): d0.value = 0.8
82  elif c == ord('9'): d0.value = 0.9
83  elif c == ord(','): d0.value = 1.
84  elif c == ord('.'): d0.value = 1.
85  else: break
86  if pruio_pwm_setValue(io, POUT, f0.value, d0.value): # update output
87  raise AssertionError("failed setting PWM output (%s)" % IO.Errr)
88  stdscr.addstr(0,4, form % (f0.value, d0.value)) # show new demand
89  finally:
90  pruio_destroy(io) # we're done
91 
92 if __name__ == "__main__":
93  curses.wrapper(pwm_cap)
char * pruio_config(pruIo *Io, uint32 Samp, uint32 Mask, uint32 Tmr, uint16 Mds)
Wrapper function for PruIo::config().
char * pruio_cap_Value(pruIo *Io, uint8 Ball, float_t *Hz, float_t *Du)
Wrapper function for CapMod::Value().
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for constructor PruIo::PruIo().
char * pruio_cap_config(pruIo *Io, uint8 Ball, float_t FLow)
Wrapper function for CapMod::config().
char * pruio_pwm_setValue(pruIo *Io, uint8 Ball, float_t Hz, float_t Du)
Wrapper function for PwmMod::setValue().
void pruio_destroy(pruIo *Io)
Wrapper function for destructor PruIo::~PruIo().