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

mavlink / MAVSDK / 11767930807

10 Nov 2024 07:33PM UTC coverage: 38.608% (+0.7%) from 37.921%
11767930807

push

github

web-flow
Merge pull request #2394 from mavlink/pr-consolidate-ci

Consolidate CI

12030 of 31159 relevant lines covered (38.61%)

243.33 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
#include "mavlink_address.h"
4

5
namespace mavsdk {
6

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

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

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

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

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

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

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

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

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

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

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

52
    std::string tune_str("MFT" + std::to_string(tempo) + "O2");
×
53

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

58
    int last_duration = 1;
×
59

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

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

141
    if (!_system_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
×
142
            mavlink_message_t message;
143
            mavlink_msg_play_tune_v2_pack_chan(
×
144
                mavlink_address.system_id,
×
145
                mavlink_address.component_id,
×
146
                channel,
147
                &message,
148
                _system_impl->get_system_id(),
×
149
                _system_impl->get_autopilot_id(),
×
150
                TUNE_FORMAT_QBASIC1_1,
151
                tune_str.c_str());
×
152
            return message;
×
153
        })) {
154
        report_tune_result(callback, Tune::Result::Error);
×
155
        return;
×
156
    }
157

158
    report_tune_result(callback, Tune::Result::Success);
×
159
}
×
160

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

168
    _system_impl->call_user_callback([callback, result]() { callback(result); });
×
169
}
170

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