Add customisable EEPROM driver selection (#7274)
[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
b624f32f 23bool fauxclicky_enabled = true;
24uint16_t note_start = 0;
25bool note_playing = false;
26uint16_t note_period = 0;
c68e596f 27
b624f32f 28void fauxclicky_init() {
c68e596f
PIN
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);
b624f32f 34 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
c68e596f
PIN
35}
36
b624f32f 37void fauxclicky_stop() {
c68e596f
PIN
38 FAUXCLICKY_DISABLE_OUTPUT;
39 note_playing = false;
40}
41
ffa4c72a 42void fauxclicky_play(float note[]) {
c68e596f
PIN
43 if (!fauxclicky_enabled) return;
44 if (note_playing) fauxclicky_stop();
ffa4c72a 45 FAUXCLICKY_TIMER_PERIOD = (uint16_t)(((float)F_CPU) / (note[0] * (float)FAUXCLICKY_CPU_PRESCALER));
b624f32f 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();
c68e596f
PIN
50 FAUXCLICKY_ENABLE_OUTPUT;
51}
52
53void fauxclicky_check() {
54 if (!note_playing) return;
55
56 if (timer_elapsed(note_start) > note_period) {
57 fauxclicky_stop();
58 }
59}