Skip to content

Commit

Permalink
Better example
Browse files Browse the repository at this point in the history
  • Loading branch information
TuriSc committed Feb 15, 2023
1 parent cc28427 commit 4a9c1e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
21 changes: 1 addition & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,4 @@ A C library to define and debounce push buttons on a Raspberry Pi Pico.
It generates interrupts after listening to GPIO_IRQ events.
It allows to define multiple buttons simultaneously.

Fork of [jkroso/pico-button.c](https://github.com/jkroso/pico-button.c) including [jkroso/pico-gpio-interrupt.c](https://github.com/jkroso/pico-gpio-interrupt.c), both by Jake Rosoman. MIT license.

## Usage Example
```c
#include <button.h>
#define BUTTON_PIN 21

void onchange(button_t *button_p) {
button_t *button = (button_t*)button_p;
printf("Button on pin %d changed its state to %d\n", button->pin, button->state);
}

int main() {
stdio_init_all();
button_t *my_button = create_button(BUTTON_PIN, onchange);
printf("Button created. Its state is %d\n", my_button->state);
while (true) tight_loop_contents();
return 0;
}
```
Fork of [jkroso/pico-button.c](https://github.com/jkroso/pico-button.c) including [jkroso/pico-gpio-interrupt.c](https://github.com/jkroso/pico-gpio-interrupt.c), both by Jake Rosoman. MIT license.
20 changes: 17 additions & 3 deletions example.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
#include "pico/stdlib.h"
#include <stdio.h>
#include <button.h>
#define BUTTON_PIN 21
#define PLAY_BUTTON 16 // Button on GPIO 16
#define PAUSE_BUTTON 17 // Button on GPIO 17

void onchange(button_t *button_p) {
button_t *button = (button_t*)button_p;
printf("Button on pin %d changed its state to %d\n", button->pin, button->state);

if(button->state) return; // Ignore button release. Invert the logic if using
// a pullup (internal or external).

switch(button->pin){
case PLAY_BUTTON:
printf("Play\n");
break;
case PAUSE_BUTTON:
printf("Pause\n");
break;
}
}

int main() {
stdio_init_all();
button_t *my_button = create_button(BUTTON_PIN, onchange);
printf("Button created. Its state is %d\n", my_button->state);
button_t *play_button = create_button(PLAY_BUTTON, onchange);
button_t *pause_button = create_button(PAUSE_BUTTON, onchange);

while (true) tight_loop_contents();
return 0;
}

0 comments on commit 4a9c1e8

Please sign in to comment.