libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
pwm_cap.c
Go to the documentation of this file.
1 
20 #define _GNU_SOURCE 1
21 #include "stdio.h"
22 #include <termios.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include "libpruio/pruio.h"
27 #include "libpruio/pruio_pins.h"
28 
30 #define P_OUT P9_21
32 #define P_IN P9_42
33 
41 int
42 isleep(unsigned int mseconds)
43 {
44  fd_set set;
45  struct timeval timeout;
46 
47  /* Initialize the file descriptor set. */
48  FD_ZERO(&set);
49  FD_SET(STDIN_FILENO, &set);
50 
51  /* Initialize the timeout data structure. */
52  timeout.tv_sec = 0;
53  timeout.tv_usec = mseconds * 1000;
54 
55  return TEMP_FAILURE_RETRY(select(FD_SETSIZE,
56  &set, NULL, NULL,
57  &timeout));
58 }
59 
61 int main(int argc, char **argv)
62 {
63  pruIo *Io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0);
64  do {
65  if (Io->Errr) {
66  printf("initialisation failed (%s)\n", Io->Errr); break;}
67 
68  if (pruio_cap_config(Io, P_IN, 2.)) { // configure input pin
69  printf("failed setting input @P_IN (%s)\n", Io->Errr); break;}
70 
71  float_t
72  f1 // Variable for calculated frequency.
73  , d1 // Variable for calculated duty cycle.
74  , f0 = 31250 // The required frequency.
75  , d0 = .5; // The required duty cycle.
76  if (pruio_pwm_setValue(Io, P_OUT, f0, d0)) {
77  printf("failed setting output @P_OUT (%s)\n", Io->Errr); break;}
78 
79  // pin config OK, transfer local settings to PRU and start
80  if (pruio_config(Io, 1, 0x1FE, 0, 4)) {
81  printf("config failed (%s)\n", Io->Errr); break;}
82 
83  struct termios oldt, newt; // make terminal non-blocking
84  tcgetattr( STDIN_FILENO, &oldt );
85  newt = oldt;
86  newt.c_lflag &= ~( ICANON );
87  newt.c_cc[VMIN] = 0;
88  newt.c_cc[VTIME] = 1;
89  tcsetattr( STDIN_FILENO, TCSANOW, &newt );
90 
91  while(1) { // run endless loop
92  if (1 == isleep(1)) {
93  switch (getchar()) { // evaluate keystroke
94  case '0' : d0 = 0.0; break;
95  case '1' : d0 = 0.1; break;
96  case '2' : d0 = 0.2; break;
97  case '3' : d0 = 0.3; break;
98  case '4' : d0 = 0.4; break;
99  case '5' : d0 = 0.5; break;
100  case '6' : d0 = 0.6; break;
101  case '7' : d0 = 0.7; break;
102  case '8' : d0 = 0.8; break;
103  case '9' : d0 = 0.9; break;
104  case ',' : d0 = 1.0; break;
105  case '.' : d0 = 1.0; break;
106  case 'm' : f0 = (f0 > 5.5 ? f0 - 5. : .5); break;
107  case 'p' : f0 = (f0 < 999995. ? f0 + 5. : 1000000.); break;
108  case '*' : f0 = (f0 < 500000. ? f0 * 2 : 1000000.); break;
109  case '/' : f0 = (f0 > 1. ? f0 / 2 : .5); break;
110  case '+' : f0 = 1000000.; break;
111  case '-' : f0 = .5; break;
112  default: goto finish;
113  };
114  if (pruio_pwm_setValue(Io, P_OUT, f0, d0)) { // set new output
115  printf("failed setting PWM output (%s)\n", Io->Errr); break;}
116 
117  printf("\n--> Frequency: %10f , Duty: %10f\n", f0, d0); // info
118  }
119 
120  if (pruio_cap_Value(Io, P_IN, &f1, &d1)) { // get current input
121  printf("failed reading input @P_IN (%s)\n", Io->Errr); break;}
122 
123  printf("\r Frequency: %10f , Duty: %10f ", f1, d1); // info
124  fflush(STDIN_FILENO);
125  }
126 
127 finish:
128  tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); // reset terminal
129 
130  printf("\n");
131  } while (0);
132 
133  pruio_destroy(Io); /* destroy driver structure */
134  return 0;
135 }
Float_t f0
Variable for measured frequency.
Definition: performance.bas:85
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().
float float_t
float data type.
Definition: pruio.h:42
char * pruio_pwm_setValue(pruIo *Io, uint8 Ball, float_t Hz, float_t Du)
Wrapper function for PwmMod::setValue().
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for the constructor PruIo::PruIo().
char * pruio_cap_config(pruIo *Io, uint8 Ball, float_t FLow)
Wrapper function for CapMod::config().
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.
Float_t d1
Variable for calculated duty cycle.
Definition: pwm_cap.bas:40
Float_t d0
The required duty cycle.
Definition: pwm_cap.bas:42
Float_t f1
Variable for calculated frequency.
Definition: pwm_cap.bas:39
#define P_OUT
The pin for PWM output.
Definition: pwm_cap.c:30
int isleep(unsigned int mseconds)
Wait for keystroke or timeout.
Definition: pwm_cap.c:42
int main(int argc, char **argv)
The main function.
Definition: pwm_cap.c:61
#define P_IN
The pin for CAP input.
Definition: pwm_cap.c:32
Wrapper structure for PruIo.
Definition: pruio.h:550
char * Errr
Pointer for error messages.
Definition: pruio.h:551