/* * Copyright (C) 2019 Max Regan * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #pragma once #include #include "Bsp/TaskScheduler.h" #include "Bsp/Task.h" #include "Bsp/ReturnCode.h" #include "Bsp/Drivers/GpioDriver.h" namespace BSP { class ButtonManager : public BSP::Schedule::Task { public: ButtonManager(BSP::Schedule::TaskScheduler &scheduler, GpioPin &btn_down, GpioPin &btn_mid, GpioPin &btn_up, BSP::time_t debounce_time) : m_scheduler(scheduler) , m_buttons { button_state(btn_down, debounce_time, ButtonManager::nop_callback), button_state(btn_mid, debounce_time, ButtonManager::nop_callback), button_state(btn_up, debounce_time, ButtonManager::nop_callback), } { m_instance = this; } ButtonManager() = delete; // TODO: Make a real singleton instead static ButtonManager *m_instance; enum Button { DOWN = 0, MID, UP, Count }; enum class ButtonState { PRESSED, NOT_PRESSED }; using ChangeCallback = std::function; BSP::Schedule::NextTime execute() override; BSP::ReturnCode init(); void set_callback(Button btn, ChangeCallback callback); void remove_callback(Button btn); static void irq(); private: struct button_state { button_state(GpioPin &pin, BSP::time_t debounce_time, ChangeCallback callback) : m_pin(pin) , m_debounce_time(debounce_time) , m_state(ButtonState::NOT_PRESSED) , m_state_change_ts(0) , m_callback(callback) {} GpioPin &m_pin; BSP::time_t const m_debounce_time; ButtonState m_prev_call_state; /*