You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.9 KiB

  1. /*
  2. Copyright 2017 Priyadi Iman Nurcahyo
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/interrupt.h>
  15. #include <avr/io.h>
  16. #include <timer.h>
  17. #include <fauxclicky.h>
  18. #include <stdbool.h>
  19. #include <musical_notes.h>
  20. bool fauxclicky_enabled = true;
  21. uint16_t note_start = 0;
  22. bool note_playing = false;
  23. uint16_t note_period = 0;
  24. void fauxclicky_init()
  25. {
  26. // Set port PC6 (OC3A and /OC4A) as output
  27. DDRC |= _BV(PORTC6);
  28. // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
  29. TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
  30. TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
  31. }
  32. void fauxclicky_stop()
  33. {
  34. FAUXCLICKY_DISABLE_OUTPUT;
  35. note_playing = false;
  36. }
  37. void fauxclicky_play(float note[]) {
  38. if (!fauxclicky_enabled) return;
  39. if (note_playing) fauxclicky_stop();
  40. FAUXCLICKY_TIMER_PERIOD = (uint16_t)(((float)F_CPU) / (note[0] * (float)FAUXCLICKY_CPU_PRESCALER));
  41. FAUXCLICKY_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (note[0] * (float)FAUXCLICKY_CPU_PRESCALER)) / (float)2);
  42. note_playing = true;
  43. note_period = (note[1] / (float)16) * ((float)60 / (float)FAUXCLICKY_TEMPO) * 1000;
  44. note_start = timer_read();
  45. FAUXCLICKY_ENABLE_OUTPUT;
  46. }
  47. void fauxclicky_check() {
  48. if (!note_playing) return;
  49. if (timer_elapsed(note_start) > note_period) {
  50. fauxclicky_stop();
  51. }
  52. }