forked from jkroso/pico-button.c
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbutton.h
117 lines (103 loc) · 2.58 KB
/
button.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @file button.h
* @brief Button debounce library for Raspberry Pi Pico
*
* This library provides a simple way to debounce push buttons on a Raspberry Pi Pico.
* It generates interrupts after listening to GPIO_IRQ events and allows defining multiple buttons simultaneously.
* Fork of https://github.com/jkroso/pico-button.c
* including https://github.com/jkroso/pico-gpio-interrupt.c,
* both by Jake Rosoman. MIT license.
*
* @author Turi Scandurra
* @date 2023-02-13
*/
#ifndef PICO_BUTTON_H
#define PICO_BUTTON_H
#include "pico/stdlib.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @def DEBOUNCE_US
* @brief Debounce time in microseconds
*/
#define DEBOUNCE_US 200
/**
* @struct button_t
* @brief Represents a button with its pin, state, and onchange callback
*/
typedef struct button_t {
/**
* @var pin
* @brief The GPIO pin number of the button
*/
uint8_t pin;
/**
* @var state
* @brief The current state of the button (true for high, false for low)
*/
bool state;
/**
* @var onchange
* @brief The callback function to be called when the button state changes
*/
void (*onchange)(struct button_t *button);
} button_t;
/**
* @typedef handler
* @brief A function pointer type for onchange callbacks
*/
typedef void (*handler)(void *argument);
/**
* @struct closure_t
* @brief Represents a closure with a function pointer and an argument
*/
typedef struct {
/**
* @var argument
* @brief The argument to be passed to the function
*/
void * argument;
/**
* @var fn
* @brief The function pointer
*/
handler fn;
} closure_t;
/**
* @brief Handles a button alarm
* @param a The alarm ID (not used)
* @param p The button structure
* @return 0
*/
long long int handle_button_alarm(long int a, void *p);
/**
* @brief Handles a button interrupt
* @param p The button structure
*/
void handle_button_interrupt(void *p);
/**
* @brief Handles a GPIO interrupt
* @param gpio The GPIO pin number
* @param events The interrupt events
*/
void handle_interrupt(uint gpio, uint32_t events);
/**
* @brief Listens for a GPIO interrupt
* @param pin The GPIO pin number
* @param condition The interrupt condition
* @param fn The callback function
* @param arg The argument to be passed to the callback function
*/
void listen(uint pin, int condition, handler fn, void *arg);
/**
* @brief Creates a new button structure
* @param pin The GPIO pin number
* @param onchange The onchange callback function
* @return The new button structure
*/
button_t * create_button(int pin, void (*onchange)(button_t *));
#ifdef __cplusplus
}
#endif
#endif