OK, here we are:
Code: Select all
//
// test_wave.c
// regul_pi
//
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
//#include <unistd.h>
//#include <signal.h>
#include <string.h>
//#include <fcntl.h>
//#include <byteswap.h>
#include <math.h>
#include <pigpio.h>
#define FREQ_IN 17 //frequency in signal
#define Q1 9 //command Q1
#define Q2 10 //Q2
static uint8_t run = 1;
void func_20ms(int user_gpio, int level, uint32_t tick) {
static int wave_cour = -1, wave_prec = -1, wave_old = -1, n_iter = 0;
static gpioPulse_t pulse[4] = {
//on, off, delay
{1<<Q2,0, 5000},
{0, 1<<Q1, 5000},
{1<<Q1, 0, 5000},
{0, 1<<Q2, 5000}
};
if (level == PI_LOW) return;
if (wave_old >= 0) {
int w_at = gpioWaveTxAt();
if (w_at != wave_old) gpioWaveDelete(wave_old);
else {
fprintf(stderr, "mess in waves : at %d old %d prec %d cour %d nb pulse %d\n", w_at, wave_old, wave_prec, wave_cour, n_iter);
run = 0;
return;
}
}
n_iter++; //to count waves generated
wave_old = wave_prec;
wave_prec = wave_cour;
gpioWaveAddNew();
gpioWaveAddGeneric(4, pulse);
wave_cour = gpioWaveCreate();
if (wave_cour >= 0) {
gpioWaveTxSend(wave_cour, PI_WAVE_MODE_ONE_SHOT_SYNC);
}
else {
fprintf(stderr, "invalid wave id %d @ nb pulse %d\n", wave_cour, n_iter);
run = 0;
return;
}
}
void terminate() {
gpioSetAlertFunc(FREQ_IN, NULL);
gpioTerminate();
fprintf(stderr, "pigpio terminated.\n");
}
int main(int argc, char *argv[]) {
if(gpioInitialise() < 0) {
/* init dans main pour retour code erreur */
fprintf(stderr, "pigpio initialisation failed.\n");
return 1;
}
else fprintf(stderr, "pigpio ok\n");
gpioSetMode(Q1, PI_OUTPUT);
gpioSetMode(Q2, PI_OUTPUT);
// //this is to be used with a hadware signal on input
// gpioSetMode(FREQ_IN, PI_INPUT);
// gpioSetAlertFunc(FREQ_IN, func_20ms);
//alternate simple timer invocation
gpioSetTimerFunc(0, 20, func_20ms);
while (run) ;
terminate();
}
Compilation invocation:
Code: Select all
pi@moulin-turbine ~/pi_turbine/regul_pi $ gcc -o test_wave test_wave.c -lpigpio -lpthread -lrt
pi@moulin-turbine ~/pi_turbine/regul_pi $ sudo ./test_wave
pigpio ok
mess in waves : at 0 old 0 prec 1 cour 2 nb pulse 1113
pigpio terminated.
pi@moulin-turbine ~/pi_turbine/regul_pi $
This sample code fails at some point, when run with a square 50hz signal at input.
In the source code there is a alternate initialization to activate the wave function on a timer basis. Surprisingly enough, when run from timer, the code runs ok (did not wait it to fail). I discovered it when setting up the sample code.
So now the question becomes "Is 20ms too short for the wave gestion to proceed ?" It may depend on other task around. In my real code I have to read an ADC (current intensity value) through i2C. I'm doing it on the low edge of the 50hz input. The whole processing may be too much.