• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mavlink / MAVSDK / 4578731844

pending completion
4578731844

push

github

GitHub
Merge pull request #2012 from mavlink/rename-parent

885 of 885 new or added lines in 31 files covered. (100.0%)

7416 of 24250 relevant lines covered (30.58%)

21.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/mavsdk/plugins/tune/tune_impl.cpp
1
#include "tune_impl.h"
2
#include "log.h"
3

4
namespace mavsdk {
5

6
TuneImpl::TuneImpl(System& system) : PluginImplBase(system), _mavlink_tune_item_messages()
×
7
{
8
    _system_impl->register_plugin(this);
×
9
}
×
10

11
TuneImpl::TuneImpl(std::shared_ptr<System> system) :
×
12
    PluginImplBase(std::move(system)),
×
13
    _mavlink_tune_item_messages()
×
14
{
15
    _system_impl->register_plugin(this);
×
16
}
×
17

18
TuneImpl::~TuneImpl()
×
19
{
20
    _system_impl->unregister_plugin(this);
×
21
}
×
22

23
void TuneImpl::init() {}
×
24

25
void TuneImpl::deinit() {}
×
26

27
void TuneImpl::enable() {}
×
28

29
void TuneImpl::disable() {}
×
30

31
Tune::Result TuneImpl::play_tune(const Tune::TuneDescription& tune)
×
32
{
33
    std::promise<Tune::Result> prom;
×
34
    auto fut = prom.get_future();
×
35

36
    play_tune_async(tune, [&prom](const Tune::Result result) { prom.set_value(result); });
×
37
    return fut.get();
×
38
}
39

40
void TuneImpl::play_tune_async(
×
41
    const Tune::TuneDescription& tune, const Tune::ResultCallback& callback)
42
{
43
    const auto song_elements = tune.song_elements;
×
44
    const int tempo = tune.tempo;
×
45

46
    if (tempo < 32 || tempo > 255) {
×
47
        report_tune_result(callback, Tune::Result::InvalidTempo);
×
48
        return;
×
49
    }
50

51
    std::string tune_str("MFT" + std::to_string(tempo) + "O2");
×
52
    int last_duration = 1;
×
53

54
    for (auto song_elem : song_elements) {
×
55
        switch (song_elem) {
×
56
            case Tune::SongElement::StyleLegato:
×
57
                tune_str.append("ML");
×
58
                break;
×
59
            case Tune::SongElement::StyleNormal:
×
60
                tune_str.append("MN");
×
61
                break;
×
62
            case Tune::SongElement::StyleStaccato:
×
63
                tune_str.append("MS");
×
64
                break;
×
65
            case Tune::SongElement::Duration1:
×
66
                tune_str.append("L1");
×
67
                last_duration = 1;
×
68
                break;
×
69
            case Tune::SongElement::Duration2:
×
70
                tune_str.append("L2");
×
71
                last_duration = 2;
×
72
                break;
×
73
            case Tune::SongElement::Duration4:
×
74
                tune_str.append("L4");
×
75
                last_duration = 4;
×
76
                break;
×
77
            case Tune::SongElement::Duration8:
×
78
                tune_str.append("L8");
×
79
                last_duration = 8;
×
80
                break;
×
81
            case Tune::SongElement::Duration16:
×
82
                tune_str.append("L16");
×
83
                last_duration = 16;
×
84
                break;
×
85
            case Tune::SongElement::Duration32:
×
86
                tune_str.append("L32");
×
87
                last_duration = 32;
×
88
                break;
×
89
            case Tune::SongElement::NoteA:
×
90
                tune_str.append("A");
×
91
                break;
×
92
            case Tune::SongElement::NoteB:
×
93
                tune_str.append("B");
×
94
                break;
×
95
            case Tune::SongElement::NoteC:
×
96
                tune_str.append("C");
×
97
                break;
×
98
            case Tune::SongElement::NoteD:
×
99
                tune_str.append("D");
×
100
                break;
×
101
            case Tune::SongElement::NoteE:
×
102
                tune_str.append("E");
×
103
                break;
×
104
            case Tune::SongElement::NoteF:
×
105
                tune_str.append("F");
×
106
                break;
×
107
            case Tune::SongElement::NoteG:
×
108
                tune_str.append("G");
×
109
                break;
×
110
            case Tune::SongElement::NotePause:
×
111
                tune_str.append("P" + std::to_string(last_duration));
×
112
                break;
×
113
            case Tune::SongElement::Sharp:
×
114
                tune_str.append("+");
×
115
                break;
×
116
            case Tune::SongElement::Flat:
×
117
                tune_str.append("-");
×
118
                break;
×
119
            case Tune::SongElement::OctaveUp:
×
120
                tune_str.append(">");
×
121
                break;
×
122
            case Tune::SongElement::OctaveDown:
×
123
                tune_str.append("<");
×
124
                break;
×
125
            default:
×
126
                break;
×
127
        }
128
    }
129

130
    LogDebug() << "About to send tune: " << tune_str;
×
131

132
    if (tune_str.size() > MAVLINK_MSG_PLAY_TUNE_V2_FIELD_TUNE_LEN - 1) {
×
133
        report_tune_result(callback, Tune::Result::TuneTooLong);
×
134
        return;
×
135
    }
136

137
    mavlink_message_t message;
×
138
    mavlink_msg_play_tune_v2_pack(
×
139
        _system_impl->get_own_system_id(),
×
140
        _system_impl->get_own_component_id(),
×
141
        &message,
142
        _system_impl->get_system_id(),
×
143
        _system_impl->get_autopilot_id(),
×
144
        TUNE_FORMAT_QBASIC1_1,
145
        tune_str.c_str());
146

147
    if (!_system_impl->send_message(message)) {
×
148
        report_tune_result(callback, Tune::Result::Error);
×
149
        return;
×
150
    }
151

152
    report_tune_result(callback, Tune::Result::Success);
×
153
}
154

155
void TuneImpl::report_tune_result(const Tune::ResultCallback& callback, Tune::Result result)
×
156
{
157
    if (callback == nullptr) {
×
158
        LogWarn() << "Callback is not set";
×
159
        return;
×
160
    }
161

162
    _system_impl->call_user_callback([callback, result]() { callback(result); });
×
163
}
164

165
} // namespace mavsdk
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc