Updates bootloader settings, adds file size check (#2029)
[jackhill/qmk/firmware.git] / quantum / fauxclicky.c
CommitLineData
c68e596f
PIN
1/*
2Copyright 2017 Priyadi Iman Nurcahyo
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12You should have received a copy of the GNU General Public License
13along 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
23bool fauxclicky_enabled = true;
24uint16_t note_start = 0;
25bool note_playing = false;
26uint16_t note_period = 0;
27
28void 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
38void fauxclicky_stop()
39{
40 FAUXCLICKY_DISABLE_OUTPUT;
41 note_playing = false;
42}
43
ffa4c72a 44void 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
55void fauxclicky_check() {
56 if (!note_playing) return;
57
58 if (timer_elapsed(note_start) > note_period) {
59 fauxclicky_stop();
60 }
61}