LCOV - code coverage report
Current view: top level - SITL - SIM_Buzzer.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 1 2 50.0 %
Date: 2026-09-09 11:02:08 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /*
       2             :    This program is free software: you can redistribute it and/or modify
       3             :    it under the terms of the GNU General Public License as published by
       4             :    the Free Software Foundation, either version 3 of the License, or
       5             :    (at your option) any later version.
       6             : 
       7             :    This program is distributed in the hope that it will be useful,
       8             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
       9             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      10             :    GNU General Public License for more details.
      11             : 
      12             :    You should have received a copy of the GNU General Public License
      13             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      14             :  */
      15             : /*
      16             :   simple buzzer simulator class
      17             : */
      18             : 
      19             : 
      20             : #include <GCS_MAVLink/GCS.h>
      21             : #include <SITL/SITL.h>
      22             : 
      23             : #include "SIM_Buzzer.h"
      24             : 
      25             : #ifdef WITH_SITL_TONEALARM
      26             : 
      27             : #ifdef HAVE_SFML_AUDIO_H
      28             : #include <SFML/Audio.h>
      29             : #else
      30             : #include <SFML/Audio.hpp>
      31             : #endif
      32             : 
      33             : // need to namespace this or we end up with multiple definitions of
      34             : // things from Synth.hpp
      35             : namespace BuzzerSynth {
      36             : #include <AP_HAL_SITL/Synth.hpp>
      37             : };
      38             : 
      39             : #endif
      40             : 
      41             : using namespace SITL;
      42             : 
      43             : #ifdef WITH_SITL_TONEALARM
      44             : 
      45             : // table of user settable parameters
      46             : const AP_Param::GroupInfo Buzzer::var_info[] = {
      47             : 
      48             :     // @Param: ENABLE
      49             :     // @DisplayName: Buzzer enable/disable
      50             :     // @Description: Allows you to enable (1) or disable (0) the simulated buzzer
      51             :     // @Values: 0:Disabled,1:Enabled
      52             :     // @User: Advanced
      53             :     AP_GROUPINFO("ENABLE", 0, Buzzer, _enable, 0),
      54             : 
      55             :     // @Param: PIN
      56             :     // @DisplayName: buzzer pin
      57             :     // @Description: The pin number that the Buzzer is connected to (start at 1)
      58             :     // @Range: 0 15
      59             :     // @User: Advanced
      60             :     AP_GROUPINFO("PIN", 1, Buzzer, _pin, -1),
      61             : 
      62             :     AP_GROUPEND
      63             : };
      64             : 
      65             : static sf::SoundBuffer xsoundBuffer;
      66             : static sf::Sound xdemoSound;
      67             : 
      68             : static uint32_t duration_ms = 10000;
      69             : 
      70             : Buzzer::Buzzer() {
      71             :     AP_Param::setup_object_defaults(this, var_info);
      72             : };
      73             : 
      74             : void Buzzer::update(const struct sitl_input &input)
      75             : {
      76             :     // prepare buzz tone
      77             : //    BuzzerSynth::Synth::sEnvelope xenvelope;
      78             :     if (!prep_done) {
      79             :         BuzzerSynth::Synth::sEnvelope xenvelope;
      80             :         xenvelope.dAttackTime = 0.000001;
      81             :         xenvelope.dDecayTime = 0.000001;
      82             :         xenvelope.dSustainTime = 10;
      83             :         xenvelope.dReleaseTime = 0.00001;
      84             :         xenvelope.dStartAmplitude = 1.0;
      85             :         xenvelope.dSustainAmplitude = 1.0;
      86             : 
      87             :         const uint32_t frequency = 2000;
      88             :         BuzzerSynth::Synth::sTone tone;
      89             :         tone.waveType = BuzzerSynth::Synth::OSC_SQUARE;
      90             :         tone.dStartFrequency = frequency;
      91             :         tone.dEndFrequency = frequency;
      92             :         tone.dAmplitude = 1;
      93             : 
      94             :         xenvelope.dSustainTime = duration_ms/1000.0f;
      95             : 
      96             :         BuzzerSynth::Synth::generate(&xsoundBuffer, xenvelope, tone, 20000, 44100);
      97             :         xdemoSound.setBuffer(xsoundBuffer);
      98             :         prep_done = true;
      99             :     }
     100             : 
     101             :     const bool on = _pin >= 1 && (AP::sitl()->pin_mask.get() & (1<<_pin));
     102             :     const uint32_t now = AP_HAL::millis();
     103             :     if (on) {
     104             :         if (!was_on) {
     105             :             GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "%u: Buzzer on", now);
     106             :             on_time = now;
     107             :             was_on = true;
     108             :             xdemoSound.play();
     109             :         }
     110             :         if (now - on_time > duration_ms/2) {
     111             :             GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "%u: Buzzer on again", now);
     112             :             on_time = now;
     113             :             xdemoSound.play();
     114             :         }
     115             :     } else {
     116             :         if (was_on) {
     117             :             GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "%u: Buzzer off", now);
     118             :             xdemoSound.stop();
     119             :             was_on = false;
     120             :         }
     121             :     }
     122             : 
     123             : }
     124             : 
     125             : #else
     126             : 
     127             : using namespace SITL;
     128             : 
     129             : const AP_Param::GroupInfo Buzzer::var_info[] = { AP_GROUPEND };
     130             : 
     131        2096 : Buzzer::Buzzer() { };
     132             : 
     133           0 : void Buzzer::update(const struct sitl_input &input) {}
     134             : 
     135             : #endif

Generated by: LCOV version 1.14