libpynq (release 5EWC0-2023 version 0.1.0 of 2023-08-14 14:01)
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
36void buttons_init(void) {
41}
42
43void buttons_destroy(void) { /* Anything to do here? */
44}
45
46void switches_init(void) {
49}
50
51void switches_destroy(void) { /* Anything to do here? */
52}
53
54int get_button_state(const int button) {
55 if (button < 0 || button >= NUM_BUTTONS) {
56 pynq_error("get_button_state: invalid button=%d, must be 0..%d-1\n",
58 }
59 return (gpio_get_level(SWB_BTN0 + button) == GPIO_LEVEL_LOW
62}
63
64int wait_until_button_state(const int button, const int state) {
65 if (button < 0 || button >= NUM_BUTTONS) {
66 pynq_error("get_button_state: invalid button=%d, must be 0..%d-1\n", button,
68 }
69 const gpio_t btn = SWB_BTN0 + button;
71 pynq_error("get_button_state: button %d has not been set as input\n",
72 button);
73 }
74 struct timeval call, close;
75 int dTime;
76 gettimeofday(&call, NULL);
77 const unsigned int check =
79 while (gpio_get_level(btn) != check) {
80 }
81 gettimeofday(&close, NULL);
82 dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
83 dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
84 return dTime;
85}
86
87int sleep_msec_button_pushed(const int button, const int ms) {
88 if (button < 0 || button >= NUM_BUTTONS) {
89 pynq_error("sleep_msec_button_pushed: invalid button=%d, must be 0..%d-1\n",
90 button, NUM_BUTTONS);
91 }
92 const gpio_t btn = SWB_BTN0 + button;
95 "sleep_msec_button_pushed: button %d has not been set as input\n",
96 button);
97 }
98 int status;
99 struct timeval call, close;
100 double dTime;
101 // mapping call time to call struct
102 gettimeofday(&call, NULL);
103 do {
104 // update level and latch if is pushed
105 if (status != GPIO_LEVEL_HIGH) {
106 status = gpio_get_level(btn);
107 }
108 (void)gettimeofday(&close, NULL);
109 dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
110 dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
111 } while (dTime < ms);
112 return (status == GPIO_LEVEL_LOW ? BUTTON_NOT_PUSHED : BUTTON_PUSHED);
113}
114
115void sleep_msec_buttons_pushed(int button_states[], const int ms) {
116 if (button_states == NULL) {
117 pynq_error("sleep_msec_buttons_pushed: button_states is NULL\n");
118 }
119 struct timeval call, close;
120 int dTime;
121 const gpio_t buttons[NUM_BUTTONS] = {SWB_BTN0, SWB_BTN1, SWB_BTN2, SWB_BTN3};
122 // mapping call time to call struct
123 (void)gettimeofday(&call, NULL);
124 do {
125 for (int i = 0; i < NUM_BUTTONS; i++) {
126 if (button_states[i] != BUTTON_PUSHED) {
127 button_states[i] =
130 }
131 }
132 (void)gettimeofday(&close, NULL);
133 dTime = (close.tv_sec - call.tv_sec) * 1000.0; // # of ms
134 dTime += (close.tv_usec - call.tv_usec) / 1000.0; // # of usec in ms
135 } while (dTime < ms);
136}
137
138int wait_until_button_pushed(const int button) {
140}
141
142int wait_until_button_released(const int button) {
144}
145
147 const gpio_t buttons[NUM_BUTTONS] = {SWB_BTN0, SWB_BTN1, SWB_BTN2, SWB_BTN3};
148 for (int b = 0; b < NUM_BUTTONS; b++) {
151 "wait_until_any_button_pushed: button %d has not been set as input\n",
152 b);
153 }
154 }
155 do {
156 for (int b = 0; b < NUM_BUTTONS; b++) {
157 if (gpio_get_level(buttons[b]) == GPIO_LEVEL_HIGH) {
158 return b; // we return the index, i.e. 0..NUM_BUTTONS-1
159 }
160 }
161 } while (true);
162}
163
165 const gpio_t buttons[NUM_BUTTONS] = {SWB_BTN0, SWB_BTN1, SWB_BTN2, SWB_BTN3};
166 for (int b = 0; b < NUM_BUTTONS; b++) {
168 pynq_error("wait_until_any_button_released: button %d has not been set "
169 "as input\n",
170 b);
171 }
172 }
173 do {
174 for (int b = 0; b < NUM_BUTTONS; b++) {
175 if (gpio_get_level(buttons[b]) == GPIO_LEVEL_LOW)
176 return b; // we return the index, i.e. 0..NUM_BUTTONS-1
177 }
178 } while (true);
179}
180
181int get_switch_state(const int switch_num) {
182 if (switch_num != SWITCH0 && switch_num != SWITCH1) {
183 pynq_error("get_switch_state: invalid switch_num=%d, must be 0..%i-1\n",
184 switch_num, NUM_SWITCHES);
185 }
186 return (gpio_get_level(SWB_SW0 + switch_num) == GPIO_LEVEL_LOW ? SWITCH_ON
187 : SWITCH_OFF);
188}
#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:87
int wait_until_button_pushed(const int button)
Wait until the given button is pushed (which may be immediately).
Definition buttons.c:138
void switches_init(void)
Initialise the switches before they can be used.
Definition buttons.c:46
#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:146
void buttons_destroy(void)
Unitialize the buttons.
Definition buttons.c:43
#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:164
void switches_destroy(void)
Unitialize the buttons.
Definition buttons.c:51
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:64
#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:142
int get_switch_state(const int switch_num)
Definition buttons.c:181
int get_button_state(const int button)
Return the state of the button (BUTTON_(NOT_)PUSHED).
Definition buttons.c:54
void buttons_init(void)
Initialise the buttons before they can be used.
Definition buttons.c:36
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:115
@ NUM_SWITCHES
Definition buttons.h:94
@ SWITCH0
Definition buttons.h:94
@ SWITCH1
Definition buttons.h:94
@ NUM_BUTTONS
Definition buttons.h:86
uint8_t gpio_t
Definition gpio.h:99
gpio_direction_t gpio_get_direction(const gpio_t pin)
Returns the direction the set pin is initialized in.
Definition gpio.c:89
gpio_level_t gpio_get_level(const gpio_t pin)
Return the level of the GPIO pin.
Definition gpio.c:118
void gpio_set_direction(const gpio_t pin, const gpio_direction_t dir)
Set the GPIO pin as in input or output.
Definition gpio.c:72
@ GPIO_DIR_INPUT
Definition gpio.h:81
@ GPIO_LEVEL_LOW
Definition gpio.h:91
@ GPIO_LEVEL_HIGH
Definition gpio.h:93
#define pynq_error(...)
Definition log.h:118
#define SWB_BTN0
Button input pins.
Definition pinmap.h:83
#define SWB_SW0
Switch input pins.
Definition pinmap.h:77
#define SWB_BTN2
Definition pinmap.h:85
#define SWB_BTN3
Definition pinmap.h:86
#define SWB_BTN1
Definition pinmap.h:84
#define SWB_SW1
Definition pinmap.h:78