buzzer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * buzzer.c
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #include "buzzer.h"
  8. #include "iron.h"
  9. #include "settings.h"
  10. #include "main.h"
  11. typedef enum {STATE_SB, STATE_LB, STATE_FB, STATE_AL_H,STATE_AL_L, STATE_IDLE} buzzer_state_type;
  12. static buzzer_state_type buzzer_state = STATE_IDLE;
  13. static uint32_t last_time;
  14. static void tempReachedCall(uint16_t temp) {
  15. buzzer_short_beep();
  16. }
  17. static setTemperatureReachedCallback ironTempReachedCallback = &tempReachedCall;
  18. void buzzer_init() {
  19. addSetTemperatureReachedCallback(ironTempReachedCallback);
  20. }
  21. void buzzer_short_beep() {
  22. buzzer_state = STATE_SB;
  23. if(systemSettings.settings.buzzerMode){
  24. BUZZER_ON;
  25. }
  26. last_time = HAL_GetTick();
  27. }
  28. void buzzer_long_beep() {
  29. buzzer_state = STATE_LB;
  30. if(systemSettings.settings.buzzerMode){
  31. BUZZER_ON;
  32. }
  33. last_time = HAL_GetTick();
  34. }
  35. void buzzer_fatal_beep() {
  36. buzzer_state = STATE_FB;
  37. if(systemSettings.settings.buzzerMode){
  38. BUZZER_ON;
  39. }
  40. last_time = HAL_GetTick();
  41. }
  42. void buzzer_alarm_start(){
  43. if(!systemSettings.settings.buzzerMode){ return; }
  44. buzzer_state = STATE_AL_H;
  45. if(systemSettings.settings.buzzerMode){
  46. BUZZER_ON;
  47. }
  48. last_time = HAL_GetTick();
  49. }
  50. void buzzer_alarm_stop() {
  51. buzzer_state = STATE_IDLE;
  52. BUZZER_OFF;
  53. }
  54. void handle_buzzer() {
  55. uint32_t delta = HAL_GetTick() - last_time;
  56. switch (buzzer_state) {
  57. case STATE_SB:
  58. if (delta > SHORT_BEEP) {
  59. BUZZER_OFF;
  60. buzzer_state = STATE_IDLE;
  61. }
  62. break;
  63. case STATE_LB:
  64. if (delta > LONG_BEEP) {
  65. BUZZER_OFF;
  66. buzzer_state = STATE_IDLE;
  67. }
  68. break;
  69. case STATE_FB:
  70. if (delta > FATAL_BEEP) {
  71. BUZZER_OFF;
  72. buzzer_state = STATE_IDLE;
  73. }
  74. break;
  75. case STATE_AL_H:
  76. if(delta > ALARM_HIGH){
  77. buzzer_state=STATE_AL_L;
  78. BUZZER_OFF;
  79. last_time = HAL_GetTick();
  80. }
  81. break;
  82. case STATE_AL_L:
  83. if(delta > ALARM_LOW){
  84. buzzer_state=STATE_AL_H;
  85. if(systemSettings.settings.buzzerMode){
  86. BUZZER_ON;
  87. }
  88. last_time = HAL_GetTick();
  89. }
  90. break;
  91. case STATE_IDLE:
  92. break;
  93. default:
  94. BUZZER_OFF;
  95. buzzer_state = STATE_IDLE;
  96. break;
  97. }
  98. }