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.

59 lines
2.0 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. // Set port PC6 (OC3A and /OC4A) as output
  26. DDRC |= _BV(PORTC6);
  27. // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
  28. TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
  29. TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
  30. }
  31. void fauxclicky_stop() {
  32. FAUXCLICKY_DISABLE_OUTPUT;
  33. note_playing = false;
  34. }
  35. void fauxclicky_play(float note[]) {
  36. if (!fauxclicky_enabled) return;
  37. if (note_playing) fauxclicky_stop();
  38. FAUXCLICKY_TIMER_PERIOD = (uint16_t)(((float)F_CPU) / (note[0] * (float)FAUXCLICKY_CPU_PRESCALER));
  39. FAUXCLICKY_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (note[0] * (float)FAUXCLICKY_CPU_PRESCALER)) / (float)2);
  40. note_playing = true;
  41. note_period = (note[1] / (float)16) * ((float)60 / (float)FAUXCLICKY_TEMPO) * 1000;
  42. note_start = timer_read();
  43. FAUXCLICKY_ENABLE_OUTPUT;
  44. }
  45. void fauxclicky_check() {
  46. if (!note_playing) return;
  47. if (timer_elapsed(note_start) > note_period) {
  48. fauxclicky_stop();
  49. }
  50. }