Help with Switch Cases
Scottpulman
Posts: 48
I have a question just to really prove to myself that I am not being stupid..
Can you nest Switch Cases inside Switch Cases? ( EXAMPLE #1 )
How about nesting If statements inside a Switch Case? ( I couldn't get this to work at all so I reverted back to a simple button event per case )
Any comments or guidance would be greatly appreaciated
EXAMPLE #1
BUTTON_EVENT[MioButtons]
{
PUSH:
{
TO[BUTTON.INPUT]
SWITCH(BUTTON.INPUT.CHANNEL)
{
CASE 200:{ // BT POWER
}
CASE 201:{ // Enter
IF(Src_Selected = SCR_DVR)
{
PULSE [dvDVR, DVR_Enter]
}
IF(Src_Selected = SCR_DVD)
{
PULSE [dvDVD, DVD_Select]
}
IF(Src_Selected = SCR_PLASMA)
{
PULSE [dvDVD, Plasma_Enter]
}
}
CASE 202:{ // Input
IF(Src_Selected = SCR_PLASMA)
{
STACK_VAR x
x = PLASMA_INPUT
SWITCH (x)
{
CASE 1 :
{
PULSE [dvPLASMA, Plasma_Video_Input_2]
PLASMA_INPUT = 2
}
CASE 2 :
{
PULSE [dvPLASMA, Plasma_Video_Input_3]
PLASMA_INPUT = 3
}
CASE 3 :
{
PULSE [dvPLASMA, Plasma_Video_Input_4]
PLASMA_INPUT = 4
}
CASE 4 :
{
PULSE [dvPLASMA, Plasma_Video_Input_1]
PLASMA_INPUT = 1
}
}
}
}
EXAMPLE #2
BUTTON_EVENT[MioButtons]
{
PUSH:
{
TO[BUTTON.INPUT]
SWITCH(BUTTON.INPUT.CHANNEL)
{
CASE 200:{ // BT POWER
IF(Src_Selected = SCR_DVR)
{
IF (nDVR_POWER=0)
{
PULSE [dvDVR, DVR_Power]
nDVR_POWER = 1
}
IF (nDVR_POWER=1)
{
PULSE [ dvDVR, DVR_Power]
nDVR_POWER = 0
}
}
IF(Src_Selected = SCR_DVD)
{
IF (nDVD_POWER=0)
{
PULSE [dvDVD, DVD_Power_ON]
nDVD_POWER = 1
}
IF (nDVD_POWER=1)
{
PULSE [ dvDVD, DVD_Power_OFF]
nDVD_POWER = 0
}
}
IF(Src_Selected = SCR_PLASMA)
{
IF (nPLASMA_POWER=0)
{
PULSE [dvPLASMA, PLASMA_POWER]
nPLASMA_POWER = 1
}
IF (nPLASMA_POWER=1)
{
PULSE [ dvPLASMA, PLASMA_Power]
nPLASMA_POWER = 0
}
}
}
Can you nest Switch Cases inside Switch Cases? ( EXAMPLE #1 )
How about nesting If statements inside a Switch Case? ( I couldn't get this to work at all so I reverted back to a simple button event per case )
Any comments or guidance would be greatly appreaciated
EXAMPLE #1
BUTTON_EVENT[MioButtons]
{
PUSH:
{
TO[BUTTON.INPUT]
SWITCH(BUTTON.INPUT.CHANNEL)
{
CASE 200:{ // BT POWER
}
CASE 201:{ // Enter
IF(Src_Selected = SCR_DVR)
{
PULSE [dvDVR, DVR_Enter]
}
IF(Src_Selected = SCR_DVD)
{
PULSE [dvDVD, DVD_Select]
}
IF(Src_Selected = SCR_PLASMA)
{
PULSE [dvDVD, Plasma_Enter]
}
}
CASE 202:{ // Input
IF(Src_Selected = SCR_PLASMA)
{
STACK_VAR x
x = PLASMA_INPUT
SWITCH (x)
{
CASE 1 :
{
PULSE [dvPLASMA, Plasma_Video_Input_2]
PLASMA_INPUT = 2
}
CASE 2 :
{
PULSE [dvPLASMA, Plasma_Video_Input_3]
PLASMA_INPUT = 3
}
CASE 3 :
{
PULSE [dvPLASMA, Plasma_Video_Input_4]
PLASMA_INPUT = 4
}
CASE 4 :
{
PULSE [dvPLASMA, Plasma_Video_Input_1]
PLASMA_INPUT = 1
}
}
}
}
EXAMPLE #2
BUTTON_EVENT[MioButtons]
{
PUSH:
{
TO[BUTTON.INPUT]
SWITCH(BUTTON.INPUT.CHANNEL)
{
CASE 200:{ // BT POWER
IF(Src_Selected = SCR_DVR)
{
IF (nDVR_POWER=0)
{
PULSE [dvDVR, DVR_Power]
nDVR_POWER = 1
}
IF (nDVR_POWER=1)
{
PULSE [ dvDVR, DVR_Power]
nDVR_POWER = 0
}
}
IF(Src_Selected = SCR_DVD)
{
IF (nDVD_POWER=0)
{
PULSE [dvDVD, DVD_Power_ON]
nDVD_POWER = 1
}
IF (nDVD_POWER=1)
{
PULSE [ dvDVD, DVD_Power_OFF]
nDVD_POWER = 0
}
}
IF(Src_Selected = SCR_PLASMA)
{
IF (nPLASMA_POWER=0)
{
PULSE [dvPLASMA, PLASMA_POWER]
nPLASMA_POWER = 1
}
IF (nPLASMA_POWER=1)
{
PULSE [ dvPLASMA, PLASMA_Power]
nPLASMA_POWER = 0
}
}
}
0
Comments
Yes to both; if your code doesn't work it has a bug somewhere.
The first problem explains why it wont compile at all . . . You don't have enough closing brackets at the end of either example. The end of example one needs 2 more end brackets. The end of example two needs 3 more.
In example two, though, you either need to switch from IF to Switch:Case, or Start using Else|If statements. Right now you're saying
The 3rd if statement there runs after the 2nd one completes, which means that if nPLASMA_POWER is 0, it pulses the command to the plasma, sets power to one, then it checks to see if Plasma Power is 1, which it is, and it pulses power to it again, and sets plasma power back to 0. This is no good. The 3rd if statement in there needs to be an ELSE IF, which only fires if the first is not true. Repeated If statements will all check, Else If statements stop executing when one is found to be true.
I must also admit to being confused as to why exactly you're using stack_var X instead of just switching case on PLASMA_INPUT. Also, it looks like with the naming convention you're using, PLASMA_INPUT should be a constant, not a variable. Are you sure you're performing the switch|case on the right thing?
J
Apologies if any of this is poorly formed in a grammatical sense or factually incorrect in a programming sense. I'm in New Orleans for business and just got home from Bourbon Street, but decided to impart programming wisdom upon those unable to be here with me
Thanks for your comments...
My mistake I didn't post all the closing brackets, they are there in the code, and yes it does compile.
After reading your explanation, the little light bulb went off and i started muttering to myself of course... I think that I now understand what I have been doing wrong. Thanx.
Scott
~ Diving into the deep end is the only way to learn how to swim ~