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