User`s manual
RabbitFLEX User’s Manual www.rabbit.com 79
Since we have accepted the switch press and turned on the corresponding LED, then we might as well play
the tone. We use the switchnum variable that we saved earlier to choose the tone to play.
while (1) {
// Wait for the switch to be released
waitfor(flexDigIn(switch_pressed) == 0);
// Wait additional 200 ms
waitfor(DelayMs(200));
// If the switch is still released, then break out of the while loop
if (flexDigIn(switch_pressed) == 0) {
// Turn the LED back off
flexDigOut(led, 1);
break;
}
}
}
}
}
This last section implements debouncing on switch release. We wait until the switch is released, and then
check the switch again 200 ms later. If the switch is still released, then we turn off the corresponding LED
and we break out of the while loop. The costatement starts over again, which means that we again monitor
all of the switches.
This concludes the main while() loop. With these changes, we can now use the switches on the demo
board to play a tone and light a corresponding LED.
6.4.3 Extending speaker_tone.c with I/O Grouping
In this section, we will make further changes to speaker_tone.c to use RabbitFLEX I/O grouping.
This will make the resulting code somewhat more flexible, and will demonstrate how to use I/O groups.
First, we need to declare some variables.
Flex_IOPin **switch_pressed;
Flex_IOPin **led;
int switchnum;
unsigned int switch_values;
unsigned int value;
Note that switch_pressed and led are now double pointers (Flex_IOPin **). This is because
we will be using these variables to iterate through a group (or array) of Flex_IOPin* pointers. This will
become more apparent as we walk through this sample.
We have also created switch_values and value variables, which we will explain later.
static const Flex_IOPin *switches[] = {
&flex_digin31,
&flex_digin33,
&flex_digin35,
&flex_digin37,
FLEX_GROUP_END
};