-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimpleGPIO_tester.cpp
executable file
·89 lines (81 loc) · 3.34 KB
/
SimpleGPIO_tester.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "SimpleGPIO_thread.h"
#include <stdio.h>
/* ******************************** makes a GPIO output that does software pulse width modulation *********************************************/
static const int GPIO_PIN =22;
static const int POLARITY = 0;
int main(int argc, char **argv){
// test a threadless
printf ("GPIO peri users = %d.\n", GPIOperi_users);
SimpleGPIOStructPtr nt = newThreadlessGPIO (GPIO_PIN, POLARITY);
printf ("GPIO peri users = %d.\n", GPIOperi_users);
for (int ii =0; ii < 100; ii +=1){
setThreadlessGPIO (nt, 1);
for (int ii =0; ii < 10000000; ii +=1){
if (ii % 100000 == 0){
printf ("waiting high = %d.\n", ii);
}
}
setThreadlessGPIO (nt, 0);
for (int ii =0; ii < 10000000; ii +=1){
if (ii % 100000 == 0){
printf ("waiting low = %d.\n", ii);
}
}
}
delete nt;
unUseGPIOperi ();
printf ("GPIO peri users = %d.\n", GPIOperi_users);
// Use threadMaker function to make a simpleGPIO_thread, an infinite train with frequency = 1kHz, and duty cycle = 0.001
// SimpleGPIO_threadMaker calls pulsedThread constructor
SimpleGPIO_thread * myGPIO= SimpleGPIO_thread::SimpleGPIO_threadMaker (GPIO_PIN, POLARITY, (float)100,(float)0.001, (float)0, ACC_MODE_SLEEPS_AND_OR_SPINS);
printf ("GPIO peri users = %d.\n", GPIOperi_users);
if (myGPIO == nullptr){
printf ("SimpleGPIO_thread object was not created. Now exiting...\n");
return 1;
}
// make an array of floats and fill it with values that vary from 0 to 1 in a sinusoidal fashion
float * endFuncArrayData = new float [100];
myGPIO->cosineDutyCycleArray (endFuncArrayData, 100, 100, 0.5, 0.5);
// Set a pointer to this array as endFunc data
myGPIO->setUpEndFuncArray (endFuncArrayData, 100, 1);
// set the endFunc to modify pulse dutycycle from the array
//myGPIO->setEndFunc (&pulsedThreadDutyCycleFromArrayEndFunc);
myGPIO->chooseArrayEndFunc (kDUTY_CYCLE);
/*pulsedThreadArrayModStructPtr myModder = new pulsedThreadArrayModStruct;
myModder->modBits =3 ; // to mod array start and end position
myModder-> startPos = 64; // start outputting in the middle
myModder-> endPos = 96;
int result = myGPIO->modCustom (&pulsedThreadSetArrayLimitsCallback, myModder, 1); */
// request thread to output 128 trains, as this is a multiple of the period, we start low and end low, should take 32 seconds
myGPIO ->startInfiniteTrain ();
myGPIO->waitOnBusy (20);
int result = myGPIO->setEndFuncArrayLimits (24, 75, 1);
if (result){
printf ("modCustom calling setEndFuncArrayLimits reported an error. Now exiting...\n");
return 1;
}
myGPIO->waitOnBusy (20);
myGPIO ->stopInfiniteTrain();
myGPIO->waitOnBusy (100);
/*
myGPIO ->DoTasks (12800);
// wait til the trains are all done, or 60 seconds, which is lots
printf ("Trains left = %d.\n", myGPIO->waitOnBusy (60));
// test use of pulsedThreadSetArrayLimitsCallback from modCustom to set array poition
int result = myGPIO->setEndFuncArrayLimits (64, 96, 0);
if (result){
printf ("modCustom calling setEndFuncArrayLimits reported an error. Now exiting...\n");
return 1;
}
myGPIO ->DoTasks (12800);
printf ("Trains left = %d.\n", myGPIO->waitOnBusy (60));
*/
// clean up
delete myGPIO;
printf ("GPIO peri users = %d.\n", GPIOperi_users);
delete (endFuncArrayData);
return 0;
}
/*
g++ -O3 -std=gnu++11 -Wall -lpulsedThread GPIOlowlevel.cpp SimpleGPIO_thread.cpp SimpleGPIO_tester.cpp -o SimpleGPIOtester
*/