ToneAlarm.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * ToneAlarm driver
  3. */
  4. /*
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma once
  19. #include "NotifyDevice.h"
  20. #include "MMLPlayer.h"
  21. // wait 2 seconds before assuming a tone is done and continuing the continuous tone
  22. #define AP_NOTIFY_TONEALARM_MAX_TONE_LENGTH_MS 2000
  23. #define AP_NOTIFY_TONEALARM_TONE_BUF_SIZE 100
  24. class AP_ToneAlarm: public NotifyDevice {
  25. public:
  26. /// init - initialised the tone alarm
  27. bool init(void) override;
  28. /// update - updates led according to timed_updated. Should be called at 50Hz
  29. void update() override;
  30. // handle a PLAY_TUNE message
  31. void handle_play_tune(const mavlink_message_t &msg) override;
  32. // play_tune - play tone specified by the provided string of notes
  33. void play_tune(const char *tune) override;
  34. private:
  35. /// play_tune - play one of the pre-defined tunes
  36. void play_tone(const uint8_t tone_index);
  37. // stop_cont_tone - stop playing the currently playing continuous tone
  38. void stop_cont_tone();
  39. // check_cont_tone - check if we should begin playing a continuous tone
  40. void check_cont_tone();
  41. // timer task - runs at 1khz
  42. void _timer_task();
  43. /// tonealarm_type - bitmask of states we track
  44. struct tonealarm_type {
  45. uint16_t armed : 1; // 0 = disarmed, 1 = armed
  46. uint16_t failsafe_battery : 1; // 1 if battery failsafe
  47. uint16_t parachute_release : 1; // 1 if parachute is being released
  48. uint16_t pre_arm_check : 1; // 0 = failing checks, 1 = passed
  49. uint16_t failsafe_radio : 1; // 1 if radio failsafe
  50. uint16_t vehicle_lost : 1; // 1 if lost copter tone requested
  51. uint16_t compass_cal_running : 1; // 1 if compass calibration is running
  52. uint16_t waiting_for_throw : 1; // 1 if waiting for copter throw launch
  53. uint16_t leak_detected : 1; // 1 if leak detected
  54. uint16_t powering_off : 1; // 1 if smart battery is powering off
  55. } flags;
  56. bool _have_played_ready_tone : 1;
  57. int8_t _cont_tone_playing;
  58. int8_t _tone_playing;
  59. uint32_t _tone_beginning_ms;
  60. struct Tone {
  61. const char *str;
  62. const uint8_t continuous : 1;
  63. };
  64. const static Tone _tones[];
  65. HAL_Semaphore _sem;
  66. MMLPlayer _mml_player;
  67. char _tone_buf[AP_NOTIFY_TONEALARM_TONE_BUF_SIZE];
  68. };