I combine C examples of pigpio for gpioSetAlertFunc
and gpioWaveTxSend
working with the same pin.
My PWM has several pulses, so I expect alert function to print out a message for me several times. Sometimes it happens, but sometimes I run the program, but alert function is not called.
```
int res=gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT);
```
...and res
gets correct value (no error).
So what might be a reason, that alert function is not called?
I add code snippets to clarify:
//includes
static int counter_edge=0;
void countEdge(int gpio, int level, uint32_t tick)
{
counter_edge++;
printf("GPIO %d became %d at %u\n", gpio, level, tick);
}
//the function follows example from pigpio
int send_pwm(uint32_t pin)
{
counter_edge = 0;
gpioCfgClock(1,0,-1);
if (gpioInitialise() < 0)
{
fprintf(stderr, "pigpio initialisation failed\n");
return 1;
}
//!!! alert function assigned to the pin
//!!! call countEdge whenever GPIO changes state
gpioSetAlertFunc(pin, countEdge);
// Construct and send a 30 microsecond square wave.
gpioSetMode(pin, PI_OUTPUT);
const int pulse_size=16;
gpioPulse_t pulse[pulse_size];
uint32_t try_send_cnt=0,crc_pwm;
uint32_t i=0;
uint32_t interval=10; //us microseconds
uint32_t gpio_pin = (1<<pin);
crc_pwm=0;
for(i=0;i<pulse_size;i++){
if(i%2)
{
pulse[i].gpioOn = gpio_pin;
pulse[i].gpioOff = 0;
pulse[i].usDelay = interval;
}
else
{
pulse[i].gpioOn = 0;
pulse[i].gpioOff = gpio_pin;
pulse[i].usDelay = interval;
}
}
crc_pwm=0;
for(i=1;i<pulse_size;i++)
if(pulse[i].gpioOn!=pulse[i-1].gpioOn)
crc_pwm++;
// alert function should be triggered crc_pwm times
if (wave_id >= 0)
{
for(try_send_cnt=0;try_send_cnt<20;try_send_cnt++)
{
counter_edge = 0;
res_pwm=gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT);
if(counter_edge==crc_pwm)
break;
}
if(res_pwm==PI_BAD_WAVE_ID||res_pwm==PI_BAD_WAVE_MODE)
printf("ERROR: failure in PWM\n");
if(counter_edge!=crc_pwm)
printf("ERROR: Pwm failed after several attemps (%d %d)\n",counter_edge,crc_pwm);
else
printf("DONE %d %d\n",counter_edge,crc_pwm);
}
gpioTerminate();
}
//tests in main
int main(){
send_pwm(17);
send_pwm(18);
return 0;
}
as a result I observe a message like "GPIO 17 became 1" only sometimes.