Buzzer.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Buzzer 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. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <AP_HAL/AP_HAL.h>
  18. #include "NotifyDevice.h"
  19. class Buzzer: public NotifyDevice
  20. {
  21. public:
  22. /// Constructor
  23. Buzzer() {}
  24. /// init - initialise the buzzer
  25. bool init(void) override;
  26. /// update - updates buzzer according to timed_updated. Should be called at 50Hz
  27. void update() override;
  28. private:
  29. /// on - turns the buzzer on or off
  30. void on(bool on_off);
  31. // Patterns - how many beeps will be played; read from
  32. // left-to-right, each bit represents 100ms
  33. static const uint32_t SINGLE_BUZZ = 0b10000000000000000000000000000000UL;
  34. static const uint32_t DOUBLE_BUZZ = 0b10100000000000000000000000000000UL;
  35. static const uint32_t ARMING_BUZZ = 0b11111111111111111111111111111100UL; // 3s
  36. static const uint32_t BARO_BUZZ = 0b10101010100000000000000000000000UL;
  37. static const uint32_t EKF_BAD = 0b11101101010000000000000000000000UL;
  38. /// play_pattern - plays the defined buzzer pattern
  39. void play_pattern(const uint32_t pattern);
  40. /// buzzer_flag_type - bitmask of current state and ap_notify states we track
  41. struct buzzer_flag_type {
  42. uint8_t on : 1; // 1 if the buzzer is currently on
  43. uint8_t arming : 1; // 1 if we are beginning the arming process
  44. uint8_t armed : 1; // 0 = disarmed, 1 = armed
  45. uint8_t failsafe_battery : 1; // 1 if battery failsafe has triggered
  46. uint8_t ekf_bad : 1; // 1 if ekf position has gone bad
  47. } _flags;
  48. uint32_t _pattern; // current pattern
  49. uint8_t _pin;
  50. uint32_t _pattern_start_time;
  51. // enforce minumum 100ms interval between patterns:
  52. const uint16_t _pattern_start_interval_time_ms = 32*100 + 100;
  53. void update_playing_pattern();
  54. void update_pattern_to_play();
  55. };