-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ad6672e
Showing
6 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
build | ||
build.sh | ||
deps | ||
CMakeLists.txt | ||
pico_sdk_import.cmake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# pico-button.c | ||
|
||
## Installation | ||
|
||
Install with [clib(1)](https://github.com/clibs/clib): | ||
|
||
```sh | ||
$ clib install jkroso/pico-button.c | ||
``` | ||
|
||
## Api | ||
|
||
`create_button(pin, onchange)`: It will set the pin to pull up. It returns a `button_t` struct | ||
|
||
```c | ||
typedef struct button_t { | ||
uint8_t pin; | ||
bool state; | ||
void (*onchange)(struct button_t *button); | ||
} button_t; | ||
``` | ||
|
||
## Example | ||
|
||
```c | ||
#include "./deps/pico-button/button.c" | ||
|
||
void onchange(button_t *button_p) { | ||
button_t *button = (button_t*)button_p; | ||
printf("Button on pin %d changed state to %d\n", button->pin, button->state); | ||
} | ||
|
||
int main() { | ||
stdio_init_all(); | ||
button_t *b = create_button(21, onchange); | ||
printf("Button created and it's state is %d\n", b->state); | ||
while (true) tight_loop_contents(); | ||
return 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "./deps/pico-gpio-interrupt/gpio-interrupt.c" | ||
#include "pico/stdlib.h" | ||
#include "./button.h" | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
long long int handle_button_alarm(long int a, void *p) { | ||
button_t *b = (button_t *)(p); | ||
bool state = gpio_get(b->pin); | ||
if (state != b->state) { | ||
b->state = state; | ||
b->onchange(b); | ||
} | ||
return 0; | ||
} | ||
|
||
void handle_button_interrupt(void *p) { | ||
button_t *b = (button_t *)(p); | ||
bool state = gpio_get(b->pin); | ||
add_alarm_in_us(100, handle_button_alarm, b, true); | ||
} | ||
|
||
button_t * create_button(int pin, void (*onchange)(button_t *)) { | ||
button_t *b = (button_t *)(malloc(sizeof(button_t))); | ||
listen(pin, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, handle_button_interrupt, b); | ||
b->pin = pin; | ||
b->onchange = onchange; | ||
b->state = gpio_get(pin); | ||
return b; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef PICO_BUTTON_H | ||
#define PICO_BUTTON_H | ||
|
||
typedef struct button_t { | ||
uint8_t pin; | ||
bool state; | ||
void (*onchange)(struct button_t *button); | ||
} button_t; | ||
|
||
long long int handle_button_alarm(long int a, void *p); | ||
void handle_button_interrupt(void *p); | ||
button_t * create_button(int pin, void (*onchange)(button_t *)); | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "pico-button", | ||
"version": "0.1.0", | ||
"repo": "jkroso/pico-button.c", | ||
"description": "A debounced button struct for the Raspberry Pico", | ||
"keywords": ["pico", "button"], | ||
"license": "MIT", | ||
"src": [ | ||
"button.h", | ||
"button.c" | ||
], | ||
"dependencies": { | ||
"jkroso/pico-gpio-interrupt.c": "0.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "./deps/GPIO-Interrupt/GPIO-Interrupt.c" | ||
#include "./src/Button.c" | ||
#include "pico/stdlib.h" | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
void onchange(button_t *button_p) { | ||
button_t *button = (button_t*)button_p; | ||
printf("Button on pin %d changed state to %d\n", button->pin, button->state); | ||
} | ||
|
||
int main() { | ||
stdio_init_all(); | ||
button_t *b = create_button(21, onchange); | ||
printf("Button created and it's state is %d\n", b->state); | ||
while (true) tight_loop_contents(); | ||
return 0; | ||
} |