libpynq  (release 5EID0-2023 version 0.3.0 of 2024-04-25 09:42 )
buttons.c
Go to the documentation of this file.
1 /*
2 Copyright (c) 2023 Eindhoven University of Technology
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22 #include <buttons.h>
23 #include <gpio.h>
24 #include <log.h>
25 #include <pinmap.h>
26 #include <platform.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/time.h>
31 #include <unistd.h>
32 
33 #undef LOG_DOMAIN
34 #define LOG_DOMAIN "buttons"
35 
36 static bool buttons_initialized = false;
37 static bool switches_initialized = false;
38 
39 void buttons_init(void) {
40  if (buttons_initialized == true) {
41  pynq_error("buttons_destroy: buttons already initialized\n");
42  }
47  buttons_initialized = true;
48 }
49 
50 void buttons_destroy(void) { /* Anything to do here? */
51  if (buttons_initialized == false) {
52  pynq_error("buttons_destroy: buttons weren't initialized\n");
53  }
54  buttons_initialized = false;
55 }
56 
57 void switches_init(void) {
58  if (switches_initialized == true) {
59  pynq_error("switches_destroy: switches already initialized\n");
60  }
63  switches_initialized = true;
64 }
65 
66 void switches_destroy(void) { /* Anything to do here? */
67  if (switches_initialized == false) {
68  pynq_error("switches_destroy: switches weren't initialized\n");
69  }
70  switches_initialized = false;
71 }
72 
73 int get_button_state(const int button) {
74  if (buttons_initialized == false) {
75  pynq_error("get_button_state: buttons weren't initialized\n");
76  }
77  if (button < 0 || button >= NUM_BUTTONS) {
78  pynq_error("get_button_state: invalid button=%d, must be 0..%d-1\n",
79  NUM_BUTTONS);
80  }
82  : BUTTON_PUSHED);
83 }
84 
85 int wait_until_button_state(const int button, const int state) {
86  if (buttons_initialized == false) {
87  pynq_error("wait_until_button_state: buttons weren't initialized\n");
88  }
89  if (button < 0 || button >= NUM_BUTTONS) {
90  pynq_error("get_button_state: invalid button=%d, must be 0..%d-1\n", button,
91  NUM_BUTTONS);
92  }
93  const io_t btn = IO_BTN0 + button;
94  if (gpio_get_direction(btn) != GPIO_DIR_INPUT) {
95  pynq_error("get_button_state: button %d has not been set as input\n",
96  button);
97  }
98  struct timeval call, close;
99  int dTime;
100  gettimeofday(&call, NULL);
101  const unsigned int check =
103  while (gpio_get_level(btn) != check) {
104  }
105  gettimeofday(&close, NULL);
106  dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
107  dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
108  return dTime;
109 }
110 
111 int sleep_msec_button_pushed(const int button, const int ms) {
112  if (buttons_initialized == false) {
113  pynq_error("sleep_msec_button: buttons weren't initialized\n");
114  }
115  if (button < 0 || button >= NUM_BUTTONS) {
116  pynq_error("sleep_msec_button_pushed: invalid button=%d, must be 0..%d-1\n",
117  button, NUM_BUTTONS);
118  }
119  const io_t btn = IO_BTN0 + button;
120  if (gpio_get_direction(btn) != GPIO_DIR_INPUT) {
121  pynq_error(
122  "sleep_msec_button_pushed: button %d has not been set as input\n",
123  button);
124  }
125  int status;
126  struct timeval call, close;
127  double dTime;
128  // mapping call time to call struct
129  gettimeofday(&call, NULL);
130  do {
131  // update level and latch if is pushed
132  if (status != GPIO_LEVEL_HIGH) {
133  status = gpio_get_level(btn);
134  }
135  (void)gettimeofday(&close, NULL);
136  dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
137  dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
138  } while (dTime < ms);
139  return (status == GPIO_LEVEL_LOW ? BUTTON_NOT_PUSHED : BUTTON_PUSHED);
140 }
141 
142 void sleep_msec_buttons_pushed(int button_states[], const int ms) {
143  if (buttons_initialized == false) {
144  pynq_error("sleep_msec_buttons_pushed: buttons weren't initialized\n");
145  }
146  if (button_states == NULL) {
147  pynq_error("sleep_msec_buttons_pushed: button_states is NULL\n");
148  }
149  struct timeval call, close;
150  int dTime;
151  const io_t buttons[NUM_BUTTONS] = {IO_BTN0, IO_BTN1, IO_BTN2, IO_BTN3};
152  // mapping call time to call struct
153  (void)gettimeofday(&call, NULL);
154  do {
155  for (int i = 0; i < NUM_BUTTONS; i++) {
156  if (button_states[i] != BUTTON_PUSHED) {
157  button_states[i] =
160  }
161  }
162  (void)gettimeofday(&close, NULL);
163  dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
164  dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
165  } while (dTime < ms);
166 }
167 
168 int wait_until_button_pushed(const int button) {
169  // all checks are done in wait_until_button state
170  return wait_until_button_state(button, BUTTON_PUSHED);
171 }
172 
173 int wait_until_button_released(const int button) {
174  // all checks are done in wait_until_button state
176 }
177 
179  const io_t buttons[NUM_BUTTONS] = {IO_BTN0, IO_BTN1, IO_BTN2, IO_BTN3};
180  if (buttons_initialized == false) {
181  pynq_error("wait_until_any_button_pushed: buttons weren't initialized\n");
182  }
183  for (int b = 0; b < NUM_BUTTONS; b++) {
185  pynq_error(
186  "wait_until_any_button_pushed: button %d has not been set as input\n",
187  b);
188  }
189  }
190  do {
191  for (int b = 0; b < NUM_BUTTONS; b++) {
192  if (gpio_get_level(buttons[b]) == GPIO_LEVEL_HIGH) {
193  return b; // we return the index, i.e. 0..NUM_BUTTONS-1
194  }
195  }
196  } while (true);
197 }
198 
200  const io_t buttons[NUM_BUTTONS] = {IO_BTN0, IO_BTN1, IO_BTN2, IO_BTN3};
201  if (buttons_initialized == false) {
202  pynq_error("wait_until_any_button_released: buttons weren't initialized\n");
203  }
204  for (int b = 0; b < NUM_BUTTONS; b++) {
206  pynq_error("wait_until_any_button_released: button %d has not been set "
207  "as input\n",
208  b);
209  }
210  }
211  do {
212  for (int b = 0; b < NUM_BUTTONS; b++) {
213  if (gpio_get_level(buttons[b]) == GPIO_LEVEL_LOW)
214  return b; // we return the index, i.e. 0..NUM_BUTTONS-1
215  }
216  } while (true);
217 }
218 
219 int get_switch_state(const int switch_num) {
220  if (switches_initialized == false) {
221  pynq_error("get_switch_state: switches weren't initialized\n");
222  }
223  if (switch_num != SWITCH0 && switch_num != SWITCH1) {
224  pynq_error("get_switch_state: invalid switch_num=%d, must be 0..%i-1\n",
225  switch_num, NUM_SWITCHES);
226  }
227  return (gpio_get_level(IO_SW0 + switch_num) == GPIO_LEVEL_LOW ? SWITCH_ON
228  : SWITCH_OFF);
229 }
wait_until_button_released
int wait_until_button_released(const int button)
Wait until the given button is not pushed (which may be immediately).
Definition: buttons.c:173
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).
Definition: buttons.c:85
SWITCH1
@ SWITCH1
Definition: buttons.h:94
switches_init
void switches_init(void)
Initialise the switches before they can be used.
Definition: buttons.c:57
IO_BTN1
@ IO_BTN1
Definition: pinmap.h:84
wait_until_button_pushed
int wait_until_button_pushed(const int button)
Wait until the given button is pushed (which may be immediately).
Definition: buttons.c:168
pynq_error
#define pynq_error(...)
Definition: log.h:118
get_switch_state
int get_switch_state(const int switch_num)
Definition: buttons.c:219
SWITCH_ON
#define SWITCH_ON
Definition: buttons.h:77
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.
Definition: buttons.c:142
sleep_msec_button_pushed
int sleep_msec_button_pushed(const int button, const int ms)
Check if the given button is pushed in msec milliseconds. The function does NOT return early.
Definition: buttons.c:111
GPIO_LEVEL_LOW
@ GPIO_LEVEL_LOW
Definition: gpio.h:100
buttons_init
void buttons_init(void)
Initialise the buttons before they can be used.
Definition: buttons.c:39
buttons.h
wait_until_any_button_pushed
int wait_until_any_button_pushed(void)
Wait until any button is not pushed (which may be immediately).
Definition: buttons.c:178
IO_BTN2
@ IO_BTN2
Definition: pinmap.h:85
buttons_destroy
void buttons_destroy(void)
Unitialize the buttons.
Definition: buttons.c:50
gpio_get_level
gpio_level_t gpio_get_level(const io_t pin)
Return the level of the IO pin.
Definition: gpio.c:8
gpio_set_direction
void gpio_set_direction(const io_t pin, const gpio_direction_t direction)
Set the IO pin as in input or output.
Definition: gpio.c:5
SWITCH0
@ SWITCH0
Definition: buttons.h:94
GPIO_DIR_INPUT
@ GPIO_DIR_INPUT
Definition: gpio.h:90
pinmap.h
NUM_BUTTONS
@ NUM_BUTTONS
Definition: buttons.h:86
BUTTON_PUSHED
#define BUTTON_PUSHED
Definition: buttons.h:75
BUTTON_NOT_PUSHED
#define BUTTON_NOT_PUSHED
Definition: buttons.h:74
IO_BTN0
@ IO_BTN0
Button input pins.
Definition: pinmap.h:83
NUM_SWITCHES
@ NUM_SWITCHES
Definition: buttons.h:94
SWITCH_OFF
#define SWITCH_OFF
Definition: buttons.h:76
log.h
gpio.h
wait_until_any_button_released
int wait_until_any_button_released(void)
Wait until the given button is not pushed (which may be immediately).
Definition: buttons.c:199
gpio_get_direction
gpio_direction_t gpio_get_direction(const io_t pin)
Returns the direction the set pin is initialized in.
Definition: gpio.c:6
IO_SW0
@ IO_SW0
Switch input pins.
Definition: pinmap.h:77
IO_SW1
@ IO_SW1
Definition: pinmap.h:78
GPIO_LEVEL_HIGH
@ GPIO_LEVEL_HIGH
Definition: gpio.h:102
IO_BTN3
@ IO_BTN3
Definition: pinmap.h:86
switches_destroy
void switches_destroy(void)
Unitialize the buttons.
Definition: buttons.c:66
io_t
io_t
Definition: pinmap.h:45
get_button_state
int get_button_state(const int button)
Return the state of the button (BUTTON_(NOT_)PUSHED).
Definition: buttons.c:73