libpynq (release 5EWC0-2023 version 0.2.4 of 2023-10-07 15:07)
Loading...
Searching...
No Matches
Button library

Macros

#define BUTTON_NOT_PUSHED   0
 
#define BUTTON_PUSHED   1
 
#define SWITCH_OFF   0
 
#define SWITCH_ON   1
 

Enumerations

enum  button_index_t {
  BUTTON0 , BUTTON1 , BUTTON2 , BUTTON3 ,
  NUM_BUTTONS
}
 
enum  switches_index_t { SWITCH0 , SWITCH1 , NUM_SWITCHES }
 

Functions

void switches_init (void)
 
void switches_destroy (void)
 
void buttons_init (void)
 
void buttons_destroy (void)
 
int get_button_state (const int button)
 
int wait_until_button_state (const int button, const int state)
 
int sleep_msec_button_pushed (const int button, const int msec)
 
void sleep_msec_buttons_pushed (int button_states[], const int ms)
 
int wait_until_button_pushed (const int button)
 
int wait_until_button_released (const int button)
 
int wait_until_any_button_pushed (void)
 
int wait_until_any_button_released (void)
 
int get_switch_state (const int switch_num)
 

Detailed Description

Wrappers to simplify the use of buttons.

An example of how to use this library.

#include <libpynq.h>
int main (void)
{
// initialise all I/O
printf("Waiting until button 0 is pushed...\n");
printf("Waited %d milliseconds\n\n", wait_until_button_pushed(0));
printf("Waiting until button 0 is released...\n");
printf("Waited %d milliseconds\n\n", wait_until_button_released(0));
// clean up after use
return EXIT_SUCCESS;
}
int wait_until_button_pushed(const int button)
Wait until the given button is pushed (which may be immediately).
Definition buttons.c:166
void buttons_destroy(void)
Unitialize the buttons.
Definition buttons.c:50
int wait_until_button_released(const int button)
Wait until the given button is not pushed (which may be immediately).
Definition buttons.c:171
void buttons_init(void)
Initialise the buttons before they can be used.
Definition buttons.c:39
void pynq_init(void)
Initialise the switchbox and GPIO of the PYNQ.
Definition libpynq.c:24
void pynq_destroy(void)
Reset and destroy the switchbox and GPIO of the PYNQ.
Definition libpynq.c:35

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.

Macro Definition Documentation

◆ BUTTON_NOT_PUSHED

#define BUTTON_NOT_PUSHED   0

Definition at line 74 of file buttons.h.

◆ BUTTON_PUSHED

#define BUTTON_PUSHED   1

Definition at line 75 of file buttons.h.

◆ SWITCH_OFF

#define SWITCH_OFF   0

Definition at line 76 of file buttons.h.

◆ SWITCH_ON

#define SWITCH_ON   1

Definition at line 77 of file buttons.h.

Enumeration Type Documentation

◆ 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.

Function Documentation

◆ 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.

Here is the call graph for this function:

◆ get_button_state()

int get_button_state ( const int  button)

Return the state of the button (BUTTON_(NOT_)PUSHED).

Parameters
buttonThe 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 71 of file buttons.c.

Here is the call graph for this function:

◆ 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 217 of file buttons.c.

Here is the call graph for this function:

◆ 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
buttonThe button of which the state is monitored.
msecThe 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 109 of file buttons.c.

Here is the call graph for this function:

◆ 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_statesThe 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 140 of file buttons.c.

Here is the call graph for this function:

◆ switches_destroy()

void switches_destroy ( void  )

Unitialize the buttons.

Definition at line 65 of file buttons.c.

◆ switches_init()

void switches_init ( void  )

Initialise the switches before they can be used.

Definition at line 56 of file buttons.c.

Here is the call graph for this function:

◆ 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 176 of file buttons.c.

Here is the call graph for this function:

◆ 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 197 of file buttons.c.

Here is the call graph for this function:

◆ wait_until_button_pushed()

int wait_until_button_pushed ( const int  button)

Wait until the given button is pushed (which may be immediately).

Parameters
buttonThe 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 166 of file buttons.c.

Here is the call graph for this function:

◆ 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
buttonThe 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 171 of file buttons.c.

Here is the call graph for this function:

◆ 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
buttonThe button of which the state is monitored.
stateThe 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 83 of file buttons.c.

Here is the call graph for this function:
Here is the caller graph for this function: