~aleteoryx/muditaos

ref: sign_test muditaos/module-services/service-evtmgr/vibra/Vibra.cpp -rw-r--r-- 3.1 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Vibra.hpp"
#include "system/Constants.hpp"
#include <Timers/TimerFactory.hpp>

namespace vibra_handle
{
    Vibra::Vibra(sys::Service *parent) : parent{parent}
    {}

    void Vibra::intPulse(bool repetitive)
    {
        if (repetitive) {
            vibratorTimer =
                sys::TimerFactory::createSingleShotTimer(parent,
                                                         "VibraOneshotTimer",
                                                         std::chrono::milliseconds{bsp::vibrator::defaultVibraPulseMs},
                                                         [this](sys::Timer & /*timer*/) {
                                                             bsp::vibrator::disable();
                                                             vibratorTimerPause.start();
                                                         });
        }
        else {
            vibratorTimer =
                sys::TimerFactory::createSingleShotTimer(parent,
                                                         "VibraOneshotTimer",
                                                         std::chrono::milliseconds{bsp::vibrator::defaultVibraPulseMs},
                                                         [](sys::Timer & /*timer*/) { bsp::vibrator::disable(); });
        }

        bsp::vibrator::enable();
        vibratorTimer.start();
    }

    void Vibra::Pulse()
    {
        intPulse(false);
    }

    void Vibra::PulseRepeat(std::chrono::milliseconds time)
    {
        repetitions = static_cast<int>(time.count()) / (static_cast<int>(bsp::vibrator::defaultVibraPulseMs) +
                                                        static_cast<int>(bsp::vibrator::defaultVibraPauseMs));

        vibratorTimerPause = sys::TimerFactory::createSingleShotTimer(
            parent,
            "VibraPauseTimer",
            std::chrono::milliseconds{bsp::vibrator::defaultVibraPauseMs},
            [this](sys::Timer & /*timer*/) {
                if (repetitions > 0) // call itself for calculated number of repetitions
                {
                    repetitions--;
                    intPulse(true);
                }
            });
        intPulse(true);
    }

    void Vibra::PulseRepeat()
    {
        vibratorTimerPause =
            sys::TimerFactory::createSingleShotTimer(parent,
                                                     "VibraPauseTimer",
                                                     std::chrono::milliseconds{bsp::vibrator::defaultVibraPauseMs},
                                                     [&](sys::Timer & /*timer*/) { intPulse(true); });
        intPulse(true);
    }

    void Vibra::PulseRepeatStop()
    {
        repetitions = 1;
        bsp::vibrator::disable();
        vibratorTimerPause.stop();
        vibratorTimer.stop();
    }

    void Vibra::SetVibrationLevel(unsigned int vibrationLevel)
    {
        bsp::vibrator::setVibrationLevel(vibrationLevel);
    }

} // namespace vibra_handle