sound.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "sound.h"
  2. #include "Marlin.h"
  3. //#include <inttypes.h>
  4. //#include <avr/eeprom.h>
  5. //#include "eeprom.h"
  6. //eSOUND_MODE eSoundMode=e_SOUND_MODE_LOUD;
  7. // nema vyznam, pokud se bude volat Sound_Init (tzn. poc. hodnota je v EEPROM)
  8. // !?! eSOUND_MODE eSoundMode; v ultraldc.cpp :: cd_settings_menu() se takto jevi jako lokalni promenna
  9. eSOUND_MODE eSoundMode; //=e_SOUND_MODE_DEFAULT;
  10. static void Sound_SaveMode(void);
  11. static void Sound_DoSound_Echo(void);
  12. static void Sound_DoSound_Prompt(void);
  13. static void Sound_DoSound_Alert(bool bOnce);
  14. static void Sound_DoSound_Encoder_Move(void);
  15. static void Sound_DoSound_Blind_Alert(void);
  16. void Sound_Init(void)
  17. {
  18. SET_OUTPUT(BEEPER);
  19. eSoundMode=(eSOUND_MODE)eeprom_read_byte((uint8_t*)EEPROM_SOUND_MODE);
  20. if(eSoundMode==e_SOUND_MODE_NULL)
  21. Sound_Default(); // je potreba provest i ulozeni do EEPROM
  22. }
  23. void Sound_Default(void)
  24. {
  25. eSoundMode=e_SOUND_MODE_DEFAULT;
  26. Sound_SaveMode();
  27. }
  28. void Sound_SaveMode(void)
  29. {
  30. eeprom_update_byte((uint8_t*)EEPROM_SOUND_MODE,(uint8_t)eSoundMode);
  31. }
  32. void Sound_CycleState(void)
  33. {
  34. switch(eSoundMode)
  35. {
  36. case e_SOUND_MODE_LOUD:
  37. eSoundMode=e_SOUND_MODE_ONCE;
  38. break;
  39. case e_SOUND_MODE_ONCE:
  40. eSoundMode=e_SOUND_MODE_SILENT;
  41. break;
  42. case e_SOUND_MODE_SILENT:
  43. eSoundMode=e_SOUND_MODE_BLIND;
  44. break;
  45. case e_SOUND_MODE_BLIND:
  46. eSoundMode=e_SOUND_MODE_LOUD;
  47. break;
  48. default:
  49. eSoundMode=e_SOUND_MODE_LOUD;
  50. }
  51. Sound_SaveMode();
  52. }
  53. void Sound_MakeCustom(uint16_t ms,uint16_t tone_,bool critical){
  54. if (!critical){
  55. if (eSoundMode != e_SOUND_MODE_SILENT){
  56. if(!tone_){
  57. WRITE(BEEPER, HIGH);
  58. delayMicroseconds(ms);
  59. WRITE(BEEPER, LOW);
  60. }
  61. else{
  62. _tone(BEEPER, tone_);
  63. delayMicroseconds(ms);
  64. _noTone(BEEPER);
  65. }
  66. }
  67. }
  68. else{
  69. if(!tone_){
  70. WRITE(BEEPER, HIGH);
  71. delayMicroseconds(ms);
  72. WRITE(BEEPER, LOW);
  73. delayMicroseconds(100);
  74. }
  75. else{
  76. _tone(BEEPER, tone_);
  77. delayMicroseconds(ms);
  78. _noTone(BEEPER);
  79. }
  80. }
  81. }
  82. void Sound_MakeSound(eSOUND_TYPE eSoundType)
  83. {
  84. switch(eSoundMode)
  85. {
  86. case e_SOUND_MODE_LOUD:
  87. if(eSoundType==e_SOUND_TYPE_ButtonEcho)
  88. Sound_DoSound_Echo();
  89. if(eSoundType==e_SOUND_TYPE_StandardPrompt)
  90. Sound_DoSound_Prompt();
  91. if(eSoundType==e_SOUND_TYPE_StandardAlert)
  92. Sound_DoSound_Alert(false);
  93. break;
  94. case e_SOUND_MODE_ONCE:
  95. if(eSoundType==e_SOUND_TYPE_ButtonEcho)
  96. Sound_DoSound_Echo();
  97. if(eSoundType==e_SOUND_TYPE_StandardPrompt)
  98. Sound_DoSound_Prompt();
  99. if(eSoundType==e_SOUND_TYPE_StandardAlert)
  100. Sound_DoSound_Alert(true);
  101. break;
  102. case e_SOUND_MODE_SILENT:
  103. if(eSoundType==e_SOUND_TYPE_StandardAlert)
  104. Sound_DoSound_Alert(true);
  105. break;
  106. case e_SOUND_MODE_BLIND:
  107. if(eSoundType==e_SOUND_TYPE_ButtonEcho)
  108. Sound_DoSound_Echo();
  109. if(eSoundType==e_SOUND_TYPE_StandardPrompt)
  110. Sound_DoSound_Prompt();
  111. if(eSoundType==e_SOUND_TYPE_StandardAlert)
  112. Sound_DoSound_Alert(false);
  113. if(eSoundType==e_SOUND_TYPE_EncoderMove)
  114. Sound_DoSound_Encoder_Move();
  115. if(eSoundType==e_SOUND_TYPE_BlindAlert)
  116. Sound_DoSound_Blind_Alert();
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. static void Sound_DoSound_Blind_Alert(void)
  123. {
  124. _tone(BEEPER,100);
  125. delayMicroseconds(50);
  126. _noTone(BEEPER);
  127. delayMicroseconds(200);
  128. }
  129. static void Sound_DoSound_Encoder_Move(void)
  130. {
  131. uint8_t nI;
  132. for(nI=0;nI<5;nI++)
  133. {
  134. WRITE(BEEPER,HIGH);
  135. delayMicroseconds(75);
  136. WRITE(BEEPER,LOW);
  137. delayMicroseconds(75);
  138. }
  139. }
  140. static void Sound_DoSound_Echo(void)
  141. {
  142. uint8_t nI;
  143. for(nI=0;nI<10;nI++)
  144. {
  145. WRITE(BEEPER,HIGH);
  146. delayMicroseconds(100);
  147. WRITE(BEEPER,LOW);
  148. delayMicroseconds(100);
  149. }
  150. }
  151. static void Sound_DoSound_Prompt(void)
  152. {
  153. WRITE(BEEPER,HIGH);
  154. delayMicroseconds(500);
  155. WRITE(BEEPER,LOW);
  156. }
  157. static void Sound_DoSound_Alert(bool bOnce)
  158. {
  159. uint8_t nI,nMax;
  160. nMax=bOnce?1:3;
  161. for(nI=0;nI<nMax;nI++)
  162. {
  163. WRITE(BEEPER,HIGH);
  164. delayMicroseconds(200);
  165. WRITE(BEEPER,LOW);
  166. delayMicroseconds(500);
  167. }
  168. }