Enforce definition of `DIODE_DIRECTION` for non-custom matrix boards (#7915)
[jackhill/qmk/firmware.git] / quantum / fauxclicky.c
1 /*
2 Copyright 2017 Priyadi Iman Nurcahyo
3
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/>.
14 */
15
16 #include <avr/interrupt.h>
17 #include <avr/io.h>
18 #include <timer.h>
19 #include <fauxclicky.h>
20 #include <stdbool.h>
21 #include <musical_notes.h>
22
23 bool fauxclicky_enabled = true;
24 uint16_t note_start = 0;
25 bool note_playing = false;
26 uint16_t note_period = 0;
27
28 void fauxclicky_init() {
29 // Set port PC6 (OC3A and /OC4A) as output
30 DDRC |= _BV(PORTC6);
31
32 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
33 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
34 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
35 }
36
37 void fauxclicky_stop() {
38 FAUXCLICKY_DISABLE_OUTPUT;
39 note_playing = false;
40 }
41
42 void fauxclicky_play(float note[]) {
43 if (!fauxclicky_enabled) return;
44 if (note_playing) fauxclicky_stop();
45 FAUXCLICKY_TIMER_PERIOD = (uint16_t)(((float)F_CPU) / (note[0] * (float)FAUXCLICKY_CPU_PRESCALER));
46 FAUXCLICKY_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (note[0] * (float)FAUXCLICKY_CPU_PRESCALER)) / (float)2);
47 note_playing = true;
48 note_period = (note[1] / (float)16) * ((float)60 / (float)FAUXCLICKY_TEMPO) * 1000;
49 note_start = timer_read();
50 FAUXCLICKY_ENABLE_OUTPUT;
51 }
52
53 void fauxclicky_check() {
54 if (!note_playing) return;
55
56 if (timer_elapsed(note_start) > note_period) {
57 fauxclicky_stop();
58 }
59 }