Skip to content

Commit

Permalink
button: adapted gpio handling to latest riot
Browse files Browse the repository at this point in the history
  • Loading branch information
smlng committed Sep 20, 2016
1 parent fd16341 commit 8c49962
Showing 1 changed file with 11 additions and 42 deletions.
53 changes: 11 additions & 42 deletions src/button/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,14 @@
#include "board.h"
#include "periph/gpio.h"

// compatibility
#ifndef LED_ON
#define LED_ON LED_RED_ON
#define LED_OFF LED_RED_OFF
#define LED_TOGGLE LED_RED_TOGGLE
#endif

// button parameters, matching Atmel SAM R21 board
#ifdef BOARD_SAMR21_XPRO
#define BTN_PORT 0 // Port+Pin A28
#define BTN_PIN 28
#define BTN_PULL GPIO_PULLUP
#define BTN_FLANK GPIO_BOTH
#else
// or button parameters, matching Phytec phyWAVE
// Note: port is ignored, only pin is used by GPIO
//#ifdef BOARD_PBA_D_01_KW2X
#define BTN_PORT 0 // Port+Pin D1
#define BTN_PIN 3
#define BTN_PULL GPIO_PULLUP
#define BTN_FLANK GPIO_BOTH
#endif

/**
* @brief callback function for button interrupt
*
* @param[in] arg contains gpio pin
* @param[in] arg NULL
*/
static void btn_callback(void* arg)
{
gpio_t pin = *((gpio_t*)arg);
if (gpio_read(pin)) {
puts("LED off");
LED_OFF;
}
else {
puts("LED on");
LED_ON;
}
LED0_TOGGLE;
}

/**
Expand All @@ -53,21 +22,21 @@ static void btn_callback(void* arg)
*/
int main(void)
{
puts("SmartUniversity - Button Example!");
puts("================");
puts(" SmartUniversity - Button Example! ");
puts("===================================");
printf("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
printf("This board features a(n) %s MCU.\n", RIOT_MCU);
puts("================");
puts("===================================");

// init button as interrupt to trigger LED
int gpio_pin = GPIO(BTN_PORT, BTN_PIN);
if (gpio_init_int(gpio_pin, BTN_PULL, BTN_FLANK, btn_callback, (void *)&gpio_pin) < 0) {
printf("Error while initializing PORT_%i.%02i as external interrupt\n",
BTN_PORT, BTN_PIN);
gpio_t pin = BUTTON_GPIO;
gpio_mode_t mode = GPIO_IN_PU;
gpio_flank_t flank = GPIO_FALLING;
if (gpio_init_int(pin, mode, flank, btn_callback, NULL) < 0) {
puts("Error while initializing BUTTON_GPIO as external interrupt!");
return 1;
}
printf("PORT_%i.%02i initialized successful as external interrupt\n",
BTN_PORT, BTN_PIN);
printf("BUTTON_GPIO initialized successful as external interrupt.");

// infinity loop 1
while(1) {
Expand Down

0 comments on commit 8c49962

Please sign in to comment.