libpynq (release 5EWC0-2023 version 0.2.4 of 2023-10-07 15:07)
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}
55
56void switches_init(void) {
57 if (switches_initialized == true) {
58 pynq_error("switches_destroy: switches already initialized\n");
59 }
62 switches_initialized = true;
63}
64
65void switches_destroy(void) { /* Anything to do here? */
66 if (switches_initialized == false) {
67 pynq_error("switches_destroy: switches weren't initialized\n");
68 }
69}
70
71int get_button_state(const int button) {
72 if (buttons_initialized == false) {
73 pynq_error("get_button_state: buttons weren't initialized\n");
74 }
75 if (button < 0 || button >= NUM_BUTTONS) {
76 pynq_error("get_button_state: invalid button=%d, must be 0..%d-1\n",
78 }
81}
82
83int wait_until_button_state(const int button, const int state) {
84 if (buttons_initialized == false) {
85 pynq_error("wait_until_button_state: buttons weren't initialized\n");
86 }
87 if (button < 0 || button >= NUM_BUTTONS) {
88 pynq_error("get_button_state: invalid button=%d, must be 0..%d-1\n", button,
90 }
91 const io_t btn = IO_BTN0 + button;
93 pynq_error("get_button_state: button %d has not been set as input\n",
94 button);
95 }
96 struct timeval call, close;
97 int dTime;
98 gettimeofday(&call, NULL);
99 const unsigned int check =
101 while (gpio_get_level(btn) != check) {
102 }
103 gettimeofday(&close, NULL);
104 dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
105 dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
106 return dTime;
107}
108
109int sleep_msec_button_pushed(const int button, const int ms) {
110 if (buttons_initialized == false) {
111 pynq_error("sleep_msec_button: buttons weren't initialized\n");
112 }
113 if (button < 0 || button >= NUM_BUTTONS) {
114 pynq_error("sleep_msec_button_pushed: invalid button=%d, must be 0..%d-1\n",
115 button, NUM_BUTTONS);
116 }
117 const io_t btn = IO_BTN0 + button;
120 "sleep_msec_button_pushed: button %d has not been set as input\n",
121 button);
122 }
123 int status;
124 struct timeval call, close;
125 double dTime;
126 // mapping call time to call struct
127 gettimeofday(&call, NULL);
128 do {
129 // update level and latch if is pushed
130 if (status != GPIO_LEVEL_HIGH) {
131 status = gpio_get_level(btn);
132 }
133 (void)gettimeofday(&close, NULL);
134 dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
135 dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
136 } while (dTime < ms);
137 return (status == GPIO_LEVEL_LOW ? BUTTON_NOT_PUSHED : BUTTON_PUSHED);
138}
139
140void sleep_msec_buttons_pushed(int button_states[], const int ms) {
141 if (buttons_initialized == false) {
142 pynq_error("sleep_msec_buttons_pushed: buttons weren't initialized\n");
143 }
144 if (button_states == NULL) {
145 pynq_error("sleep_msec_buttons_pushed: button_states is NULL\n");
146 }
147 struct timeval call, close;
148 int dTime;
149 const io_t buttons[NUM_BUTTONS] = {IO_BTN0, IO_BTN1, IO_BTN2, IO_BTN3};
150 // mapping call time to call struct
151 (void)gettimeofday(&call, NULL);
152 do {
153 for (int i = 0; i < NUM_BUTTONS; i++) {
154 if (button_states[i] != BUTTON_PUSHED) {
155 button_states[i] =
158 }
159 }
160 (void)gettimeofday(&close, NULL);
161 dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
162 dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
163 } while (dTime < ms);
164}
165
166int wait_until_button_pushed(const int button) {
167 // all checks are done in wait_until_button state
169}
170
171int wait_until_button_released(const int button) {
172 // all checks are done in wait_until_button state
174}
175
177 const io_t buttons[NUM_BUTTONS] = {IO_BTN0, IO_BTN1, IO_BTN2, IO_BTN3};
178 if (buttons_initialized == false) {
179 pynq_error("wait_until_any_button_pushed: buttons weren't initialized\n");
180 }
181 for (int b = 0; b < NUM_BUTTONS; b++) {
184 "wait_until_any_button_pushed: button %d has not been set as input\n",
185 b);
186 }
187 }
188 do {
189 for (int b = 0; b < NUM_BUTTONS; b++) {
190 if (gpio_get_level(buttons[b]) == GPIO_LEVEL_HIGH) {
191 return b; // we return the index, i.e. 0..NUM_BUTTONS-1
192 }
193 }
194 } while (true);
195}
196
198 const io_t buttons[NUM_BUTTONS] = {IO_BTN0, IO_BTN1, IO_BTN2, IO_BTN3};
199 if (buttons_initialized == false) {
200 pynq_error("wait_until_any_button_released: buttons weren't initialized\n");
201 }
202 for (int b = 0; b < NUM_BUTTONS; b++) {
204 pynq_error("wait_until_any_button_released: button %d has not been set "
205 "as input\n",
206 b);
207 }
208 }
209 do {
210 for (int b = 0; b < NUM_BUTTONS; b++) {
211 if (gpio_get_level(buttons[b]) == GPIO_LEVEL_LOW)
212 return b; // we return the index, i.e. 0..NUM_BUTTONS-1
213 }
214 } while (true);
215}
216
217int get_switch_state(const int switch_num) {
218 if (switches_initialized == false) {
219 pynq_error("get_switch_state: switches weren't initialized\n");
220 }
221 if (switch_num != SWITCH0 && switch_num != SWITCH1) {
222 pynq_error("get_switch_state: invalid switch_num=%d, must be 0..%i-1\n",
223 switch_num, NUM_SWITCHES);
224 }
225 return (gpio_get_level(IO_SW0 + switch_num) == GPIO_LEVEL_LOW ? SWITCH_ON
226 : SWITCH_OFF);
227}
#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:109
int wait_until_button_pushed(const int button)
Wait until the given button is pushed (which may be immediately).
Definition buttons.c:166
void switches_init(void)
Initialise the switches before they can be used.
Definition buttons.c:56
#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:176
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:197
void switches_destroy(void)
Unitialize the buttons.
Definition buttons.c:65
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:83
#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:171
int get_switch_state(const int switch_num)
Definition buttons.c:217
int get_button_state(const int button)
Return the state of the button (BUTTON_(NOT_)PUSHED).
Definition buttons.c:71
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:140
@ 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:95
void gpio_set_direction(const io_t pin, const gpio_direction_t dir)
Set the IO pin as in input or output.
Definition gpio.c:81
gpio_level_t gpio_get_level(const io_t pin)
Return the level of the IO pin.
Definition gpio.c:118
@ 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