-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_shots.c
78 lines (56 loc) · 1.56 KB
/
audio_shots.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
/*
$ gcc -O6 -o audio_shots audio_shots.c -lpigpio -lrt -lpthread
$
After start of raspivid_ges:
$ sudo killall pigpiod
$ sudo ./audio_alert N d f o
$
audio_alert waits for digital audio signal.
After o[us], N flashes of length d[us] will be generated at frequency f[Hz].
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <pigpio.h>
#define gpioStrobe 13
#define gpioDaudio 17
int wave_id, N=1;
volatile int done=0;
gpioPulse_t *pulse;
int main(int argc, char *argv[])
{
unsigned int d,f,o;
assert(argc == 1+4);
assert(0 < (N = atoi(argv[1])));
assert(0 < (d = atoi(argv[2])));
assert(0 < (f = atoi(argv[3])));
f = 1000000/f - d;
assert(0 < (o = atoi(argv[4])));
assert(gpioInitialise()>=0);
gpioSetMode(gpioStrobe, PI_OUTPUT);
gpioWaveAddNew();
assert(pulse = malloc(sizeof(gpioPulse_t) * (2*N+1)));
pulse[0].gpioOn = 0;
pulse[0].gpioOff = 1<<gpioStrobe;
pulse[0].usDelay = o;
for(int i=1; i<=N; ++i)
{
pulse[2*i-1].gpioOn = 1<<gpioStrobe;
pulse[2*i-1].gpioOff = 0;
pulse[2*i-1].usDelay = d;
pulse[2*i-0].gpioOn = 0;
pulse[2*i-0].gpioOff = 1<<gpioStrobe;
pulse[2*i-0].usDelay = f;
}
gpioWaveAddGeneric(2*N+1, pulse);
wave_id = gpioWaveCreate();
assert((wave_id >= 0) || !"wave create failed\n");
gpioSetMode(gpioDaudio, PI_INPUT);
while (! gpioRead(gpioDaudio)) { }
gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT);
usleep(N*(d+f)+1000000);
gpioTerminate();
free(pulse);
return 0;
}