How do I use a volatile variable as a boolean?

M

mahdert

Hello,
I am doing imbedded programming in C and I was wondering how to use a
volatile variable as a Boolean. In this case, I am reading some data
and my variable is 'delta_X’ which is a floating point. I would like
the unit to power down if this value does not change, say for 1
minute. How do I do this?

if (delta_X does not change for one minute) {powerdown();}
 
E

Eric Sosman

Hello,
I am doing imbedded programming in C and I was wondering how to use a
volatile variable as a Boolean. In this case, I am reading some data
and my variable is 'delta_X’ which is a floating point. I would like
the unit to power down if this value does not change, say for 1
minute. How do I do this?

if (delta_X does not change for one minute) {powerdown();}

Assuming delta_X is the volatile variable, and that its
value changes "all by itself" with no other notifications or
side-effects, I think the best you can do is read delta_X
frequently, and note the time whenever its new value is unequal
to the last-read value:

void call_me_often(void) {
static float old_delta_X = SOME_IMPOSSIBLE_VALUE;
static time_t last_change_time;
float new_delta_X;
time_t current_time;

new_delta_x = delta_X;
current_time = time(NULL);
if (new_delta_x == old_delta_x) {
if (difftime(current_time, last_change_time) >= 60.0)
powerdown();
}
else {
old_delta_X = new_delta_X;
last_change_time = current_time;
}
}

(I'm not sure `float' is correct; adjust as needed. Also, a
free-standing implementation might not have time_t, time(), and
difftime(); substitute whatever timekeeping facilities are
available.)

A drawback of this approach is that if delta_X changes from
42 to 24 and then back to 42 between two call_me_often() calls,
you'll think there was no change. Unless there's more going on
than you've told us, I see no way to avoid the problem. Even
if your environment supports multiple threads of execution and
you can devote a thread to doing nothing but call_me_often()
over and over again, the change-and-change-back vulnerability
will still be there. If you have some knowledge of how fast
delta_X can change, and can poll faster, the problem goes
away -- but if delta_X can change faster than you can poll,
I think you'll just have to hope for the best.
 
M

mahdert

     Assuming delta_X is the volatile variable, and that its
value changes "all by itself" with no other notifications or
side-effects, I think the best you can do is read delta_X
frequently, and note the time whenever its new value is unequal
to the last-read value:

        void call_me_often(void) {
            static float old_delta_X = SOME_IMPOSSIBLE_VALUE;
            static time_t last_change_time;
            float new_delta_X;
            time_t current_time;

            new_delta_x = delta_X;
            current_time = time(NULL);
            if (new_delta_x == old_delta_x) {
                if (difftime(current_time, last_change_time) >= 60.0)
                    powerdown();
            }
            else {
                old_delta_X = new_delta_X;
                last_change_time = current_time;
            }
        }

(I'm not sure `float' is correct; adjust as needed.  Also, a
free-standing implementation might not have time_t, time(), and
difftime(); substitute whatever timekeeping facilities are
available.)

     A drawback of this approach is that if delta_X changes from
42 to 24 and then back to 42 between two call_me_often() calls,
you'll think there was no change.  Unless there's more going on
than you've told us, I see no way to avoid the problem.  Even
if your environment supports multiple threads of execution and
you can devote a thread to doing nothing but call_me_often()
over and over again, the change-and-change-back vulnerability
will still be there.  If you have some knowledge of how fast
delta_X can change, and can poll faster, the problem goes
away -- but if delta_X can change faster than you can poll,
I think you'll just have to hope for the best.

Thanks Eric,
I like your approach and I have used it before. However, I was asking
if the was a simple command in C that does this automatically.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top