Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroso committed Aug 19, 2021
0 parents commit ad6672e
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
build.sh
deps
CMakeLists.txt
pico_sdk_import.cmake
40 changes: 40 additions & 0 deletions Readme.md
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;
}
```
31 changes: 31 additions & 0 deletions button.c
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;
}
15 changes: 15 additions & 0 deletions button.h
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
15 changes: 15 additions & 0 deletions clib.json
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"
}
}
18 changes: 18 additions & 0 deletions example.c
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;
}

0 comments on commit ad6672e

Please sign in to comment.