View previous topic :: View next topic |
Author |
Message |
jruibarroso
Joined: 07 Jan 2006 Posts: 64 Location: Braga
|
how to do a switch with variables |
Posted: Sun Oct 29, 2006 10:05 am |
|
|
i have this code :
SWITCH (com) {
Case 62 : output_high(L1); // TECLA 3 TV
delay_ms(250);
output_low(L1);
com=0;
break;
Case 61 : output_high(L2); // TECLA 2 TV
delay_ms(250);
output_low(L2);
com=0;
break;
Case 60 : output_high(L3); // TECLA 1 TV
delay_ms(250);
output_low(L3);
com=0;
break;
// Everything is fine ------------------------------------------------------
but i want something like this
SWITCH (variable) {
Case (variable1) : output_high(L1); // TECLA 3 TV
delay_ms(250);
output_low(L1);
com=0;
break;
Case (variable2) : output_high(L2); // TECLA 2 TV
delay_ms(250);
output_low(L2);
com=0;
break;
Case (variable3) : output_high(L3); // TECLA 1 TV
delay_ms(250);
output_low(L3);
com=0;
break;
Any Ideas , please ? |
|
|
Ttelmah Guest
|
|
Posted: Sun Oct 29, 2006 2:19 pm |
|
|
"The _switch_ statement, is a multi-way decision, that tests whether an expression matches one of a number of _constant_ integer values, and branches accordingly". This from Kerninghan & Ritchie, Note the _constant_...
Best Wishes[/i] |
|
|
Guest
|
|
Posted: Sun Oct 29, 2006 5:04 pm |
|
|
Hi all,
As Ttelmah said, the switch is limited to constants.
BUT,
Here are some thoughts...
#1. Instead of variable within the switch, can you use "variables" that are defined within a "#define" statement?
Code: |
#define TECLA_3_TV 62
.
.
.
switch(com)
{
case(TECLA_3_TV): bla bla bla
.
.
.
}
|
#2. Don't use a switch!!! Can you use something like
Code: |
.
.
.
if(com==variable1) {// do variable 1 stuff}
elseif(com==variable2} { }
elseif(com==variable3} { }
.
.
.
|
When you look at the logic of what you want, you are just comparing com with your variable.
#3. Good ol' pointers to functions!
Code: |
.
.
.
void (*pof)[10]; // array if 10 pof's
.
pof[0]=&func1; // assign them manualy like this, or [better] Assign them in the def above
.
.
pof[com](); // call it
|
yes yes yes, this take memory if your index's are not seqencial
yes yes yes CCS does not support this...at least not v3x, I forget if v4x does...
Well, thats food for thought
~Kam (^8* |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
pointer-to-function... Mmmm... |
Posted: Sun Oct 29, 2006 6:05 pm |
|
|
Anonymous wrote: | yes yes yes CCS does not support this...at least not v3x, I forget if v4x does... |
v4.x should support pointer-to-function. It seems that this feature is being debugged by CCS. There's a lot of cool tricks that can be done with pointer-to-function... |
|
|
Guest
|
Re: pointer-to-function... Mmmm... |
Posted: Sun Oct 29, 2006 8:54 pm |
|
|
kender wrote: | There's a lot of cool tricks that can be done with pointer-to-function... |
So true...so true...that opens up many programming techniques...pre oo progrmaming! CCS you're nearly a C compiler!
~Kam (^8* |
|
|
|