From a5d1fadf2ea8f33c7baf6d60724e18b3f9724042 Mon Sep 17 00:00:00 2001 From: Lukasz Skrzypczak Date: Wed, 17 Feb 2021 01:02:19 -0800 Subject: [PATCH] [EGD-3685] Vibra handling Added driver & business logic. Responds to messages. --- .../application-call/ApplicationCall.cpp | 5 ++ module-bsp/board/linux/vibrator/vibrator.cpp | 26 +++++-- .../board/rt1051/bsp/vibrator/vibrator.cpp | 22 ++++-- module-bsp/bsp/vibrator/vibrator.hpp | 20 ++++- module-services/service-evtmgr/CMakeLists.txt | 1 + .../service-evtmgr/EventManager.cpp | 30 ++++++- .../service-evtmgr/WorkerEvent.cpp | 1 + .../service-evtmgr/EVMessages.hpp | 14 ++++ .../service-evtmgr/EventManager.hpp | 3 + .../service-vibra/VibraService.cpp | 78 +++++++++++++++++++ .../service-vibra/VibraService.hpp | 49 ++++++++++++ source/MessageType.hpp | 3 + 12 files changed, 234 insertions(+), 18 deletions(-) create mode 100644 module-services/service-evtmgr/service-vibra/VibraService.cpp create mode 100644 module-services/service-evtmgr/service-vibra/VibraService.hpp diff --git a/module-apps/application-call/ApplicationCall.cpp b/module-apps/application-call/ApplicationCall.cpp index a2d2078f3dab1d7a99fee567e673b035c2577afa..2fa7b9abbe7e3121bd71777e05babd6c41ac8eb8 100644 --- a/module-apps/application-call/ApplicationCall.cpp +++ b/module-apps/application-call/ApplicationCall.cpp @@ -27,6 +27,11 @@ #include #include +#include +#include +#include +#include "service-evtmgr/EVMessages.hpp" + namespace app { ApplicationCall::ApplicationCall(std::string name, std::string parent, StartInBackground startInBackground) diff --git a/module-bsp/board/linux/vibrator/vibrator.cpp b/module-bsp/board/linux/vibrator/vibrator.cpp index 352aeaedddab2aef015248dfeb4ccf2060b0ed57..426a4679f3b15145fa540d8d98d5e2817f1e37d8 100644 --- a/module-bsp/board/linux/vibrator/vibrator.cpp +++ b/module-bsp/board/linux/vibrator/vibrator.cpp @@ -1,24 +1,34 @@ // Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md -#include -#include - -static std::shared_ptr port; +#include "bsp/vibrator/vibrator.hpp" +#include namespace bsp { namespace vibrator { - void init() - { - } void enable() { + LOG_DEBUG("vibration starts\t\U0001f7e2\U0001f4f3"); } void disable() { + LOG_DEBUG("vibration ends \t\U0001f6d1\U0001f4f3"); + } + void init() + {} + void deinit() + {} + void set(State state) + { + if (state == State::On) { + enable(); + } + else { + disable(); + } } } // namespace vibrator -} // namespace bsp +} // namespace bsp \ No newline at end of file diff --git a/module-bsp/board/rt1051/bsp/vibrator/vibrator.cpp b/module-bsp/board/rt1051/bsp/vibrator/vibrator.cpp index 9207fc08f00aa21949f41bc869419b7f25fd5ae8..7a77285ecb65794ef5a6ec81588ea85cc66364c1 100644 --- a/module-bsp/board/rt1051/bsp/vibrator/vibrator.cpp +++ b/module-bsp/board/rt1051/bsp/vibrator/vibrator.cpp @@ -15,8 +15,16 @@ namespace bsp using namespace drivers; - void disable(); - void init() + void enable() + { + port->WritePin(static_cast(BoardDefinitions::VIBRATOR_EN), 1); + } + void disable() + { + port->WritePin(static_cast(BoardDefinitions::VIBRATOR_EN), 0); + } + + void init(sys::ms pulse) { port = DriverGPIO::Create(static_cast(BoardDefinitions::VIBRATOR_GPIO), DriverGPIOParams{}); port->ConfPin(DriverGPIOPinParams{.dir = DriverGPIOPinParams::Direction::Output, @@ -26,13 +34,11 @@ namespace bsp disable(); } - void enable() - { - port->WritePin(static_cast(BoardDefinitions::VIBRATOR_EN), 1); - } - void disable() + + void deinit() { - port->WritePin(static_cast(BoardDefinitions::VIBRATOR_EN), 0); + disable(); } + } // namespace vibrator } // namespace bsp diff --git a/module-bsp/bsp/vibrator/vibrator.hpp b/module-bsp/bsp/vibrator/vibrator.hpp index 9401ea409eb5eef1cfa14033821d033ce5d12792..c065ee3adf989bc561914615ed6a58f4eac4d9df 100644 --- a/module-bsp/bsp/vibrator/vibrator.hpp +++ b/module-bsp/bsp/vibrator/vibrator.hpp @@ -1,11 +1,29 @@ #pragma once +#include +#include + +#include +#include + namespace bsp { namespace vibrator { - void init(); + inline constexpr auto default_vibra_pulse = 1000; /// default: 1000 ms vibra pulse + inline constexpr auto default_vibra_pause = 1000; /// default: 1000 ms vibra pause between pulses + + enum class Action + { + pulse, + pulseRepeat, + stop, + checkState, + }; + void enable(); void disable(); + void init(sys::ms pulse = static_cast(default_vibra_pulse)); + void deinit(); } // namespace vibrator } // namespace bsp diff --git a/module-services/service-evtmgr/CMakeLists.txt b/module-services/service-evtmgr/CMakeLists.txt index 0a540b81fe7769dd4d54e779a033b05710504100..de399decb12a50da47b07178a1f91d8f9b379e7e 100644 --- a/module-services/service-evtmgr/CMakeLists.txt +++ b/module-services/service-evtmgr/CMakeLists.txt @@ -10,6 +10,7 @@ set(SOURCES battery-level-check/BatteryLevelCheck.cpp screen-light-control/ControlFunctions.cpp screen-light-control/ScreenLightControl.cpp + service-vibra/VibraService.cpp ) add_library(${PROJECT_NAME} STATIC ${SOURCES}) diff --git a/module-services/service-evtmgr/EventManager.cpp b/module-services/service-evtmgr/EventManager.cpp index 741947edf3e579cde64338c55191912c19a5fcec..84e93d5ffdaa58594c23828f4421b9738aa88f8d 100644 --- a/module-services/service-evtmgr/EventManager.cpp +++ b/module-services/service-evtmgr/EventManager.cpp @@ -41,10 +41,12 @@ #include #include #include +#include EventManager::EventManager(const std::string &name) : sys::Service(name), settings(std::make_shared(this)), - screenLightControl(std::make_unique(settings, this)) + screenLightControl(std::make_unique(settings, this)), + vibraService(std::make_unique(this)) { LOG_INFO("[%s] Initializing", name.c_str()); alarmTimestamp = 0; @@ -288,6 +290,12 @@ sys::ReturnCodes EventManager::InitHandler() return std::make_shared(); }); + connect(sevm::VibraMessage(bsp::vibrator::Action::stop), [&](sys::Message *msgl) { + auto request = static_cast(msgl); + processVibraRequest(request->action, request->repetitionTime); + return std::make_shared(); + }); + // initialize keyboard worker EventWorker = std::make_unique(this); @@ -376,5 +384,25 @@ bool EventManager::processKeypadBacklightRequest(bsp::keypad_backlight::Action a return response; } +bool EventManager::processVibraRequest(bsp::vibrator::Action act, sys::ms RepetitionTime) +{ + bool response = true; + switch (act) { + case bsp::vibrator::Action::pulse: + vibraService->Pulse(); + break; + case bsp::vibrator::Action::pulseRepeat: + vibraService->PulseRepeat(RepetitionTime); + break; + case bsp::vibrator::Action::stop: + vibraService->PulseRepeatStop(); + break; + case bsp::vibrator::Action::checkState: + response = vibraService->checkState(); + break; + } + return response; +} + void EventManager::GetNextAlarmTimestamp(time_t timestamp) {} diff --git a/module-services/service-evtmgr/WorkerEvent.cpp b/module-services/service-evtmgr/WorkerEvent.cpp index 53e61a8029a1477bb1aab3abbaca2a0080416fb6..9e082a7f8ba7e9c3f7cede351d23b97239546c74 100644 --- a/module-services/service-evtmgr/WorkerEvent.cpp +++ b/module-services/service-evtmgr/WorkerEvent.cpp @@ -244,6 +244,7 @@ bool WorkerEvent::deinit(void) bsp::keypad_backlight::deinit(); bsp::eink_frontlight::deinit(); bsp::light_sensor::deinit(); + bsp::vibrator::deinit(); battery_level_check::deinit(); diff --git a/module-services/service-evtmgr/service-evtmgr/EVMessages.hpp b/module-services/service-evtmgr/service-evtmgr/EVMessages.hpp index 9cd80799b2bf5b2590251f13f268e0b5f9b01acd..6df6df50ea1158f7425b56995ed78203fa5f627b 100644 --- a/module-services/service-evtmgr/service-evtmgr/EVMessages.hpp +++ b/module-services/service-evtmgr/service-evtmgr/EVMessages.hpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include @@ -132,6 +134,7 @@ namespace sevm {} bsp::keypad_backlight::Action action; + std::chrono::seconds repetitionTime; }; class KeypadBacklightResponseMessage : public sys::Message @@ -142,4 +145,15 @@ namespace sevm bool success; }; + class VibraMessage : public Message + { + public: + VibraMessage(bsp::vibrator::Action act, sys::ms rptTime = bsp::vibrator::default_vibra_pause) + : Message(MessageType::VibraPulseMessage), action(act), repetitionTime(rptTime) + {} + + bsp::vibrator::Action action; + sys::ms repetitionTime; + }; + } /* namespace sevm*/ diff --git a/module-services/service-evtmgr/service-evtmgr/EventManager.hpp b/module-services/service-evtmgr/service-evtmgr/EventManager.hpp index 5d3cc7e3a5a36ee18e1f90796cbbe4f384f0daed..879a9501d5d12ddfef7425254590867001cc84da 100644 --- a/module-services/service-evtmgr/service-evtmgr/EventManager.hpp +++ b/module-services/service-evtmgr/service-evtmgr/EventManager.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include @@ -30,6 +31,7 @@ class EventManager : public sys::Service void HandleAlarmTrigger(sys::DataMessage *msgl); void GetNextAlarmTimestamp(time_t timestamp); bool processKeypadBacklightRequest(bsp::keypad_backlight::Action act); + bool processVibraRequest(bsp::vibrator::Action act, sys::ms RepetitionTime = static_cast(1000)); std::shared_ptr settings; @@ -48,6 +50,7 @@ class EventManager : public sys::Service bool alarmIsValid = false; std::unique_ptr screenLightControl; + std::unique_ptr vibraService; public: EventManager(const std::string &name = service::name::evt_manager); diff --git a/module-services/service-evtmgr/service-vibra/VibraService.cpp b/module-services/service-evtmgr/service-vibra/VibraService.cpp new file mode 100644 index 0000000000000000000000000000000000000000..185617d1939f5d5ca2a2580cf7600957d272f359 --- /dev/null +++ b/module-services/service-evtmgr/service-vibra/VibraService.cpp @@ -0,0 +1,78 @@ +// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "VibraService.hpp" +#include "service-evtmgr/BatteryMessages.hpp" +#include "SystemManager/Constants.hpp" +#include + +#include + +namespace service_vibra +{ + VibraService::VibraService(sys::Service *parent) + { + vibratorTimerOneshot = + std::make_unique("VibraOneshotTimer", parent, bsp::vibrator::default_vibra_pulse); + vibratorTimerPause = + std::make_unique("VibraPauseTimer", parent, bsp::vibrator::default_vibra_pause); + + vibratorTimerOneshot->setInterval(bsp::vibrator::default_vibra_pulse); + vibratorTimerPause->setInterval(bsp::vibrator::default_vibra_pause); + } + + VibraService::~VibraService() + {} + + void VibraService::intPulse(bool repetitive) + { + if (repetitive) { + vibratorTimerOneshot->connect([&](sys::Timer &) { + bsp::vibrator::disable(); + vibratorTimerPause->start(); + }); + } + else { + vibratorTimerOneshot->connect([&](sys::Timer &) { bsp::vibrator::disable(); }); + } + bsp::vibrator::enable(); + vibratorTimerOneshot->start(); + } + + void VibraService::Pulse() + { + intPulse(false); + } + + void VibraService::PulseRepeat(sys::ms time) + { + _repetitions = static_cast(time) / (static_cast(bsp::vibrator::default_vibra_pulse) + + static_cast(bsp::vibrator::default_vibra_pause)); + + vibratorTimerPause->connect([&](sys::Timer &) { + if ((_repetitions) && (_repetitions--)) /// call itself for calculated number of repetitions + { + intPulse(true); + } + }); + intPulse(true); + } + + void VibraService::PulseRepeatStop() + { + _repetitions = 1; + bsp::vibrator::disable(); + vibratorTimerPause->stop(); + vibratorTimerOneshot->stop(); + } + bool VibraService::checkState() + { + if (_repetitions > 1) { + return true; + } + else { + return false; + } + } + +} // namespace service_vibra diff --git a/module-services/service-evtmgr/service-vibra/VibraService.hpp b/module-services/service-evtmgr/service-vibra/VibraService.hpp new file mode 100644 index 0000000000000000000000000000000000000000..28a0deffd3ba5185e1e2909a7eefb1b1417ac8ec --- /dev/null +++ b/module-services/service-evtmgr/service-vibra/VibraService.hpp @@ -0,0 +1,49 @@ +// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include +#include + +namespace sys +{ + class Timer; + class Service; +} // namespace sys + +namespace service_vibra +{ + class VibraService + { + public: + explicit VibraService(sys::Service *parent); + ~VibraService(); + + /// According to design : 04.06 — Apps Tools.png there is only ON/OFF setting for vibra. + /// But there is also number of sound repetitions so we may assume that vibra should be + /// set the same way. + void Pulse(); + /// but we also want to repeat pulses during eg. full length of ringtone + void PulseRepeat(sys::ms time); + void PulseRepeatStop(); + bool checkState(); + + /// to use, send message: + /// eg. vibrate 10 seconds: + /// bus.sendUnicast(std::make_shared(bsp::vibrator::Action::pulseRepeat, 10000), + /// service::name::evt_manager); vibrate once: + /// bus.sendUnicast(std::make_shared(bsp::vibrator::Action::pulse), + /// service::name::evt_manager); stop vibrating: + /// bus.sendUnicast(std::make_shared(bsp::vibrator::Action::stop), + /// service::name::evt_manager); + + private: + std::unique_ptr vibratorTimerOneshot; + std::unique_ptr vibratorTimerPause; + + int _repetitions = 1; + + void intPulse(bool repetitive); + }; +} // namespace service_vibra diff --git a/source/MessageType.hpp b/source/MessageType.hpp index c9db2c2043bd4ec9fb1ce34508274d9bcd3ca471..9fe21ef08098ef1139a34604a6cbc4886c5649ba 100644 --- a/source/MessageType.hpp +++ b/source/MessageType.hpp @@ -245,6 +245,9 @@ enum class MessageType ScreenLightControlAction, ScreenLightControlParameters, ScreenLightControlParametersResponse, + + // Vibra messages + VibraPulseMessage, }; #endif /* SOURCE_MESSAGETYPE_HPP_ */