summaryrefslogtreecommitdiffstats
path: root/quantum
diff options
context:
space:
mode:
Diffstat (limited to 'quantum')
-rw-r--r--quantum/action.c5
-rw-r--r--quantum/api.c182
-rw-r--r--quantum/api.h55
-rw-r--r--quantum/api/api_sysex.c72
-rw-r--r--quantum/api/api_sysex.h25
-rw-r--r--quantum/audio/audio.h13
-rw-r--r--quantum/audio/driver_avr_pwm.h17
-rw-r--r--quantum/audio/driver_avr_pwm_hardware.c332
-rw-r--r--quantum/audio/driver_chibios_dac.h126
-rw-r--r--quantum/audio/driver_chibios_dac_additive.c335
-rw-r--r--quantum/audio/driver_chibios_dac_basic.c245
-rw-r--r--quantum/audio/driver_chibios_pwm.h40
-rw-r--r--quantum/audio/driver_chibios_pwm_hardware.c144
-rw-r--r--quantum/audio/driver_chibios_pwm_software.c164
-rw-r--r--quantum/audio/song_list.h8
-rw-r--r--quantum/backlight/backlight_chibios.c10
-rw-r--r--quantum/eeconfig.c11
-rw-r--r--quantum/keyboard.c13
-rw-r--r--quantum/matrix.c28
-rw-r--r--quantum/mcu_selection.mk34
-rw-r--r--quantum/process_keycode/process_haptic.c1
-rw-r--r--quantum/process_keycode/process_programmable_button.c31
-rw-r--r--quantum/process_keycode/process_programmable_button.h23
-rw-r--r--quantum/process_keycode/process_unicode_common.c47
-rw-r--r--quantum/programmable_button.c37
-rw-r--r--quantum/programmable_button.h30
-rw-r--r--quantum/quantum.c122
-rw-r--r--quantum/quantum.h6
-rw-r--r--quantum/quantum_keycodes.h70
-rw-r--r--quantum/usb_device_state.c51
-rw-r--r--quantum/usb_device_state.h39
31 files changed, 499 insertions, 1817 deletions
diff --git a/quantum/action.c b/quantum/action.c
index be135f18f2..95f39d23d4 100644
--- a/quantum/action.c
+++ b/quantum/action.c
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "keycode.h"
#include "keyboard.h"
#include "mousekey.h"
+#include "programmable_button.h"
#include "command.h"
#include "led.h"
#include "action_layer.h"
@@ -988,6 +989,10 @@ void clear_keyboard_but_mods_and_keys() {
mousekey_clear();
mousekey_send();
#endif
+#ifdef PROGRAMMABLE_BUTTON_ENABLE
+ programmable_button_clear();
+ programmable_button_send();
+#endif
}
/** \brief Utilities for actions. (FIXME: Needs better description)
diff --git a/quantum/api.c b/quantum/api.c
deleted file mode 100644
index 1685744589..0000000000
--- a/quantum/api.c
+++ /dev/null
@@ -1,182 +0,0 @@
-/* Copyright 2016 Jack Humbert
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "api.h"
-#include "quantum.h"
-
-void dword_to_bytes(uint32_t dword, uint8_t* bytes) {
- bytes[0] = (dword >> 24) & 0xFF;
- bytes[1] = (dword >> 16) & 0xFF;
- bytes[2] = (dword >> 8) & 0xFF;
- bytes[3] = (dword >> 0) & 0xFF;
-}
-
-uint32_t bytes_to_dword(uint8_t* bytes, uint8_t index) { return ((uint32_t)bytes[index + 0] << 24) | ((uint32_t)bytes[index + 1] << 16) | ((uint32_t)bytes[index + 2] << 8) | (uint32_t)bytes[index + 3]; }
-
-__attribute__((weak)) bool process_api_quantum(uint8_t length, uint8_t* data) { return process_api_keyboard(length, data); }
-
-__attribute__((weak)) bool process_api_keyboard(uint8_t length, uint8_t* data) { return process_api_user(length, data); }
-
-__attribute__((weak)) bool process_api_user(uint8_t length, uint8_t* data) { return true; }
-
-void process_api(uint16_t length, uint8_t* data) {
- // SEND_STRING("\nRX: ");
- // for (uint8_t i = 0; i < length; i++) {
- // send_byte(data[i]);
- // SEND_STRING(" ");
- // }
- if (!process_api_quantum(length, data)) return;
-
- switch (data[0]) {
- case MT_SET_DATA:
- switch (data[1]) {
- case DT_DEFAULT_LAYER: {
- eeconfig_update_default_layer(data[2]);
- default_layer_set((uint32_t)(data[2]));
- break;
- }
- case DT_KEYMAP_OPTIONS: {
- eeconfig_update_keymap(data[2]);
- break;
- }
- case DT_RGBLIGHT: {
-#ifdef RGBLIGHT_ENABLE
- uint32_t rgblight = bytes_to_dword(data, 2);
- eeconfig_update_rgblight(rgblight);
-#endif
- break;
- }
- }
- case MT_GET_DATA:
- switch (data[1]) {
- case DT_HANDSHAKE: {
- MT_GET_DATA_ACK(DT_HANDSHAKE, NULL, 0);
- break;
- }
- case DT_DEBUG: {
- uint8_t debug_bytes[1] = {eeprom_read_byte(EECONFIG_DEBUG)};
- MT_GET_DATA_ACK(DT_DEBUG, debug_bytes, 1);
- break;
- }
- case DT_DEFAULT_LAYER: {
- uint8_t default_bytes[1] = {eeprom_read_byte(EECONFIG_DEFAULT_LAYER)};
- MT_GET_DATA_ACK(DT_DEFAULT_LAYER, default_bytes, 1);
- break;
- }
- case DT_CURRENT_LAYER: {
- uint8_t layer_state_bytes[4];
- dword_to_bytes(layer_state, layer_state_bytes);
- MT_GET_DATA_ACK(DT_CURRENT_LAYER, layer_state_bytes, 4);
- break;
- }
- case DT_AUDIO: {
-#ifdef AUDIO_ENABLE
- uint8_t audio_bytes[1] = {eeprom_read_byte(EECONFIG_AUDIO)};
- MT_GET_DATA_ACK(DT_AUDIO, audio_bytes, 1);
-#else
- MT_GET_DATA_ACK(DT_AUDIO, NULL, 0);
-#endif
- break;
- }
- case DT_BACKLIGHT: {
-#ifdef BACKLIGHT_ENABLE
- uint8_t backlight_bytes[1] = {eeprom_read_byte(EECONFIG_BACKLIGHT)};
- MT_GET_DATA_ACK(DT_BACKLIGHT, backlight_bytes, 1);
-#else
- MT_GET_DATA_ACK(DT_BACKLIGHT, NULL, 0);
-#endif
- break;
- }
- case DT_RGBLIGHT: {
-#ifdef RGBLIGHT_ENABLE
- uint8_t rgblight_bytes[4];
- dword_to_bytes(eeconfig_read_rgblight(), rgblight_bytes);
- MT_GET_DATA_ACK(DT_RGBLIGHT, rgblight_bytes, 4);
-#else
- MT_GET_DATA_ACK(DT_RGBLIGHT, NULL, 0);
-#endif
- break;
- }
- case DT_KEYMAP_OPTIONS: {
- uint8_t keymap_bytes[1] = {eeconfig_read_keymap()};
- MT_GET_DATA_ACK(DT_KEYMAP_OPTIONS, keymap_bytes, 1);
- break;
- }
- case DT_KEYMAP_SIZE: {
- uint8_t keymap_size[2] = {MATRIX_ROWS, MATRIX_COLS};
- MT_GET_DATA_ACK(DT_KEYMAP_SIZE, keymap_size, 2);
- break;
- }
- // This may be too much
- // case DT_KEYMAP: {
- // uint8_t keymap_data[MATRIX_ROWS * MATRIX_COLS * 4 + 3];
- // keymap_data[0] = data[2];
- // keymap_data[1] = MATRIX_ROWS;
- // keymap_data[2] = MATRIX_COLS;
- // for (int i = 0; i < MATRIX_ROWS; i++) {
- // for (int j = 0; j < MATRIX_COLS; j++) {
- // keymap_data[3 + (i*MATRIX_COLS*2) + (j*2)] = pgm_read_word(&keymaps[data[2]][i][j]) >> 8;
- // keymap_data[3 + (i*MATRIX_COLS*2) + (j*2) + 1] = pgm_read_word(&keymaps[data[2]][i][j]) & 0xFF;
- // }
- // }
- // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, MATRIX_ROWS * MATRIX_COLS * 4 + 3);
- // // uint8_t keymap_data[5];
- // // keymap_data[0] = data[2];
- // // keymap_data[1] = data[3];
- // // keymap_data[2] = data[4];
- // // keymap_data[3] = pgm_read_word(&keymaps[data[2]][data[3]][data[4]]) >> 8;
- // // keymap_data[4] = pgm_read_word(&keymaps[data[2]][data[3]][data[4]]) & 0xFF;
-
- // // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, 5);
- // break;
- // }
- default:
- break;
- }
- break;
- case MT_SET_DATA_ACK:
- case MT_GET_DATA_ACK:
- break;
- case MT_SEND_DATA:
- break;
- case MT_SEND_DATA_ACK:
- break;
- case MT_EXE_ACTION:
- break;
- case MT_EXE_ACTION_ACK:
- break;
- case MT_TYPE_ERROR:
- break;
- default:; // command not recognised
- SEND_BYTES(MT_TYPE_ERROR, DT_NONE, data, length);
- break;
-
- // #ifdef RGBLIGHT_ENABLE
- // case 0x27: ; // RGB LED functions
- // switch (*data++) {
- // case 0x00: ; // Update HSV
- // rgblight_sethsv((data[0] << 8 | data[1]) % 360, data[2], data[3]);
- // break;
- // case 0x01: ; // Update RGB
- // break;
- // case 0x02: ; // Update mode
- // rgblight_mode(data[0]);
- // break;
- // }
- // break;
- // #endif
- }
-}
diff --git a/quantum/api.h b/quantum/api.h
deleted file mode 100644
index 0a30e9d6cc..0000000000
--- a/quantum/api.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/* Copyright 2016 Jack Humbert
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#pragma once
-
-#ifdef __AVR__
-# include "lufa.h"
-#endif
-
-enum MESSAGE_TYPE {
- MT_GET_DATA = 0x10, // Get data from keyboard
- MT_GET_DATA_ACK = 0x11, // returned data to process (ACK)
- MT_SET_DATA = 0x20, // Set data on keyboard
- MT_SET_DATA_ACK = 0x21, // returned data to confirm (ACK)
- MT_SEND_DATA = 0x30, // Sending data/action from keyboard
- MT_SEND_DATA_ACK = 0x31, // returned data/action confirmation (ACK)
- MT_EXE_ACTION = 0x40, // executing actions on keyboard
- MT_EXE_ACTION_ACK = 0x41, // return confirmation/value (ACK)
- MT_TYPE_ERROR = 0x80 // type not recognised (ACK)
-};
-
-enum DATA_TYPE { DT_NONE = 0x00, DT_HANDSHAKE, DT_DEFAULT_LAYER, DT_CURRENT_LAYER, DT_KEYMAP_OPTIONS, DT_BACKLIGHT, DT_RGBLIGHT, DT_UNICODE, DT_DEBUG, DT_AUDIO, DT_QUANTUM_ACTION, DT_KEYBOARD_ACTION, DT_USER_ACTION, DT_KEYMAP_SIZE, DT_KEYMAP };
-
-void dword_to_bytes(uint32_t dword, uint8_t* bytes);
-uint32_t bytes_to_dword(uint8_t* bytes, uint8_t index);
-
-#define MT_GET_DATA(data_type, data, length) SEND_BYTES(MT_GET_DATA, data_type, data, length)
-#define MT_GET_DATA_ACK(data_type, data, length) SEND_BYTES(MT_GET_DATA_ACK, data_type, data, length)
-#define MT_SET_DATA(data_type, data, length) SEND_BYTES(MT_SET_DATA, data_type, data, length)
-#define MT_SET_DATA_ACK(data_type, data, length) SEND_BYTES(MT_SET_DATA_ACK, data_type, data, length)
-#define MT_SEND_DATA(data_type, data, length) SEND_BYTES(MT_SEND_DATA, data_type, data, length)
-#define MT_SEND_DATA_ACK(data_type, data, length) SEND_BYTES(MT_SEND_DATA_ACK, data_type, data, length)
-#define MT_EXE_ACTION(data_type, data, length) SEND_BYTES(MT_EXE_ACTION, data_type, data, length)
-#define MT_EXE_ACTION_ACK(data_type, data, length) SEND_BYTES(MT_EXE_ACTION_ACK, data_type, data, length)
-
-void process_api(uint16_t length, uint8_t* data);
-
-__attribute__((weak)) bool process_api_quantum(uint8_t length, uint8_t* data);
-
-__attribute__((weak)) bool process_api_keyboard(uint8_t length, uint8_t* data);
-
-__attribute__((weak)) bool process_api_user(uint8_t length, uint8_t* data);
diff --git a/quantum/api/api_sysex.c b/quantum/api/api_sysex.c
deleted file mode 100644
index 07c90cf804..0000000000
--- a/quantum/api/api_sysex.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/* Copyright 2016 Jack Humbert, Fred Sundvik
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-#include "api_sysex.h"
-#include "sysex_tools.h"
-#include "print.h"
-#include "qmk_midi.h"
-
-void send_bytes_sysex(uint8_t message_type, uint8_t data_type, uint8_t* bytes, uint16_t length) {
- // SEND_STRING("\nTX: ");
- // for (uint8_t i = 0; i < length; i++) {
- // send_byte(bytes[i]);
- // SEND_STRING(" ");
- // }
- if (length > API_SYSEX_MAX_SIZE) {
- xprintf("Sysex msg too big %d %d %d", message_type, data_type, length);
- return;
- }
-
- // The buffer size required is calculated as the following
- // API_SYSEX_MAX_SIZE is the maximum length
- // In addition to that we have a two byte message header consisting of the message_type and data_type
- // This has to be encoded with an additional overhead of one byte for every starting 7 bytes
- // We just add one extra byte in case it's not divisible by 7
- // Then we have an unencoded header consisting of 4 bytes
- // Plus a one byte terminator
- const unsigned message_header = 2;
- const unsigned unencoded_message = API_SYSEX_MAX_SIZE + message_header;
- const unsigned encoding_overhead = unencoded_message / 7 + 1;
- const unsigned encoded_size = unencoded_message + encoding_overhead;
- const unsigned unencoded_header = 4;
- const unsigned terminator = 1;
- const unsigned buffer_size = encoded_size + unencoded_header + terminator;
- uint8_t buffer[encoded_size + unencoded_header + terminator];
- // The unencoded header
- buffer[0] = 0xF0;
- buffer[1] = 0x00;
- buffer[2] = 0x00;
- buffer[3] = 0x00;
-
- // We copy the message to the end of the array, this way we can do an inplace encoding, using the same
- // buffer for both input and output
- const unsigned message_size = length + message_header;
- uint8_t* unencoded_start = buffer + buffer_size - message_size;
- uint8_t* ptr = unencoded_start;
- *(ptr++) = message_type;
- *(ptr++) = data_type;
- memcpy(ptr, bytes, length);
-
- unsigned encoded_length = sysex_encode(buffer + unencoded_header, unencoded_start, message_size);
- unsigned final_size = unencoded_header + encoded_length + terminator;
- buffer[final_size - 1] = 0xF7;
- midi_send_array(&midi_device, final_size, buffer);
-
- // SEND_STRING("\nTD: ");
- // for (uint8_t i = 0; i < encoded_length + 5; i++) {
- // send_byte(buffer[i]);
- // SEND_STRING(" ");
- // }
-}
diff --git a/quantum/api/api_sysex.h b/quantum/api/api_sysex.h
deleted file mode 100644
index eb0a18848d..0000000000
--- a/quantum/api/api_sysex.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/* Copyright 2016 Jack Humbert
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#pragma once
-
-#include "api.h"
-
-#define API_SYSEX_MAX_SIZE 32
-
-void send_bytes_sysex(uint8_t message_type, uint8_t data_type, uint8_t* bytes, uint16_t length);
-
-#define SEND_BYTES(mt, dt, b, l) send_bytes_sysex(mt, dt, b, l)
diff --git a/quantum/audio/audio.h b/quantum/audio/audio.h
index 56b9158a1a..290d461f5a 100644
--- a/quantum/audio/audio.h
+++ b/quantum/audio/audio.h
@@ -26,17 +26,12 @@
#if defined(__AVR__)
# include <avr/io.h>
-# if defined(AUDIO_DRIVER_PWM)
-# include "driver_avr_pwm.h"
-# endif
#endif
-#if defined(PROTOCOL_CHIBIOS)
-# if defined(AUDIO_DRIVER_PWM)
-# include "driver_chibios_pwm.h"
-# elif defined(AUDIO_DRIVER_DAC)
-# include "driver_chibios_dac.h"
-# endif
+#if defined(AUDIO_DRIVER_PWM)
+# include "audio_pwm.h"
+#elif defined(AUDIO_DRIVER_DAC)
+# include "audio_dac.h"
#endif
typedef union {
diff --git a/quantum/audio/driver_avr_pwm.h b/quantum/audio/driver_avr_pwm.h
deleted file mode 100644
index d6eb3571da..0000000000
--- a/quantum/audio/driver_avr_pwm.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/* Copyright 2020 Jack Humbert
- * Copyright 2020 JohSchneider
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-#pragma once
diff --git a/quantum/audio/driver_avr_pwm_hardware.c b/quantum/audio/driver_avr_pwm_hardware.c
deleted file mode 100644
index df03a4558c..0000000000
--- a/quantum/audio/driver_avr_pwm_hardware.c
+++ /dev/null
@@ -1,332 +0,0 @@
-/* Copyright 2016 Jack Humbert
- * Copyright 2020 JohSchneider
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#if defined(__AVR__)
-# include <avr/pgmspace.h>
-# include <avr/interrupt.h>
-# include <avr/io.h>
-#endif
-
-#include "audio.h"
-
-extern bool playing_note;
-extern bool playing_melody;
-extern uint8_t note_timbre;
-
-#define CPU_PRESCALER 8
-
-/*
- Audio Driver: PWM
-
- drive up to two speakers through the AVR PWM hardware-peripheral, using timer1 and/or timer3 on Atmega32U4.
-
- the primary channel_1 can be connected to either pin PC4 PC5 or PC6 (the later being used by most AVR based keyboards) with a PMW signal generated by timer3
- and an optional secondary channel_2 on either pin PB5, PB6 or PB7, with a PWM signal from timer1
-
- alternatively, the PWM pins on PORTB can be used as only/primary speaker
-*/
-
-#if defined(AUDIO_PIN) && (AUDIO_PIN != C4) && (AUDIO_PIN != C5) && (AUDIO_PIN != C6) && (AUDIO_PIN != B5) && (AUDIO_PIN != B6) && (AUDIO_PIN != B7) && (AUDIO_PIN != D5)
-# error "Audio feature enabled, but no suitable pin selected as AUDIO_PIN - see docs/feature_audio under the AVR settings for available options."
-#endif
-
-#if (AUDIO_PIN == C4) || (AUDIO_PIN == C5) || (AUDIO_PIN == C6)
-# define AUDIO1_PIN_SET
-# define AUDIO1_TIMSKx TIMSK3
-# define AUDIO1_TCCRxA TCCR3A
-# define AUDIO1_TCCRxB TCCR3B
-# define AUDIO1_ICRx ICR3
-# define AUDIO1_WGMx0 WGM30
-# define AUDIO1_WGMx1 WGM31
-# define AUDIO1_WGMx2 WGM32
-# define AUDIO1_WGMx3 WGM33
-# define AUDIO1_CSx0 CS30
-# define AUDIO1_CSx1 CS31
-# define AUDIO1_CSx2 CS32
-
-# if (AUDIO_PIN == C6)
-# define AUDIO1_COMxy0 COM3A0
-# define AUDIO1_COMxy1 COM3A1
-# define AUDIO1_OCIExy OCIE3A
-# define AUDIO1_OCRxy OCR3A
-# define AUDIO1_PIN C6
-# define AUDIO1_TIMERx_COMPy_vect TIMER3_COMPA_vect
-# elif (AUDIO_PIN == C5)
-# define AUDIO1_COMxy0 COM3B0
-# define AUDIO1_COMxy1 COM3B1
-# define AUDIO1_OCIExy OCIE3B
-# define AUDIO1_OCRxy OCR3B
-# define AUDIO1_PIN C5
-# define AUDIO1_TIMERx_COMPy_vect TIMER3_COMPB_vect
-# elif (AUDIO_PIN == C4)
-# define AUDIO1_COMxy0 COM3C0
-# define AUDIO1_COMxy1 COM3C1
-# define AUDIO1_OCIExy OCIE3C
-# define AUDIO1_OCRxy OCR3C
-# define AUDIO1_PIN C4
-# define AUDIO1_TIMERx_COMPy_vect TIMER3_COMPC_vect
-# endif
-#endif
-
-#if defined(AUDIO_PIN) && defined(AUDIO_PIN_ALT) && (AUDIO_PIN == AUDIO_PIN_ALT)
-# error "Audio feature: AUDIO_PIN and AUDIO_PIN_ALT on the same pin makes no sense."
-#endif
-
-#if ((AUDIO_PIN == B5) && ((AUDIO_PIN_ALT == B6) || (AUDIO_PIN_ALT == B7))) || ((AUDIO_PIN == B6) && ((AUDIO_PIN_ALT == B5) || (AUDIO_PIN_ALT == B7))) || ((AUDIO_PIN == B7) && ((AUDIO_PIN_ALT == B5) || (AUDIO_PIN_ALT == B6)))
-# error "Audio feature: PORTB as AUDIO_PIN and AUDIO_PIN_ALT at the same time is not supported."
-#endif
-
-#if defined(AUDIO_PIN_ALT) && (AUDIO_PIN_ALT != B5) && (AUDIO_PIN_ALT != B6) && (AUDIO_PIN_ALT != B7)
-# error "Audio feature: the pin selected as AUDIO_PIN_ALT is not supported."
-#endif
-
-#if (AUDIO_PIN == B5) || (AUDIO_PIN == B6) || (AUDIO_PIN == B7) || (AUDIO_PIN_ALT == B5) || (AUDIO_PIN_ALT == B6) || (AUDIO_PIN_ALT == B7) || (AUDIO_PIN == D5)
-# define AUDIO2_PIN_SET
-# define AUDIO2_TIMSKx TIMSK1
-# define AUDIO2_TCCRxA TCCR1A
-# define AUDIO2_TCCRxB TCCR1B
-# define AUDIO2_ICRx ICR1
-# define AUDIO2_WGMx0 WGM10
-# define AUDIO2_WGMx1 WGM11
-# define AUDIO2_WGMx2 WGM12
-# define AUDIO2_WGMx3 WGM13
-# define AUDIO2_CSx0 CS10
-# define AUDIO2_CSx1 CS11
-# define AUDIO2_CSx2 CS12
-
-# if (AUDIO_PIN == B5) || (AUDIO_PIN_ALT == B5)
-# define AUDIO2_COMxy0 COM1A0
-# define AUDIO2_COMxy1 COM1A1
-# define AUDIO2_OCIExy OCIE1A
-# define AUDIO2_OCRxy OCR1A
-# define AUDIO2_PIN B5
-# define AUDIO2_TIMERx_COMPy_vect TIMER1_COMPA_vect
-# elif (AUDIO_PIN == B6) || (AUDIO_PIN_ALT == B6)
-# define AUDIO2_COMxy0 COM1B0
-# define AUDIO2_COMxy1 COM1B1
-# define AUDIO2_OCIExy OCIE1B
-# define AUDIO2_OCRxy OCR1B
-# define AUDIO2_PIN B6
-# define AUDIO2_TIMERx_COMPy_vect TIMER1_COMPB_vect
-# elif (AUDIO_PIN == B7) || (AUDIO_PIN_ALT == B7)
-# define AUDIO2_COMxy0 COM1C0
-# define AUDIO2_COMxy1 COM1C1
-# define AUDIO2_OCIExy OCIE1C
-# define AUDIO2_OCRxy OCR1C
-# define AUDIO2_PIN B7
-# define AUDIO2_TIMERx_COMPy_vect TIMER1_COMPC_vect
-# elif (AUDIO_PIN == D5) && defined(__AVR_ATmega32A__)
-# pragma message "Audio support for ATmega32A is experimental and can cause crashes."
-# undef AUDIO2_TIMSKx
-# define AUDIO2_TIMSKx TIMSK
-# define AUDIO2_COMxy0 COM1A0
-# define AUDIO2_COMxy1 COM1A1
-# define AUDIO2_OCIExy OCIE1A
-# define AUDIO2_OCRxy OCR1A
-# define AUDIO2_PIN D5
-# define AUDIO2_TIMERx_COMPy_vect TIMER1_COMPA_vect
-# endif
-#endif
-
-// C6 seems to be the assumed default by many existing keyboard - but sill warn the user
-#if !defined(AUDIO1_PIN_SET) && !defined(AUDIO2_PIN_SET)
-# pragma message "Audio feature enabled, but no suitable pin selected - see docs/feature_audio under the AVR settings for available options. Don't expect to hear anything... :-)"
-// TODO: make this an error - go through the breaking-change-process and change all keyboards to the new define
-#endif
-// -----------------------------------------------------------------------------
-
-#ifdef AUDIO1_PIN_SET
-static float channel_1_frequency = 0.0f;
-void channel_1_set_frequency(float freq) {
- if (freq == 0.0f) // a pause/rest is a valid "note" with freq=0
- {
- // disable the output, but keep the pwm-ISR going (with the previous
- // frequency) so the audio-state keeps getting updated
- // Note: setting the duty-cycle 0 is not possible on non-inverting PWM mode - see the AVR data-sheet
- AUDIO1_TCCRxA &= ~(_BV(AUDIO1_COMxy1) | _BV(AUDIO1_COMxy0));
- return;
- } else {
- AUDIO1_TCCRxA |= _BV(AUDIO1_COMxy1); // enable output, PWM mode
- }
-
- channel_1_frequency = freq;
-
- // set pwm period
- AUDIO1_ICRx = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
- // and duty cycle
- AUDIO1_OCRxy = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre / 100);
-}
-
-void channel_1_start(void) {
- // enable timer-counter ISR
- AUDIO1_TIMSKx |= _BV(AUDIO1_OCIExy);
- // enable timer-counter output
- AUDIO1_TCCRxA |= _BV(AUDIO1_COMxy1);
-}
-
-void channel_1_stop(void) {
- // disable timer-counter ISR
- AUDIO1_TIMSKx &= ~_BV(AUDIO1_OCIExy);
- // disable timer-counter output
- AUDIO1_TCCRxA &= ~(_BV(AUDIO1_COMxy1) | _BV(AUDIO1_COMxy0));
-}
-#endif
-
-#ifdef AUDIO2_PIN_SET
-static float channel_2_frequency = 0.0f;
-void channel_2_set_frequency(float freq) {
- if (freq == 0.0f) {
- AUDIO2_TCCRxA &= ~(_BV(AUDIO2_COMxy1) | _BV(AUDIO2_COMxy0));
- return;
- } else {
- AUDIO2_TCCRxA |= _BV(AUDIO2_COMxy1);
- }
-
- channel_2_frequency = freq;
-
- AUDIO2_ICRx = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
- AUDIO2_OCRxy = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre / 100);
-}
-
-float channel_2_get_frequency(void) { return channel_2_frequency; }
-
-void channel_2_start(void) {
- AUDIO2_TIMSKx |= _BV(AUDIO2_OCIExy);
- AUDIO2_TCCRxA |= _BV(AUDIO2_COMxy1);
-}
-
-void channel_2_stop(void) {
- AUDIO2_TIMSKx &= ~_BV(AUDIO2_OCIExy);
- AUDIO2_TCCRxA &= ~(_BV(AUDIO2_COMxy1) | _BV(AUDIO2_COMxy0));
-}
-#endif
-
-void audio_driver_initialize() {
-#ifdef AUDIO1_PIN_SET
- channel_1_stop();
- setPinOutput(AUDIO1_PIN);
-#endif
-
-#ifdef AUDIO2_PIN_SET
- channel_2_stop();
- setPinOutput(AUDIO2_PIN);
-#endif
-
- // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers TCCR3A/TCCR3B, TCCR1A/TCCR1B
- // Compare Output Mode (COM3An and COM1An) = 0b00 = Normal port operation
- // OC3A -- PC6
- // OC3B -- PC5
- // OC3C -- PC4
- // OC1A -- PB5
- // OC1B -- PB6
- // OC1C -- PB7
-
- // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14. Period = ICR3, Duty Cycle OCR3A)
- // OCR3A - PC6
- // OCR3B - PC5
- // OCR3C - PC4
- // OCR1A - PB5
- // OCR1B - PB6
- // OCR1C - PB7
-
- // Clock Select (CS3n) = 0b010 = Clock / 8
-#ifdef AUDIO1_PIN_SET
- // initialize timer-counter
- AUDIO1_TCCRxA = (0 << AUDIO1_COMxy1) | (0 << AUDIO1_COMxy0) | (1 << AUDIO1_WGMx1) | (0 << AUDIO1_WGMx0);
- AUDIO1_TCCRxB = (1 << AUDIO1_WGMx3) | (1 << AUDIO1_WGMx2) | (0 << AUDIO1_CSx2) | (1 << AUDIO1_CSx1) | (0 << AUDIO1_CSx0);
-#endif
-
-#ifdef AUDIO2_PIN_SET
- AUDIO2_TCCRxA = (0 << AUDIO2_COMxy1) | (0 << AUDIO2_COMxy0) | (1 << AUDIO2_WGMx1) | (0 << AUDIO2_WGMx0);
- AUDIO2_TCCRxB = (1 << AUDIO2_WGMx3) | (1 << AUDIO2_WGMx2) | (0 << AUDIO2_CSx2) | (1 << AUDIO2_CSx1) | (0 << AUDIO2_CSx0);
-#endif
-}
-
-void audio_driver_stop() {
-#ifdef AUDIO1_PIN_SET
- channel_1_stop();
-#endif
-
-#ifdef AUDIO2_PIN_SET
- channel_2_stop();
-#endif
-}
-
-void audio_driver_start(void) {
-#ifdef AUDIO1_PIN_SET
- channel_1_start();
- if (playing_note) {
- channel_1_set_frequency(audio_get_processed_frequency(0));
- }
-#endif
-
-#if !defined(AUDIO1_PIN_SET) && defined(AUDIO2_PIN_SET)
- channel_2_start();
- if (playing_note) {
- channel_2_set_frequency(audio_get_processed_frequency(0));
- }
-#endif
-}
-
-static volatile uint32_t isr_counter = 0;
-#ifdef AUDIO1_PIN_SET
-ISR(AUDIO1_TIMERx_COMPy_vect) {
- isr_counter++;
- if (isr_counter < channel_1_frequency / (CPU_PRESCALER * 8)) return;
-
- isr_counter = 0;
- bool state_changed = audio_update_state();
-
- if (!playing_note && !playing_melody) {
- channel_1_stop();
-# ifdef AUDIO2_PIN_SET
- channel_2_stop();
-# endif