diff --git a/src/shared/utils/PinObserver/PinObserver.cpp b/src/shared/utils/PinObserver/PinObserver.cpp index 7690c3b5e1dbd3097b7e00109120faedfbc77ba3..145b5ae795b6e96bfb410b371f26e07df1291417 100644 --- a/src/shared/utils/PinObserver/PinObserver.cpp +++ b/src/shared/utils/PinObserver/PinObserver.cpp @@ -43,7 +43,7 @@ bool PinObserver::registerPinCallback(miosix::GpioPin pin, PinCallback callback, { if (scheduler.addTask( std::bind(&PinObserver::periodicPinValueCheck, this, pin), - SAMPLE_PERIOD, TaskScheduler::Policy::RECOVER)) + pollInterval, TaskScheduler::Policy::RECOVER)) return true; else callbacks.erase(pin); @@ -65,8 +65,6 @@ void PinObserver::resetPinChangesCount(miosix::GpioPin pin) callbacks[pin].changesCount = 0; } -PinObserver::PinObserver() {} - void PinObserver::periodicPinValueCheck(miosix::GpioPin pin) { // Make sure the pin informations are still present diff --git a/src/shared/utils/PinObserver/PinObserver.h b/src/shared/utils/PinObserver/PinObserver.h index 6c798a6e64afffd7c13ec4ed0c4721d709224d92..86cec33f1cfdfd77f7f6a4672b88b11dc27779f9 100644 --- a/src/shared/utils/PinObserver/PinObserver.h +++ b/src/shared/utils/PinObserver/PinObserver.h @@ -22,7 +22,6 @@ #pragma once -#include <Singleton.h> #include <scheduler/TaskScheduler.h> #include <utils/GpioPinCompare.h> @@ -34,10 +33,10 @@ namespace Boardcore /** * @brief Pin transition. */ -enum class PinTransition +enum class PinTransition : uint8_t { - FALLING_EDGE, ///< The pin goes from high to low. - RISING_EDGE ///< The pin goes from low to high. + FALLING_EDGE = 0, ///< The pin goes from high to low. + RISING_EDGE ///< The pin goes from low to high. }; /** @@ -64,15 +63,22 @@ struct PinData * transition is also available, in order to be able to observe the current * state of the pin. */ -class PinObserver : public Singleton<PinObserver> +class PinObserver { - friend Singleton<PinObserver>; - - static constexpr uint32_t SAMPLE_PERIOD = 20; // 50Hz - public: using PinCallback = std::function<void(PinTransition)>; + /** + * @brief Construct a new PinObserver object. + * + * @param scheduler Scheduler to be used by this PinObserver. + * @param pollInterval Pin transition polling interval, defaults to 20 [ms]. + */ + explicit PinObserver(TaskScheduler &scheduler, uint32_t pollInterval = 20) + : scheduler{scheduler}, pollInterval{pollInterval} + { + } + /** * Observe a pin for a specific transition, and optionally for every * single state change. @@ -119,13 +125,6 @@ public: void resetPinChangesCount(miosix::GpioPin pin); private: - /** - * @brief Construct a new PinObserver object. - * - * @param pollInterval Pin transition polling interval, defaults to 20 [ms]. - */ - PinObserver(); - /** * @brief This function is added to the scheduler for every pin registered * in the PinObserver. @@ -134,7 +133,8 @@ private: */ void periodicPinValueCheck(miosix::GpioPin pin); - TaskScheduler scheduler; + TaskScheduler &scheduler; + uint32_t pollInterval; /// Map of all the callbacks registered in the PinObserver. std::map<miosix::GpioPin, PinData, GpioPinCompare> callbacks; diff --git a/src/tests/test-pinobserver.cpp b/src/tests/test-pinobserver.cpp index 9856200ef36e72da7b604ae2d496a830ee3dc912..10dd5e98780b78a9c3e84e44d5949f59a5b45ec9 100644 --- a/src/tests/test-pinobserver.cpp +++ b/src/tests/test-pinobserver.cpp @@ -56,6 +56,9 @@ void onTransition(GpioPin pin, PinTransition transition) int main() { + TaskScheduler scheduler; + PinObserver observer{scheduler}; + auto btn = GpioPin(BTN_PORT, BTN_PIN); auto pin1 = GpioPin(PIN1_PORT, PIN1_PIN); auto pin2 = GpioPin(PIN2_PORT, PIN2_PIN); @@ -64,12 +67,9 @@ int main() pin1.mode(Mode::INPUT_PULL_DOWN); pin2.mode(Mode::INPUT_PULL_UP); - PinObserver::getInstance().registerPinCallback( - btn, std::bind(onTransition, btn, _1), 10); - PinObserver::getInstance().registerPinCallback( - pin1, std::bind(onTransition, pin1, _1), 10); - PinObserver::getInstance().registerPinCallback( - pin2, std::bind(onTransition, pin2, _1), 10); + observer.registerPinCallback(btn, std::bind(onTransition, btn, _1), 10); + observer.registerPinCallback(pin1, std::bind(onTransition, pin1, _1), 10); + observer.registerPinCallback(pin2, std::bind(onTransition, pin2, _1), 10); while (true) Thread::sleep(10000);