-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_wavegen.c
161 lines (133 loc) · 3.56 KB
/
test_wavegen.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pigpio.h>
#include <time.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <unistd.h>
int gpio=21;
#define DUR_OFF 10
#define RAMP_STEP 1 //each pulse during the ramp up period will be shorter by this value
int start_wave(int on);
int rampup_wave(int duration){
printf("Rampup to %i", duration);
// gpioWaveClear();
int ramp_up = 1000;
int wave_no = 0;
while (ramp_up > duration) {
#define NO_OF_PULSES_PER_RAMPSTEP 4
gpioPulse_t pulse[NO_OF_PULSES_PER_RAMPSTEP*2];
for (int i= 0; i < 2*NO_OF_PULSES_PER_RAMPSTEP-1; i++){
pulse[i].gpioOn = (1<<gpio);
pulse[i].gpioOff = 0;
pulse[i].usDelay = ramp_up;
pulse[i+1].gpioOn = 0;
pulse[i+1].gpioOff = (1<<gpio);
pulse[i+1].usDelay = ramp_up;
}
gpioWaveAddGeneric(NO_OF_PULSES_PER_RAMPSTEP*2, pulse);
int wave_id = gpioWaveCreate();
gpioWaveTxSend(wave_id, PI_WAVE_MODE_REPEAT_SYNC);
//gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT);
//while (gpioWaveTxBusy()) usleep(10);
//time_sleep(0.001);
time_sleep(0.0001);
gpioWaveDelete(wave_id);
ramp_up -= RAMP_STEP;
}
//gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT_SYNC);
//gpioWaveChain(wid, wave_no);
//while (gpioWaveTxBusy()) time_sleep(0.0001);
start_wave(duration);
//for (int i=0; i<wave_no; i++) gpioWaveDelete(wid[i]);
return 0;
}
void usage()
{
fprintf
(stderr,
"\n" \
"Usage: sudo test_wavegen gpio_pin pulse_duration\n" \
"\n"
);
exit(1);
}
#define CK_VALID_ADDR(x) if (! ((x>0) && (x<26) ) ) fatal(1, "%d is not a valid gpio \n", x);
int start_wave(int on){
gpioPulse_t pulse[2];
pulse[0].gpioOn = (1<<gpio);
pulse[0].gpioOff = 0;
pulse[0].usDelay = on;
pulse[1].gpioOn = 0;
pulse[1].gpioOff = (1<<gpio);
pulse[1].usDelay = on;
gpioWaveAddGeneric(2, pulse);
int wave_id = gpioWaveCreate();
if (wave_id < 0){
fprintf(stderr, "Wave create error\n");
if (wave_id == PI_EMPTY_WAVEFORM){
fprintf(stderr, "PI_EMPTY_WAVEFORM\n");
}else if (wave_id == PI_TOO_MANY_CBS){
fprintf(stderr, "PI_TOO_MANY_CBS\n");
}else if (wave_id == PI_TOO_MANY_OOL){
fprintf(stderr, "PI_TOO_MANY_OOL\n");
}else if (wave_id == PI_NO_WAVEFORM_ID){
fprintf(stderr, "PI_NO_WAVEFORM_ID\n");
};
return 1;
}
gpioWaveTxSend(wave_id, PI_WAVE_MODE_REPEAT_SYNC);
return 0;
}
void shutdown(int dummy) {
gpioWaveClear();
gpioTerminate();
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
int on=1000;
signal(SIGINT, shutdown);
signal(SIGCONT, shutdown);
signal(SIGTERM, shutdown);
if (argc <= 2){
usage();
}
if (argc > 3)
{
gpioInitialise();
gpioWaveClear();
fprintf(stderr, "Too many params");
return 1;
}
else if (argc > 2)
{
on = atoi(argv[2]);
gpio = atoi(argv[1]);
}
else if (argc > 1)
{
gpio = atoi(argv[1]);
}
printf("gpio=%d on=%d\n", gpio, on);
if (gpioInitialise()<0) return -1;
gpioSetMode(gpio, PI_OUTPUT);
rampup_wave(on);
//start_wave(on);
while (1) {
int var = getchar();
if(var == 's') break;
if(var == '+') {
on += 1;
start_wave(on);
}
if(var == '-') {
on -= 1;
start_wave(on);
}
time_sleep(0.01);
}
gpioWaveClear();
gpioTerminate();
}