SIGALRM and Wheezy

Hi All,

Is there anything special to getting the handler for SIGALRM working in the (default PREEMPT) Wheezy distro?

I've followed the examples I've found to a tee (I *think*) and from all indications it seems that the handler is not 'firing' so I'm not sure if the alarm is not being raised or the hander is faulty.

I've tried two methods: 'timer' and via the alarm() function call.

Any tips appreciated, example code follows:

#include #include #include #include #include #include

#define LED_R 7

void blinkRed(int); void timerSetup(long int, long int); void sig_hdlr(int);

volatile sig_atomic_t toggle_flag = false;

int main(void) { wiringPiSetupGpio();

printf("GPIO Setup\n"); pinMode(LED_R, OUTPUT);

signal(SIGALRM, sig_hdlr);

alarm(1); // timerSetup(0, 500000);

for(;;) {

if(toggle_flag) { blinkRed(); printf("Something's happening!\n"); toggle_flag = false; alarm(1); }

return 0; }

void timerSetup(long int sec, long int usec) { struct itimerval timer1;

timer1.it_interval.tv_usec = usec; timer1.it_interval.tv_sec = sec; timer1.it_value.tv_usec = usec; timer1.it_value.tv_sec = sec;

setitimer(ITIMER_REAL, &timer1, NULL); }

void sig_hdlr(int sig) { toggle_flag = true; }

void blinkRed(void) { static int toggle = 0;

if(toggle == 1) { digitalWrite(LED_R, HIGH); toggle = 0; } else { digitalWrite(LED_R, LOW); toggle = 1; } }

--
Cheers, 
Chris
Reply to
Chris
Loading thread data ...

If intersted in optimising:

can be replaced with:

void blinkRed (void) { digitalWrite (LED_R, !digitalRead (LED_R)) ; }

Gordon

Reply to
Gordon Henderson

Decicded to have a look at the rest of it.

This program will not compile as it stands, however after fixing a few typos it works just fine.

Well - I did it on a Linux desktop and not a Pi and replaced the blink with a simple printf and that printed out "blink" once a second.

What is it that you're actually trying to achieve? If it's just to blink an LED at a regular interval in your code, then you might be better off using a thread to do it.

The easy way

#include

void blinkRed (void) { for (;;) { delay (1000) ; digitalWrite (LED_R, !digitalRead (LED_R)) ; } }

and start it with:

piThreadCreate (blinkRed) ;

inside the main program.

compile with -lpthread -lwiringPi

Gordon

Reply to
Gordon Henderson

That probably compiles to the same amount of code :-)

--
the biggest threat to humanity comes from socialism, which has utterly  
diverted our attention away from what really matters to our existential  
survival, to indulging in navel gazing and faux moral investigations  
into what the world ought to be, whilst we fail utterly to deal with  
what it actually is.
Reply to
The Natural Philosopher

Yet it's optimised for reading.

Reply to
Stefan Enzinger

That depends on how good your skill set is: I would say the first example is easier for a code newb to understand.

And, depending on the overhead of digitalRead() may run faster too..

void blinkRed(void) { static int toggle; digitalWrite(LED_R, (toggle = !toggle)? HIGH:LOW); }

works for me ;-)

One more variable, one less function call.

BUT ARMS are very good at memory accesses.

--
the biggest threat to humanity comes from socialism, which has utterly  
diverted our attention away from what really matters to our existential  
survival, to indulging in navel gazing and faux moral investigations  
into what the world ought to be, whilst we fail utterly to deal with  
what it actually is.
Reply to
The Natural Philosopher

It could be there's errors, I excised a few as-yet-unused parts of code..

But the whole compiled fine on the Pi itself (it's an RPi2 running Raspbian Wheezy 7) the compiler options included -Wall...

Basically building up to a datalogger of sorts with no UI except for some LEDs and SSH to be put in an equipment cabinet for weeks/months.

Looks nice, I'll try it. Haven't programmed for Linux before.

Thanks.

--
Cheers, 
Chris.
Reply to
Chris

It works, though I get the following warning:

"warning: passing argument 1 of 'piThreadCreate' from incompatible pointer type [enabled by default]" note: expected 'void * (*)(void *)' but argument is of type 'void (*)(void)'

The blinkRed is declared as above, & I get the same warning regardless of whether I call piThreadCreate with 'blinkRed', '&blinkRed', '*blinkRed'...

If I call piThreadCreate with blinkRed() then I get the error: 'error: invalid use of void expression'

The gcc version on the Pi2 is 4.6.3-14

Thoughts?

--
Cheers, 
Chris.
Reply to
Chris

[snip]

Nevermind, I found the wfi.c example :-) All good now.

--
Cheers, 
Chris.
Reply to
Chris

On Thu, 19 Nov 2015 11:00:50 +1100, Chris declaimed the following:

Forgive the neophyte intrusion, but given that error message my first thought would be to declare it as

void * blinkRed(void *)

At the least, making the argument (void *)...

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/
Reply to
Dennis Lee Bieber

Yes, sorry - I forgot my own code - should have been:

PI_THREAD (blinkRed) { ... }

which is just an easy macro to hide the void * argument.

My thread stuff is just some simple wrappers round the pthreads stuff.

Cheers,

Gordon

Reply to
Gordon Henderson

[snip]

Just wanted to say thanks for your effort in producing wiringPi, it certainly helps make the Pi a bit more Arduino-like (I think that was one of your stated aim?) with which I was more familiar.

--
Cheers, 
Chris.
Reply to
Chris

Thanks.

Yes - making it familiar to ardino folks was an initial aim, although I got the impression that a lot of Pi people wanted to eschew the whole arduino world - I had a huge amount of hassle (and even my first death threat!) over my wiringPi pin numbering scheme too (which is an abstraction from the native broadcom pin numbers) So when the foundation changed the gpio pins everyone using native numbers had to change their code, but the wiringPi users didn't...

Hey ho... Next think you know, I'll be promoting BASIC on the Pi .. ;-)

Gordon

Reply to
Gordon Henderson

No wonder you get death threats... :)

Reply to
Tony van der Hoff

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.