Conditional compilation example – Echelon Mini FX User Manual
Page 87

74 Developing
Device
Applications
and a bit output model for the latch pulse. To read any of these buttons, all eight buttons
must be read from the shift register.
The following GetButton() function takes three subsequent readings to suppress a
bouncing signal, and returns the logical state of the SW1 button:
IO_4 input bitshift numbits(8) clockedge(-) ioButtons;
IO_6 output bit ioButtonLoad = 1;
boolean GetButton(void)
{
unsigned debounce;
unsigned data;
data = 0xFF;
for (debounce = 0; debounce < 3; ++debounce) {
// strobe:
io_out(ioButtonLoad, 0);
io_out(ioButtonLoad, 1);
// sample data and debounce:
data &= (unsigned)io_in(ioButtons);
}
return ~buttons & 0x01;
}
Because the eight Mini Gizmo buttons are connected through a shift register, state
changes cannot be detected through the io_changes() event used with the SW1 button
on the FT 5000 EVB. The following example uses a repeating application timer, which
expires every 25 ms, to take the button reading. When an activated SW1 button is
detected, the timer event handler calls the same OnButtonPressed() event handler
introduced earlier:
extern void OnButtonPressed(void);
// button event handler
//
// buttonTimer expires every 25ms, and triggers the timer_expires event
// with each expiry
//
mtimer repeating buttonTimer = 25;
//
// The Neuron C scheduler activates the following when-task whenever the
// button timer expires. The task samples the button state, detects a
// newly activated SW1 button, and fires the OnButtonPressed() event when
// necessary.
//
when(timer_expires(buttonTimer) {
static boolean previousButton = TRUE;
boolean currentButton;
currentButton = GetButton();
if (currentButton && !previousButton) {
OnButtonPressed();
}
previousButton = currentButton;
}
//
// OnButtonPressed() is called whenever the button becomes active. This
// initial implementation here does nothing.
//
void OnButtonPressed()
{
}
Conditional Compilation Example
The following is the combined code for the switch driver, capable of driving the SW1
button on FT 5000 EVB and the Mini Gizmo I/O board: