sound.cpp 4.8 KB

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