Hello All,
I am writing basic state machine which maintains 8 different states and there is posibility that system may be in multiple states at a time (Except for state1 to state3. menas only once state can be active at a time from state1 to state3).
I have declared enum like one below
So you have a variable (e.g. state ) that presumably should always have no more than one of the three low order bits set. (Or, maybe you mean it should always have exactly one of the three low order bits set. Your specification is ambiguous as to which of these you mean.)
How will you tell your machine to change state?
Will you call a function to change the state? If so, why is that function not just an assignment to state ? Is it that you want to verify that the requested new state has no more than one (or exactly one) of the three low order bits set?
Maybe you want a function that takes two arguments, states to add to state and states to remove from state .
What language are you using? (I could guess C, but it isn't the only language that includes enums.)
This makes no sense. If your program isn't aware of your current state (previous state when you want to change to a new set of states), then setting the new state is just a statement like:
state = state1;
or
state = state3 | state4 | state7;
If you can't restrain yourself from writing code like:
state = state1 | state2;
and you aren't going to set up a function to perform verification of state changes, I have no idea what you are trying to do. And, doing this, there are never any unknown (or unchanged states); you replace (rather than update) states every time to assign a new value to state . So you won't have xxxx x100 , you'll have 0000 0001 , 0100 1100 , or (the undesired) 0000 0011 , respectively, for the three samples above; but never any x s.
I must be missing something in your description of what you're trying to do.
Dear ,
I am really sorry for my idiot reply in last post.
Yes I store the last state which I am exposing to my application through another macro.
Thus there are basically two macros each for saving the state and getting the current state.
If I'm understanding you correctly, INA_SAVE_APP_STATE(STATE) should clear state1, state2, and state3 from appState and then or in all bits present in the operand. So, states 4 through 8 can be set at any time and once set cannot be cleared. And whatever is set in the low order three bits of the operand will replace whatever was in those three bits before. If this is what you want, try:
Agreed. I'm just not sure that we have a true specification of what this macro is supposed to do yet. I've worked on a lot of finite state machines, but the state changes described for this project still seem unusual to me. There seems to be a state0, but instead of having a bit identifying state0; it is identified by being not in state1, state2, or state3.
I was expecting to hear that my suggested change is a step in the right direction, but still isn't doing some things right. Should INA_SAVE_APP_STATE(state4) really clear state1 , state2 , and state3 from appState ? Shouldn't there be some way to clear state4 through state8 ? Shouldn't there be a macro to initialize the state? (Maybe the header always defines appState to be an extern int (or char); but we haven't seen any evidence of that yet, and these macros won't behave as desired if appState is an uninitialized variable allocated on the stack.) There is no defined error mechanism. (What should happen if one calls INA_SAVE_APP_STATE(state1 | state2) ?) Et cetera.
I'm still hoping for an actual set of specifications of the allowed state transitions, what happens when an illegal state is passed in, etc.