sound.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. //if critical is true then silend and once mode is ignored
  54. void Sound_MakeCustom(uint16_t ms,uint16_t tone_,bool critical){
  55. if (!critical){
  56. if (eSoundMode != e_SOUND_MODE_SILENT){
  57. if(!tone_){
  58. WRITE(BEEPER, HIGH);
  59. _delay(ms);
  60. WRITE(BEEPER, LOW);
  61. }
  62. else{
  63. _tone(BEEPER, tone_);
  64. _delay(ms);
  65. _noTone(BEEPER);
  66. }
  67. }
  68. }
  69. else{
  70. if(!tone_){
  71. WRITE(BEEPER, HIGH);
  72. _delay(ms);
  73. WRITE(BEEPER, LOW);
  74. _delay(ms);
  75. }
  76. else{
  77. _tone(BEEPER, tone_);
  78. _delay(ms);
  79. _noTone(BEEPER);
  80. }
  81. }
  82. }
  83. void Sound_MakeSound(eSOUND_TYPE eSoundType)
  84. {
  85. switch(eSoundMode)
  86. {
  87. case e_SOUND_MODE_LOUD:
  88. if(eSoundType==e_SOUND_TYPE_ButtonEcho)
  89. Sound_DoSound_Echo();
  90. if(eSoundType==e_SOUND_TYPE_StandardPrompt)
  91. Sound_DoSound_Prompt();
  92. if(eSoundType==e_SOUND_TYPE_StandardAlert)
  93. Sound_DoSound_Alert(false);
  94. break;
  95. case e_SOUND_MODE_ONCE:
  96. if(eSoundType==e_SOUND_TYPE_ButtonEcho)
  97. Sound_DoSound_Echo();
  98. if(eSoundType==e_SOUND_TYPE_StandardPrompt)
  99. Sound_DoSound_Prompt();
  100. if(eSoundType==e_SOUND_TYPE_StandardAlert)
  101. Sound_DoSound_Alert(true);
  102. break;
  103. case e_SOUND_MODE_SILENT:
  104. if(eSoundType==e_SOUND_TYPE_StandardAlert)
  105. Sound_DoSound_Alert(true);
  106. break;
  107. case e_SOUND_MODE_BLIND:
  108. if(eSoundType==e_SOUND_TYPE_ButtonEcho)
  109. Sound_DoSound_Echo();
  110. if(eSoundType==e_SOUND_TYPE_StandardPrompt)
  111. Sound_DoSound_Prompt();
  112. if(eSoundType==e_SOUND_TYPE_StandardAlert)
  113. Sound_DoSound_Alert(false);
  114. if(eSoundType==e_SOUND_TYPE_EncoderMove)
  115. Sound_DoSound_Encoder_Move();
  116. if(eSoundType==e_SOUND_TYPE_BlindAlert)
  117. Sound_DoSound_Blind_Alert();
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. static void Sound_DoSound_Blind_Alert(void)
  124. {
  125. uint8_t nI;
  126. for(nI=0; nI<20; nI++)
  127. {
  128. WRITE(BEEPER,HIGH);
  129. delayMicroseconds(94);
  130. WRITE(BEEPER,LOW);
  131. delayMicroseconds(94);
  132. }
  133. }
  134. static void Sound_DoSound_Encoder_Move(void)
  135. {
  136. uint8_t nI;
  137. for(nI=0;nI<5;nI++)
  138. {
  139. WRITE(BEEPER,HIGH);
  140. delayMicroseconds(75);
  141. WRITE(BEEPER,LOW);
  142. delayMicroseconds(75);
  143. }
  144. }
  145. static void Sound_DoSound_Echo(void)
  146. {
  147. uint8_t nI;
  148. for(nI=0;nI<10;nI++)
  149. {
  150. WRITE(BEEPER,HIGH);
  151. delayMicroseconds(100);
  152. WRITE(BEEPER,LOW);
  153. delayMicroseconds(100);
  154. }
  155. }
  156. static void Sound_DoSound_Prompt(void)
  157. {
  158. WRITE(BEEPER,HIGH);
  159. _delay_ms(500);
  160. WRITE(BEEPER,LOW);
  161. }
  162. static void Sound_DoSound_Alert(bool bOnce)
  163. {
  164. uint8_t nI,nMax;
  165. nMax=bOnce?1:3;
  166. for(nI=0;nI<nMax;nI++)
  167. {
  168. WRITE(BEEPER,HIGH);
  169. delayMicroseconds(200);
  170. WRITE(BEEPER,LOW);
  171. delayMicroseconds(500);
  172. }
  173. }