-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpb.h
More file actions
102 lines (80 loc) · 3.17 KB
/
pb.h
File metadata and controls
102 lines (80 loc) · 3.17 KB
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
/*
* @file pb.h
* @brief Push-Button driver
* @author Nima Askari
* @version 1.0.0
* @license See the LICENSE file in the root folder.
*
* @note All my libraries are dual-licensed.
* Please review the licensing terms before using them.
* For any inquiries, feel free to contact me.
*
* @github https://www.github.com/nimaltd
* @linkedin https://www.linkedin.com/in/nimaltd
* @youtube https://www.youtube.com/@nimaltd
* @instagram https://instagram.com/github.nimaltd
*
* Copyright (C) 2025 Nima Askari - NimaLTD. All rights reserved.
*/
#ifndef _PB_H_
#define _PB_H_
#ifdef __cplusplus
extern "C" {
#endif
/*************************************************************************************************/
/** Includes **/
/*************************************************************************************************/
#include <stdbool.h>
#include "main.h"
#include "pb_config.h"
/*************************************************************************************************/
/** Typedef/Struct/Enum **/
/*************************************************************************************************/
/*************************************************************************************************/
/* Button event type (bitmask + long press flag) */
typedef struct
{
uint32_t long_press:1;
uint32_t mask:31;
} pb_evn_t;
/*************************************************************************************************/
/* Configuration structure for a single push-button */
typedef struct __PACKED
{
GPIO_TypeDef *gpio;
uint16_t pin;
} pb_config_t;
/*************************************************************************************************/
/* Main handle for push-button driver */
typedef struct
{
uint32_t beep; /* Beep counter */
uint32_t cnt[PB_CONFIG_COUNT]; /* Press duration counters for each button */
pb_evn_t evn[PB_EVN_QUEUE_SIZE]; /* Circular event buffer */
__IO uint32_t evn_head; /* Head index of the event queue (written by ISR) */
__IO uint32_t evn_tail; /* Tail index of the event queue (read by main loop) */
} pb_t;
/*************************************************************************************************/
/** API Functions **/
/*************************************************************************************************/
/* Initialize the push-button driver */
void pb_init(void);
/* Clear pending events */
void pb_clear(void);
/* Read pending button events */
pb_evn_t pb_read(void);
/* Process pending button events */
void pb_loop(void);
/* Callback button events */
void pb_pressed_cb(pb_evn_t evn);
/* Callback beep on */
void pb_beep_on_cb(void);
/* Callback beep off */
void pb_beep_off_cb(void);
/*************************************************************************************************/
/** End of File **/
/*************************************************************************************************/
#ifdef __cplusplus
}
#endif
#endif /* _PB_H_ */