Go Down

Topic: Switch Case function compabilities question (Read 3811 times) previous topic - next topic

DarK_AlximiK

Jun 17, 2013, 11:27 pm Last Edit: Jun 18, 2013, 12:19 am by DarK_AlximiK Reason: 1
Found CASE instruction syntax in CoDeSys from ABB, and I am curious, does arrays for the cases available in standard C++? Or is it possible to modify the macro, so that it could work the same way?

CASE instruction
With the CASE instructions one can combine several conditioned instructions with the same condition variable in one construct.

Syntax:

CASE <Var1> OF

<Value1>: <Instruction 1>

<Value2>: <Instruction 2>

<Value3, Value4, Value5>: <Instruction 3>

<Value6 .. Value10>: <Instruction 4>

...

<Value n>: <Instruction n>

ELSE <ELSE instruction>

END_CASE;

A CASE instruction is processed according to the following model:

  If the variable in <Var1> has the value <Value i>, then the instruction <Instruction i> is executed.

  If <Var 1> has none of the indicated values, then the <ELSE Instruction> is executed.

  If the same instruction is to be executed for several values of the variables, then one can write these values one after the other separated by commas, and thus condition the common execution.

  If the same instruction is to be executed for a value range of a variable, one can write the initial value and the end value separated by two dots one after the other. So you can condition the common condition.

Example:

CASE INT1 OF
1, 5: BOOL1 := TRUE;
BOOL3 := FALSE;
2: BOOL2 := FALSE;
BOOL3 := TRUE;
10..20: BOOL1 := TRUE;
BOOL3:= TRUE;
ELSE
BOOL1 := NOT BOOL1;
BOOL2 := BOOL1 OR BOOL2;
END_CASE;
http://evtouchscreen.blogspot.lt/
http://schevron.blogspot.lt/
http://destroyer3000.blogspot.lt/

dc42

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

DarK_AlximiK

Your reply doesn't answer my question/suggestion
http://evtouchscreen.blogspot.lt/
http://schevron.blogspot.lt/
http://destroyer3000.blogspot.lt/

Arrch


Your reply doesn't answer my question/suggestion


Your question/suggestion isn't clear, try rephrasing it.

DarK_AlximiK



Your reply doesn't answer my question/suggestion


Your question/suggestion isn't clear, try rephrasing it.


Check the syntax of ABB CoDeSys of the CASE statement, it has arrays in it!
Example:
1,3,5:
6..20,22..30:
Wouldn't it be great to have the same macro?
http://evtouchscreen.blogspot.lt/
http://schevron.blogspot.lt/
http://destroyer3000.blogspot.lt/

dc42


Check the syntax of ABB CoDeSys of the CASE statement, it has arrays in it!
Example:
1,3,5:
6..20,22..30:
Wouldn't it be great to have the same macro?


You can say:

Code: [Select]
case 1:
case 3:
case 5:
   do_something();
   break;


but in C++ switch statements there is no equivalent of the range notation (e.g. 6..20) you used in your example.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Arrch


but in C++ switch statements there is no equivalent of the range notation (e.g. 6..20) you used in your example.


GCC does:

Code: [Select]
switch(var)
{
  case 1 ... 5:
    break;
  case 6 ... 10:
    break;
  case 11 ... 15:
    break;
  default:
    break;
}

nickgammon

Combining the two suggestions you can have ranges and non-contiguous tests:

Code: [Select]

void setup ()
 {
 int var = 42;
 
 switch(var)
   {
   case 1 ... 5:
   case 15:
   case 20 ... 30:
      // do whatever
     break;
   case 6 ... 10:
     break;
   default:
     break;
   }  // end of switch

 }  // end of setup

void loop () { }
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

DarK_AlximiK

#8
Jun 24, 2013, 04:05 pm Last Edit: Jun 24, 2013, 04:08 pm by DarK_AlximiK Reason: 1
OK, I got everything, thank you all!

With your help guys I got this

Code: [Select]
byte Variable;
void setup(){
}
void loop(){
 switch(Variable){
   case 1:
   case 3:
   case 5: /*do stuff*/ break;
   case 6 ... 20: /*do stuff*/ break;
 }
}


It does compile actually
http://evtouchscreen.blogspot.lt/
http://schevron.blogspot.lt/
http://destroyer3000.blogspot.lt/

DarK_AlximiK

#9
Jun 27, 2013, 08:24 am Last Edit: Jun 27, 2013, 08:26 am by DarK_AlximiK Reason: 1
In summary it is possible to change this
Quote

/*IDLE RUN STATE*/ 
void idleRunUpdate(){
//  Serial.println("idleRunUpdate");
  //Conditions for transition to a Line Avoid State
  if((sensorSum>=16 && sensorSum<=63)||(sensorSum>=80 && sensorSum<=127)||(sensorSum>=208 && sensorSum<=255)){
    stateMachine.transitionTo(lineAvoidState);
  }
  //Conditions for transition to a Attack State
  if(sensorSum==2||sensorSum==4||sensorSum==6||sensorSum==7||sensorSum==14){
    stateMachine.transitionTo(attackState);
  }
  //Conditions for transition to a Spin State
  if(sensorSum==1||sensorSum==3||sensorSum==8||sensorSum==12){
    stateMachine.transitionTo(spinState);
  }
  //Conditions for transition to a Overfall Avoid State
  if((sensorSum>=64 && sensorSum<=79)||(sensorSum>=128 && sensorSum<=143)||(sensorSum>=192 && sensorSum<=207)){
    stateMachine.transitionTo(overfallAvoidState);
  }
}


to this:
Quote

/*IDLE RUN STATE*/  
void idleRunUpdate(){
//  Serial.println("idleRunUpdate");
 switch(sensorSum){
   //Conditions for transition to a Line Avoid State
   case 16 ... 63:
   case 80 ... 127:
   case 208 ... 255:
     stateMachine.transitionTo(lineAvoidState);
     break;
   //Conditions for transition to a Attack State
   case 2:
   case 4:
   case 6:
   case 7:
   case 14:
     stateMachine.transitionTo(attackState);
     break;
   //Conditions for transition to a Spin State
   case 1:
   case 3:
   case 8:
   case 12:
     stateMachine.transitionTo(spinState);
     break;
   //Conditions for transition to a Overfall Avoid State
   case 64 ... 79:
   case 128 ... 143:
   case 192 ... 207:
     stateMachine.transitionTo(overfallAvoidState);
     break;
 }
}

compiled and saved 12 bytes
http://evtouchscreen.blogspot.lt/
http://schevron.blogspot.lt/
http://destroyer3000.blogspot.lt/

wildbill

Looks the same to me. If you have doubts, you could always write a little test harness to test both versions and verify that they do the same thing.

DarK_AlximiK

Looks the same to me. If you have doubts, you could always write a little test harness to test both versions and verify that they do the same thing.
Less coding bro, more clear/accurate and saves bytes
http://evtouchscreen.blogspot.lt/
http://schevron.blogspot.lt/
http://destroyer3000.blogspot.lt/

Go Up