Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ else ()
INCLUDE_DIRS
"./include"
"./include/actuators"
"ports/ESP32/"
"../services/"
REQUIRES
"driver"
"hsm"
Expand Down
4 changes: 4 additions & 0 deletions drivers/include/bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace BSP

void BSP_init_actuators();

void BSP_init_sensors();

void BSP_LED_on();

void BSP_LED_off();
Expand All @@ -29,6 +31,8 @@ namespace BSP

void BSP_red_led_off();

void BSP_button_read();

/**
* @brief blocking wait in line
* @param ms no. of milliseconds to wait
Expand Down
13 changes: 13 additions & 0 deletions drivers/ports/ESP32/ESP32_bsp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef ESP32_BSP_H
#define ESP32_BSP_H
#include "bsp.h"
#include "freertos/FreeRTOS.h"


namespace ESP_BSP
{
void ESP32_BSP_Button_init(void);

void BSP_button_read(TimerHandle_t xTimer);
}
#endif //ESP32_BSP_H
70 changes: 69 additions & 1 deletion drivers/ports/ESP32/bsp.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
#include "led.h"
#include "bsp.h"
#include "ESP32_bsp.h"
#include "freertos/FreeRTOS.h"

#include <cstdint>
#include "driver/gpio.h"
#include "timebomb.h"

namespace BSP{
void BSP_init() {
BSP_init_actuators();
BSP_init_sensors();
}

void BSP_init_actuators() {
get_default_onboard_led()->LED_init();
get_blue_led()->LED_init();
}

void BSP_init_sensors() {
gpio_set_direction(GPIO_NUM_25, GPIO_MODE_INPUT);
gpio_set_pull_mode(GPIO_NUM_25, GPIO_PULLDOWN_ONLY);

gpio_set_direction(GPIO_NUM_26, GPIO_MODE_INPUT);
gpio_set_pull_mode(GPIO_NUM_26, GPIO_PULLDOWN_ONLY);
}


void BSP_LED_on() {
get_default_onboard_led()->LED_on();
}
Expand Down Expand Up @@ -70,8 +83,56 @@ namespace BSP{
get_red_led()->LED_off();
}

void BSP_button_read() {
int button_status[2] = {};

/* state of button. static to persist between func calls */
static struct ButtonDebouncing {
uint16_t depressed;
uint16_t previous;
} button = {0U, 0U}, button2 = {0U, 0U};

button_status[0] = gpio_get_level(GPIO_NUM_25);
button_status[1] = gpio_get_level(GPIO_NUM_26);

uint16_t tmp = button.depressed;
uint16_t tmp2 = button2.depressed;

button.depressed |= (button.previous && button_status[0]); /* set button 1 depressed */
button.depressed &= (button.previous || button_status[0]); /* set button 1 released */
button.previous = button_status[0]; /* update history for the next function call */

button2.depressed |= (button2.previous && button_status[1]); /* set button 2 depressed */
button2.depressed &= (button2.previous || button_status[1]); /* set button 2 released */
button2.previous = button_status[1]; /* update history for the next function call */

LED* get_blue_led() {
tmp ^= button.depressed;
tmp2 ^= button2.depressed;

if (tmp) { /*change of button state has occurred */
if (button_status[0]) { /* button pressed */
static const Event button_pressed_event = {BUTTON_PRESSED_SIG};
TimeBomb::get_default_instance()->_post(&button_pressed_event);
} else { /* button released */
static const Event button_released_event = {BUTTON_RELEASED_SIG};
TimeBomb::get_default_instance()->_post(&button_released_event);
}
}

if (tmp2) { /*change of button 2 state has occurred */
if (button_status[1]) { /* button 2 pressed */
static const Event button2_pressed_event = {BUTTON2_PRESSED_SIG};
TimeBomb::get_default_instance()->_post(&button2_pressed_event);
} else { /* button 2 released */
static const Event button2_released_event = {BUTTON2_RELEASED_SIG};
TimeBomb::get_default_instance()->_post(&button2_released_event);
}
}


}

LED* get_blue_led() {
static LED led(2);
return &led;
}
Expand All @@ -87,3 +148,10 @@ namespace BSP{
}

};

namespace ESP_BSP
{
void BSP_button_read(TimerHandle_t xTimer) {
BSP::BSP_button_read();
}
}
8 changes: 7 additions & 1 deletion hsm/ports/ESP32/ESP32_fsm.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "ESP32_fsm.h"
#include "bsp.h"
#include "ESP32_bsp.h"

#define QUEUE_LENGTH 10
#define MAX_TIME_EVENTS 10

#define TIMER_PERIOD_MS 100
#define BUTTON_INTERVAL_MS 10

static constexpr Event button_pressed_evt = {BUTTON_PRESSED_SIG};

Expand All @@ -15,11 +17,15 @@ void Active::_run(const Active *object) {
xTaskCreate(Active::event_loop, "TimeBomb task", 2048, &param, 1, nullptr);


object->_post(&button_pressed_evt);
// object->_post(&button_pressed_evt);

if (TimerHandle_t my_timer = xTimerCreate("MyTimer", pdMS_TO_TICKS(TIMER_PERIOD_MS), pdTRUE, nullptr, TimeEvent::tick); my_timer != nullptr) {
xTimerStart(my_timer, 0);
}

if (TimerHandle_t button_timer = xTimerCreate("ButtonTimer", pdMS_TO_TICKS(BUTTON_INTERVAL_MS), pdTRUE, nullptr, ESP_BSP::BSP_button_read); button_timer != nullptr) {
xTimerStart(button_timer, 0);
}
}


Expand Down
Loading