~aleteoryx/muditaos

ref: 16c0d166477631784e7e43ac257613aaff943501 muditaos/module-services/service-evtmgr/vibra/Vibra.cpp -rw-r--r-- 2.1 KiB
16c0d166 — Lukasz Skrzypczak [EGD-3685] Fix timers starting without callback 4 years 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Vibra.hpp"
#include "SystemManager/Constants.hpp"
#include <Service/Timer.hpp>

#include <common_data/EventStore.hpp>

namespace vibra_handle
{
    Vibra::Vibra(sys::Service *parent)
    {
        vibratorTimerOneshot =
            std::make_unique<sys::Timer>("VibraOneshotTimer", parent, bsp::vibrator::defaultVibraPulseMs);
        vibratorTimerPause =
            std::make_unique<sys::Timer>("VibraPauseTimer", parent, bsp::vibrator::defaultVibraPauseMs);

        vibratorTimerOneshot->connect(nullTimerCallback);
        vibratorTimerPause->connect(nullTimerCallback);

        vibratorTimerOneshot->stop();
        vibratorTimerPause->stop();
    }

    void Vibra::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 Vibra::Pulse()
    {
        intPulse(false);
    }

    void Vibra::PulseRepeat(sys::ms time)
    {
        repetitions = static_cast<int>(time) / (static_cast<int>(bsp::vibrator::defaultVibraPulseMs) +
                                                static_cast<int>(bsp::vibrator::defaultVibraPauseMs));

        vibratorTimerPause->connect([&](sys::Timer &) {
            if (repetitions) /// call itself for calculated number of repetitions
            {
                repetitions--;
                intPulse(true);
            }
        });
        intPulse(true);
    }

    void Vibra::PulseRepeat()
    {
        vibratorTimerPause->connect([&](sys::Timer &) { intPulse(true); });
        intPulse(true);
    }

    void Vibra::PulseRepeatStop()
    {
        repetitions = 1;
        bsp::vibrator::disable();
        vibratorTimerPause->stop();
        vibratorTimerOneshot->stop();
    }

} // namespace vibra_handle