Wrappers to simplify the use of buttons.
- Buttons are numbered 0..NUM_BUTTONS-1, and return values are BUTTON_PUSHED and BUTTON_NOT_PUSHED
- Switches are numbered 0..NUM_SWITCHES-1, and return values are SWITCH_ON and SWITCH_OFF.
- wait_ functions return early, i.e. as soon as the stated condition is true.
- sleep_ functions do not return early, i.e. always wait until the specified number of milliseconds.
An example of how to use this library.
int main (void)
{
printf("Waiting until button 0 is pushed...\n");
printf("Waiting until button 0 is released...\n");
return EXIT_SUCCESS;
}
Buttons can also be used through GPIO (see gpio.h and pinmap.h). Note that GPIO numbering (IO_BTN0..IO_BTN3) is then used instead of 0..NUM_BUTTONS-1 (BUTTON0..BUTTON3). GPIO return values are GPIO_LEVEL_LOW/HIGH instead of BUTTON_(NOT_)PUSHED.
Switches can also be used through GPIO (see gpio.h and pinmap.h). Note that GPIO numbering (IO_SW0..IO_SW1) is then used instead of 0..NUM_SWITCHES-1 (SWITCH0..SWITCH1). GPIO return values are GPIO_LEVEL_LOW/HIGH instead of SWITCH_ON/OFF.
◆ BUTTON_NOT_PUSHED
#define BUTTON_NOT_PUSHED 0 |
◆ BUTTON_PUSHED
◆ SWITCH_OFF
◆ SWITCH_ON
◆ button_index_t
Enum of buttons.
Functions use a button numbered from 0..NUM_BUTTONS-1. Alternatively, you can use BUTTONi instead of just i if you find that clearer.
Enumerator |
---|
BUTTON0 | |
BUTTON1 | |
BUTTON2 | |
BUTTON3 | |
NUM_BUTTONS | |
Definition at line 86 of file buttons.h.
◆ switches_index_t
Enum of switches. Functions use a switch numbered from 0..NUM_SWITCHES-1. Alternatively, you can use SWITCHi instead of just i if you find that clearer.
Enumerator |
---|
SWITCH0 | |
SWITCH1 | |
NUM_SWITCHES | |
Definition at line 94 of file buttons.h.
◆ buttons_destroy()
void buttons_destroy |
( |
void |
| ) |
|
Unitialize the buttons.
Definition at line 50 of file buttons.c.
◆ buttons_init()
void buttons_init |
( |
void |
| ) |
|
Initialise the buttons before they can be used.
Definition at line 39 of file buttons.c.
◆ get_button_state()
int get_button_state |
( |
const int |
button | ) |
|
Return the state of the button (BUTTON_(NOT_)PUSHED).
- Parameters
-
button | The button the state of which is returned. |
- Warning
- Fails with program exit when button is outside valid range.
-
Fails with program exit when the direction of the button was not set to input (e.g. because buttons_init was not called before).
Definition at line 73 of file buttons.c.
◆ get_switch_state()
int get_switch_state |
( |
const int |
switch_num | ) |
|
- Returns
- The state of the switch number (1 for on, 0 for off).
- Warning
- Fails with program exit when switch is outside valid range.
-
Fails with program exit when the direction of any switch was not set to input (e.g. because buttons_init was not called before).
Definition at line 219 of file buttons.c.
◆ sleep_msec_button_pushed()
int sleep_msec_button_pushed |
( |
const int |
button, |
|
|
const int |
msec |
|
) |
| |
Check if the given button is pushed in msec milliseconds. The function does NOT return early.
- Parameters
-
button | The button of which the state is monitored. |
msec | The number of milliseconds to wait. |
- Returns
- BUTTON_PUSHED or BUTTON_NOT_PUSHED.
- Warning
- Fails with program exit when button is outside valid range.
-
Fails with program exit when the direction of the button was not set to input (e.g. because buttons_init was not called before).
Definition at line 111 of file buttons.c.
◆ sleep_msec_buttons_pushed()
void sleep_msec_buttons_pushed |
( |
int |
button_states[], |
|
|
const int |
ms |
|
) |
| |
Check if any button is pushed in msec milliseconds. The function does NOT return early.
- Parameters
-
button_states | The array of button states that are updated with BUTTON_PUSHED or BUTTON_NOT_PUSHED. |
- Warning
- Fails with program exit when the direction of any button was not set to input (e.g. because buttons_init was not called before).
Definition at line 142 of file buttons.c.
◆ switches_destroy()
void switches_destroy |
( |
void |
| ) |
|
Unitialize the buttons.
Definition at line 66 of file buttons.c.
◆ switches_init()
void switches_init |
( |
void |
| ) |
|
Initialise the switches before they can be used.
Definition at line 57 of file buttons.c.
◆ wait_until_any_button_pushed()
int wait_until_any_button_pushed |
( |
void |
| ) |
|
Wait until any button is not pushed (which may be immediately).
- Returns
- Wait until any button is pushed, return the number of the button that was pushed (0..NUM_BUTTONS-1).
- Warning
- Fails with program exit when the direction of any button was not set to input (e.g. because buttons_init was not called before).
Definition at line 178 of file buttons.c.
◆ wait_until_any_button_released()
int wait_until_any_button_released |
( |
void |
| ) |
|
Wait until the given button is not pushed (which may be immediately).
- Returns
- Wait until any button is released, return the number of the button that was pushed (0..NUM_BUTTONS-1).
- Warning
- Fails with program exit when the direction of any button was not set to input (e.g. because buttons_init was not called before).
Definition at line 199 of file buttons.c.
◆ wait_until_button_pushed()
int wait_until_button_pushed |
( |
const int |
button | ) |
|
Wait until the given button is pushed (which may be immediately).
- Parameters
-
button | The button of which the state is monitored. |
- Returns
- The number of milliseconds waited until the button was pushed.
- Warning
- Fails with program exit when button is outside valid range.
-
Fails with program exit when the direction of the button was not set to input (e.g. because buttons_init was not called before).
Definition at line 168 of file buttons.c.
◆ wait_until_button_released()
int wait_until_button_released |
( |
const int |
button | ) |
|
Wait until the given button is not pushed (which may be immediately).
- Parameters
-
button | The button of which the state is monitored. |
- Returns
- The number of milliseconds waited until the button was released.
- Warning
- Fails with program exit when button is outside valid range.
-
Fails with program exit when the direction of the button was not set to input (e.g. because buttons_init was not called before).
Definition at line 173 of file buttons.c.
◆ wait_until_button_state()
int wait_until_button_state |
( |
const int |
button, |
|
|
const int |
state |
|
) |
| |
Wait until the given button is in state (which may be immediately).
- Parameters
-
button | The button of which the state is monitored. |
state | The state that is waited for. Must be BUTTON_PUSHED or BUTTON_NOT_PUSHED. |
- Returns
- The number of milliseconds that was waited.
- Warning
- Fails with program exit when button is outside valid range.
-
Fails with program exit when the direction of the button was not set to input (e.g. because buttons_init was not called before).
Definition at line 85 of file buttons.c.