libpynq (release 5EWC0-2023 version 0.2.1 of 2023-09-01 11:02)
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 }
79 return (gpio_get_level(SWB_BTN0 + button) == GPIO_LEVEL_LOW
82}
83
84int wait_until_button_state(const int button, const int state) {
85 if (buttons_initialized == false) {
86 pynq_error("wait_until_button_state: buttons weren't initialized\n");
87 }
88 if (button < 0 || button >= NUM_BUTTONS) {
89 pynq_error("get_button_state: invalid button=%d, must be 0..%d-1\n", button,
91 }
92 const pin_t btn = SWB_BTN0 + button;
94 pynq_error("get_button_state: button %d has not been set as input\n",
95 button);
96 }
97 struct timeval call, close;
98 int dTime;
99 gettimeofday(&call, NULL);
100 const unsigned int check =
102 while (gpio_get_level(btn) != check) {
103 }
104 gettimeofday(&close, NULL);
105 dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
106 dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
107 return dTime;
108}
109
110int sleep_msec_button_pushed(const int button, const int ms) {
111 if (buttons_initialized == false) {
112 pynq_error("sleep_msec_button: buttons weren't initialized\n");
113 }
114 if (button < 0 || button >= NUM_BUTTONS) {
115 pynq_error("sleep_msec_button_pushed: invalid button=%d, must be 0..%d-1\n",
116 button, NUM_BUTTONS);
117 }
118 const pin_t btn = SWB_BTN0 + button;
121 "sleep_msec_button_pushed: button %d has not been set as input\n",
122 button);
123 }
124 int status;
125 struct timeval call, close;
126 double dTime;
127 // mapping call time to call struct
128 gettimeofday(&call, NULL);
129 do {
130 // update level and latch if is pushed
131 if (status != GPIO_LEVEL_HIGH) {
132 status = gpio_get_level(btn);
133 }
134 (void)gettimeofday(&close, NULL);
135 dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
136 dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
137 } while (dTime < ms);
138 return (status == GPIO_LEVEL_LOW ? BUTTON_NOT_PUSHED : BUTTON_PUSHED);
139}
140
141void sleep_msec_buttons_pushed(int button_states[], const int ms) {
142 if (buttons_initialized == false) {
143 pynq_error("sleep_msec_buttons_pushed: buttons weren't initialized\n");
144 }
145 if (button_states == NULL) {
146 pynq_error("sleep_msec_buttons_pushed: button_states is NULL\n");
147 }
148 struct timeval call, close;
149 int dTime;
150 const pin_t buttons[NUM_BUTTONS] = {SWB_BTN0, SWB_BTN1, SWB_BTN2, SWB_BTN3};
151 // mapping call time to call struct
152 (void)gettimeofday(&call, NULL);
153 do {
154 for (int i = 0; i < NUM_BUTTONS; i++) {
155 if (button_states[i] != BUTTON_PUSHED) {
156 button_states[i] =
159 }
160 }
161 (void)gettimeofday(&close, NULL);
162 dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
163 dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
164 } while (dTime < ms);
165}
166
167int wait_until_button_pushed(const int button) {
168 // all checks are done in wait_until_button state
170}
171
172int wait_until_button_released(const int button) {
173 // all checks are done in wait_until_button state
175}
176
178 const pin_t buttons[NUM_BUTTONS] = {SWB_BTN0, SWB_BTN1, SWB_BTN2, SWB_BTN3};
179 if (buttons_initialized == false) {
180 pynq_error("wait_until_any_button_pushed: buttons weren't initialized\n");
181 }
182 for (int b = 0; b < NUM_BUTTONS; b++) {
185 "wait_until_any_button_pushed: button %d has not been set as input\n",
186 b);
187 }
188 }
189 do {
190 for (int b = 0; b < NUM_BUTTONS; b++) {
191 if (gpio_get_level(buttons[b]) == GPIO_LEVEL_HIGH) {
192 return b; // we return the index, i.e. 0..NUM_BUTTONS-1
193 }
194 }
195 } while (true);
196}
197
199 const pin_t buttons[NUM_BUTTONS] = {SWB_BTN0, SWB_BTN1, SWB_BTN2, SWB_BTN3};
200 if (buttons_initialized == false) {
201 pynq_error("wait_until_any_button_released: buttons weren't initialized\n");
202 }
203 for (int b = 0; b < NUM_BUTTONS; b++) {
205 pynq_error("wait_until_any_button_released: button %d has not been set "
206 "as input\n",
207 b);
208 }
209 }
210 do {
211 for (int b = 0; b < NUM_BUTTONS; b++) {
212 if (gpio_get_level(buttons[b]) == GPIO_LEVEL_LOW)
213 return b; // we return the index, i.e. 0..NUM_BUTTONS-1
214 }
215 } while (true);
216}
217
218int get_switch_state(const int switch_num) {
219 if (switches_initialized == false) {
220 pynq_error("get_switch_state: switches weren't initialized\n");
221 }
222 if (switch_num != SWITCH0 && switch_num != SWITCH1) {
223 pynq_error("get_switch_state: invalid switch_num=%d, must be 0..%i-1\n",
224 switch_num, NUM_SWITCHES);
225 }
226 return (gpio_get_level(SWB_SW0 + switch_num) == GPIO_LEVEL_LOW ? SWITCH_ON
227 : SWITCH_OFF);
228}
gpio_direction_t gpio_get_direction(const gpio_t pin)
Definition gpio.c:6
gpio_level_t gpio_get_level(const gpio_t pin)
Definition gpio.c:8
void gpio_set_direction(const gpio_t pin, const gpio_direction_t direction)
Definition gpio.c:5
#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:110
int wait_until_button_pushed(const int button)
Wait until the given button is pushed (which may be immediately).
Definition buttons.c:167
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:177
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:198
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:84
#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:172
int get_switch_state(const int switch_num)
Definition buttons.c:218
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:141
@ NUM_SWITCHES
Definition buttons.h:94
@ SWITCH0
Definition buttons.h:94
@ SWITCH1
Definition buttons.h:94
@ NUM_BUTTONS
Definition buttons.h:86
@ GPIO_DIR_INPUT
Definition gpio.h:83
@ GPIO_LEVEL_LOW
Definition gpio.h:93
@ GPIO_LEVEL_HIGH
Definition gpio.h:95
#define pynq_error(...)
Definition log.h:118
pin_t
Definition pinmap.h:45
@ SWB_BTN0
Button input pins.
Definition pinmap.h:83
@ SWB_SW1
Definition pinmap.h:78
@ SWB_BTN1
Definition pinmap.h:84
@ SWB_BTN2
Definition pinmap.h:85
@ SWB_SW0
Switch input pins.
Definition pinmap.h:77
@ SWB_BTN3
Definition pinmap.h:86