Skip to content

Commit

Permalink
Add vibration function (#128)
Browse files Browse the repository at this point in the history
Adding the vibration function.

event VIBRATE will vibrate the device for 500 ms 

To change the vibration time, change the define VIBRATE_FOR_MSEC in the
node/code/modules/iohandler/iohadler.c
  • Loading branch information
moritzholzer committed Jun 17, 2024
2 parents 4a8c4e9 + d1d2242 commit be673af
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
10 changes: 10 additions & 0 deletions node/code/modules/io_handler/include/init_buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
extern "C" {
#endif

#include <stdio.h>
#include "thread.h"
#include "board.h"
#include "periph/gpio.h"
#include "events.h"


//TODO Workaround
extern kernel_pid_t dispatcher_pid;

//Buttons initialization functions prototypes:
int init_buttons(void);

void button_up_callback (void *arg);
Expand All @@ -20,6 +27,9 @@ void button_down_callback (void *arg);
void button_right_callback (void *arg);
void button_ok_callback (void *arg);

//Vibration functions prototypes:
void vibrate(uint16_t msec);

#ifdef __cplusplus
}
#endif
Expand Down
22 changes: 13 additions & 9 deletions node/code/modules/io_handler/init_buttons.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,25 @@

#include "init_buttons.h"

#include <stdio.h>

#include "board.h"
#include "periph/gpio.h"
#include "events.h"

#define ENABLE_DEBUG 1
#include "debug.h"

//Define the physical GPIO Pins used for buttons:
gpio_t button_ok = GPIO_PIN(0, 5); //PIN A1
gpio_t button_right = GPIO_PIN(0, 30); //PIN sA2
gpio_t button_up = GPIO_PIN(0, 28); //PIN A3
gpio_t button_down = GPIO_PIN(0, 2); //PIN A4
gpio_t button_left = GPIO_PIN(0, 3); //PIN A5

//Define the vibration module GPIO:
gpio_t vibr_gpio = GPIO_PIN(1, 9);
gpio_mode_t vibr_gpio_mode = GPIO_OUT;
gpio_mode_t vibr_gpio_mode = GPIO_OUT; //Define the vibr. GPIO as Output GPIO

int init_buttons(void)
{
puts("Buttons initialization...");

//Initialize button interrupts and define callback funcs:
gpio_init_int(button_up, GPIO_IN, GPIO_BOTH, button_up_callback, NULL);
gpio_init_int(button_left, GPIO_IN, GPIO_BOTH, button_left_callback, NULL);
gpio_init_int(button_down, GPIO_IN, GPIO_BOTH, button_down_callback, NULL);
Expand All @@ -48,11 +45,20 @@ int init_buttons(void)

puts("Vibration Module initialization...");

//Initialize vibration module
gpio_init(vibr_gpio, vibr_gpio_mode);
gpio_clear(vibr_gpio);
return 0;
}

//Vibrate for msec milliseconds:
void vibrate(uint16_t msec) {

gpio_set(vibr_gpio);
ztimer_sleep(ZTIMER_MSEC, msec);
gpio_clear(vibr_gpio);
}

void button_up_callback (void *arg)
{
(void) arg; /* the argument is not used */
Expand Down Expand Up @@ -117,5 +123,3 @@ void button_ok_callback (void *arg)
trigger_event(BUTTON_OK_RELEASED);
}
}


5 changes: 4 additions & 1 deletion node/code/modules/io_handler/io_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include "init_buttons.h"
#include "io_handler.h"

//Define the vibration time (milliseconds) here:
#define VIBRATE_FOR_MSEC 500

void io_init(void){
init_buttons();
}
Expand All @@ -31,7 +34,7 @@ handler_result_t ioHandler_handleEvent(EVENT_T event){
switch(event){
case VIBRATE:
DEBUG("[IoHandler:handleEvent]: vibrate\n");
// vibrate();
vibrate(VIBRATE_FOR_MSEC);
break;
default:
break;
Expand Down

0 comments on commit be673af

Please sign in to comment.