libpynq (release 5EWC0-2023 version 0.2.6 of 2025-11-10 09:56)
Loading...
Searching...
No Matches
buttons.c
Go to the documentation of this file.
1/*
2Copyright (c) 2023 Eindhoven University of Technology
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
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
36static bool buttons_initialized = false;
37static bool switches_initialized = false;
38
39void buttons_init(void) {
40 if (buttons_initialized == true) {
41 pynq_error("buttons_destroy: buttons already initialized\n");
42 }
47 buttons_initialized = true;
48}
49
50void 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
57void switches_init(void) {
58 if (switches_initialized == true) {
59 pynq_error("switches_destroy: switches already initialized\n");
60 }
63 switches_initialized = true;
64}
65
66void 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
73int 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",
80 }
83}
84
85int 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,
92 }
93 const io_t btn = IO_BTN0 + button;
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
111int 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;
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
142void 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
168int wait_until_button_pushed(const int button) {
169 // all checks are done in wait_until_button state
171}
172
173int 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++) {
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
219int 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}
#define SWITCH_OFF
Definition buttons.h:76
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
int wait_until_button_pushed(const int button)
Wait until the given button is pushed (which may be immediately).
Definition buttons.c:168
void switches_init(void)
Initialise the switches before they can be used.
Definition buttons.c:57
#define SWITCH_ON
Definition buttons.h:77
int wait_until_any_button_pushed(void)
Wait until any button is not pushed (which may be immediately).
Definition buttons.c:178
void buttons_destroy(void)
Unitialize the buttons.
Definition buttons.c:50
#define BUTTON_NOT_PUSHED
Definition buttons.h:74
int wait_until_any_button_released(void)
Wait until the given button is not pushed (which may be immediately).
Definition buttons.c:199
void switches_destroy(void)
Unitialize the buttons.
Definition buttons.c:66
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
#define BUTTON_PUSHED
Definition buttons.h:75
int wait_until_button_released(const int button)
Wait until the given button is not pushed (which may be immediately).
Definition buttons.c:173
int get_switch_state(const int switch_num)
Definition buttons.c:219
int get_button_state(const int button)
Return the state of the button (BUTTON_(NOT_)PUSHED).
Definition buttons.c:73
void buttons_init(void)
Initialise the buttons before they can be used.
Definition buttons.c:39
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
@ NUM_SWITCHES
Definition buttons.h:94
@ SWITCH0
Definition buttons.h:94
@ SWITCH1
Definition buttons.h:94
@ NUM_BUTTONS
Definition buttons.h:86
gpio_direction_t gpio_get_direction(const io_t pin)
Returns the direction the set pin is initialized in.
Definition gpio.c:6
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
gpio_level_t gpio_get_level(const io_t pin)
Return the level of the IO pin.
Definition gpio.c:8
@ GPIO_DIR_INPUT
Definition gpio.h:90
@ GPIO_LEVEL_LOW
Definition gpio.h:100
@ GPIO_LEVEL_HIGH
Definition gpio.h:102
#define pynq_error(...)
Definition log.h:118
io_t
Definition pinmap.h:45
@ IO_SW0
Switch input pins.
Definition pinmap.h:77
@ IO_BTN3
Definition pinmap.h:86
@ IO_BTN1
Definition pinmap.h:84
@ IO_SW1
Definition pinmap.h:78
@ IO_BTN0
Button input pins.
Definition pinmap.h:83
@ IO_BTN2
Definition pinmap.h:85