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