-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from stevenewald/callbacks
Centralize async callback storage in preparation for IPC/error handling
- Loading branch information
Showing
13 changed files
with
211 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include "config.hpp" | ||
#include "driver_enums.hpp" | ||
#include "drivers/gpio_pin.hpp" | ||
#include "drivers/gpio_pin_event.hpp" | ||
#include "microbit_v2.h" | ||
|
||
namespace edge::drivers { | ||
class ButtonController { | ||
GPIOPin button_a{BTN_A, GPIOConfiguration::IN_PUR}; | ||
GPIOPin button_b{BTN_B, GPIOConfiguration::IN_PUR}; | ||
|
||
void handle_button_press(ButtonType type, ButtonState state); | ||
void handle_gpio_interrupt(nrf_gpio_pin_sense_t sense, int pin); | ||
|
||
GPIOPinEvent event_a; | ||
GPIOPinEvent event_b; | ||
|
||
using SubscriptionArray = etl::array<SubscribeCallbackPtr, MAX_PROCESSES>; | ||
SubscriptionArray a_subscriptions; | ||
SubscriptionArray b_subscriptions; | ||
|
||
ButtonController(); | ||
~ButtonController() = default; | ||
|
||
public: | ||
ButtonController(const ButtonController&) = delete; | ||
ButtonController(ButtonController&&) = delete; | ||
ButtonController& operator=(const ButtonController&) = delete; | ||
ButtonController& operator=(ButtonController&&) = delete; | ||
|
||
static ButtonController& get(); | ||
|
||
bool get_button_pressed(ButtonType button_type); | ||
|
||
void subscribe_button_press( | ||
ButtonType type, SubscribeCallbackPtr callback, uint8_t process_id | ||
); | ||
}; | ||
} // namespace edge::drivers |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
#include "config.hpp" | ||
#include "drivers/driver_enums.hpp" | ||
|
||
namespace edge { | ||
// One option is to make the drivers and other things non singletons and have the | ||
// scheduler own everything. I'm actually okay with this, but not a big deal rn and this | ||
// works fine | ||
// This doesn't matter rn but would matter if we were worried about shutdown and wanted | ||
// very predictable dtor calls | ||
// TLDR: singleton vs everything owned by scheduler, food for thought | ||
class ProcessCallbackStorage { | ||
public: | ||
static constexpr uint8_t MAX_READY_CALLBACKS = 10; | ||
|
||
private: | ||
etl::array< | ||
etl::vector<drivers::subscribe_callback, MAX_READY_CALLBACKS>, MAX_PROCESSES> | ||
ready_callbacks; | ||
|
||
ProcessCallbackStorage() = default; | ||
~ProcessCallbackStorage() = default; | ||
|
||
public: | ||
ProcessCallbackStorage(const ProcessCallbackStorage&) = delete; | ||
ProcessCallbackStorage(ProcessCallbackStorage&&) = delete; | ||
ProcessCallbackStorage& operator=(const ProcessCallbackStorage&) = delete; | ||
ProcessCallbackStorage& operator=(ProcessCallbackStorage&&) = delete; | ||
|
||
void add_ready_callback( | ||
uint8_t process_id, drivers::SubscribeCallbackPtr callback, int arg1 = 0, | ||
int arg2 = 0 | ||
); | ||
|
||
etl::optional<drivers::subscribe_callback> get_ready_callback(uint8_t process_id); | ||
|
||
static ProcessCallbackStorage& get(); | ||
}; | ||
} // namespace edge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "drivers/button_driver.hpp" | ||
|
||
#include "drivers/driver_enums.hpp" | ||
#include "process_callback_storage.hpp" | ||
|
||
namespace edge::drivers { | ||
|
||
ButtonController& ButtonController::get() | ||
{ | ||
static ButtonController button_controller; | ||
return button_controller; | ||
} | ||
|
||
ButtonController::ButtonController() : | ||
event_a{ | ||
BTN_A, GPIOConfiguration::IN_PUR, | ||
aidan::GPIOEventController::GPIOEventCallback::create< | ||
ButtonController, &ButtonController::handle_gpio_interrupt>(*this) | ||
}, | ||
event_b{ | ||
BTN_B, GPIOConfiguration::IN_PUR, | ||
aidan::GPIOEventController::GPIOEventCallback::create< | ||
ButtonController, &ButtonController::handle_gpio_interrupt>(*this) | ||
} | ||
{} | ||
|
||
void ButtonController::subscribe_button_press( | ||
ButtonType type, SubscribeCallbackPtr callback, uint8_t process_id | ||
) | ||
{ | ||
if (type == ButtonType::A) { | ||
a_subscriptions[process_id] = callback; | ||
} | ||
else if (type == ButtonType::B) { | ||
b_subscriptions[process_id] = callback; | ||
} | ||
} | ||
|
||
bool ButtonController::get_button_pressed(ButtonType button_type) | ||
{ | ||
if (button_type == ButtonType::A) { | ||
return !button_a.read(); | ||
} | ||
else { | ||
return !button_b.read(); | ||
} | ||
} | ||
|
||
void ButtonController::handle_button_press(ButtonType type, ButtonState state) | ||
{ | ||
SubscriptionArray& button_subscriptions = | ||
type == ButtonType::A ? a_subscriptions : b_subscriptions; | ||
for (int process_id = 0; process_id < MAX_PROCESSES; process_id++) { | ||
if (button_subscriptions[process_id] != nullptr) { | ||
ProcessCallbackStorage::get().add_ready_callback( | ||
process_id, button_subscriptions[process_id], static_cast<int>(type), | ||
static_cast<int>(state) | ||
); | ||
} | ||
} | ||
} | ||
|
||
void ButtonController::handle_gpio_interrupt(nrf_gpio_pin_sense_t sense, int pin) | ||
{ | ||
ButtonType type = pin == BTN_A ? ButtonType::A : ButtonType::B; | ||
ButtonState state = | ||
sense == NRF_GPIO_PIN_SENSE_LOW ? ButtonState::DOWN : ButtonState::UP; | ||
handle_button_press(type, state); | ||
} | ||
} // namespace edge::drivers |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.