~aleteoryx/muditaos

a5d1fadf2ea8f33c7baf6d60724e18b3f9724042 — Lukasz Skrzypczak 5 years ago f724d90
[EGD-3685] Vibra handling

Added driver & business logic. Responds to messages.
M module-apps/application-call/ApplicationCall.cpp => module-apps/application-call/ApplicationCall.cpp +5 -0
@@ 27,6 27,11 @@
#include <module-apps/application-phonebook/data/PhonebookItemData.hpp>
#include <module-services/service-db/service-db/DBServiceAPI.hpp>

#include <bsp/vibrator/vibrator.hpp>
#include <service-evtmgr/Constants.hpp>
#include <service-evtmgr/Message.hpp>
#include "service-evtmgr/EVMessages.hpp"

namespace app
{
    ApplicationCall::ApplicationCall(std::string name, std::string parent, StartInBackground startInBackground)

M module-bsp/board/linux/vibrator/vibrator.cpp => module-bsp/board/linux/vibrator/vibrator.cpp +18 -8
@@ 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 <bsp/BoardDefinitions.hpp>
#include <drivers/gpio/DriverGPIO.hpp>

static std::shared_ptr<drivers::DriverGPIO> port;
#include "bsp/vibrator/vibrator.hpp"
#include <module-utils/log/log.hpp>

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

M module-bsp/board/rt1051/bsp/vibrator/vibrator.cpp => module-bsp/board/rt1051/bsp/vibrator/vibrator.cpp +14 -8
@@ 15,8 15,16 @@ namespace bsp

        using namespace drivers;

        void disable();
        void init()
        void enable()
        {
            port->WritePin(static_cast<uint32_t>(BoardDefinitions::VIBRATOR_EN), 1);
        }
        void disable()
        {
            port->WritePin(static_cast<uint32_t>(BoardDefinitions::VIBRATOR_EN), 0);
        }

        void init(sys::ms pulse)
        {
            port = DriverGPIO::Create(static_cast<GPIOInstances>(BoardDefinitions::VIBRATOR_GPIO), DriverGPIOParams{});
            port->ConfPin(DriverGPIOPinParams{.dir = DriverGPIOPinParams::Direction::Output,


@@ 26,13 34,11 @@ namespace bsp

            disable();
        }
        void enable()
        {
            port->WritePin(static_cast<uint32_t>(BoardDefinitions::VIBRATOR_EN), 1);
        }
        void disable()

        void deinit()
        {
            port->WritePin(static_cast<uint32_t>(BoardDefinitions::VIBRATOR_EN), 0);
            disable();
        }

    } // namespace vibrator
} // namespace bsp

M module-bsp/bsp/vibrator/vibrator.hpp => module-bsp/bsp/vibrator/vibrator.hpp +19 -1
@@ 1,11 1,29 @@
#pragma once

#include <Service/Timer.hpp>
#include <Utils.hpp>

#include <chrono>
#include <memory>

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<sys::ms>(default_vibra_pulse));
        void deinit();
    } // namespace vibrator
} // namespace bsp

M module-services/service-evtmgr/CMakeLists.txt => module-services/service-evtmgr/CMakeLists.txt +1 -0
@@ 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})

M module-services/service-evtmgr/EventManager.cpp => module-services/service-evtmgr/EventManager.cpp +29 -1
@@ 41,10 41,12 @@
#include <SystemManager/messages/CpuFrequencyMessage.hpp>
#include <common_data/EventStore.hpp>
#include <SystemManager/messages/PhoneModeRequest.hpp>
#include <service-vibra/VibraService.hpp>

EventManager::EventManager(const std::string &name)
    : sys::Service(name), settings(std::make_shared<settings::Settings>(this)),
      screenLightControl(std::make_unique<screen_light_control::ScreenLightControl>(settings, this))
      screenLightControl(std::make_unique<screen_light_control::ScreenLightControl>(settings, this)),
      vibraService(std::make_unique<service_vibra::VibraService>(this))
{
    LOG_INFO("[%s] Initializing", name.c_str());
    alarmTimestamp = 0;


@@ 288,6 290,12 @@ sys::ReturnCodes EventManager::InitHandler()
        return std::make_shared<sys::ResponseMessage>();
    });

    connect(sevm::VibraMessage(bsp::vibrator::Action::stop), [&](sys::Message *msgl) {
        auto request = static_cast<sevm::VibraMessage *>(msgl);
        processVibraRequest(request->action, request->repetitionTime);
        return std::make_shared<sys::ResponseMessage>();
    });

    // initialize keyboard worker
    EventWorker = std::make_unique<WorkerEvent>(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)
{}

M module-services/service-evtmgr/WorkerEvent.cpp => module-services/service-evtmgr/WorkerEvent.cpp +1 -0
@@ 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();


M module-services/service-evtmgr/service-evtmgr/EVMessages.hpp => module-services/service-evtmgr/service-evtmgr/EVMessages.hpp +14 -0
@@ 15,6 15,8 @@
#include <bsp/keyboard/key_codes.hpp>
#include <bsp/torch/torch.hpp>
#include <bsp/keypad_backlight/keypad_backlight.hpp>
#include <service-vibra/VibraService.hpp>
#include <Timer.hpp>

#include <string>



@@ 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*/

M module-services/service-evtmgr/service-evtmgr/EventManager.hpp => module-services/service-evtmgr/service-evtmgr/EventManager.hpp +3 -0
@@ 14,6 14,7 @@
#include <bsp/keyboard/key_codes.hpp>
#include <bsp/keypad_backlight/keypad_backlight.hpp>
#include <screen-light-control/ScreenLightControl.hpp>
#include <service-vibra/VibraService.hpp>

#include <service-db/DBServiceName.hpp>



@@ 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<sys::ms>(1000));

    std::shared_ptr<settings::Settings> settings;



@@ 48,6 50,7 @@ class EventManager : public sys::Service
    bool alarmIsValid = false;

    std::unique_ptr<screen_light_control::ScreenLightControl> screenLightControl;
    std::unique_ptr<service_vibra::VibraService> vibraService;

  public:
    EventManager(const std::string &name = service::name::evt_manager);

A module-services/service-evtmgr/service-vibra/VibraService.cpp => module-services/service-evtmgr/service-vibra/VibraService.cpp +78 -0
@@ 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 <Service/Timer.hpp>

#include <common_data/EventStore.hpp>

namespace service_vibra
{
    VibraService::VibraService(sys::Service *parent)
    {
        vibratorTimerOneshot =
            std::make_unique<sys::Timer>("VibraOneshotTimer", parent, bsp::vibrator::default_vibra_pulse);
        vibratorTimerPause =
            std::make_unique<sys::Timer>("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<int>(time) / (static_cast<int>(bsp::vibrator::default_vibra_pulse) +
                                                 static_cast<int>(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

A module-services/service-evtmgr/service-vibra/VibraService.hpp => module-services/service-evtmgr/service-vibra/VibraService.hpp +49 -0
@@ 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 <Service/Service.hpp>
#include <bsp/vibrator/vibrator.hpp>

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<sevm::VibraMessage>(bsp::vibrator::Action::pulseRepeat, 10000),
        /// service::name::evt_manager); vibrate once:
        /// bus.sendUnicast(std::make_shared<sevm::VibraMessage>(bsp::vibrator::Action::pulse),
        /// service::name::evt_manager); stop vibrating:
        /// bus.sendUnicast(std::make_shared<sevm::VibraMessage>(bsp::vibrator::Action::stop),
        /// service::name::evt_manager);

      private:
        std::unique_ptr<sys::Timer> vibratorTimerOneshot;
        std::unique_ptr<sys::Timer> vibratorTimerPause;

        int _repetitions = 1;

        void intPulse(bool repetitive);
    };
} // namespace service_vibra

M source/MessageType.hpp => source/MessageType.hpp +3 -0
@@ 245,6 245,9 @@ enum class MessageType
    ScreenLightControlAction,
    ScreenLightControlParameters,
    ScreenLightControlParametersResponse,

    // Vibra messages
    VibraPulseMessage,
};

#endif /* SOURCE_MESSAGETYPE_HPP_ */