2017-09-24 11:52 PM
I'm actually I want to implement some such as a flag function into my code.
But I'm struggling with implementation in c.
What is the fairly common way to implement a flag function?
int trigger =0;
main() {
while(1) {
switch (num)
case0
case1 :
trigger = 0
..
case6 : trigger = 1
if (trigger==1)
printf('trigger-on');
else
printf('trigger-off');
}
}
As you can see the problem of above is that if case 6 is activated, then we can see lots of '
trigger-on' printed messages.
But I want this just to do one time with trigger or flag..
What is the fairly common way to implement a flag function?
Solved! Go to Solution.
2017-09-25 12:52 AM
First, what has this question with STM micros issues to do? Well, I think there is a lot of ways to solve this. Anyway you have to introduce some variable that would track your prints:
int last_trigger = -1; // init to a special value out of 'trigger' range for the very first printout.
...
if (last_trigger != trigger)
{
// do print
last_trigger = trigger;
}
2017-09-25 12:34 AM
You forgot the breaks for the cases and the functional block for the entire switch.
Also, after acting on the trigger flag you can turn off the trigger.
int main() {
while(1) {
switch (num) {
case 0:
break;
case 1:
trigger = 0
break;
//..
case 6:
trigger = 1
break;
default:
break;
}
if (trigger == 1) {
printf('trigger-on');
trigger = 0;
} else {
printf('trigger-off');
}
}
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
2017-09-25 12:52 AM
First, what has this question with STM micros issues to do? Well, I think there is a lot of ways to solve this. Anyway you have to introduce some variable that would track your prints:
int last_trigger = -1; // init to a special value out of 'trigger' range for the very first printout.
...
if (last_trigger != trigger)
{
// do print
last_trigger = trigger;
}
2017-09-27 03:37 PM
Unfortunately, the code above would print out 'trigger-off' all the time, also at the next pass after a single 'trigger-on' after the case 6. You have either to track the printouts by an additional flag, as I suggested in my reply before, or to reset the 'trigger' to, say, -1 after printing 'trigger-on' out and to add 'if (trigger == 0)' to print 'trigger-off' out.
OT: what HTML tags to format the source code in the articles?