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

mavlink / MAVSDK / 5010303578

pending completion
5010303578

push

github

GitHub
Merge pull request #2036 from mavlink/pr-fix-tune

1 of 1 new or added line in 1 file covered. (100.0%)

7421 of 24301 relevant lines covered (30.54%)

21.95 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

53
    // We need to reserve enough because inside the mavlink pack
54
    // function it does a memcpy of the full length.
55
    tune_str.reserve(MAVLINK_MSG_PLAY_TUNE_V2_FIELD_TUNE_LEN);
×
56

57
    int last_duration = 1;
×
58

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

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

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

150
    if (!_system_impl->send_message(message)) {
×
151
        report_tune_result(callback, Tune::Result::Error);
×
152
        return;
×
153
    }
154

155
    report_tune_result(callback, Tune::Result::Success);
×
156
}
157

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

165
    _system_impl->call_user_callback([callback, result]() { callback(result); });
×
166
}
167

168
} // 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