aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
authorPriyadi Iman Nurcahyo <priyadi@priyadi.net>2017-02-13 08:03:07 +0700
committerPriyadi Iman Nurcahyo <priyadi@priyadi.net>2017-02-13 08:03:07 +0700
commitc68e596f32c5d450a714627871408407e9988ef7 (patch)
tree1ddd19fc28409370e191d2dbde844b2fa68a5125 /quantum
parenta0c2305bd1153d9d578d73effd33896c2dbc26c8 (diff)
downloadqmk_firmware-c68e596f32c5d450a714627871408407e9988ef7.tar.gz
qmk_firmware-c68e596f32c5d450a714627871408407e9988ef7.zip
Implement faux-clicky feature
Diffstat (limited to 'quantum')
-rw-r--r--quantum/fauxclicky.c68
-rw-r--r--quantum/fauxclicky.h87
-rw-r--r--quantum/template/rules.mk1
3 files changed, 156 insertions, 0 deletions
diff --git a/quantum/fauxclicky.c b/quantum/fauxclicky.c
new file mode 100644
index 000000000..13273e705
--- /dev/null
+++ b/quantum/fauxclicky.c
@@ -0,0 +1,68 @@
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
23__attribute__ ((weak))
24float fauxclicky_pressed_note[2] = MUSICAL_NOTE(_F3, 2);
25__attribute__ ((weak))
26float fauxclicky_released_note[2] = MUSICAL_NOTE(_A3, 2);
27__attribute__ ((weak))
28float fauxclicky_beep_note[2] = MUSICAL_NOTE(_C3, 2);
29
30bool fauxclicky_enabled = true;
31uint16_t note_start = 0;
32bool note_playing = false;
33uint16_t note_period = 0;
34
35void fauxclicky_init()
36{
37 // Set port PC6 (OC3A and /OC4A) as output
38 DDRC |= _BV(PORTC6);
39
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);
43}
44
45void fauxclicky_stop()
46{
47 FAUXCLICKY_DISABLE_OUTPUT;
48 note_playing = false;
49}
50
51void 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);
56 note_playing = true;
57 note_period = (note[1] / 16) * (60 / (float)FAUXCLICKY_TEMPO) * 100; // check this
58 note_start = timer_read();
59 FAUXCLICKY_ENABLE_OUTPUT;
60}
61
62void fauxclicky_check() {
63 if (!note_playing) return;
64
65 if (timer_elapsed(note_start) > note_period) {
66 fauxclicky_stop();
67 }
68}
diff --git a/quantum/fauxclicky.h b/quantum/fauxclicky.h
new file mode 100644
index 000000000..6cfc291c0
--- /dev/null
+++ b/quantum/fauxclicky.h
@@ -0,0 +1,87 @@
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#ifdef AUDIO_ENABLE
17#error "AUDIO_ENABLE and FAUXCLICKY_ENABLE cannot be both enabled"
18#endif
19
20#include "musical_notes.h"
21
22__attribute__ ((weak))
23float fauxclicky_pressed_note[2];
24__attribute__ ((weak))
25float fauxclicky_released_note[2];
26__attribute__ ((weak))
27float fauxclicky_beep_note[2];
28
29//
30// tempo in BPM
31//
32
33#ifndef FAUXCLICKY_TEMPO
34#define FAUXCLICKY_TEMPO TEMPO_DEFAULT
35#endif
36
37// beep on press
38#define FAUXCLICKY_ACTION_PRESS fauxclicky_play(fauxclicky_pressed_note)
39
40// beep on release
41#define FAUXCLICKY_ACTION_RELEASE fauxclicky_play(fauxclicky_released_note)
42
43// general purpose beep
44#define FAUXCLICKY_BEEP fauxclicky_play(fauxclicky_beep_note)
45
46// enable
47#define FAUXCLICKY_ON fauxclicky_enabled = true
48
49// disable
50#define FAUXCLICKY_OFF do { \
51 fauxclicky_enabled = false; \
52 fauxclicky_stop(); \
53} while (0)
54
55//
56// pin configuration
57//
58
59#ifndef FAUXCLICKY_CPU_PRESCALER
60#define FAUXCLICKY_CPU_PRESCALER 8
61#endif
62
63#ifndef FAUXCLICKY_ENABLE_OUTPUT
64#define FAUXCLICKY_ENABLE_OUTPUT TCCR3A |= _BV(COM3A1);
65#endif
66
67#ifndef FAUXCLICKY_DISABLE_OUTPUT
68#define FAUXCLICKY_DISABLE_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
69#endif
70
71#ifndef FAUXCLICKY_TIMER_PERIOD
72#define FAUXCLICKY_TIMER_PERIOD ICR3
73#endif
74
75#ifndef FAUXCLICKY_DUTY_CYCLE
76#define FAUXCLICKY_DUTY_CYCLE OCR3A
77#endif
78
79//
80// definitions
81//
82
83void fauxclicky_init(void);
84void fauxclicky_stop(void);
85void fauxclicky_play(float note[2]);
86void fauxclicky_check(void);
87
diff --git a/quantum/template/rules.mk b/quantum/template/rules.mk
index 55898147d..bad3387bf 100644
--- a/quantum/template/rules.mk
+++ b/quantum/template/rules.mk
@@ -65,3 +65,4 @@ MIDI_ENABLE ?= no # MIDI controls
65UNICODE_ENABLE ?= no # Unicode 65UNICODE_ENABLE ?= no # Unicode
66BLUETOOTH_ENABLE ?= no # Enable Bluetooth with the Adafruit EZ-Key HID 66BLUETOOTH_ENABLE ?= no # Enable Bluetooth with the Adafruit EZ-Key HID
67AUDIO_ENABLE ?= no # Audio output on port C6 67AUDIO_ENABLE ?= no # Audio output on port C6
68FAUXCLICKY_ENABLE ?= no # Use buzzer to emulate clicky switches