forked from raspberrypi/pico-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_gpio_irq.c
59 lines (48 loc) · 1.38 KB
/
hello_gpio_irq.c
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
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
static char event_str[128];
void gpio_event_string(char *buf, uint32_t events);
void gpio_callback(uint gpio, uint32_t events) {
// Put the GPIO event(s) that just happened into event_str
// so we can print it
gpio_event_string(event_str, events);
printf("GPIO %d %s\n", gpio, event_str);
}
int main() {
stdio_init_all();
printf("Hello GPIO IRQ\n");
gpio_set_irq_enabled_with_callback(2, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
// Wait forever
while (1);
}
static const char *gpio_irq_str[] = {
"LEVEL_LOW", // 0x1
"LEVEL_HIGH", // 0x2
"EDGE_FALL", // 0x4
"EDGE_RISE" // 0x8
};
void gpio_event_string(char *buf, uint32_t events) {
for (uint i = 0; i < 4; i++) {
uint mask = (1 << i);
if (events & mask) {
// Copy this event string into the user string
const char *event_str = gpio_irq_str[i];
while (*event_str != '\0') {
*buf++ = *event_str++;
}
events &= ~mask;
// If more events add ", "
if (events) {
*buf++ = ',';
*buf++ = ' ';
}
}
}
*buf++ = '\0';
}