summaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
authorDidier Loiseau <didierloiseau+git@gmail.com>2016-09-11 01:26:47 +0200
committerDidier Loiseau <didierloiseau+git@gmail.com>2016-09-11 01:26:47 +0200
commitb9014c757599ca288be07629ad1d00ef66c033f1 (patch)
treeba056e704e76327c597e25d1be200c4c991efeb8 /quantum
parent60a826923d5a6b0dbe416b0837608149ab82e470 (diff)
parent5010df3d8b68a53c382b853c7c2e45922975b631 (diff)
Merge remote-tracking branch 'remotes/jackhumbert/master' into bépo
Diffstat (limited to 'quantum')
-rw-r--r--quantum/audio.c362
-rw-r--r--quantum/audio.h11
-rw-r--r--quantum/audio/audio.c477
-rw-r--r--quantum/audio/audio.h91
-rw-r--r--quantum/audio/audio_pwm.c643
-rw-r--r--quantum/audio/luts.c382
-rw-r--r--quantum/audio/luts.h15
-rw-r--r--quantum/audio/musical_notes.h217
-rw-r--r--quantum/audio/song_list.h125
-rw-r--r--quantum/audio/voices.c165
-rw-r--r--quantum/audio/voices.h31
-rw-r--r--quantum/audio/wave.h (renamed from quantum/wave.h)0
-rw-r--r--quantum/config_common.h132
-rw-r--r--quantum/dynamic_macro.h226
-rw-r--r--quantum/keycode_config.c74
-rw-r--r--quantum/keycode_config.h26
-rw-r--r--quantum/keymap.h339
-rw-r--r--quantum/keymap_common.c326
-rw-r--r--quantum/keymap_common.h214
-rw-r--r--quantum/keymap_extras/keymap_bepo.h6
-rw-r--r--quantum/keymap_extras/keymap_colemak.h2
-rw-r--r--quantum/keymap_extras/keymap_dvorak.h17
-rw-r--r--quantum/keymap_extras/keymap_fr_ch.h4
-rw-r--r--quantum/keymap_extras/keymap_french.h6
-rw-r--r--quantum/keymap_extras/keymap_french_osx.h2
-rw-r--r--quantum/keymap_extras/keymap_german.h4
-rw-r--r--quantum/keymap_extras/keymap_german_ch.h102
-rw-r--r--quantum/keymap_extras/keymap_german_osx.h161
-rw-r--r--quantum/keymap_extras/keymap_neo2.h4
-rw-r--r--quantum/keymap_extras/keymap_nordic.h6
-rw-r--r--quantum/keymap_extras/keymap_norwegian.h (renamed from quantum/keymap_extras/keymap_norwegian.c)2
-rw-r--r--quantum/keymap_extras/keymap_plover.h32
-rw-r--r--quantum/keymap_extras/keymap_russian.h77
-rw-r--r--quantum/keymap_extras/keymap_spanish.h8
-rw-r--r--quantum/keymap_extras/keymap_uk.h6
-rw-r--r--quantum/keymap_extras/keymap_unicode_cyrillic.h163
-rw-r--r--quantum/keymap_midi.c109
-rw-r--r--quantum/keymap_unicode.c61
-rw-r--r--quantum/led.c47
-rwxr-xr-xquantum/light_ws2812.c25
-rw-r--r--quantum/matrix.c196
-rw-r--r--quantum/process_keycode/process_chording.c60
-rw-r--r--quantum/process_keycode/process_chording.h16
-rw-r--r--quantum/process_keycode/process_leader.c38
-rw-r--r--quantum/process_keycode/process_leader.h23
-rw-r--r--quantum/process_keycode/process_midi.c66
-rw-r--r--quantum/process_keycode/process_midi.h (renamed from quantum/keymap_midi.h)35
-rw-r--r--quantum/process_keycode/process_music.c176
-rw-r--r--quantum/process_keycode/process_music.h27
-rw-r--r--quantum/process_keycode/process_tap_dance.c135
-rw-r--r--quantum/process_keycode/process_tap_dance.h70
-rw-r--r--quantum/process_keycode/process_unicode.c212
-rw-r--r--quantum/process_keycode/process_unicode.h161
-rw-r--r--quantum/quantum.c850
-rw-r--r--quantum/quantum.h113
-rw-r--r--quantum/quantum.mk53
-rw-r--r--quantum/rgblight.c736
-rw-r--r--quantum/rgblight.h12
-rw-r--r--quantum/serial_link/LICENSE21
-rw-r--r--quantum/serial_link/README.md1
-rw-r--r--quantum/serial_link/protocol/byte_stuffer.c142
-rw-r--r--quantum/serial_link/protocol/byte_stuffer.h37
-rw-r--r--quantum/serial_link/protocol/frame_router.c69
-rw-r--r--quantum/serial_link/protocol/frame_router.h38
-rw-r--r--quantum/serial_link/protocol/frame_validator.c121
-rw-r--r--quantum/serial_link/protocol/frame_validator.h34
-rw-r--r--quantum/serial_link/protocol/physical.h30
-rw-r--r--quantum/serial_link/protocol/transport.c128
-rw-r--r--quantum/serial_link/protocol/transport.h152
-rw-r--r--quantum/serial_link/protocol/triple_buffered_object.c78
-rw-r--r--quantum/serial_link/protocol/triple_buffered_object.h51
-rw-r--r--quantum/serial_link/system/serial_link.c265
-rw-r--r--quantum/serial_link/system/serial_link.h63
-rw-r--r--quantum/serial_link/tests/Makefile61
-rw-r--r--quantum/serial_link/tests/byte_stuffer_tests.cpp483
-rw-r--r--quantum/serial_link/tests/frame_router_tests.cpp229
-rw-r--r--quantum/serial_link/tests/frame_validator_tests.cpp115
-rw-r--r--quantum/serial_link/tests/rules.mk22
-rw-r--r--quantum/serial_link/tests/testlist.mk6
-rw-r--r--quantum/serial_link/tests/transport_tests.cpp188
-rw-r--r--quantum/serial_link/tests/triple_buffered_object_tests.cpp84
-rw-r--r--quantum/template/Makefile143
-rw-r--r--quantum/template/config.h23
-rw-r--r--quantum/template/keymaps/default/Makefile21
-rw-r--r--quantum/template/keymaps/default/config.h8
-rw-r--r--quantum/template/keymaps/default/keymap.c (renamed from quantum/template/keymaps/default.c)20
-rw-r--r--quantum/template/keymaps/default/readme.md1
-rw-r--r--quantum/template/readme.md (renamed from quantum/template/README.md)18
-rw-r--r--quantum/template/rules.mk67
-rw-r--r--quantum/template/template.c24
-rw-r--r--quantum/template/template.h10
-rw-r--r--quantum/tools/eeprom_reset.hex9
-rw-r--r--quantum/tools/readme.md6
-rw-r--r--quantum/visualizer/LICENSE.md29
-rw-r--r--quantum/visualizer/example_integration/callbacks.c36
-rw-r--r--quantum/visualizer/example_integration/gfxconf.h325
-rw-r--r--quantum/visualizer/example_integration/lcd_backlight_hal.c91
-rw-r--r--quantum/visualizer/example_integration/visualizer_user.c121
-rw-r--r--quantum/visualizer/lcd_backlight.c85
-rw-r--r--quantum/visualizer/lcd_backlight.h42
-rw-r--r--quantum/visualizer/led_test.c170
-rw-r--r--quantum/visualizer/led_test.h41
-rw-r--r--quantum/visualizer/readme.md18
-rw-r--r--quantum/visualizer/visualizer.c545
-rw-r--r--quantum/visualizer/visualizer.h147
-rw-r--r--quantum/visualizer/visualizer.mk61
106 files changed, 10224 insertions, 1934 deletions
diff --git a/quantum/audio.c b/quantum/audio.c
deleted file mode 100644
index 3a3a1a4910..0000000000
--- a/quantum/audio.c
+++ /dev/null
@@ -1,362 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <math.h>
-#include <avr/pgmspace.h>
-#include <avr/interrupt.h>
-#include <avr/io.h>
-
-#include "audio.h"
-#include "keymap_common.h"
-
-#define PI 3.14159265
-
-// #define PWM_AUDIO
-
-#ifdef PWM_AUDIO
- #include "wave.h"
- #define SAMPLE_DIVIDER 39
- #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
- // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
-#endif
-
-void delay_us(int count) {
- while(count--) {
- _delay_us(1);
- }
-}
-
-int voices = 0;
-int voice_place = 0;
-double frequency = 0;
-int volume = 0;
-long position = 0;
-
-double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
-int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
-bool sliding = false;
-
-int max = 0xFF;
-float sum = 0;
-int value = 128;
-float place = 0;
-float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
-
-uint16_t place_int = 0;
-bool repeat = true;
-uint8_t * sample;
-uint16_t sample_length = 0;
-
-
-bool notes = false;
-bool note = false;
-float note_frequency = 0;
-float note_length = 0;
-uint16_t note_position = 0;
-float (* notes_pointer)[][2];
-uint8_t notes_length;
-bool notes_repeat;
-uint8_t current_note = 0;
-
-void stop_all_notes() {
- voices = 0;
- #ifdef PWM_AUDIO
- TIMSK3 &= ~_BV(OCIE3A);
- #else
- TIMSK3 &= ~_BV(OCIE3A);
- TCCR3A &= ~_BV(COM3A1);
- #endif
- notes = false;
- note = false;
- frequency = 0;
- volume = 0;
-
- for (int i = 0; i < 8; i++) {
- frequencies[i] = 0;
- volumes[i] = 0;
- }
-}
-
-void stop_note(double freq) {
- #ifdef PWM_AUDIO
- freq = freq / SAMPLE_RATE;
- #endif
- for (int i = 7; i >= 0; i--) {
- if (frequencies[i] == freq) {
- frequencies[i] = 0;
- volumes[i] = 0;
- for (int j = i; (j < 7); j++) {
- frequencies[j] = frequencies[j+1];
- frequencies[j+1] = 0;
- volumes[j] = volumes[j+1];
- volumes[j+1] = 0;
- }
- }
- }
- voices--;
- if (voices < 0)
- voices = 0;
- if (voices == 0) {
- #ifdef PWM_AUDIO
- TIMSK3 &= ~_BV(OCIE3A);
- #else
- TIMSK3 &= ~_BV(OCIE3A);
- TCCR3A &= ~_BV(COM3A1);
- #endif
- frequency = 0;
- volume = 0;
- note = false;
- } else {
- double freq = frequencies[voices - 1];
- int vol = volumes[voices - 1];
- double starting_f = frequency;
- if (frequency < freq) {
- sliding = true;
- for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
- frequency = f;
- }
- sliding = false;
- } else if (frequency > freq) {
- sliding = true;
- for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
- frequency = f;
- }
- sliding = false;
- }
- frequency = freq;
- volume = vol;
- }
-}
-
-void init_notes() {
-
- #ifdef PWM_AUDIO
- PLLFRQ = _BV(PDIV2);
- PLLCSR = _BV(PLLE);
- while(!(PLLCSR & _BV(PLOCK)));
- PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
-
- /* Init a fast PWM on Timer4 */
- TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
- TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
- OCR4A = 0;
-
- /* Enable the OC4A output */
- DDRC |= _BV(PORTC6);
-
- TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
-
- TCCR3A = 0x0; // Options not needed
- TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
- OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
- #else
- DDRC |= _BV(PORTC6);
-
- TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
-
- TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
- TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
- #endif
-}
-
-
-ISR(TIMER3_COMPA_vect) {
-
- if (note) {
- #ifdef PWM_AUDIO
- if (voices == 1) {
- // SINE
- OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
-
- // SQUARE
- // if (((int)place) >= 1024){
- // OCR4A = 0xFF >> 2;
- // } else {
- // OCR4A = 0x00;
- // }
-
- // SAWTOOTH
- // OCR4A = (int)place / 4;
-
- // TRIANGLE
- // if (((int)place) >= 1024) {
- // OCR4A = (int)place / 2;
- // } else {
- // OCR4A = 2048 - (int)place / 2;
- // }
-
- place += frequency;
-
- if (place >= SINE_LENGTH)
- place -= SINE_LENGTH;
-
- } else {
- int sum = 0;
- for (int i = 0; i < voices; i++) {
- // SINE
- sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
-
- // SQUARE
- // if (((int)places[i]) >= 1024){
- // sum += 0xFF >> 2;
- // } else {
- // sum += 0x00;
- // }
-
- places[i] += frequencies[i];
-
- if (places[i] >= SINE_LENGTH)
- places[i] -= SINE_LENGTH;
- }
- OCR4A = sum;
- }
- #else
- if (frequency > 0) {
- // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period
- // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period
- if (place > 10) {
- voice_place = (voice_place + 1) % voices;
- place = 0.0;
- }
- ICR3 = (int)(((double)F_CPU) / frequencies[voice_place]); // Set max to the period
- OCR3A = (int)(((double)F_CPU) / frequencies[voice_place]) >> 1; // Set compare to half the period
- place++;
- }
- #endif
- }
-
- // SAMPLE
- // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
-
- // place_int++;
-
- // if (place_int >= sample_length)
- // if (repeat)
- // place_int -= sample_length;
- // else
- // TIMSK3 &= ~_BV(OCIE3A);
-
-
- if (notes) {
- #ifdef PWM_AUDIO
- OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
-
- place += note_frequency;
- if (place >= SINE_LENGTH)
- place -= SINE_LENGTH;
- #else
- if (note_frequency > 0) {
- ICR3 = (int)(((double)F_CPU) / note_frequency); // Set max to the period
- OCR3A = (int)(((double)F_CPU) / note_frequency) >> 1; // Set compare to half the period
- }
- #endif
-
-
- note_position++;
- if (note_position >= note_length) {
- current_note++;
- if (current_note >= notes_length) {
- if (notes_repeat) {
- current_note = 0;
- } else {
- #ifdef PWM_AUDIO
- TIMSK3 &= ~_BV(OCIE3A);
- #else
- TIMSK3 &= ~_BV(OCIE3A);
- TCCR3A &= ~_BV(COM3A1);
- #endif
- notes = false;
- return;
- }
- }
- #ifdef PWM_AUDIO
- note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
- note_length = (*notes_pointer)[current_note][1];
- #else
- note_frequency = (*notes_pointer)[current_note][0];
- note_length = (*notes_pointer)[current_note][1] / 4;
- #endif
- note_position = 0;
- }
-
- }
-
-}
-
-void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) {
- if (note)
- stop_all_notes();
- notes = true;
-
- notes_pointer = np;
- notes_length = n_length;
- notes_repeat = n_repeat;
-
- place = 0;
- current_note = 0;
- #ifdef PWM_AUDIO
- note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
- note_length = (*notes_pointer)[current_note][1];
- #else
- note_frequency = (*notes_pointer)[current_note][0];
- note_length = (*notes_pointer)[current_note][1] / 4;
- #endif
- note_position = 0;
-
-
- #ifdef PWM_AUDIO
- TIMSK3 |= _BV(OCIE3A);
- #else
- TIMSK3 |= _BV(OCIE3A);
- TCCR3A |= _BV(COM3A1);
- #endif
-}
-
-void play_sample(uint8_t * s, uint16_t l, bool r) {
- stop_all_notes();
- place_int = 0;
- sample = s;
- sample_length = l;
- repeat = r;
-
- #ifdef PWM_AUDIO
- TIMSK3 |= _BV(OCIE3A);
- #else
- #endif
-}
-
-void play_note(double freq, int vol) {
- if (notes)
- stop_all_notes();
- note = true;
- #ifdef PWM_AUDIO
- freq = freq / SAMPLE_RATE;
- #endif
- if (freq > 0) {
- if (frequency != 0) {
- double starting_f = frequency;
- if (frequency < freq) {
- for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
- frequency = f;
- }
- } else if (frequency > freq) {
- for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
- frequency = f;
- }
- }
- }
- frequency = freq;
- volume = vol;
-
- frequencies[voices] = frequency;
- volumes[voices] = volume;
- voices++;
- }
-
- #ifdef PWM_AUDIO
- TIMSK3 |= _BV(OCIE3A);
- #else
- TIMSK3 |= _BV(OCIE3A);
- TCCR3A |= _BV(COM3A1);
- #endif
-
-} \ No newline at end of file
diff --git a/quantum/audio.h b/quantum/audio.h
deleted file mode 100644
index 99203cea7a..0000000000
--- a/quantum/audio.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#include <stdint.h>
-#include <stdbool.h>
-#include <avr/io.h>
-#include <util/delay.h>
-
-void play_sample(uint8_t * s, uint16_t l, bool r);
-void play_note(double freq, int vol);
-void stop_note(double freq);
-void stop_all_notes();
-void init_notes();
-void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat); \ No newline at end of file
diff --git a/quantum/audio/audio.c b/quantum/audio/audio.c
new file mode 100644
index 0000000000..ead5fbf3e9
--- /dev/null
+++ b/quantum/audio/audio.c
@@ -0,0 +1,477 @@
+#include <stdio.h>
+#include <string.h>
+//#include <math.h>
+#include <avr/pgmspace.h>
+#include <avr/interrupt.h>
+#include <avr/io.h>
+#include "print.h"
+#include "audio.h"
+#include "keymap.h"
+
+#include "eeconfig.h"
+
+#define CPU_PRESCALER 8
+
+// -----------------------------------------------------------------------------
+// Timer Abstractions
+// -----------------------------------------------------------------------------
+
+// TIMSK3 - Timer/Counter #3 Interrupt Mask Register
+// Turn on/off 3A interputs, stopping/enabling the ISR calls
+#define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
+#define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
+
+// TCCR3A: Timer/Counter #3 Control Register
+// Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
+#define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
+#define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
+
+// Fast PWM Mode Controls
+#define TIMER_3_PERIOD ICR3
+#define TIMER_3_DUTY_CYCLE OCR3A
+
+// -----------------------------------------------------------------------------
+
+
+int voices = 0;
+int voice_place = 0;
+float frequency = 0;
+int volume = 0;
+long position = 0;
+
+float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
+int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
+bool sliding = false;
+
+float place = 0;
+
+uint8_t * sample;
+uint16_t sample_length = 0;
+
+bool playing_notes = false;
+bool playing_note = false;
+float note_frequency = 0;
+float note_length = 0;
+uint8_t note_tempo = TEMPO_DEFAULT;
+float note_timbre = TIMBRE_DEFAULT;
+uint16_t note_position = 0;
+float (* notes_pointer)[][2];
+uint16_t notes_count;
+bool notes_repeat;
+float notes_rest;
+bool note_resting = false;
+
+uint8_t current_note = 0;
+uint8_t rest_counter = 0;
+
+#ifdef VIBRATO_ENABLE
+float vibrato_counter = 0;
+float vibrato_strength = .5;
+float vibrato_rate = 0.125;
+#endif
+
+float polyphony_rate = 0;
+
+static bool audio_initialized = false;
+
+audio_config_t audio_config;
+
+uint16_t envelope_index = 0;
+
+void audio_init()
+{
+
+ // Check EEPROM
+ if (!eeconfig_is_enabled())
+ {
+ eeconfig_init();
+ }
+ audio_config.raw = eeconfig_read_audio();
+
+ // Set port PC6 (OC3A and /OC4A) as output
+ DDRC |= _BV(PORTC6);
+
+ DISABLE_AUDIO_COUNTER_3_ISR;
+
+ // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
+ // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
+ // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
+ // Clock Select (CS3n) = 0b010 = Clock / 8
+ TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
+ TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
+
+ audio_initialized = true;
+}
+
+void stop_all_notes()
+{
+ if (!audio_initialized) {
+ audio_init();
+ }
+ voices = 0;
+
+ DISABLE_AUDIO_COUNTER_3_ISR;
+ DISABLE_AUDIO_COUNTER_3_OUTPUT;
+
+ playing_notes = false;
+ playing_note = false;
+ frequency = 0;
+ volume = 0;
+
+ for (uint8_t i = 0; i < 8; i++)
+ {
+ frequencies[i] = 0;
+ volumes[i] = 0;
+ }
+}
+
+void stop_note(float freq)
+{
+ if (playing_note) {
+ if (!audio_initialized) {
+ audio_init();
+ }
+ for (int i = 7; i >= 0; i--) {
+ if (frequencies[i] == freq) {
+ frequencies[i] = 0;
+ volumes[i] = 0;
+ for (int j = i; (j < 7); j++) {
+ frequencies[j] = frequencies[j+1];
+ frequencies[j+1] = 0;
+ volumes[j] = volumes[j+1];
+ volumes[j+1] = 0;
+ }
+ break;
+ }
+ }
+ voices--;
+ if (voices < 0)
+ voices = 0;
+ if (voice_place >= voices) {
+ voice_place = 0;
+ }
+ if (voices == 0) {
+ DISABLE_AUDIO_COUNTER_3_ISR;
+ DISABLE_AUDIO_COUNTER_3_OUTPUT;
+ frequency = 0;
+ volume = 0;
+ playing_note = false;
+ }
+ }
+}
+
+#ifdef VIBRATO_ENABLE
+
+float mod(float a, int b)
+{
+ float r = fmod(a, b);
+ return r < 0 ? r + b : r;
+}
+
+float vibrato(float average_freq) {
+ #ifdef VIBRATO_STRENGTH_ENABLE
+ float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
+ #else
+ float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
+ #endif
+ vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
+ return vibrated_freq;
+}
+
+#endif
+
+ISR(TIMER3_COMPA_vect)
+{
+ float freq;
+
+ if (playing_note) {
+ if (voices > 0) {
+ if (polyphony_rate > 0) {
+ if (voices > 1) {
+ voice_place %= voices;
+ if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
+ voice_place = (voice_place + 1) % voices;
+ place = 0.0;
+ }
+ }
+
+ #ifdef VIBRATO_ENABLE
+ if (vibrato_strength > 0) {
+ freq = vibrato(frequencies[voice_place]);
+ } else {
+ freq = frequencies[voice_place];
+ }
+ #else
+ freq = frequencies[voice_place];
+ #endif
+ } else {
+ if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
+ frequency = frequency * pow(2, 440/frequency/12/2);
+ } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
+ frequency = frequency * pow(2, -440/frequency/12/2);
+ } else {
+ frequency = frequencies[voices - 1];
+ }
+
+ #ifdef VIBRATO_ENABLE
+ if (vibrato_strength > 0) {
+ freq = vibrato(frequency);
+ } else {
+ freq = frequency;
+ }
+ #else
+ freq = frequency;
+ #endif
+ }
+
+ if (envelope_index < 65535) {
+ envelope_index++;
+ }
+
+ freq = voice_envelope(freq);
+
+ if (freq < 30.517578125) {
+ freq = 30.52;
+ }
+
+ TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
+ TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
+ }
+ }
+
+ if (playing_notes) {
+ if (note_frequency > 0) {
+ #ifdef VIBRATO_ENABLE
+ if (vibrato_strength > 0) {
+ freq = vibrato(note_frequency);
+ } else {
+ freq = note_frequency;
+ }
+ #else
+ freq = note_frequency;
+ #endif
+
+ if (envelope_index < 65535) {
+ envelope_index++;
+ }
+ freq = voice_envelope(freq);
+
+ TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
+ TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
+ } else {
+ TIMER_3_PERIOD = 0;
+ TIMER_3_DUTY_CYCLE = 0;
+ }
+
+ note_position++;
+ bool end_of_note = false;
+ if (TIMER_3_PERIOD > 0) {
+ end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF));
+ } else {
+ end_of_note = (note_position >= (note_length * 0x7FF));
+ }
+
+ if (end_of_note) {
+ current_note++;
+ if (current_note >= notes_count) {
+ if (notes_repeat) {
+ current_note = 0;
+ } else {
+ DISABLE_AUDIO_COUNTER_3_ISR;
+ DISABLE_AUDIO_COUNTER_3_OUTPUT;
+ playing_notes = false;
+ return;
+ }
+ }
+ if (!note_resting && (notes_rest > 0)) {
+ note_resting = true;
+ note_frequency = 0;
+ note_length = notes_rest;
+ current_note--;
+ } else {
+ note_resting = false;
+ envelope_index = 0;
+ note_frequency = (*notes_pointer)[current_note][0];
+ note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
+ }
+
+ note_position = 0;
+ }
+ }
+
+ if (!audio_config.enable) {
+ playing_notes = false;
+ playing_note = false;
+ }
+}
+
+void play_note(float freq, int vol) {
+
+ if (!audio_initialized) {
+ audio_init();
+ }
+
+ if (audio_config.enable && voices < 8) {
+ DISABLE_AUDIO_COUNTER_3_ISR;
+
+ // Cancel notes if notes are playing
+ if (playing_notes)
+ stop_all_notes();
+
+ playing_note = true;
+
+ envelope_index = 0;
+
+ if (freq > 0) {
+ frequencies[voices] = freq;
+ volumes[voices] = vol;
+ voices++;
+ }
+
+ ENABLE_AUDIO_COUNTER_3_ISR;
+ ENABLE_AUDIO_COUNTER_3_OUTPUT;
+ }
+
+}
+
+void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
+{
+
+ if (!audio_initialized) {
+ audio_init();
+ }
+
+ if (audio_config.enable) {
+
+ DISABLE_AUDIO_COUNTER_3_ISR;
+
+ // Cancel note if a note is playing
+ if (playing_note)
+ stop_all_notes();
+
+ playing_notes = true;
+
+ notes_pointer = np;
+ notes_count = n_count;
+ notes_repeat = n_repeat;
+ notes_rest = n_rest;
+
+ place = 0;
+ current_note = 0;
+
+ note_frequency = (*notes_pointer)[current_note][0];
+ note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
+ note_position = 0;
+
+
+ ENABLE_AUDIO_COUNTER_3_ISR;
+ ENABLE_AUDIO_COUNTER_3_OUTPUT;
+ }
+
+}
+
+bool is_playing_notes(void) {
+ return playing_notes;
+}
+
+bool is_audio_on(void) {
+ return (audio_config.enable != 0);
+}
+
+void audio_toggle(void) {
+ audio_config.enable ^= 1;
+ eeconfig_update_audio(audio_config.raw);
+ if (audio_config.enable)
+ audio_on_user();
+}
+
+void audio_on(void) {
+ audio_config.enable = 1;
+ eeconfig_update_audio(audio_config.raw);
+ audio_on_user();
+}
+
+void audio_off(void) {
+ audio_config.enable = 0;
+ eeconfig_update_audio(audio_config.raw);
+}
+
+#ifdef VIBRATO_ENABLE
+
+// Vibrato rate functions
+
+void set_vibrato_rate(float rate) {
+ vibrato_rate = rate;
+}
+
+void increase_vibrato_rate(float change) {
+ vibrato_rate *= change;
+}
+
+void decrease_vibrato_rate(float change) {
+ vibrato_rate /= change;
+}
+
+#ifdef VIBRATO_STRENGTH_ENABLE
+
+void set_vibrato_strength(float strength) {
+ vibrato_strength = strength;
+}
+
+void increase_vibrato_strength(float change) {
+ vibrato_strength *= change;
+}
+
+void decrease_vibrato_strength(float change) {
+ vibrato_strength /= change;
+}
+
+#endif /* VIBRATO_STRENGTH_ENABLE */
+
+#endif /* VIBRATO_ENABLE */
+
+// Polyphony functions
+
+void set_polyphony_rate(float rate) {
+ polyphony_rate = rate;
+}
+
+void enable_polyphony() {
+ polyphony_rate = 5;
+}
+
+void disable_polyphony() {
+ polyphony_rate = 0;
+}
+
+void increase_polyphony_rate(float change) {
+ polyphony_rate *= change;
+}
+
+void decrease_polyphony_rate(float change) {
+ polyphony_rate /= change;
+}
+
+// Timbre function
+
+void set_timbre(float timbre) {
+ note_timbre = timbre;
+}
+
+// Tempo functions
+
+void set_tempo(uint8_t tempo) {
+ note_tempo = tempo;
+}
+
+void decrease_tempo(uint8_t tempo_change) {
+ note_tempo += tempo_change;
+}
+
+void increase_tempo(uint8_t tempo_change) {
+ if (note_tempo - tempo_change < 10) {
+ note_tempo = 10;
+ } else {
+ note_tempo -= tempo_change;
+ }
+}
diff --git a/quantum/audio/audio.h b/quantum/audio/audio.h
new file mode 100644
index 0000000000..47f326ea0a
--- /dev/null
+++ b/quantum/audio/audio.h
@@ -0,0 +1,91 @@
+#ifndef AUDIO_H
+#define AUDIO_H
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <avr/io.h>
+#include <util/delay.h>
+#include "musical_notes.h"
+#include "song_list.h"
+#include "voices.h"
+#include "quantum.h"
+
+// Largely untested PWM audio mode (doesn't sound as good)
+// #define PWM_AUDIO
+
+// #define VIBRATO_ENABLE
+
+// Enable vibrato strength/amplitude - slows down ISR too much
+// #define VIBRATO_STRENGTH_ENABLE
+
+typedef union {
+ uint8_t raw;
+ struct {
+ bool enable :1;
+ uint8_t level :7;
+ };
+} audio_config_t;
+
+bool is_audio_on(void);
+void audio_toggle(void);
+void audio_on(void);
+void audio_off(void);
+
+// Vibrato rate functions
+
+#ifdef VIBRATO_ENABLE
+
+void set_vibrato_rate(float rate);
+void increase_vibrato_rate(float change);
+void decrease_vibrato_rate(float change);
+
+#ifdef VIBRATO_STRENGTH_ENABLE
+
+void set_vibrato_strength(float strength);
+void increase_vibrato_strength(float change);
+void decrease_vibrato_strength(float change);
+
+#endif
+
+#endif
+
+// Polyphony functions
+
+void set_polyphony_rate(float rate);
+void enable_polyphony(void);
+void disable_polyphony(void);
+void increase_polyphony_rate(float change);
+void decrease_polyphony_rate(float change);
+
+void set_timbre(float timbre);
+void set_tempo(uint8_t tempo);
+
+void increase_tempo(uint8_t tempo_change);
+void decrease_tempo(uint8_t tempo_change);
+
+void audio_init(void);
+
+#ifdef PWM_AUDIO
+void play_sample(uint8_t * s, uint16_t l, bool r);
+#endif
+void play_note(float freq, int vol);
+void stop_note(float freq);
+void stop_all_notes(void);
+void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest);
+
+#define SCALE (int8_t []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \
+ 0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \
+ 0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \
+ 0 + (12*3), 2 + (12*3), 4 + (12*3), 5 + (12*3), 7 + (12*3), 9 + (12*3), 11 + (12*3), \
+ 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
+
+// These macros are used to allow play_notes to play an array of indeterminate
+// length. This works around the limitation of C's sizeof operation on pointers.
+// The global float array for the song must be used here.
+#define NOTE_ARRAY_SIZE(x) ((int16_t)(sizeof(x) / (sizeof(x[0]))))
+#define PLAY_NOTE_ARRAY(note_array, note_repeat, note_rest_style) play_notes(&note_array, NOTE_ARRAY_SIZE((note_array)), (note_repeat), (note_rest_style));
+
+
+bool is_playing_notes(void);
+
+#endif \ No newline at end of file
diff --git a/quantum/audio/audio_pwm.c b/quantum/audio/audio_pwm.c
new file mode 100644
index 0000000000..f820eec1be
--- /dev/null
+++ b/quantum/audio/audio_pwm.c
@@ -0,0 +1,643 @@
+#include <stdio.h>
+#include <string.h>
+//#include <math.h>
+#include <avr/pgmspace.h>
+#include <avr/interrupt.h>
+#include <avr/io.h>
+#include "print.h"
+#include "audio.h"
+#include "keymap.h"
+
+#include "eeconfig.h"
+
+#define PI 3.14159265
+
+#define CPU_PRESCALER 8
+
+
+// Timer Abstractions
+
+// TIMSK3 - Timer/Counter #3 Interrupt Mask Register
+// Turn on/off 3A interputs, stopping/enabling the ISR calls
+#define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
+#define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
+
+
+// TCCR3A: Timer/Counter #3 Control Register
+// Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
+#define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
+#define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
+
+
+#define NOTE_PERIOD ICR3
+#define NOTE_DUTY_CYCLE OCR3A
+
+
+#ifdef PWM_AUDIO
+ #include "wave.h"
+ #define SAMPLE_DIVIDER 39
+ #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
+ // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
+
+ float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
+ uint16_t place_int = 0;
+ bool repeat = true;
+#endif
+
+void delay_us(int count) {
+ while(count--) {
+ _delay_us(1);
+ }
+}
+
+int voices = 0;
+int voice_place = 0;
+float frequency = 0;
+int volume = 0;
+long position = 0;
+
+float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
+int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
+bool sliding = false;
+
+float place = 0;
+
+uint8_t * sample;
+uint16_t sample_length = 0;
+// float freq = 0;
+
+bool playing_notes = false;
+bool playing_note = false;
+float note_frequency = 0;
+float note_length = 0;
+uint8_t note_tempo = TEMPO_DEFAULT;
+float note_timbre = TIMBRE_DEFAULT;
+uint16_t note_position = 0;
+float (* notes_pointer)[][2];
+uint16_t notes_count;
+bool notes_repeat;
+float notes_rest;
+bool note_resting = false;
+
+uint8_t current_note = 0;
+uint8_t rest_counter = 0;
+
+#ifdef VIBRATO_ENABLE
+float vibrato_counter = 0;
+float vibrato_strength = .5;
+float vibrato_rate = 0.125;
+#endif
+
+float polyphony_rate = 0;
+
+static bool audio_initialized = false;
+
+audio_config_t audio_config;
+
+uint16_t envelope_index = 0;
+
+void audio_init() {
+
+ // Check EEPROM
+ if (!eeconfig_is_enabled())
+ {
+ eeconfig_init();
+ }
+ audio_config.raw = eeconfig_read_audio();
+
+ #ifdef PWM_AUDIO
+
+ PLLFRQ = _BV(PDIV2);
+ PLLCSR = _BV(PLLE);
+ while(!(PLLCSR & _BV(PLOCK)));
+ PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
+
+ /* Init a fast PWM on Timer4 */
+ TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
+ TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
+ OCR4A = 0;
+
+ /* Enable the OC4A output */
+ DDRC |= _BV(PORTC6);
+
+ DISABLE_AUDIO_COUNTER_3_ISR; // Turn off 3A interputs
+
+ TCCR3A = 0x0; // Options not needed
+ TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
+ OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
+
+ #else
+
+ // Set port PC6 (OC3A and /OC4A) as output
+ DDRC |= _BV(PORTC6);
+
+ DISABLE_AUDIO_COUNTER_3_ISR;
+
+ // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
+ // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
+ // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
+ // Clock Select (CS3n) = 0b010 = Clock / 8
+ TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
+ TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
+
+ #endif
+
+ audio_initialized = true;
+}
+
+void stop_all_notes() {
+ if (!audio_initialized) {
+ audio_init();
+ }
+ voices = 0;
+ #ifdef PWM_AUDIO
+ DISABLE_AUDIO_COUNTER_3_ISR;
+ #else
+ DISABLE_AUDIO_COUNTER_3_ISR;
+ DISABLE_AUDIO_COUNTER_3_OUTPUT;
+ #endif
+
+ playing_notes = false;
+ playing_note = false;
+ frequency = 0;
+ volume = 0;
+
+ for (uint8_t i = 0; i < 8; i++)
+ {
+ frequencies[i] = 0;
+ volumes[i] = 0;
+ }
+}
+
+void stop_note(float freq)
+{
+ if (playing_note) {
+ if (!audio_initialized) {
+ audio_init();
+ }
+ #ifdef PWM_AUDIO
+ freq = freq / SAMPLE_RATE;
+ #endif
+ for (int i = 7; i >= 0; i--) {
+ if (frequencies[i] == freq) {
+ frequencies[i] = 0;
+ volumes[i] = 0;
+ for (int j = i; (j < 7); j++) {
+ frequencies[j] = frequencies[j+1];
+ frequencies[j+1] = 0;
+ volumes[j] = volumes[j+1];
+ volumes[j+1] = 0;
+ }
+ break;
+ }
+ }
+ voices--;
+ if (voices < 0)
+ voices = 0;
+ if (voice_place >= voices) {
+ voice_place = 0;
+ }
+ if (voices == 0) {
+ #ifdef PWM_AUDIO
+ DISABLE_AUDIO_COUNTER_3_ISR;
+ #else
+ DISABLE_AUDIO_COUNTER_3_ISR;
+ DISABLE_AUDIO_COUNTER_3_OUTPUT;
+ #endif
+ frequency = 0;
+ volume = 0;
+ playing_note = false;
+ }
+ }
+}
+
+#ifdef VIBRATO_ENABLE
+
+float mod(float a, int b)
+{
+ float r = fmod(a, b);
+ return r < 0 ? r + b : r;
+}
+
+float vibrato(float average_freq) {
+ #ifdef VIBRATO_STRENGTH_ENABLE
+ float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
+ #else
+ float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
+ #endif
+ vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
+ return vibrated_freq;
+}
+
+#endif
+
+ISR(TIMER3_COMPA_vect)
+{
+ if (playing_note) {
+ #ifdef PWM_AUDIO
+ if (voices == 1) {
+ // SINE
+ OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
+
+ // SQUARE
+ // if (((int)place) >= 1024){
+ // OCR4A = 0xFF >> 2;
+ // } else {
+ // OCR4A = 0x00;
+ // }
+
+ // SAWTOOTH
+ // OCR4A = (int)place / 4;
+
+ // TRIANGLE
+ // if (((int)place) >= 1024) {
+ // OCR4A = (int)place / 2;
+ // } else {
+ // OCR4A = 2048 - (int)place / 2;
+ // }
+
+ place += frequency;
+
+ if (place >= SINE_LENGTH)
+ place -= SINE_LENGTH;
+
+ } else {
+ int sum = 0;
+ for (int i = 0; i < voices; i++) {
+ // SINE
+ sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
+
+ // SQUARE
+ // if (((int)places[i]) >= 1024){
+ // sum += 0xFF >> 2;
+ // } else {
+ // sum += 0x00;
+ // }
+
+ places[i] += frequencies[i];
+
+ if (places[i] >= SINE_LENGTH)
+ places[i] -= SINE_LENGTH;
+ }
+ OCR4A = sum;
+ }
+ #else
+ if (voices > 0) {
+ float freq;
+ if (polyphony_rate > 0) {
+ if (voices > 1) {
+ voice_place %= voices;
+ if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
+ voice_place = (voice_place + 1) % voices;
+ place = 0.0;
+ }
+ }
+ #ifdef VIBRATO_ENABLE
+ if (vibrato_strength > 0) {
+ freq = vibrato(frequencies[voice_place]);
+ } else {
+ #else
+ {
+ #endif
+ freq = frequencies[voice_place];
+ }
+ } else {
+ if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
+ frequency = frequency * pow(2, 440/frequency/12/2);
+ } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
+ frequency = frequency * pow(2, -440/frequency/12/2);
+ } else {
+ frequency = frequencies[voices - 1];
+ }
+
+
+ #ifdef VIBRATO_ENABLE
+ if (vibrato_strength > 0) {
+ freq = vibrato(frequency);
+ } else {
+ #else
+ {
+ #endif
+ freq = frequency;
+ }
+ }
+
+ if (envelope_index < 65535) {
+ envelope_index++;
+ }
+ freq = voice_envelope(freq);
+
+ if (freq < 30.517578125)
+ freq = 30.52;
+ NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
+ NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
+ }
+ #endif
+ }
+
+ // SAMPLE
+ // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
+
+ // place_int++;
+
+ // if (place_int >= sample_length)
+ // if (repeat)
+ // place_int -= sample_length;
+ // else
+ // DISABLE_AUDIO_COUNTER_3_ISR;
+
+
+ if (playing_notes) {
+ #ifdef PWM_AUDIO
+ OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
+
+ place += note_frequency;
+ if (place >= SINE_LENGTH)
+ place -= SINE_LENGTH;
+ #else
+ if (note_frequency > 0) {
+ float freq;
+
+ #ifdef VIBRATO_ENABLE
+ if (vibrato_strength > 0) {
+ freq = vibrato(note_frequency);
+ } else {
+ #else
+ {
+ #endif
+ freq = note_frequency;
+ }
+
+ if (envelope_index < 65535) {
+ envelope_index++;
+ }
+ freq = voice_envelope(freq);
+
+ NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
+ NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
+ } else {
+ NOTE_PERIOD = 0;
+ NOTE_DUTY_CYCLE = 0;
+ }
+ #endif
+
+
+ note_position++;
+ bool end_of_note = false;
+ if (NOTE_PERIOD > 0)
+ end_of_note = (note_position >= (note_length / NOTE_PERIOD * 0xFFFF));
+ else
+ end_of_note = (note_position >= (note_length * 0x7FF));
+ if (end_of_note) {
+ current_note++;
+ if (current_note >= notes_count) {
+ if (notes_repeat) {
+ current_note = 0;
+ } else {
+ #ifdef PWM_AUDIO
+ DISABLE_AUDIO_COUNTER_3_ISR;
+ #else
+ DISABLE_AUDIO_COUNTER_3_ISR;
+ DISABLE_AUDIO_COUNTER_3_OUTPUT;
+ #endif
+ playing_notes = false;
+ return;
+ }
+ }
+ if (!note_resting && (notes_rest > 0)) {
+ note_resting = true;
+ note_frequency = 0;
+ note_length = notes_rest;
+ current_note--;
+ } else {
+ note_resting = false;
+ #ifdef PWM_AUDIO
+ note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
+ note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
+ #else
+ envelope_index = 0;
+ note_frequency = (*notes_pointer)[current_note][0];
+ note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
+ #endif
+ }
+ note_position = 0;
+ }
+
+ }
+
+ if (!audio_config.enable) {
+ playing_notes = false;
+ playing_note = false;
+ }
+}
+
+void play_note(float freq, int vol) {
+
+ if (!audio_initialized) {
+ audio_init();
+ }
+
+ if (audio_config.enable && voices < 8) {
+ DISABLE_AUDIO_COUNTER_3_ISR;
+
+ // Cancel notes if notes are playing
+ if (playing_notes)
+ stop_all_notes();
+
+ playing_note = true;
+
+ envelope_index = 0;
+
+ #ifdef PWM_AUDIO
+ freq = freq / SAMPLE_RATE;
+ #endif
+ if (freq > 0) {
+ frequencies[voices] = freq;
+ volumes[voices] = vol;
+ voices++;
+ }
+
+ #ifdef PWM_AUDIO
+ ENABLE_AUDIO_COUNTER_3_ISR;
+ #else
+ ENABLE_AUDIO_COUNTER_3_ISR;
+ ENABLE_AUDIO_COUNTER_3_OUTPUT;
+ #endif
+ }
+
+}
+
+void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
+{
+
+ if (!audio_initialized) {
+ audio_init();
+ }
+
+ if (audio_config.enable) {
+
+ DISABLE_AUDIO_COUNTER_3_ISR;
+
+ // Cancel note if a note is playing
+ if (playing_note)
+ stop_all_notes();
+
+ playing_notes = true;
+
+ notes_pointer = np;
+ notes_count = n_count;
+ notes_repeat = n_repeat;
+ notes_rest = n_rest;
+
+ place = 0;
+ current_note = 0;
+
+ #ifdef PWM_AUDIO
+ note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
+ note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
+ #else
+ note_frequency = (*notes_pointer)[current_note][0];
+ note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
+ #endif
+ note_position = 0;
+
+
+ #ifdef PWM_AUDIO
+ ENABLE_AUDIO_COUNTER_3_ISR;
+ #else
+ ENABLE_AUDIO_COUNTER_3_ISR;
+ ENABLE_AUDIO_COUNTER_3_OUTPUT;
+ #endif
+ }
+
+}
+
+#ifdef PWM_AUDIO
+void play_sample(uint8_t * s, uint16_t l, bool r) {
+ if (!audio_initialized) {
+ audio_init();
+ }
+
+ if (audio_config.enable) {
+ DISABLE_AUDIO_COUNTER_3_ISR;
+ stop_all_notes();
+ place_int = 0;
+ sample = s;
+ sample_length = l;
+ repeat = r;
+
+ ENABLE_AUDIO_COUNTER_3_ISR;
+ }
+}
+#endif
+
+
+void audio_toggle(void) {
+ audio_config.enable ^= 1;
+ eeconfig_update_audio(audio_config.raw);
+}
+
+void audio_on(void) {
+ audio_config.enable = 1;
+ eeconfig_update_audio(audio_config.raw);
+}
+
+void audio_off(void) {
+ audio_config.enable = 0;
+ eeconfig_update_audio(audio_config.raw);
+}
+
+#ifdef VIBRATO_ENABLE
+
+// Vibrato rate functions
+
+void set_vibrato_rate(float rate) {
+ vibrato_rate = rate;
+}
+
+void increase_vibrato_rate(float change) {
+ vibrato_rate *= change;
+}
+
+void decrease_vibrato_rate(float change) {
+ vibrato_rate /= change;
+}
+
+#ifdef VIBRATO_STRENGTH_ENABLE
+
+void set_vibrato_strength(float strength) {
+ vibrato_strength = strength;
+}
+
+void increase_vibrato_strength(float change) {
+ vibrato_strength *= change;
+}
+
+void decrease_vibrato_strength(float change) {
+ vibrato_strength /= change;
+}
+
+#endif /* VIBRATO_STRENGTH_ENABLE */
+
+#endif /* VIBRATO_ENABLE */
+
+// Polyphony functions
+
+void set_polyphony_rate(float rate) {
+ polyphony_rate = rate;
+}
+
+void enable_polyphony() {
+ polyphony_rate = 5;
+}
+
+void disable_polyphony() {
+ polyphony_rate = 0;
+}
+
+void increase_polyphony_rate(float change) {
+ polyphony_rate *= change;
+}
+
+void decrease_polyphony_rate(float change) {
+ polyphony_rate /= change;
+}
+
+// Timbre function
+
+void set_timbre(float timbre) {
+ note_timbre = timbre;
+}
+
+// Tempo functions
+
+void set_tempo(uint8_t tempo) {
+ note_tempo = tempo;
+}
+
+void decrease_tempo(uint8_t tempo_change) {
+ note_tempo += tempo_change;
+}
+
+void increase_tempo(uint8_t tempo_change) {
+ if (note_tempo - tempo_change < 10) {
+ note_tempo = 10;
+ } else {
+ note_tempo -= tempo_change;
+ }
+}
+
+
+//------------------------------------------------------------------------------
+// Override these functions in your keymap file to play different tunes on
+// startup and bootloader jump
+__attribute__ ((weak))
+void play_startup_tone()
+{
+}
+
+__attribute__ ((weak))
+void play_goodbye_tone()
+{
+}
+//------------------------------------------------------------------------------
diff --git a/quantum/audio/luts.c b/quantum/audio/luts.c
new file mode 100644
index 0000000000..9f3de9a05c
--- /dev/null
+++ b/quantum/audio/luts.c
@@ -0,0 +1,382 @@
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <avr/pgmspace.h>
+#include "luts.h"
+
+const float vibrato_lut[VIBRATO_LUT_LENGTH] =
+{
+ 1.0022336811487,
+ 1.0042529943610,
+ 1.0058584256028,
+ 1.0068905285205,
+ 1.0072464122237,
+ 1.0068905285205,
+ 1.0058584256028,
+ 1.0042529943610,
+ 1.0022336811487,
+ 1.0000000000000,
+ 0.9977712970630,
+ 0.9957650169978,
+ 0.9941756956510,
+ 0.9931566259436,
+ 0.9928057204913,
+ 0.9931566259436,
+ 0.9941756956510,
+ 0.9957650169978,
+ 0.9977712970630,
+ 1.0000000000000,
+};
+
+const uint16_t frequency_lut[FREQUENCY_LUT_LENGTH] =
+{
+ 0x8E0B,
+ 0x8C02,
+ 0x8A00,
+ 0x8805,
+ 0x8612,
+ 0x8426,
+ 0x8241,
+ 0x8063,
+ 0x7E8C,
+ 0x7CBB,
+ 0x7AF2,
+ 0x792E,
+ 0x7772,
+ 0x75BB,
+ 0x740B,
+ 0x7261,
+ 0x70BD,
+ 0x6F20,
+ 0x6D88,
+ 0x6BF6,
+ 0x6A69,
+ 0x68E3,
+ 0x6762,
+ 0x65E6,
+ 0x6470,
+ 0x6300,
+ 0x6194,
+ 0x602E,
+ 0x5ECD,
+ 0x5D71,
+ 0x5C1A,
+ 0x5AC8,
+ 0x597B,
+ 0x5833,
+ 0x56EF,
+ 0x55B0,
+ 0x5475,
+ 0x533F,
+ 0x520E,
+ 0x50E1,
+ 0x4FB8,
+ 0x4E93,
+ 0x4D73,
+ 0x4C57,
+ 0x4B3E,
+ 0x4A2A,
+ 0x491A,
+ 0x480E,
+ 0x4705,
+ 0x4601,
+ 0x4500,
+ 0x4402,
+ 0x4309,
+ 0x4213,
+ 0x4120,
+ 0x4031,
+ 0x3F46,
+ 0x3E5D,
+ 0x3D79,
+ 0x3C97,
+ 0x3BB9,
+ 0x3ADD,
+ 0x3A05,
+ 0x3930,
+ 0x385E,
+ 0x3790,
+ 0x36C4,
+ 0x35FB,
+ 0x3534,
+ 0x3471,
+ 0x33B1,
+ 0x32F3,
+ 0x3238,
+ 0x3180,
+ 0x30CA,
+ 0x3017,
+ 0x2F66,
+ 0x2EB8,
+ 0x2E0D,
+ 0x2D64,
+ 0x2CBD,
+ 0x2C19,
+ 0x2B77,
+ 0x2AD8,
+ 0x2A3A,
+ 0x299F,
+ 0x2907,
+ 0x2870,
+ 0x27DC,
+ 0x2749,
+ 0x26B9,
+ 0x262B,
+ 0x259F,
+ 0x2515,
+ 0x248D,
+ 0x2407,
+ 0x2382,
+ 0x2300,
+ 0x2280,
+ 0x2201,
+ 0x2184,
+ 0x2109,
+ 0x2090,
+ 0x2018,
+ 0x1FA3,
+ 0x1F2E,
+ 0x1EBC,
+ 0x1E4B,
+ 0x1DDC,
+ 0x1D6E,
+ 0x1D02,
+ 0x1C98,
+ 0x1C2F,
+ 0x1BC8,
+ 0x1B62,
+ 0x1AFD,
+ 0x1A9A,
+ 0x1A38,
+ 0x19D8,
+ 0x1979,
+ 0x191C,
+ 0x18C0,
+ 0x1865,
+ 0x180B,
+ 0x17B3,
+ 0x175C,
+ 0x1706,
+ 0x16B2,
+ 0x165E,
+ 0x160C,
+ 0x15BB,
+ 0x156C,
+ 0x151D,
+ 0x14CF,
+ 0x1483,
+ 0x1438,
+ 0x13EE,
+ 0x13A4,
+ 0x135C,
+ 0x1315,
+ 0x12CF,
+ 0x128A,
+ 0x1246,
+ 0x1203,
+ 0x11C1,
+ 0x1180,
+ 0x1140,
+ 0x1100,
+ 0x10C2,
+ 0x1084,
+ 0x1048,
+ 0x100C,
+ 0xFD1,
+ 0xF97,
+ 0xF5E,
+ 0xF25,
+ 0xEEE,
+ 0xEB7,
+ 0xE81,
+ 0xE4C,
+ 0xE17,
+ 0xDE4,
+ 0xDB1,
+ 0xD7E,
+ 0xD4D,
+ 0xD1C,
+ 0xCEC,
+ 0xCBC,
+ 0xC8E,
+ 0xC60,
+ 0xC32,
+ 0xC05,
+ 0xBD9,
+ 0xBAE,
+ 0xB83,
+ 0xB59,
+ 0xB2F,
+ 0xB06,
+ 0xADD,
+ 0xAB6,
+ 0xA8E,
+ 0xA67,
+ 0xA41,
+ 0xA1C,
+ 0x9F7,
+ 0x9D2,
+ 0x9AE,
+ 0x98A,
+ 0x967,
+ 0x945,
+ 0x923,
+ 0x901,
+ 0x8E0,
+ 0x8C0,
+ 0x8A0,
+ 0x880,
+ 0x861,
+ 0x842,
+ 0x824,
+ 0x806,
+ 0x7E8,
+ 0x7CB,
+ 0x7AF,
+ 0x792,
+ 0x777,
+ 0x75B,
+ 0x740,
+ 0x726,
+ 0x70B,
+ 0x6F2,
+ 0x6D8,
+ 0x6BF,
+ 0x6A6,
+ 0x68E,
+ 0x676,
+ 0x65E,
+ 0x647,
+ 0x630,
+ 0x619,
+ 0x602,
+ 0x5EC,
+ 0x5D7,
+ 0x5C1,
+ 0x5AC,
+ 0x597,
+ 0x583,
+ 0x56E,
+ 0x55B,
+ 0x547,
+ 0x533,
+ 0x520,
+ 0x50E,
+ 0x4FB,
+ 0x4E9,
+ 0x4D7,
+ 0x4C5,
+ 0x4B3,
+ 0x4A2,
+ 0x491,
+ 0x480,
+ 0x470,
+ 0x460,
+ 0x450,
+ 0x440,
+ 0x430,
+ 0x421,
+ 0x412,
+ 0x403,
+ 0x3F4,
+ 0x3E5,
+ 0x3D7,
+ 0x3C9,
+ 0x3BB,
+ 0x3AD,
+ 0x3A0,
+ 0x393,
+ 0x385,
+ 0x379,
+ 0x36C,
+ 0x35F,
+ 0x353,
+ 0x347,
+ 0x33B,
+ 0x32F,
+ 0x323,
+ 0x318,
+ 0x30C,
+ 0x301,
+ 0x2F6,
+ 0x2EB,
+ 0x2E0,
+ 0x2D6,
+ 0x2CB,
+ 0x2C1,
+ 0x2B7,
+ 0x2AD,
+ 0x2A3,
+ 0x299,
+ 0x290,
+ 0x287,
+ 0x27D,
+ 0x274,
+ 0x26B,
+ 0x262,
+ 0x259,
+ 0x251,
+ 0x248,
+ 0x240,
+ 0x238,
+ 0x230,
+ 0x228,
+ 0x220,
+ 0x218,
+ 0x210,
+ 0x209,
+ 0x201,
+ 0x1FA,
+ 0x1F2,
+ 0x1EB,
+ 0x1E4,
+ 0x1DD,
+ 0x1D6,
+ 0x1D0,
+ 0x1C9,
+ 0x1C2,
+ 0x1BC,
+ 0x1B6,
+ 0x1AF,
+ 0x1A9,
+ 0x1A3,
+ 0x19D,
+ 0x197,
+ 0x191,
+ 0x18C,
+ 0x186,
+ 0x180,
+ 0x17B,
+ 0x175,
+ 0x170,
+ 0x16B,
+ 0x165,
+ 0x160,
+ 0x15B,
+ 0x156,
+ 0x151,
+ 0x14C,
+ 0x148,
+ 0x143,
+ 0x13E,
+ 0x13A,
+ 0x135,
+ 0x131,
+ 0x12C,
+ 0x128,
+ 0x124,
+ 0x120,
+ 0x11C,
+ 0x118,
+ 0x114,
+ 0x110,
+ 0x10C,
+ 0x108,
+ 0x104,
+ 0x100,
+ 0xFD,
+ 0xF9,
+ 0xF5,
+ 0xF2,
+ 0xEE,
+};
+
diff --git a/quantum/audio/luts.h b/quantum/audio/luts.h
new file mode 100644
index 0000000000..7df3078a7f
--- /dev/null
+++ b/quantum/audio/luts.h
@@ -0,0 +1,15 @@
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <avr/pgmspace.h>
+
+#ifndef LUTS_H
+#define LUTS_H
+
+#define VIBRATO_LUT_LENGTH 20
+
+#define FREQUENCY_LUT_LENGTH 349
+
+extern const float vibrato_lut[VIBRATO_LUT_LENGTH];
+extern const uint16_t frequency_lut[FREQUENCY_LUT_LENGTH];
+
+#endif /* LUTS_H */ \ No newline at end of file
diff --git a/quantum/audio/musical_notes.h b/quantum/audio/musical_notes.h
new file mode 100644
index 0000000000..b08d16a6fa
--- /dev/null
+++ b/quantum/audio/musical_notes.h
@@ -0,0 +1,217 @@
+#ifndef MUSICAL_NOTES_H
+#define MUSICAL_NOTES_H
+
+// Tempo Placeholder
+#define TEMPO_DEFAULT 100
+
+
+#define SONG(notes...) { notes }
+
+
+// Note Types
+#define MUSICAL_NOTE(note, duration) {(NOTE##note), duration}
+#define WHOLE_NOTE(note) MUSICAL_NOTE(note, 64)
+#define HALF_NOTE(note) MUSICAL_NOTE(note, 32)
+#define QUARTER_NOTE(note) MUSICAL_NOTE(note, 16)
+#define EIGHTH_NOTE(note) MUSICAL_NOTE(note, 8)
+#define SIXTEENTH_NOTE(note) MUSICAL_NOTE(note, 4)
+
+#define WHOLE_DOT_NOTE(note) MUSICAL_NOTE(note, 64+32)
+#define HALF_DOT_NOTE(note) MUSICAL_NOTE(note, 32+16)
+#define QUARTER_DOT_NOTE(note) MUSICAL_NOTE(note, 16+8)
+#define EIGHTH_DOT_NOTE(note) MUSICAL_NOTE(note, 8+4)
+#define SIXTEENTH_DOT_NOTE(note) MUSICAL_NOTE(note, 4+2)
+
+// Note Type Shortcuts
+#define M__NOTE(note, duration) MUSICAL_NOTE(note, duration)
+#define W__NOTE(n) WHOLE_NOTE(n)
+#define H__NOTE(n) HALF_NOTE(n)
+#define Q__NOTE(n) QUARTER_NOTE(n)
+#define E__NOTE(n) EIGHTH_NOTE(n)
+#define S__NOTE(n) SIXTEENTH_NOTE(n)
+#define WD_NOTE(n) WHOLE_DOT_NOTE(n)
+#define HD_NOTE(n) HALF_DOT_NOTE(n)
+#define QD_NOTE(n) QUARTER_DOT_NOTE(n)
+#define ED_NOTE(n) EIGHTH_DOT_NOTE(n)
+#define SD_NOTE(n) SIXTEENTH_DOT_NOTE(n)
+
+// Note Styles
+// Staccato makes sure there is a rest between each note. Think: TA TA TA
+// Legato makes notes flow together. Think: TAAA
+#define STACCATO 0.01
+#define LEGATO 0
+
+// Note Timbre
+// Changes how the notes sound
+#define TIMBRE_12 0.125
+#define TIMBRE_25 0.250
+#define TIMBRE_50 0.500
+#define TIMBRE_75 0.750
+#define TIMBRE_DEFAULT TIMBRE_50
+
+
+// Notes - # = Octave
+
+#define NOTE_REST 0.00
+
+/* These notes are currently bugged
+#define NOTE_C0 16.35
+#define NOTE_CS0 17.32
+#define NOTE_D0 18.35
+#define NOTE_DS0 19.45
+#define NOTE_E0 20.60
+#define NOTE_F0 21.83
+#define NOTE_FS0 23.12
+#define NOTE_G0 24.50
+#define NOTE_GS0 25.96
+#define NOTE_A0 27.50
+#define NOTE_AS0 29.14
+#define NOTE_B0 30.87
+#define NOTE_C1 32.70
+#define NOTE_CS1 34.65
+#define NOTE_D1 36.71
+#define NOTE_DS1 38.89
+#define NOTE_E1 41.20
+#define NOTE_F1 43.65
+#define NOTE_FS1 46.25
+#define NOTE_G1 49.00
+#define NOTE_GS1 51.91
+#define NOTE_A1 55.00
+#define NOTE_AS1 58.27
+*/
+
+#define NOTE_B1 61.74
+#define NOTE_C2 65.41
+#define NOTE_CS2 69.30
+#define NOTE_D2 73.42
+#define NOTE_DS2 77.78
+#define NOTE_E2 82.41
+#define NOTE_F2 87.31
+#define NOTE_FS2 92.50
+#define NOTE_G2 98.00
+#define NOTE_GS2 103.83
+#define NOTE_A2 110.00
+#define NOTE_AS2 116.54
+#define NOTE_B2 123.47
+#define NOTE_C3 130.81
+#define NOTE_CS3 138.59
+#define NOTE_D3 146.83
+#define NOTE_DS3 155.56
+#define NOTE_E3 164.81
+#define NOTE_F3 174.61
+#define NOTE_FS3 185.00
+#define NOTE_G3 196.00
+#define NOTE_GS3 207.65
+#define NOTE_A3 220.00
+#define NOTE_AS3 233.08
+#define NOTE_B3 246.94
+#define NOTE_C4 261.63
+#define NOTE_CS4 277.18
+#define NOTE_D4 293.66
+#define NOTE_DS4 311.13
+#define NOTE_E4 329.63
+#define NOTE_F4 349.23
+#define NOTE_FS4 369.99
+#define NOTE_G4 392.00
+#define NOTE_GS4 415.30
+#define NOTE_A4 440.00
+#define NOTE_AS4 466.16
+#define NOTE_B4 493.88
+#define NOTE_C5 523.25
+#define NOTE_CS5 554.37
+#define NOTE_D5 587.33
+#define NOTE_DS5 622.25
+#define NOTE_E5 659.26
+#define NOTE_F5 698.46
+#define NOTE_FS5 739.99
+#define NOTE_G5 783.99
+#define NOTE_GS5 830.61
+#define NOTE_A5 880.00
+#define NOTE_AS5 932.33
+#define NOTE_B5 987.77
+#define NOTE_C6 1046.50
+#define NOTE_CS6 1108.73
+#define NOTE_D6 1174.66
+#define NOTE_DS6 1244.51
+#define NOTE_E6 1318.51
+#define NOTE_F6 1396.91
+#define NOTE_FS6 1479.98
+#define NOTE_G6 1567.98
+#define NOTE_GS6 1661.22
+#define NOTE_A6 1760.00
+#define NOTE_AS6 1864.66
+#define NOTE_B6 1975.53
+#define NOTE_C7 2093.00
+#define NOTE_CS7 2217.46
+#define NOTE_D7 2349.32
+#define NOTE_DS7 2489.02
+#define NOTE_E7 2637.02
+#define NOTE_F7 2793.83
+#define NOTE_FS7 2959.96
+#define NOTE_G7 3135.96
+#define NOTE_GS7 3322.44
+#define NOTE_A7 3520.00
+#define NOTE_AS7 3729.31
+#define NOTE_B7 3951.07
+#define NOTE_C8 4186.01
+#define NOTE_CS8 4434.92
+#define NOTE_D8 4698.64
+#define NOTE_DS8 4978.03
+#define NOTE_E8 5274.04
+#define NOTE_F8 5587.65
+#define NOTE_FS8 5919.91
+#define NOTE_G8 6271.93
+#define NOTE_GS8 6644.88
+#define NOTE_A8 7040.00
+#define NOTE_AS8 7458.62
+#define NOTE_B8 7902.13
+
+// Flat Aliases
+#define NOTE_DF0 NOTE_CS0
+#define NOTE_EF0 NOTE_DS0
+#define NOTE_GF0 NOTE_FS0
+#define NOTE_AF0 NOTE_GS0
+#define NOTE_BF0 NOTE_AS0
+#define NOTE_DF1 NOTE_CS1
+#define NOTE_EF1 NOTE_DS1
+#define NOTE_GF1 NOTE_FS1
+#define NOTE_AF1 NOTE_GS1
+#define NOTE_BF1 NOTE_AS1
+#define NOTE_DF2 NOTE_CS2
+#define NOTE_EF2 NOTE_DS2
+#define NOTE_GF2 NOTE_FS2
+#define NOTE_AF2 NOTE_GS2
+#define NOTE_BF2 NOTE_AS2
+#define NOTE_DF3 NOTE_CS3
+#define NOTE_EF3 NOTE_DS3
+#define NOTE_GF3 NOTE_FS3
+#define NOTE_AF3 NOTE_GS3
+#define NOTE_BF3 NOTE_AS3
+#define NOTE_DF4 NOTE_CS4
+#define NOTE_EF4 NOTE_DS4
+#define NOTE_GF4 NOTE_FS4
+#define NOTE_AF4 NOTE_GS4
+#define NOTE_BF4 NOTE_AS4
+#define NOTE_DF5 NOTE_CS5
+#define NOTE_EF5 NOTE_DS5
+#define NOTE_GF5 NOTE_FS5
+#define NOTE_AF5 NOTE_GS5
+#define NOTE_BF5 NOTE_AS5
+#define NOTE_DF6 NOTE_CS6
+#define NOTE_EF6 NOTE_DS6
+#define NOTE_GF6 NOTE_FS6
+#define NOTE_AF6 NOTE_GS6
+#define NOTE_BF6 NOTE_AS6
+#define NOTE_DF7 NOTE_CS7
+#define NOTE_EF7 NOTE_DS7
+#define NOTE_GF7 NOTE_FS7
+#define NOTE_AF7 NOTE_GS7
+#define NOTE_BF7 NOTE_AS7
+#define NOTE_DF8 NOTE_CS8
+#define NOTE_EF8 NOTE_DS8
+#define NOTE_GF8 NOTE_FS8
+#define NOTE_AF8 NOTE_GS8
+#define NOTE_BF8 NOTE_AS8
+
+
+#endif \ No newline at end of file
diff --git a/quantum/audio/song_list.h b/quantum/audio/song_list.h
new file mode 100644
index 0000000000..8022ca6729
--- /dev/null
+++ b/quantum/audio/song_list.h
@@ -0,0 +1,125 @@
+#include "musical_notes.h"
+
+#ifndef SONG_LIST_H
+#define SONG_LIST_H
+
+#define ODE_TO_JOY \
+ Q__NOTE(_E4), Q__NOTE(_E4), Q__NOTE(_F4), Q__NOTE(_G4), \
+ Q__NOTE(_G4), Q__NOTE(_F4), Q__NOTE(_E4), Q__NOTE(_D4), \
+ Q__NOTE(_C4), Q__NOTE(_C4), Q__NOTE(_D4), Q__NOTE(_E4), \
+ QD_NOTE(_E4), E__NOTE(_D4), H__NOTE(_D4),
+
+#define ROCK_A_BYE_BABY \
+ QD_NOTE(_B4), E__NOTE(_D4), Q__NOTE(_B5), \
+ H__NOTE(_A5), Q__NOTE(_G5), \
+ QD_NOTE(_B4), E__NOTE(_D5), Q__NOTE(_G5), \
+ H__NOTE(_FS5),
+
+#define CLOSE_ENCOUNTERS_5_NOTE \
+ Q__NOTE(_D5), \
+ Q__NOTE(_E5), \
+ Q__NOTE(_C5), \
+ Q__NOTE(_C4), \
+ Q__NOTE(_G4),
+
+#define DOE_A_DEER \
+ QD_NOTE(_C4), E__NOTE(_D4), \
+ QD_NOTE(_E4), E__NOTE(_C4), \
+ Q__NOTE(_E4), Q__NOTE(_C4), \
+ Q__NOTE(_E4),
+
+/* Requires: PLAY_NOTE_ARRAY(..., ..., STACCATO); */
+#define IN_LIKE_FLINT \
+ E__NOTE(_AS4), E__NOTE(_AS4), QD_NOTE(_B4), \
+ E__NOTE(_AS4), E__NOTE(_B4), QD_NOTE(_CS4), \
+ E__NOTE(_B4), E__NOTE(_CS4), QD_NOTE(_DS4), \
+ E__NOTE(_CS4), E__NOTE(_B4), QD_NOTE(_AS4), \
+ E__NOTE(_AS4), E__NOTE(_AS4), QD_NOTE(_B4),
+
+#define GOODBYE_SOUND \
+ E__NOTE(_E7), \
+ E__NOTE(_A6), \
+ ED_NOTE(_E6),
+
+#define STARTUP_SOUND \
+ ED_NOTE(_E7 ), \
+ E__NOTE(_CS7), \
+ E__NOTE(_E6 ), \
+ E__NOTE(_A6 ), \
+ M__NOTE(_CS7, 20),
+
+#define QWERTY_SOUND \
+ E__NOTE(_GS6 ), \
+ E__NOTE(_A6 ), \
+ S__NOTE(_REST), \
+ Q__NOTE(_E7 ),
+
+#define COLEMAK_SOUND \
+ E__NOTE(_GS6 ), \
+ E__NOTE(_A6 ), \
+ S__NOTE(_REST), \
+ ED_NOTE(_E7 ), \
+ S__NOTE(_REST), \
+ ED_NOTE(_GS7 ),
+
+#define DVORAK_SOUND \
+ E__NOTE(_GS6 ), \
+ E__NOTE(_A6 ), \
+ S__NOTE(_REST), \
+ E__NOTE(_E7 ), \
+ S__NOTE(_REST), \
+ E__NOTE(_FS7 ), \
+ S__NOTE(_REST), \
+ E__NOTE(_E7 ),
+
+#define PLOVER_SOUND \
+ E__NOTE(_GS6 ), \
+ E__NOTE(_A6 ), \
+ S__NOTE(_REST), \
+ ED_NOTE(_E7 ), \
+ S__NOTE(_REST), \
+ ED_NOTE(_A7 ),
+
+#define PLOVER_GOODBYE_SOUND \
+ E__NOTE(_GS6 ), \
+ E__NOTE(_A6 ), \
+ S__NOTE(_REST), \
+ ED_NOTE(_A7 ), \
+ S__NOTE(_REST), \
+ ED_NOTE(_E7 ),
+
+#define MUSIC_SCALE_SOUND \
+ E__NOTE(_A5 ), \
+ E__NOTE(_B5 ), \
+ E__NOTE(_CS6), \
+ E__NOTE(_D6 ), \
+ E__NOTE(_E6 ), \
+ E__NOTE(_FS6), \
+ E__NOTE(_GS6), \
+ E__NOTE(_A6 ),
+
+#define CAPS_LOCK_ON_SOUND \
+ E__NOTE(_A3), \
+ E__NOTE(_B3),
+
+#define CAPS_LOCK_OFF_SOUND \
+ E__NOTE(_B3), \
+ E__NOTE(_A3),
+
+#define SCROLL_LOCK_ON_SOUND \
+ E__NOTE(_D4), \
+ E__NOTE(_E4),
+
+#define SCROLL_LOCK_OFF_SOUND \
+ E__NOTE(_E4), \
+ E__NOTE(_D4),
+
+#define NUM_LOCK_ON_SOUND \
+ E__NOTE(_D5), \
+ E__NOTE(_E5),
+
+#define NUM_LOCK_OFF_SOUND \
+ E__NOTE(_E5), \
+ E__NOTE(_D5),
+
+#endif
diff --git a/quantum/audio/voices.c b/quantum/audio/voices.c
new file mode 100644
index 0000000000..6d4172a06c
--- /dev/null
+++ b/quantum/audio/voices.c
@@ -0,0 +1,165 @@
+#include "voices.h"
+#include "audio.h"
+#include "stdlib.h"
+
+// these are imported from audio.c
+extern uint16_t envelope_index;
+extern float note_timbre;
+extern float polyphony_rate;
+
+voice_type voice = default_voice;
+
+void set_voice(voice_type v) {
+ voice = v;
+}
+
+void voice_iterate() {
+ voice = (voice + 1) % number_of_voices;
+}
+
+void voice_deiterate() {
+ voice = (voice - 1) % number_of_voices;
+}
+
+float voice_envelope(float frequency) {
+ // envelope_index ranges from 0 to 0xFFFF, which is preserved at 880.0 Hz
+ uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / frequency));
+
+ switch (voice) {
+ case default_voice:
+ note_timbre = TIMBRE_50;
+ polyphony_rate = 0;
+ break;
+
+ case butts_fader:
+ polyphony_rate = 0;
+ switch (compensated_index) {
+ case 0 ... 9:
+ frequency = frequency / 4;
+ note_timbre = TIMBRE_12;
+ break;
+
+ case 10 ... 19:
+ frequency = frequency / 2;
+ note_timbre = TIMBRE_12;
+ break;
+
+ case 20 ... 200:
+ note_timbre = .125 - pow(((float)compensated_index - 20) / (200 - 20), 2)*.125;
+ break;
+
+ default:
+ note_timbre = 0;
+ break;
+ }
+ break;
+
+ // case octave_crunch:
+ // polyphony_rate = 0;
+ // switch (compensated_index) {
+ // case 0 ... 9:
+ // case 20 ... 24:
+ // case 30 ... 32:
+ // frequency = frequency / 2;
+ // note_timbre = TIMBRE_12;
+ // break;
+
+ // case 10 ... 19:
+ // case 25 ... 29:
+ // case 33 ... 35:
+ // frequency = frequency * 2;
+ // note_timbre = TIMBRE_12;
+ // break;
+
+ // default:
+ // note_timbre = TIMBRE_12;
+ // break;
+ // }
+ // break;
+
+ case duty_osc:
+ // This slows the loop down a substantial amount, so higher notes may freeze
+ polyphony_rate = 0;
+ switch (compensated_index) {
+ default:
+ #define OCS_SPEED 10
+ #define OCS_AMP .25
+ // sine wave is slow
+ // note_timbre = (sin((float)compensated_index/10000*OCS_SPEED) * OCS_AMP / 2) + .5;
+ // triangle wave is a bit faster
+ note_timbre = (float)abs((compensated_index*OCS_SPEED % 3000) - 1500) * ( OCS_AMP / 1500 ) + (1 - OCS_AMP) / 2;
+ break;
+ }
+ break;
+
+ case duty_octave_down:
+ polyphony_rate = 0;
+ note_timbre = (envelope_index % 2) * .125 + .375 * 2;
+ if ((envelope_index % 4) == 0)
+ note_timbre = 0.5;
+ if ((envelope_index % 8) == 0)
+ note_timbre = 0;
+ break;
+ case delayed_vibrato:
+ polyphony_rate = 0;
+ note_timbre = TIMBRE_50;
+ #define VOICE_VIBRATO_DELAY 150
+ #define VOICE_VIBRATO_SPEED 50
+ switch (compensated_index) {
+ case 0 ... VOICE_VIBRATO_DELAY:
+ break;
+ default:
+ frequency = frequency * vibrato_lut[(int)fmod((((float)compensated_index - (VOICE_VIBRATO_DELAY + 1))/1000*VOICE_VIBRATO_SPEED), VIBRATO_LUT_LENGTH)];
+ break;
+ }
+ break;
+ // case delayed_vibrato_octave:
+ // polyphony_rate = 0;
+ // if ((envelope_index % 2) == 1) {
+ // note_timbre = 0.55;
+ // } else {
+ // note_timbre = 0.45;
+ // }
+ // #define VOICE_VIBRATO_DELAY 150
+ // #define VOICE_VIBRATO_SPEED 50
+ // switch (compensated_index) {
+ // case 0 ... VOICE_VIBRATO_DELAY:
+ // break;
+ // default:
+ // frequency = frequency * VIBRATO_LUT[(int)fmod((((float)compensated_index - (VOICE_VIBRATO_DELAY + 1))/1000*VOICE_VIBRATO_SPEED), VIBRATO_LUT_LENGTH)];
+ // break;
+ // }
+ // break;
+ // case duty_fifth_down:
+ // note_timbre = 0.5;
+ // if ((envelope_index % 3) == 0)
+ // note_timbre = 0.75;
+ // break;
+ // case duty_fourth_down:
+ // note_timbre = 0.0;
+ // if ((envelope_index % 12) == 0)
+ // note_timbre = 0.75;
+ // if (((envelope_index % 12) % 4) != 1)
+ // note_timbre = 0.75;
+ // break;
+ // case duty_third_down:
+ // note_timbre = 0.5;
+ // if ((envelope_index % 5) == 0)
+ // note_timbre = 0.75;
+ // break;
+ // case duty_fifth_third_down:
+ // note_timbre = 0.5;
+ // if ((envelope_index % 5) == 0)
+ // note_timbre = 0.75;
+ // if ((envelope_index % 3) == 0)
+ // note_timbre = 0.25;
+ // break;
+
+ default:
+ break;
+ }
+
+ return frequency;
+}
+
+
diff --git a/quantum/audio/voices.h b/quantum/audio/voices.h
new file mode 100644
index 0000000000..b2495b23b5
--- /dev/null
+++ b/quantum/audio/voices.h
@@ -0,0 +1,31 @@
+#include <stdint.h>
+#include <stdbool.h>
+#include <avr/io.h>
+#include <util/delay.h>
+#include "luts.h"
+
+#ifndef VOICES_H
+#define VOICES_H
+
+float voice_envelope(float frequency);
+
+typedef enum {
+ default_voice,
+ butts_fader,
+ octave_crunch,
+ duty_osc,
+ duty_octave_down,
+ delayed_vibrato,
+ // delayed_vibrato_octave,
+ // duty_fifth_down,
+ // duty_fourth_down,
+ // duty_third_down,
+ // duty_fifth_third_down,
+ number_of_voices // important that this is last
+} voice_type;
+
+void set_voice(voice_type v);
+void voice_iterate(void);
+void voice_deiterate(void);
+
+#endif \ No newline at end of file
diff --git a/quantum/wave.h b/quantum/audio/wave.h
index 6ebc348519..6ebc348519 100644
--- a/quantum/wave.h
+++ b/quantum/audio/wave.h
diff --git a/quantum/config_common.h b/quantum/config_common.h
index da53fce89b..8ed5f4a106 100644
--- a/quantum/config_common.h
+++ b/quantum/config_common.h
@@ -1,70 +1,83 @@
#ifndef CONFIG_DEFINITIONS_H
#define CONFIG_DEFINITIONS_H
-#define B0 0x20
-#define B1 0x21
-#define B2 0x22
-#define B3 0x23
-#define B4 0x24
-#define B5 0x25
-#define B6 0x26
-#define B7 0x27
-#define C0 0x30
-#define C1 0x31
-#define C2 0x32
-#define C3 0x33
-#define C4 0x34
-#define C5 0x35
-#define C6 0x36
-#define C7 0x37
-#define D0 0x40
-#define D1 0x41
-#define D2 0x42
-#define D3 0x43
-#define D4 0x44
-#define D5 0x45
-#define D6 0x46
-#define D7 0x47
-#define E0 0x50
-#define E1 0x51
-#define E2 0x52
-#define E3 0x53
-#define E4 0x54
-#define E5 0x55
-#define E6 0x56
-#define E7 0x57
-#define F0 0x60
-#define F1 0x61
-#define F2 0x62
-#define F3 0x63
-#define F4 0x64
-#define F5 0x65
-#define F6 0x66
-#define F7 0x67
+/* diode directions */
+#define COL2ROW 0
+#define ROW2COL 1
+/* I/O pins */
+#define B0 0x30
+#define B1 0x31
+#define B2 0x32
+#define B3 0x33
+#define B4 0x34
+#define B5 0x35
+#define B6 0x36
+#define B7 0x37
+#define C0 0x60
+#define C1 0x61
+#define C2 0x62
+#define C3 0x63
+#define C4 0x64
+#define C5 0x65
+#define C6 0x66
+#define C7 0x67
+#define D0 0x90
+#define D1 0x91
+#define D2 0x92
+#define D3 0x93
+#define D4 0x94
+#define D5 0x95
+#define D6 0x96
+#define D7 0x97
+#define E0 0xC0
+#define E1 0xC1
+#define E2 0xC2
+#define E3 0xC3
+#define E4 0xC4
+#define E5 0xC5
+#define E6 0xC6
+#define E7 0xC7
+#define F0 0xF0
+#define F1 0xF1
+#define F2 0xF2
+#define F3 0xF3
+#define F4 0xF4
+#define F5 0xF5
+#define F6 0xF6
+#define F7 0xF7
+#define A0 0x00
+#define A1 0x01
+#define A2 0x02
+#define A3 0x03
+#define A4 0x04
+#define A5 0x05
+#define A6 0x06
+#define A7 0x07
-#define COL2ROW 0x0
-#define ROW2COL 0x1
+/* USART configuration */
#ifdef BLUETOOTH_ENABLE
-#ifdef __AVR_ATmega32U4__
- #define SERIAL_UART_BAUD 9600
- #define SERIAL_UART_DATA UDR1
- #define SERIAL_UART_UBRR ((F_CPU/(16UL*SERIAL_UART_BAUD))-1)
- #define SERIAL_UART_RXD_VECT USART1_RX_vect
- #define SERIAL_UART_TXD_READY (UCSR1A&(1<<UDRE1))
- #define SERIAL_UART_INIT() do { \
- UBRR1L = (uint8_t) SERIAL_UART_UBRR; /* baud rate */ \
- UBRR1H = (uint8_t) (SERIAL_UART_UBRR>>8); /* baud rate */ \
- UCSR1B = (1<<TXEN1); /* TX: enable */ \
- UCSR1C = (0<<UPM11) | (0<<UPM10) | /* parity: none(00), even(01), odd(11) */ \
- (0<<UCSZ12) | (1<<UCSZ11) | (1<<UCSZ10); /* data-8bit(011) */ \
- sei(); \
- } while(0)
-#else
-# error "USART configuration is needed."
+# ifdef __AVR_ATmega32U4__
+# define SERIAL_UART_BAUD 9600
+# define SERIAL_UART_DATA UDR1
+# define SERIAL_UART_UBRR (F_CPU / (16UL * SERIAL_UART_BAUD) - 1)
+# define SERIAL_UART_RXD_VECT USART1_RX_vect
+# define SERIAL_UART_TXD_READY (UCSR1A & _BV(UDRE1))
+# define SERIAL_UART_INIT() do { \
+ /* baud rate */ \
+ UBRR1L = SERIAL_UART_UBRR; \
+ /* baud rate */ \
+ UBRR1H = SERIAL_UART_UBRR >> 8; \
+ /* enable TX */ \
+ UCSR1B = _BV(TXEN1); \
+ /* 8-bit data */ \
+ UCSR1C = _BV(UCSZ11) | _BV(UCSZ10); \
+ sei(); \
+ } while(0)
+# else
+# error "USART configuration is needed."
#endif
-
// I'm fairly sure these aren't needed, but oh well - Jack
/*
@@ -113,4 +126,3 @@
#endif
#endif
-
diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h
new file mode 100644
index 0000000000..a3ad61bc7e
--- /dev/null
+++ b/quantum/dynamic_macro.h
@@ -0,0 +1,226 @@
+/* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */
+#ifndef DYNAMIC_MACROS_H
+#define DYNAMIC_MACROS_H
+
+#include "action_layer.h"
+
+#ifndef DYNAMIC_MACRO_SIZE
+/* May be overridden with a custom value. Be aware that the effective
+ * macro length is half of this value: each keypress is recorded twice
+ * because of the down-event and up-event. This is not a bug, it's the
+ * intended behavior. */
+#define DYNAMIC_MACRO_SIZE 256
+#endif
+
+/* DYNAMIC_MACRO_RANGE must be set as the last element of user's
+ * "planck_keycodes" enum prior to including this header. This allows
+ * us to 'extend' it.
+ */
+enum dynamic_macro_keycodes {
+ DYN_REC_START1 = DYNAMIC_MACRO_RANGE,
+ DYN_REC_START2,
+ DYN_MACRO_PLAY1,
+ DYN_MACRO_PLAY2,
+};
+
+/* Blink the LEDs to notify the user about some event. */
+void dynamic_macro_led_blink(void)
+{
+ backlight_toggle();
+ _delay_ms(100);
+ backlight_toggle();
+}
+
+/**
+ * Start recording of the dynamic macro.
+ *
+ * @param[out] macro_pointer The new macro buffer iterator.
+ * @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
+ */
+void dynamic_macro_record_start(
+ keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
+{
+ dynamic_macro_led_blink();
+
+ clear_keyboard();
+ layer_clear();
+ *macro_pointer = macro_buffer;
+}
+
+/**
+ * Play the dynamic macro.
+ *
+ * @param macro_buffer[in] The beginning of the macro buffer being played.
+ * @param macro_end[in] The element after the last macro buffer element.
+ * @param direction[in] Either +1 or -1, which way to iterate the buffer.
+ */
+void dynamic_macro_play(
+ keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
+{
+ uint32_t saved_layer_state = layer_state;
+
+ clear_keyboard();
+ layer_clear();
+
+ while (macro_buffer != macro_end) {
+ process_record(macro_buffer);
+ macro_buffer += direction;
+ }
+
+ clear_keyboard();
+
+ layer_state = saved_layer_state;
+}
+
+/**
+ * Record a single key in a dynamic macro.
+ *
+ * @param macro_pointer[in,out] The current buffer position.
+ * @param macro_end2[in] The end of the other macro which shouldn't be overwritten.
+ * @param direction[in] Either +1 or -1, which way to iterate the buffer.
+ * @param record[in] The current keypress.
+ */
+void dynamic_macro_record_key(
+ keyrecord_t **macro_pointer,
+ keyrecord_t *macro_end2,
+ int8_t direction,
+ keyrecord_t *record)
+{
+ if (*macro_pointer + direction != macro_end2) {
+ **macro_pointer = *record;
+ *macro_pointer += direction;
+ } else {
+ /* Notify about the end of buffer. The blinks are paired
+ * because they should happen on both down and up events. */
+ backlight_toggle();
+ }
+}
+
+/**
+ * End recording of the dynamic macro. Essentially just update the
+ * pointer to the end of the macro.
+ */
+void dynamic_macro_record_end(keyrecord_t *macro_pointer, keyrecord_t **macro_end)
+{
+ dynamic_macro_led_blink();
+
+ *macro_end = macro_pointer;
+}
+
+/* Handle the key events related to the dynamic macros. Should be
+ * called from process_record_user() like this:
+ *
+ * bool process_record_user(uint16_t keycode, keyrecord_t *record) {
+ * if (!process_record_dynamic_macro(keycode, record)) {
+ * return false;
+ * }
+ * <...THE REST OF THE FUNCTION...>
+ * }
+ */
+bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
+{
+ /* Both macros use the same buffer but read/write on different
+ * ends of it.
+ *
+ * Macro1 is written left-to-right starting from the beginning of
+ * the buffer.
+ *
+ * Macro2 is written right-to-left starting from the end of the
+ * buffer.
+ *
+ * &macro_buffer macro_end
+ * v v
+ * +------------------------------------------------------------+
+ * |>>>>>> MACRO1 >>>>>>| |<<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
+ * +------------------------------------------------------------+
+ * ^ ^
+ * r_macro_end r_macro_buffer
+ *
+ * During the recording when one macro encounters the end of the
+ * other macro, the recording is stopped. Apart from this, there
+ * are no arbitrary limits for the macros' length in relation to
+ * each other: for example one can either have two medium sized
+ * macros or one long macro and one short macro. Or even one empty
+ * and one using the whole buffer.
+ */
+ static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
+
+ /* Pointer to the first buffer element after the first macro.
+ * Initially points to the very beginning of the buffer since the
+ * macro is empty. */
+ static keyrecord_t *macro_end = macro_buffer;
+
+ /* The other end of the macro buffer. Serves as the beginning of
+ * the second macro. */
+ static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
+
+ /* Like macro_end but for the second macro. */
+ static keyrecord_t *r_macro_end = r_macro_buffer;
+
+ /* A persistent pointer to the current macro position (iterator)
+ * used during the recording. */
+ static keyrecord_t *macro_pointer = NULL;
+
+ /* 0 - no macro is being recorded right now
+ * 1,2 - either macro 1 or 2 is being recorded */
+ static uint8_t macro_id = 0;
+
+ if (macro_id == 0) {
+ /* No macro recording in progress. */
+ if (!record->event.pressed) {
+ switch (keycode) {
+ case DYN_REC_START1:
+ dynamic_macro_record_start(&macro_pointer, macro_buffer);
+ macro_id = 1;
+ return false;
+ case DYN_REC_START2:
+ dynamic_macro_record_start(&macro_pointer, r_macro_buffer);
+ macro_id = 2;
+ return false;
+ case DYN_MACRO_PLAY1:
+ dynamic_macro_play(macro_buffer, macro_end, +1);
+ return false;
+ case DYN_MACRO_PLAY2:
+ dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
+ return false;
+ }
+ }
+ } else {
+ /* A macro is being recorded right now. */
+ switch (keycode) {
+ case MO(_DYN):
+ /* Use the layer key used to access the macro recording as
+ * a stop button. */
+ if (record->event.pressed) { /* Ignore the initial release
+ * just after the recoding
+ * starts. */
+ switch (macro_id) {
+ case 1:
+ dynamic_macro_record_end(macro_pointer, &macro_end);
+ break;
+ case 2:
+ dynamic_macro_record_end(macro_pointer, &r_macro_end);
+ break;
+ }
+ macro_id = 0;
+ }
+ return false;
+ default:
+ /* Store the key in the macro buffer and process it normally. */
+ switch (macro_id) {
+ case 1:
+ dynamic_macro_record_key(&macro_pointer, r_macro_end, +1, record);
+ break;
+ case 2:
+ dynamic_macro_record_key(&macro_pointer, macro_end, -1, record);
+ break;
+ }
+ return true;
+ break;
+ }
+ }
+
+ return true;
+}
+
+#endif
diff --git a/quantum/keycode_config.c b/quantum/keycode_config.c
new file mode 100644
index 0000000000..6d90781a17
--- /dev/null
+++ b/quantum/keycode_config.c
@@ -0,0 +1,74 @@
+#include "keycode_config.h"
+
+extern keymap_config_t keymap_config;
+
+uint16_t keycode_config(uint16_t keycode) {
+
+ switch (keycode) {
+ case KC_CAPSLOCK:
+ case KC_LOCKING_CAPS:
+ if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
+ return KC_LCTL;
+ }
+ return keycode;
+ case KC_LCTL:
+ if (keymap_config.swap_control_capslock) {
+ return KC_CAPSLOCK;
+ }
+ return KC_LCTL;
+ case KC_LALT:
+ if (keymap_config.swap_lalt_lgui) {
+ if (keymap_config.no_gui) {
+ return KC_NO;
+ }
+ return KC_LGUI;
+ }
+ return KC_LALT;
+ case KC_LGUI:
+ if (keymap_config.swap_lalt_lgui) {
+ return KC_LALT;
+ }
+ if (keymap_config.no_gui) {
+ return KC_NO;
+ }
+ return KC_LGUI;
+ case KC_RALT:
+ if (keymap_config.swap_ralt_rgui) {
+ if (keymap_config.no_gui) {
+ return KC_NO;
+ }
+ return KC_RGUI;
+ }
+ return KC_RALT;
+ case KC_RGUI:
+ if (keymap_config.swap_ralt_rgui) {
+ return KC_RALT;
+ }
+ if (keymap_config.no_gui) {
+ return KC_NO;
+ }
+ return KC_RGUI;
+ case KC_GRAVE:
+ if (keymap_config.swap_grave_esc) {
+ return KC_ESC;
+ }
+ return KC_GRAVE;
+ case KC_ESC:
+ if (keymap_config.swap_grave_esc) {
+ return KC_GRAVE;
+ }
+ return KC_ESC;
+ case KC_BSLASH:
+ if (keymap_config.swap_backslash_backspace) {
+ return KC_BSPACE;
+ }
+ return KC_BSLASH;
+ case KC_BSPACE:
+ if (keymap_config.swap_backslash_backspace) {
+ return KC_BSLASH;
+ }
+ return KC_BSPACE;
+ default:
+ return keycode;
+ }
+} \ No newline at end of file
diff --git a/quantum/keycode_config.h b/quantum/keycode_config.h
new file mode 100644
index 0000000000..c15b0d32f8
--- /dev/null
+++ b/quantum/keycode_config.h
@@ -0,0 +1,26 @@
+#include "eeconfig.h"
+#include "keycode.h"
+
+#ifndef KEYCODE_CONFIG_H
+#define KEYCODE_CONFIG_H
+
+uint16_t keycode_config(uint16_t keycode);
+
+/* NOTE: Not portable. Bit field order depends on implementation */
+typedef union {
+ uint16_t raw;
+ struct {
+ bool swap_control_capslock:1;
+ bool capslock_to_control:1;
+ bool swap_lalt_lgui:1;
+ bool swap_ralt_rgui:1;
+ bool no_gui:1;
+ bool swap_grave_esc:1;
+ bool swap_backslash_backspace:1;
+ bool nkro:1;
+ };
+} keymap_config_t;
+
+extern keymap_config_t keymap_config;
+
+#endif /* KEYCODE_CONFIG_H */
diff --git a/quantum/keymap.h b/quantum/keymap.h
new file mode 100644
index 0000000000..98ddfd0c53
--- /dev/null
+++ b/quantum/keymap.h
@@ -0,0 +1,339 @@
+/*
+Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
+
+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/>.
+*/
+
+#ifndef KEYMAP_H
+#define KEYMAP_H
+
+#include <stdint.h>
+#include <stdbool.h>
+#include "action.h"
+#if defined(__AVR__)
+#include <avr/pgmspace.h>
+#endif
+#include "keycode.h"
+#include "action_macro.h"
+#include "report.h"
+#include "host.h"
+// #include "print.h"
+#include "debug.h"
+#include "keycode_config.h"
+
+// ChibiOS uses RESET in its FlagStatus enumeration
+// Therefore define it as QK_RESET here, to avoid name collision
+#if defined(PROTOCOL_CHIBIOS)
+#define RESET QK_RESET
+#endif
+
+/* translates key to keycode */
+uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key);
+
+extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
+extern const uint16_t fn_actions[];
+
+enum quantum_keycodes {
+ // Ranges used in shortucuts - not to be used directly
+ QK_TMK = 0x0000,
+ QK_TMK_MAX = 0x00FF,
+ QK_MODS = 0x0100,
+ QK_LCTL = 0x0100,
+ QK_LSFT = 0x0200,
+ QK_LALT = 0x0400,
+ QK_LGUI = 0x0800,
+ QK_RCTL = 0x1100,
+ QK_RSFT = 0x1200,
+ QK_RALT = 0x1400,
+ QK_RGUI = 0x1800,
+ QK_MODS_MAX = 0x1FFF,
+ QK_FUNCTION = 0x2000,
+ QK_FUNCTION_MAX = 0x2FFF,
+ QK_MACRO = 0x3000,
+ QK_MACRO_MAX = 0x3FFF,
+ QK_LAYER_TAP = 0x4000,
+ QK_LAYER_TAP_MAX = 0x4FFF,
+ QK_TO = 0x5000,
+ QK_TO_MAX = 0x50FF,
+ QK_MOMENTARY = 0x5100,
+ QK_MOMENTARY_MAX = 0x51FF,
+ QK_DEF_LAYER = 0x5200,
+ QK_DEF_LAYER_MAX = 0x52FF,
+ QK_TOGGLE_LAYER = 0x5300,
+ QK_TOGGLE_LAYER_MAX = 0x53FF,
+ QK_ONE_SHOT_LAYER = 0x5400,
+ QK_ONE_SHOT_LAYER_MAX = 0x54FF,
+ QK_ONE_SHOT_MOD = 0x5500,
+ QK_ONE_SHOT_MOD_MAX = 0x55FF,
+#ifndef DISABLE_CHORDING
+ QK_CHORDING = 0x5600,
+ QK_CHORDING_MAX = 0x56FF,
+#endif
+ QK_MOD_TAP = 0x6000,
+ QK_MOD_TAP_MAX = 0x6FFF,
+ QK_TAP_DANCE = 0x7100,
+ QK_TAP_DANCE_MAX = 0x71FF,
+#ifdef UNICODE_ENABLE
+ QK_UNICODE = 0x8000,
+ QK_UNICODE_MAX = 0xFFFF,
+#endif
+
+ // Loose keycodes - to be used directly
+
+ RESET = 0x7000,
+ DEBUG,
+ MAGIC_SWAP_CONTROL_CAPSLOCK,
+ MAGIC_CAPSLOCK_TO_CONTROL,
+ MAGIC_SWAP_LALT_LGUI,
+ MAGIC_SWAP_RALT_RGUI,
+ MAGIC_NO_GUI,
+ MAGIC_SWAP_GRAVE_ESC,
+ MAGIC_SWAP_BACKSLASH_BACKSPACE,
+ MAGIC_HOST_NKRO,
+ MAGIC_SWAP_ALT_GUI,
+ MAGIC_UNSWAP_CONTROL_CAPSLOCK,
+ MAGIC_UNCAPSLOCK_TO_CONTROL,
+ MAGIC_UNSWAP_LALT_LGUI,
+ MAGIC_UNSWAP_RALT_RGUI,
+ MAGIC_UNNO_GUI,
+ MAGIC_UNSWAP_GRAVE_ESC,
+ MAGIC_UNSWAP_BACKSLASH_BACKSPACE,
+ MAGIC_UNHOST_NKRO,
+ MAGIC_UNSWAP_ALT_GUI,
+ MAGIC_TOGGLE_NKRO,
+
+ // Leader key
+#ifndef DISABLE_LEADER
+ KC_LEAD,
+#endif
+
+ // Audio on/off/toggle
+ AU_ON,
+ AU_OFF,
+ AU_TOG,
+
+ // Music mode on/off/toggle
+ MU_ON,
+ MU_OFF,
+ MU_TOG,
+
+ // Music voice iterate
+ MUV_IN,
+ MUV_DE,
+
+ // Midi mode on/off
+ MIDI_ON,
+ MIDI_OFF,
+
+ // Backlight functionality
+ BL_0,
+ BL_1,
+ BL_2,
+ BL_3,
+ BL_4,
+ BL_5,
+ BL_6,
+ BL_7,
+ BL_8,
+ BL_9,
+ BL_10,
+ BL_11,
+ BL_12,
+ BL_13,
+ BL_14,
+ BL_15,
+ BL_DEC,
+ BL_INC,
+ BL_TOGG,
+ BL_STEP,
+
+ // RGB functionality
+ RGB_TOG,
+ RGB_MOD,
+ RGB_HUI,
+ RGB_HUD,
+ RGB_SAI,
+ RGB_SAD,
+ RGB_VAI,
+ RGB_VAD,
+
+ // Left shift, open paren
+ KC_LSPO,
+
+ // Right shift, close paren
+ KC_RSPC,
+
+ // always leave at the end
+ SAFE_RANGE
+};
+
+// Ability to use mods in layouts
+#define LCTL(kc) (kc | QK_LCTL)
+#define LSFT(kc) (kc | QK_LSFT)
+#define LALT(kc) (kc | QK_LALT)
+#define LGUI(kc) (kc | QK_LGUI)
+#define RCTL(kc) (kc | QK_RCTL)
+#define RSFT(kc) (kc | QK_RSFT)
+#define RALT(kc) (kc | QK_RALT)
+#define RGUI(kc) (kc | QK_RGUI)
+
+#define HYPR(kc) (kc | QK_LCTL | QK_LSFT | QK_LALT | QK_LGUI)
+#define MEH(kc) (kc | QK_LCTL | QK_LSFT | QK_LALT)
+#define LCAG(kc) (kc | QK_LCTL | QK_LALT | QK_LGUI)
+
+#define MOD_HYPR 0xf
+#define MOD_MEH 0x7
+
+
+// Aliases for shifted symbols
+// Each key has a 4-letter code, and some have longer aliases too.
+// While the long aliases are descriptive, the 4-letter codes
+// make for nicer grid layouts (everything lines up), and are
+// the preferred style for Quantum.
+#define KC_TILD LSFT(KC_GRV) // ~
+#define KC_TILDE KC_TILD
+
+#define KC_EXLM LSFT(KC_1) // !
+#define KC_EXCLAIM KC_EXLM
+
+#define KC_AT LSFT(KC_2) // @
+
+#define KC_HASH LSFT(KC_3) // #
+
+#define KC_DLR LSFT(KC_4) // $
+#define KC_DOLLAR KC_DLR
+
+#define KC_PERC LSFT(KC_5) // %
+#define KC_PERCENT KC_PERC
+
+#define KC_CIRC LSFT(KC_6) // ^
+#define KC_CIRCUMFLEX KC_CIRC
+
+#define KC_AMPR LSFT(KC_7) // &
+#define KC_AMPERSAND KC_AMPR
+
+#define KC_ASTR LSFT(KC_8) // *
+#define KC_ASTERISK KC_ASTR
+
+#define KC_LPRN LSFT(KC_9) // (
+#define KC_LEFT_PAREN KC_LPRN
+
+#define KC_RPRN LSFT(KC_0) // )
+#define KC_RIGHT_PAREN KC_RPRN
+
+#define KC_UNDS LSFT(KC_MINS) // _
+#define KC_UNDERSCORE KC_UNDS
+
+#define KC_PLUS LSFT(KC_EQL) // +
+
+#define KC_LCBR LSFT(KC_LBRC) // {
+#define KC_LEFT_CURLY_BRACE KC_LCBR
+
+#define KC_RCBR LSFT(KC_RBRC) // }
+#define KC_RIGHT_CURLY_BRACE KC_RCBR
+
+#define KC_LABK LSFT(KC_COMM) // <
+#define KC_LEFT_ANGLE_BRACKET KC_LABK
+
+#define KC_RABK LSFT(KC_DOT) // >
+#define KC_RIGHT_ANGLE_BRACKET KC_RABK
+
+#define KC_COLN LSFT(KC_SCLN) // :
+#define KC_COLON KC_COLN
+
+#define KC_PIPE LSFT(KC_BSLS) // |
+
+#define KC_LT LSFT(KC_COMM) // <
+
+#define KC_GT LSFT(KC_DOT) // >
+
+#define KC_QUES LSFT(KC_SLSH) // ?
+#define KC_QUESTION KC_QUES
+
+#define KC_DQT LSFT(KC_QUOT) // "
+#define KC_DOUBLE_QUOTE KC_DQT
+#define KC_DQUO KC_DQT
+
+#define KC_DELT KC_DELETE // Del key (four letter code)
+
+// Alias for function layers than expand past FN31
+#define FUNC(kc) (kc | QK_FUNCTION)
+
+// Aliases
+#define S(kc) LSFT(kc)
+#define F(kc) FUNC(kc)
+
+#define M(kc) (kc | QK_MACRO)
+
+#define MACRODOWN(...) (record->event.pressed ? MACRO(__VA_ARGS__) : MACRO_NONE)
+
+// L-ayer, T-ap - 256 keycode max, 16 layer max
+#define LT(layer, kc) (kc | QK_LAYER_TAP | ((layer & 0xF) << 8))
+
+#define AG_SWAP MAGIC_SWAP_ALT_GUI
+#define AG_NORM MAGIC_UNSWAP_ALT_GUI
+
+#define BL_ON BL_9
+#define BL_OFF BL_0
+
+#define MI_ON MIDI_ON
+#define MI_OFF MIDI_OFF
+
+// GOTO layer - 16 layers max
+// when:
+// ON_PRESS = 1
+// ON_RELEASE = 2
+// Unless you have a good reason not to do so, prefer ON_PRESS (1) as your default.
+#define TO(layer, when) (layer | QK_TO | (when << 0x4))
+
+// Momentary switch layer - 256 layer max
+#define MO(layer) (layer | QK_MOMENTARY)
+
+// Set default layer - 256 layer max
+#define DF(layer) (layer | QK_DEF_LAYER)
+
+// Toggle to layer - 256 layer max
+#define TG(layer) (layer | QK_TOGGLE_LAYER)
+
+// One-shot layer - 256 layer max
+#define OSL(layer) (layer | QK_ONE_SHOT_LAYER)
+
+// One-shot mod
+#define OSM(mod) (mod | QK_ONE_SHOT_MOD)
+
+// M-od, T-ap - 256 keycode max
+#define MT(mod, kc) (kc | QK_MOD_TAP | ((mod & 0xF) << 8))
+#define CTL_T(kc) MT(MOD_LCTL, kc)
+#define SFT_T(kc) MT(MOD_LSFT, kc)
+#define ALT_T(kc) MT(MOD_LALT, kc)
+#define GUI_T(kc) MT(MOD_LGUI, kc)
+#define C_S_T(kc) MT((MOD_LCTL | MOD_LSFT), kc) // Control + Shift e.g. for gnome-terminal
+#define MEH_T(kc) MT((MOD_LCTL | MOD_LSFT | MOD_LALT), kc) // Meh is a less hyper version of the Hyper key -- doesn't include Win or Cmd, so just alt+shift+ctrl
+#define LCAG_T(kc) MT((MOD_LCTL | MOD_LALT | MOD_LGUI), kc) // Left control alt and gui
+#define ALL_T(kc) MT((MOD_LCTL | MOD_LSFT | MOD_LALT | MOD_LGUI), kc) // see http://brettterpstra.com/2012/12/08/a-useful-caps-lock-key/
+
+// Dedicated keycode versions for Hyper and Meh, if you want to use them as standalone keys rather than mod-tap
+#define KC_HYPR HYPR(KC_NO)
+#define KC_MEH MEH(KC_NO)
+
+#ifdef UNICODE_ENABLE
+ // For sending unicode codes.
+ // You may not send codes over 7FFF -- this supports most of UTF8.
+ // To have a key that sends out Œ, go UC(0x0152)
+ #define UNICODE(n) (n | QK_UNICODE)
+ #define UC(n) UNICODE(n)
+#endif
+
+
+#endif
diff --git a/quantum/keymap_common.c b/quantum/keymap_common.c
index 2eef4f15a7..833e5a8f8d 100644
--- a/quantum/keymap_common.c
+++ b/quantum/keymap_common.c
@@ -15,240 +15,46 @@ 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 "keymap_common.h"
+#include "keymap.h"
#include "report.h"
#include "keycode.h"
#include "action_layer.h"
+#if defined(__AVR__)
#include <util/delay.h>
+#include <stdio.h>
+#endif
#include "action.h"
#include "action_macro.h"
#include "debug.h"
#include "backlight.h"
-#include "keymap_midi.h"
-#include "bootloader.h"
-
-#include <stdio.h>
-#include <inttypes.h>
-#ifdef AUDIO_ENABLE
- #include "audio.h"
+#include "quantum.h"
- float goodbye[][2] = {
- {440.0*pow(2.0,(67)/12.0), 400},
- {0, 50},
- {440.0*pow(2.0,(60)/12.0), 400},
- {0, 50},
- {440.0*pow(2.0,(55)/12.0), 600},
- };
+#ifdef MIDI_ENABLE
+ #include "process_midi.h"
#endif
-static action_t keycode_to_action(uint16_t keycode);
+extern keymap_config_t keymap_config;
+
+#include <inttypes.h>
/* converts key to action */
action_t action_for_key(uint8_t layer, keypos_t key)
{
- // 16bit keycodes - important
+ // 16bit keycodes - important
uint16_t keycode = keymap_key_to_keycode(layer, key);
- if (keycode >= 0x0100 && keycode < 0x2000) {
- // Has a modifier
- action_t action;
- // Split it up
- action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
- return action;
- } else if (keycode >= 0x2000 && keycode < 0x3000) {
- // Is a shortcut for function layer, pull last 12bits
- // This means we have 4,096 FN macros at our disposal
- return keymap_func_to_action(keycode & 0xFFF);
- } else if (keycode >= 0x3000 && keycode < 0x4000) {
- // When the code starts with 3, it's an action macro.
- action_t action;
- action.code = ACTION_MACRO(keycode & 0xFF);
- return action;
-#ifdef BACKLIGHT_ENABLE
- } else if (keycode >= BL_0 && keycode <= BL_15) {
- action_t action;
- action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
- return action;
- } else if (keycode == BL_DEC) {
- action_t action;
- action.code = ACTION_BACKLIGHT_DECREASE();
- return action;
- } else if (keycode == BL_INC) {
- action_t action;
- action.code = ACTION_BACKLIGHT_INCREASE();
- return action;
- } else if (keycode == BL_TOGG) {
- action_t action;
- action.code = ACTION_BACKLIGHT_TOGGLE();
- return action;
- } else if (keycode == BL_STEP) {
- action_t action;
- action.code = ACTION_BACKLIGHT_STEP();
- return action;
-#endif
- } else if (keycode == RESET) { // RESET is 0x5000, which is why this is here
- clear_keyboard();
- #ifdef AUDIO_ENABLE
- play_notes(&goodbye, 5, false);
- #endif
- _delay_ms(250);
- #ifdef ATREUS_ASTAR
- *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
- #endif
- bootloader_jump();
- return;
- } else if (keycode == DEBUG) { // DEBUG is 0x5001
- // TODO: Does this actually work?
- print("\nDEBUG: enabled.\n");
- debug_enable = true;
- return;
- } else if (keycode >= 0x5000 && keycode < 0x6000) {
- // Layer movement shortcuts
- // See .h to see constraints/usage
- int type = (keycode >> 0x8) & 0xF;
- if (type == 0x1) {
- // Layer set "GOTO"
- int when = (keycode >> 0x4) & 0x3;
- int layer = keycode & 0xF;
- action_t action;
- action.code = ACTION_LAYER_SET(layer, when);
- return action;
- } else if (type == 0x2) {
- // Momentary layer
- int layer = keycode & 0xFF;
- action_t action;
- action.code = ACTION_LAYER_MOMENTARY(layer);
- return action;
- } else if (type == 0x3) {
- // Set default layer
- int layer = keycode & 0xFF;
- action_t action;
- action.code = ACTION_DEFAULT_LAYER_SET(layer);
- return action;
- } else if (type == 0x4) {
- // Set default layer
- int layer = keycode & 0xFF;
- action_t action;
- action.code = ACTION_LAYER_TOGGLE(layer);
- return action;
- }
-#ifdef MIDI_ENABLE
- } else if (keycode >= 0x6000 && keycode < 0x7000) {
- action_t action;
- action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
- return action;
-#endif
- } else if (keycode >= 0x7000 && keycode < 0x8000) {
- action_t action;
- action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
- return action;
- } else if (keycode >= 0x8000 && keycode < 0x9000) {
- action_t action;
- action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
- return action;
-#ifdef UNICODE_ENABLE
- } else if (keycode >= 0x8000000) {
- action_t action;
- uint16_t unicode = keycode & ~(0x8000);
- action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
- return action;
-#endif
- } else {
+ // keycode remapping
+ keycode = keycode_config(keycode);
- }
+ action_t action;
+ uint8_t action_layer, when, mod;
+ // The arm-none-eabi compiler generates out of bounds warnings when using the fn_actions directly for some reason
+ const uint16_t* actions = fn_actions;
switch (keycode) {
case KC_FN0 ... KC_FN31:
- return keymap_fn_to_action(keycode);
-#ifdef BOOTMAGIC_ENABLE
- case KC_CAPSLOCK:
- case KC_LOCKING_CAPS:
- if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
- return keycode_to_action(KC_LCTL);
- }
- return keycode_to_action(keycode);
- case KC_LCTL:
- if (keymap_config.swap_control_capslock) {
- return keycode_to_action(KC_CAPSLOCK);
- }
- return keycode_to_action(KC_LCTL);
- case KC_LALT:
- if (keymap_config.swap_lalt_lgui) {
- if (keymap_config.no_gui) {
- return keycode_to_action(ACTION_NO);
- }
- return keycode_to_action(KC_LGUI);
- }
- return keycode_to_action(KC_LALT);
- case KC_LGUI:
- if (keymap_config.swap_lalt_lgui) {
- return keycode_to_action(KC_LALT);
- }
- if (keymap_config.no_gui) {
- return keycode_to_action(ACTION_NO);
- }
- return keycode_to_action(KC_LGUI);
- case KC_RALT:
- if (keymap_config.swap_ralt_rgui) {
- if (keymap_config.no_gui) {
- return keycode_to_action(ACTION_NO);
- }
- return keycode_to_action(KC_RGUI);
- }
- return keycode_to_action(KC_RALT);
- case KC_RGUI:
- if (keymap_config.swap_ralt_rgui) {
- return keycode_to_action(KC_RALT);
- }
- if (keymap_config.no_gui) {
- return keycode_to_action(ACTION_NO);
- }
- return keycode_to_action(KC_RGUI);
- case KC_GRAVE:
- if (keymap_config.swap_grave_esc) {
- return keycode_to_action(KC_ESC);
- }
- return keycode_to_action(KC_GRAVE);
- case KC_ESC:
- if (keymap_config.swap_grave_esc) {
- return keycode_to_action(KC_GRAVE);
- }
- return keycode_to_action(KC_ESC);
- case KC_BSLASH:
- if (keymap_config.swap_backslash_backspace) {
- return keycode_to_action(KC_BSPACE);
- }
- return keycode_to_action(KC_BSLASH);
- case KC_BSPACE:
- if (keymap_config.swap_backslash_backspace) {
- return keycode_to_action(KC_BSLASH);
- }
- return keycode_to_action(KC_BSPACE);
-#endif
- default:
- return keycode_to_action(keycode);
- }
-}
-
-
-/* Macro */
-__attribute__ ((weak))
-const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
-{
- return MACRO_NONE;
-}
-
-/* Function */
-__attribute__ ((weak))
-void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
-{
-}
-
-/* translates keycode to action */
-static action_t keycode_to_action(uint16_t keycode)
-{
- action_t action;
- switch (keycode) {
+ action.code = pgm_read_word(&actions[FN_INDEX(keycode)]);
+ break;
case KC_A ... KC_EXSEL:
case KC_LCTRL ... KC_RGUI:
action.code = ACTION_KEY(keycode);
@@ -256,7 +62,7 @@ static action_t keycode_to_action(uint16_t keycode)
case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
break;
- case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
+ case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
break;
case KC_MS_UP ... KC_MS_ACCEL2:
@@ -265,6 +71,73 @@ static action_t keycode_to_action(uint16_t keycode)
case KC_TRNS:
action.code = ACTION_TRANSPARENT;
break;
+ case QK_MODS ... QK_MODS_MAX: ;
+ // Has a modifier
+ // Split it up
+ action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
+ break;
+ case QK_FUNCTION ... QK_FUNCTION_MAX: ;
+ // Is a shortcut for function action_layer, pull last 12bits
+ // This means we have 4,096 FN macros at our disposal
+ action.code = pgm_read_word(&actions[(int)keycode & 0xFFF]);
+ break;
+ case QK_MACRO ... QK_MACRO_MAX:
+ action.code = ACTION_MACRO(keycode & 0xFF);
+ break;
+ case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
+ action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
+ break;
+ case QK_TO ... QK_TO_MAX: ;
+ // Layer set "GOTO"
+ when = (keycode >> 0x4) & 0x3;
+ action_layer = keycode & 0xF;
+ action.code = ACTION_LAYER_SET(action_layer, when);
+ break;
+ case QK_MOMENTARY ... QK_MOMENTARY_MAX: ;
+ // Momentary action_layer
+ action_layer = keycode & 0xFF;
+ action.code = ACTION_LAYER_MOMENTARY(action_layer);
+ break;
+ case QK_DEF_LAYER ... QK_DEF_LAYER_MAX: ;
+ // Set default action_layer
+ action_layer = keycode & 0xFF;
+ action.code = ACTION_DEFAULT_LAYER_SET(action_layer);
+ break;
+ case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX: ;
+ // Set toggle
+ action_layer = keycode & 0xFF;
+ action.code = ACTION_LAYER_TOGGLE(action_layer);
+ break;
+ case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX: ;
+ // OSL(action_layer) - One-shot action_layer
+ action_layer = keycode & 0xFF;
+ action.code = ACTION_LAYER_ONESHOT(action_layer);
+ break;
+ case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX: ;
+ // OSM(mod) - One-shot mod
+ mod = keycode & 0xFF;
+ action.code = ACTION_MODS_ONESHOT(mod);
+ break;
+ case QK_MOD_TAP ... QK_MOD_TAP_MAX:
+ action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
+ break;
+ #ifdef BACKLIGHT_ENABLE
+ case BL_0 ... BL_15:
+ action.code = ACTION_BACKLIGHT_LEVEL(keycode - BL_0);
+ break;
+ case BL_DEC:
+ action.code = ACTION_BACKLIGHT_DECREASE();
+ break;
+ case BL_INC:
+ action.code = ACTION_BACKLIGHT_INCREASE();
+ break;
+ case BL_TOGG:
+ action.code = ACTION_BACKLIGHT_TOGGLE();
+ break;
+ case BL_STEP:
+ action.code = ACTION_BACKLIGHT_STEP();
+ break;
+ #endif
default:
action.code = ACTION_NO;
break;
@@ -272,22 +145,27 @@ static action_t keycode_to_action(uint16_t keycode)
return action;
}
+__attribute__ ((weak))
+const uint16_t PROGMEM fn_actions[] = {
-/* translates key to keycode */
-uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
+};
+
+/* Macro */
+__attribute__ ((weak))
+const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
{
- // Read entire word (16bits)
- return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
+ return MACRO_NONE;
}
-/* translates Fn keycode to action */
-action_t keymap_fn_to_action(uint16_t keycode)
+/* Function */
+__attribute__ ((weak))
+void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
{
- return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
}
-action_t keymap_func_to_action(uint16_t keycode)
+/* translates key to keycode */
+uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
{
- // For FUNC without 8bit limit
- return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
+ // Read entire word (16bits)
+ return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
}
diff --git a/quantum/keymap_common.h b/quantum/keymap_common.h
deleted file mode 100644
index 3db40772e0..0000000000
--- a/quantum/keymap_common.h
+++ /dev/null
@@ -1,214 +0,0 @@
-/*
-Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
-
-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/>.
-*/
-
-#ifndef KEYMAP_H
-#define KEYMAP_H
-
-#include <stdint.h>
-#include <stdbool.h>
-#include "action.h"
-#include <avr/pgmspace.h>
-#include "keycode.h"
-#include "keymap.h"
-#include "action_macro.h"
-#include "report.h"
-#include "host.h"
-// #include "print.h"
-#include "debug.h"
-
-#ifdef BOOTMAGIC_ENABLE
-/* NOTE: Not portable. Bit field order depends on implementation */
-typedef union {
- uint16_t raw;
- struct {
- bool swap_control_capslock:1;
- bool capslock_to_control:1;
- bool swap_lalt_lgui:1;
- bool swap_ralt_rgui:1;
- bool no_gui:1;
- bool swap_grave_esc:1;
- bool swap_backslash_backspace:1;
- bool nkro:1;
- };
-} keymap_config_t;
-keymap_config_t keymap_config;
-#endif
-
-
-/* translates key to keycode */
-uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key);
-
-/* translates Fn keycode to action */
-action_t keymap_fn_to_action(uint16_t keycode);
-
-/* translates Fn keycode to action */
-action_t keymap_func_to_action(uint16_t keycode);
-
-extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
-extern const uint16_t fn_actions[];
-
-// Ability to use mods in layouts
-#define LCTL(kc) kc | 0x0100
-#define LSFT(kc) kc | 0x0200
-#define LALT(kc) kc | 0x0400
-#define LGUI(kc) kc | 0x0800
-#define HYPR(kc) kc | 0x0F00
-#define MEH(kc) kc | 0x0700
-#define LCAG(kc) kc | 0x0D00 // Modifier Ctrl Alt and GUI
-
-#define RCTL(kc) kc | 0x1100
-#define RSFT(kc) kc | 0x1200
-#define RALT(kc) kc | 0x1400
-#define RGUI(kc) kc | 0x1800
-
-// Aliases for shifted symbols
-// Each key has a 4-letter code, and some have longer aliases too.
-// While the long aliases are descriptive, the 4-letter codes
-// make for nicer grid layouts (everything lines up), and are
-// the preferred style for Quantum.
-#define KC_TILD LSFT(KC_GRV) // ~
-#define KC_TILDE KC_TILD
-
-#define KC_EXLM LSFT(KC_1) // !
-#define KC_EXCLAIM KC_EXLM
-
-#define KC_AT LSFT(KC_2) // @
-#define KC_HASH LSFT(KC_3) // #
-
-#define KC_DLR LSFT(KC_4) // $
-#define KC_DOLLAR KC_DLR
-
-#define KC_PERC LSFT(KC_5) // %
-#define KC_PERCENT KC_PERC
-
-#define KC_CIRC LSFT(KC_6) // ^
-#define KC_CIRCUMFLEX KC_CIRC
-
-#define KC_AMPR LSFT(KC_7) // &
-#define KC_AMPERSAND KC_AMPR
-
-#define KC_ASTR LSFT(KC_8) // *
-#define KC_ASTERISK KC_ASTR
-
-#define KC_LPRN LSFT(KC_9) // (
-#define KC_LEFT_PAREN KC_LPRN
-
-#define KC_RPRN LSFT(KC_0) // )
-#define KC_RIGHT_PAREN KC_RPRN
-
-#define KC_UNDS LSFT(KC_MINS) // _
-#define KC_UNDERSCORE KC_UNDS
-
-#define KC_PLUS LSFT(KC_EQL) // +
-
-#define KC_LCBR LSFT(KC_LBRC) // {
-#define KC_LEFT_CURLY_BRACE KC_LCBR
-
-#define KC_RCBR LSFT(KC_RBRC) // }
-#define KC_RIGHT_CURLY_BRACE KC_RCBR
-
-#define KC_COLN LSFT(KC_SCLN) // :
-#define KC_COLON KC_COLN
-
-#define KC_PIPE LSFT(KC_BSLS) // |
-
-#define KC_DELT KC_DELETE // Del key (four letter code)
-
-// Alias for function layers than expand past FN31
-#define FUNC(kc) kc | 0x2000
-
-// Aliases
-#define S(kc) LSFT(kc)
-#define F(kc) FUNC(kc)
-
-#define M(kc) kc | 0x3000
-
-#define MACRODOWN(...) (record->event.pressed ? MACRO(__VA_ARGS__) : MACRO_NONE)
-
-// These affect the backlight (if your keyboard has one).
-// We don't need to comment them out if your keyboard doesn't have a backlight,
-// since they don't take up any space.
-#define BL_ON 0x4009
-#define BL_OFF 0x4000
-#define BL_0 0x4000
-#define BL_1 0x4001
-#define BL_2 0x4002
-#define BL_3 0x4003
-#define BL_4 0x4004
-#define BL_5 0x4005
-#define BL_6 0x4006
-#define BL_7 0x4007
-#define BL_8 0x4008
-#define BL_9 0x4009
-#define BL_10 0x400A
-#define BL_11 0x400B
-#define BL_12 0x400C
-#define BL_13 0x400D
-#define BL_14 0x400E
-#define BL_15 0x400F
-#define BL_DEC 0x4010
-#define BL_INC 0x4011
-#define BL_TOGG 0x4012
-#define BL_STEP 0x4013
-
-#define RESET 0x5000
-#define DEBUG 0x5001
-
-// GOTO layer - 16 layers max
-// when:
-// ON_PRESS = 1
-// ON_RELEASE = 2
-// Unless you have a good reason not to do so, prefer ON_PRESS (1) as your default.
-#define TO(layer, when) (layer | 0x5100 | (when << 0x4))
-
-// Momentary switch layer - 256 layer max
-#define MO(layer) (layer | 0x5200)
-
-// Set default layer - 256 layer max
-#define DF(layer) (layer | 0x5300)
-
-// Toggle to layer - 256 layer max
-#define TG(layer) (layer | 0x5400)
-
-#define MIDI(n) (n | 0x6000)
-
-// M-od, T-ap - 256 keycode max
-#define MT(mod, kc) (kc | 0x7000 | ((mod & 0xF) << 8))
-#define CTL_T(kc) MT(0x1, kc)
-#define SFT_T(kc) MT(0x2, kc)
-#define ALT_T(kc) MT(0x4, kc)
-#define GUI_T(kc) MT(0x8, kc)
-#define C_S_T(kc) MT(0x3, kc) // Control + Shift e.g. for gnome-terminal
-#define MEH_T(kc) MT(0x7, kc) // Meh is a less hyper version of the Hyper key -- doesn't include Win or Cmd, so just alt+shift+ctrl
-#define LCAG_T(kc) MT(0xD, kc) // Left control alt and gui
-#define ALL_T(kc) MT(0xF, kc) // see http://brettterpstra.com/2012/12/08/a-useful-caps-lock-key/
-
-// Dedicated keycode versions for Hyper and Meh, if you want to use them as standalone keys rather than mod-tap
-#define KC_HYPR HYPR(KC_NO)
-#define KC_MEH MEH(KC_NO)
-
-// L-ayer, T-ap - 256 keycode max, 16 layer max
-#define LT(layer, kc) (kc | 0x8000 | ((layer & 0xF) << 8))
-
-// For sending unicode codes.
-// You may not send codes over 1FFF -- this supports most of UTF8.
-// To have a key that sends out Œ, go UC(0x0152)
-#define UNICODE(n) (n | 0x8000)
-#define UC(n) UNICODE(n)
-
-
-#endif
diff --git a/quantum/keymap_extras/keymap_bepo.h b/quantum/keymap_extras/keymap_bepo.h
index 1ab2d63ddd..e5ef39552a 100644
--- a/quantum/keymap_extras/keymap_bepo.h
+++ b/quantum/keymap_extras/keymap_bepo.h
@@ -2,7 +2,7 @@
#ifndef KEYMAP_BEPO_H
#define KEYMAP_BEPO_H
-#include "keymap_common.h"
+#include "keymap.h"
// Alt gr
#ifndef ALTGR
@@ -118,7 +118,7 @@
// Fourth row
#define BP_COLON LSFT(BP_DOT) // :
#define BP_COLN BP_COLON
-#define BP_QUESTION LSFT(BP_QUOTE) // ?
+#define BP_QUESTION LSFT(BP_APOS) // ?
#define BP_QEST BP_QUESTION
// Space bar
@@ -183,7 +183,7 @@
// Third row
#define BP_AE_LIGATURE ALTGR(BP_A) // æ
#define BP_AE BP_AE_LIGATURE
-#define BP_U_GRAVE AGR(BP_U) // ù
+#define BP_U_GRAVE ALTGR(BP_U) // ù
#define BP_UGRV BP_U_GRAVE
#define BP_DEAD_TREMA ALTGR(BP_I) // dead ¨ (trema/umlaut/diaresis)
#define BP_DTRM BP_DEAD_TREMA
diff --git a/quantum/keymap_extras/keymap_colemak.h b/quantum/keymap_extras/keymap_colemak.h
index 8a418c6159..b8d6157484 100644
--- a/quantum/keymap_extras/keymap_colemak.h
+++ b/quantum/keymap_extras/keymap_colemak.h
@@ -1,7 +1,7 @@
#ifndef KEYMAP_COLEMAK_H
#define KEYMAP_COLEMAK_H
-#include "keymap_common.h"
+#include "keymap.h"
// For software implementation of colemak
#define CM_Q KC_Q
#define CM_W KC_W
diff --git a/quantum/keymap_extras/keymap_dvorak.h b/quantum/keymap_extras/keymap_dvorak.h
index 93e355bf91..17f2059774 100644
--- a/quantum/keymap_extras/keymap_dvorak.h
+++ b/quantum/keymap_extras/keymap_dvorak.h
@@ -1,7 +1,7 @@
#ifndef KEYMAP_DVORAK_H
#define KEYMAP_DVORAK_H
-#include "keymap_common.h"
+#include "keymap.h"
// Normal characters
#define DV_GRV KC_GRV
@@ -18,18 +18,19 @@
#define DV_LBRC KC_MINS
#define DV_RBRC KC_EQL
-#define DV_QUOT KC_Q
+#define DV_QUOT KC_Q
#define DV_COMM KC_W
#define DV_DOT KC_E
#define DV_P KC_R
#define DV_Y KC_T
#define DV_F KC_Y
#define DV_G KC_U
-#define DV_C KC_I
+#define DV_C KC_I
#define DV_R KC_O
#define DV_L KC_P
#define DV_SLSH KC_LBRC
#define DV_EQL KC_RBRC
+#define DV_BSLS KC_BSLS
#define DV_A KC_A
#define DV_O KC_S
@@ -68,7 +69,13 @@
#define DV_RPRN LSFT(DV_0)
#define DV_LCBR LSFT(DV_LBRC)
#define DV_RCBR LSFT(DV_RBRC)
-#define DV_UNDS LSFT(DV_MINS)
-#define DV_PLUS LSFT(DV_EQL)
+
+#define DV_QUES LSFT(DV_SLSH)
+#define DV_PLUS LSFT(DV_EQL)
+#define DV_PIPE LSFT(DV_BSLS)
+
+#define DV_UNDS LSFT(DV_MINS)
+
+#define DV_COLN LSFT(DV_SCLN)
#endif
diff --git a/quantum/keymap_extras/keymap_fr_ch.h b/quantum/keymap_extras/keymap_fr_ch.h
index d3884c19a1..87d4bb24cf 100644
--- a/quantum/keymap_extras/keymap_fr_ch.h
+++ b/quantum/keymap_extras/keymap_fr_ch.h
@@ -1,10 +1,10 @@
#ifndef KEYMAP_FR_CH
#define KEYMAP_FR_CH
-#include "keymap_common.h"
+#include "keymap.h"
// Alt gr
-#define ALGR(kc) kc | 0x1400
+#define ALGR(kc) RALT(kc)
#define FR_CH_ALGR KC_RALT
// normal characters
diff --git a/quantum/keymap_extras/keymap_french.h b/quantum/keymap_extras/keymap_french.h
index e03a121a2b..834c69650e 100644
--- a/quantum/keymap_extras/keymap_french.h
+++ b/quantum/keymap_extras/keymap_french.h
@@ -1,10 +1,10 @@
#ifndef KEYMAP_FRENCH_H
#define KEYMAP_FRENCH_H
-#include "keymap_common.h"
+#include "keymap.h"
// Alt gr
-#define ALGR(kc) kc | 0x1400
+#define ALGR(kc) RALT(kc)
#define NO_ALGR KC_RALT
// Normal characters
@@ -80,4 +80,4 @@
#define FR_EURO ALGR(KC_E)
#define FR_BULT ALGR(FR_DLR)
-#endif \ No newline at end of file
+#endif
diff --git a/quantum/keymap_extras/keymap_french_osx.h b/quantum/keymap_extras/keymap_french_osx.h
index eb31bfb4d0..004d73ee23 100644
--- a/quantum/keymap_extras/keymap_french_osx.h
+++ b/quantum/keymap_extras/keymap_french_osx.h
@@ -1,7 +1,7 @@
#ifndef KEYMAP_FRENCH_OSX_H
#define KEYMAP_FRENCH_OSX_H
-#include "keymap_common.h"
+#include "keymap.h"
// Normal characters
#define FR_AT KC_GRV
diff --git a/quantum/keymap_extras/keymap_german.h b/quantum/keymap_extras/keymap_german.h
index dbad26f8a6..7e2e0ed44e 100644
--- a/quantum/keymap_extras/keymap_german.h
+++ b/quantum/keymap_extras/keymap_german.h
@@ -1,10 +1,10 @@
#ifndef KEYMAP_GERMAN
#define KEYMAP_GERMAN
-#include "keymap_common.h"
+#include "keymap.h"
// Alt gr
-#define ALGR(kc) kc | 0x1400
+#define ALGR(kc) RALT(kc)
#define DE_ALGR KC_RALT
// normal characters
diff --git a/quantum/keymap_extras/keymap_german_ch.h b/quantum/keymap_extras/keymap_german_ch.h
new file mode 100644
index 0000000000..b66d582a44
--- /dev/null
+++ b/quantum/keymap_extras/keymap_german_ch.h
@@ -0,0 +1,102 @@
+#ifndef KEYMAP_SWISS_GERMAN
+#define KEYMAP_SWISS_GERMAN
+
+#include "keymap.h"
+
+// Alt gr
+#define ALGR(kc) RALT(kc)
+#define CH_ALGR KC_RALT
+
+// normal characters
+#define CH_Z KC_Y
+#define CH_Y KC_Z
+
+#define CH_A KC_A
+#define CH_B KC_B
+#define CH_C KC_C
+#define CH_D KC_D
+#define CH_E KC_E
+#define CH_F KC_F
+#define CH_G KC_G
+#define CH_H KC_H
+#define CH_I KC_I
+#define CH_J KC_J
+#define CH_K KC_K
+#define CH_L KC_L
+#define CH_M KC_M
+#define CH_N KC_N
+#define CH_O KC_O
+#define CH_P KC_P
+#define CH_Q KC_Q
+#define CH_R KC_R
+#define CH_S KC_S
+#define CH_T KC_T
+#define CH_U KC_U
+#define CH_V KC_V
+#define CH_W KC_W
+#define CH_X KC_X
+
+#define CH_0 KC_0
+#define CH_1 KC_1
+#define CH_2 KC_2
+#define CH_3 KC_3
+#define CH_4 KC_4
+#define CH_5 KC_5
+#define CH_6 KC_6
+#define CH_7 KC_7
+#define CH_8 KC_8
+#define CH_9 KC_9
+
+#define CH_DOT KC_DOT
+#define CH_COMM KC_COMM
+
+#define CH_QUOT KC_MINS // ' ? ´
+#define CH_AE KC_QUOT
+#define CH_UE KC_LBRC
+#define CH_OE KC_SCLN
+
+#define CH_PARA KC_GRAVE // secction sign § and °
+#define CH_CARR KC_EQL // carret ^ ` ~
+#define CH_DIER KC_RBRC // dieresis ¨ ! ]
+#define CH_DLR KC_BSLS // $ £ }
+#define CH_LESS KC_NUBS // < and > and backslash
+#define CH_MINS KC_SLSH // - and _
+
+// shifted characters
+#define CH_RING LSFT(CH_PARA) // °
+#define CH_PLUS LSFT(KC_1) // +
+#define CH_DQOT LSFT(KC_2) // "
+#define CH_PAST LSFT(KC_3) // *
+#define CH_CELA LSFT(KC_4) // ç
+#define CH_PERC LSFT(KC_5) // %
+#define CH_AMPR LSFT(KC_6) // &
+#define CH_SLSH LSFT(KC_7) // /
+#define CH_LPRN LSFT(KC_8) // (
+#define CH_RPRN LSFT(KC_9) // )
+#define CH_EQL LSFT(KC_0) // =
+#define CH_QST LSFT(CH_QUOT) // ?
+#define CH_GRV LSFT(CH_CARR) // `
+#define CH_EXLM LSFT(CH_DIER) // !
+#define CH_POND LSFT(CH_DLR) // £
+#define CH_MORE LSFT(CH_LESS) // >
+#define CH_COLN LSFT(KC_DOT) // :
+#define CH_SCLN LSFT(KC_COMM) // ;
+#define CH_UNDS LSFT(CH_MINS) // _
+
+// Alt Gr-ed characters
+#define CH_BRBR ALGR(KC_1) // ¦ brocken bar
+#define CH_AT ALGR(KC_2) // @
+#define CH_HASH ALGR(KC_3) // #
+#define CH_NOTL ALGR(KC_6) // ¬ negative logic
+#define CH_PIPE ALGR(KC_7) // |
+#define CH_CENT ALGR(KC_8) // ¢ cent
+#define CH_ACUT ALGR(CH_QUOT) // ´
+#define CH_TILD ALGR(CH_CARR) // ~
+#define CH_EURO ALGR(KC_E) // €
+#define CH_LBRC ALGR(CH_UE) // [
+#define CH_RBRC ALGR(CH_DIER) // ]
+#define CH_LCBR ALGR(CH_AE) // {
+#define CH_RCBR ALGR(CH_DLR) // }
+#define CH_BSLS ALGR(CH_LESS) // backslash
+
+#endif
diff --git a/quantum/keymap_extras/keymap_german_osx.h b/quantum/keymap_extras/keymap_german_osx.h
index da91b4bf46..f63f066183 100644
--- a/quantum/keymap_extras/keymap_german_osx.h
+++ b/quantum/keymap_extras/keymap_german_osx.h
@@ -1,100 +1,97 @@
#ifndef KEYMAP_GERMAN_OSX
#define KEYMAP_GERMAN_OSX
-#ifdef KEYMAP_GERMAN
- #warning redefining german keys
-#endif
-#include "keymap_common.h"
+#include "keymap.h"
// Alt gr
// normal characters
-#define DE_Z KC_Y
-#define DE_Y KC_Z
+#define DE_OSX_Z KC_Y
+#define DE_OSX_Y KC_Z
-#define DE_A KC_A
-#define DE_B KC_B
-#define DE_C KC_C
-#define DE_D KC_D
-#define DE_E KC_E
-#define DE_F KC_F
-#define DE_G KC_G
-#define DE_H KC_H
-#define DE_I KC_I
-#define DE_J KC_J
-#define DE_K KC_K
-#define DE_L KC_L
-#define DE_M KC_M
-#define DE_N KC_N
-#define DE_O KC_O
-#define DE_P KC_P
-#define DE_Q KC_Q
-#define DE_R KC_R
-#define DE_S KC_S
-#define DE_T KC_T
-#define DE_U KC_U
-#define DE_V KC_V
-#define DE_W KC_W
-#define DE_X KC_X
+#define DE_OSX_A KC_A
+#define DE_OSX_B KC_B
+#define DE_OSX_C KC_C
+#define DE_OSX_D KC_D
+#define DE_OSX_E KC_E
+#define DE_OSX_F KC_F
+#define DE_OSX_G KC_G
+#define DE_OSX_H KC_H
+#define DE_OSX_I KC_I
+#define DE_OSX_J KC_J
+#define DE_OSX_K KC_K
+#define DE_OSX_L KC_L
+#define DE_OSX_M KC_M
+#define DE_OSX_N KC_N
+#define DE_OSX_O KC_O
+#define DE_OSX_P KC_P
+#define DE_OSX_Q KC_Q
+#define DE_OSX_R KC_R
+#define DE_OSX_S KC_S
+#define DE_OSX_T KC_T
+#define DE_OSX_U KC_U
+#define DE_OSX_V KC_V
+#define DE_OSX_W KC_W
+#define DE_OSX_X KC_X
-#define DE_0 KC_0
-#define DE_1 KC_1
-#define DE_2 KC_2
-#define DE_3 KC_3
-#define DE_4 KC_4
-#define DE_5 KC_5
-#define DE_6 KC_6
-#define DE_7 KC_7
-#define DE_8 KC_8
-#define DE_9 KC_9
+#define DE_OSX_0 KC_0
+#define DE_OSX_1 KC_1
+#define DE_OSX_2 KC_2
+#define DE_OSX_3 KC_3
+#define DE_OSX_4 KC_4
+#define DE_OSX_5 KC_5
+#define DE_OSX_6 KC_6
+#define DE_OSX_7 KC_7
+#define DE_OSX_8 KC_8
+#define DE_OSX_9 KC_9
-#define DE_DOT KC_DOT
-#define DE_COMM KC_COMM
+#define DE_OSX_DOT KC_DOT
+#define DE_OSX_COMM KC_COMM
-#define DE_SS KC_MINS
-#define DE_AE KC_QUOT
-#define DE_UE KC_LBRC
-#define DE_OE KC_SCLN
+#define DE_OSX_SS KC_MINS
+#define DE_OSX_AE KC_QUOT
+#define DE_OSX_UE KC_LBRC
+#define DE_OSX_OE KC_SCLN
-#define DE_CIRC KC_NUBS // accent circumflex ^ and ring °
-#define DE_ACUT KC_EQL // accent acute ´ and grave `
-#define DE_PLUS KC_RBRC // + and * and ~
-#define DE_HASH KC_BSLS // # and '
-#define DE_LESS KC_GRV // < and > and |
-#define DE_MINS KC_SLSH // - and _
+#define DE_OSX_CIRC KC_NUBS // accent circumflex ^ and ring °
+#define DE_OSX_ACUT KC_EQL // accent acute ´ and grave `
+#define DE_OSX_PLUS KC_RBRC // + and * and ~
+#define DE_OSX_HASH KC_BSLS // # and '
+#define DE_OSX_LESS KC_GRV // < and > and |
+#define DE_OSX_MINS KC_SLSH // - and _
// shifted characters
-#define DE_RING LSFT(DE_CIRC) // °
-#define DE_EXLM LSFT(KC_1) // !
-#define DE_DQOT LSFT(KC_2) // "
-#define DE_PARA LSFT(KC_3) // §
-#define DE_DLR LSFT(KC_4) // $
-#define DE_PERC LSFT(KC_5) // %
-#define DE_AMPR LSFT(KC_6) // &
-#define DE_SLSH LSFT(KC_7) // /
-#define DE_LPRN LSFT(KC_8) // (
-#define DE_RPRN LSFT(KC_9) // )
-#define DE_EQL LSFT(KC_0) // =
-#define DE_QST LSFT(DE_SS) // ?
-#define DE_GRV LSFT(DE_ACUT) // `
-#define DE_ASTR LSFT(DE_PLUS) // *
-#define DE_QUOT LSFT(DE_HASH) // '
-#define DE_MORE LSFT(DE_LESS) // >
-#define DE_COLN LSFT(KC_DOT) // :
-#define DE_SCLN LSFT(KC_COMM) // ;
-#define DE_UNDS LSFT(DE_MINS) // _
+#define DE_OSX_RING LSFT(DE_OSX_CIRC) // °
+#define DE_OSX_EXLM LSFT(KC_1) // !
+#define DE_OSX_DQOT LSFT(KC_2) // "
+#define DE_OSX_PARA LSFT(KC_3) // §
+#define DE_OSX_DLR LSFT(KC_4) // $
+#define DE_OSX_PERC LSFT(KC_5) // %
+#define DE_OSX_AMPR LSFT(KC_6) // &
+#define DE_OSX_SLSH LSFT(KC_7) // /
+#define DE_OSX_LPRN LSFT(KC_8) // (
+#define DE_OSX_RPRN LSFT(KC_9) // )
+#define DE_OSX_EQL LSFT(KC_0) // =
+#define DE_OSX_QST LSFT(DE_OSX_SS) // ?
+#define DE_OSX_GRV LSFT(DE_OSX_ACUT) // `
+#define DE_OSX_ASTR LSFT(DE_OSX_PLUS) // *
+#define DE_OSX_QUOT LSFT(DE_OSX_HASH) // '
+#define DE_OSX_MORE LSFT(DE_OSX_LESS) // >
+#define DE_OSX_COLN LSFT(KC_DOT) // :
+#define DE_OSX_SCLN LSFT(KC_COMM) // ;
+#define DE_OSX_UNDS LSFT(DE_OSX_MINS) // _
// Alt-ed characters
-#define DE_SQ2 LALT(KC_2) // ²
-#define DE_SQ3 LALT(KC_3) // ³
-#define DE_LCBR LALT(KC_8) // {
-#define DE_LBRC LALT(KC_5) // [
-#define DE_RBRC LALT(KC_6) // ]
-#define DE_RCBR LALT(KC_9) // }
-#define DE_BSLS LALT(LSFT(KC_7)) // backslash
-#define DE_AT LALT(DE_L) // @
-#define DE_EURO LALT(KC_E) // €
-#define DE_TILD LALT(DE_N) // ~
-#define DE_PIPE LALT(DE_7) // |
+//#define DE_OSX_SQ2 LALT(KC_2) // ²
+//#define DE_OSX_SQ3 LALT(KC_3) // ³
+#define DE_OSX_LCBR LALT(KC_8) // {
+#define DE_OSX_LBRC LALT(KC_5) // [
+#define DE_OSX_RBRC LALT(KC_6) // ]
+#define DE_OSX_RCBR LALT(KC_9) // }
+#define DE_OSX_BSLS LALT(LSFT(KC_7)) // backslash
+#define DE_OSX_AT LALT(DE_OSX_L) // @
+#define DE_OSX_EURO LALT(KC_E) // €
+#define DE_OSX_TILD LALT(DE_OSX_N) // ~
+#define DE_OSX_PIPE LALT(DE_OSX_7) // |
#endif
diff --git a/quantum/keymap_extras/keymap_neo2.h b/quantum/keymap_extras/keymap_neo2.h
index a35ba55a55..80439af347 100644
--- a/quantum/keymap_extras/keymap_neo2.h
+++ b/quantum/keymap_extras/keymap_neo2.h
@@ -1,8 +1,8 @@
#ifndef KEYMAP_NEO2
#define KEYMAP_NEO2
-#include "keymap_common.h"
-#include "keymap_extras/keymap_german.h"
+#include "keymap.h"
+#include "keymap_german.h"
#define NEO_A KC_D
#define NEO_B KC_N
diff --git a/quantum/keymap_extras/keymap_nordic.h b/quantum/keymap_extras/keymap_nordic.h
index 7ef41fb791..da5c829757 100644
--- a/quantum/keymap_extras/keymap_nordic.h
+++ b/quantum/keymap_extras/keymap_nordic.h
@@ -1,10 +1,10 @@
#ifndef KEYMAP_NORDIC_H
#define KEYMAP_NORDIC_H
-#include "keymap_common.h"
+#include "keymap.h"
// Alt gr
-#define ALGR(kc) kc | 0x1400
+#define ALGR(kc) RALT(kc)
#define NO_ALGR KC_RALT
// Normal characters
@@ -25,7 +25,7 @@
#define NO_SECT LSFT(NO_HALF)
#define NO_QUO2 LSFT(KC_2)
#define NO_BULT LSFT(KC_4)
-#define NO_AMP LSFT(KC_6)
+#define NO_AMPR LSFT(KC_6)
#define NO_SLSH LSFT(KC_7)
#define NO_LPRN LSFT(KC_8)
#define NO_RPRN LSFT(KC_9)
diff --git a/quantum/keymap_extras/keymap_norwegian.c b/quantum/keymap_extras/keymap_norwegian.h
index 018bfeae59..5c4e8c4955 100644
--- a/quantum/keymap_extras/keymap_norwegian.c
+++ b/quantum/keymap_extras/keymap_norwegian.h
@@ -13,7 +13,7 @@
#undef NO_BSLS
#define NO_BSLS KC_EQL // '\'
#undef NO_CIRC
-#define NO_CIRC LSFT(C_RBRC) // ^
+#define NO_CIRC LSFT(KC_RBRC) // ^
#undef NO_GRV
#define NO_GRV LSFT(NO_BSLS) //
#undef NO_OSLH
diff --git a/quantum/keymap_extras/keymap_plover.h b/quantum/keymap_extras/keymap_plover.h
new file mode 100644
index 0000000000..9b88f7d84d
--- /dev/null
+++ b/quantum/keymap_extras/keymap_plover.h
@@ -0,0 +1,32 @@
+#ifndef KEYMAP_PLOVER_H
+#define KEYMAP_PLOVER_H
+
+#include "keymap.h"
+
+#define PV_NUM KC_1
+#define PV_LS KC_Q
+#define PV_LT KC_W
+#define PV_LP KC_E
+#define PV_LH KC_R
+#define PV_LK KC_S
+#define PV_LW KC_D
+#define PV_LR KC_F
+
+#define PV_STAR KC_Y
+#define PV_RF KC_U
+#define PV_RP KC_I
+#define PV_RL KC_O
+#define PV_RT KC_P
+#define PV_RD KC_LBRC
+#define PV_RR KC_J
+#define PV_RB KC_K
+#define PV_RG KC_L
+#define PV_RS KC_SCLN
+#define PV_RZ KC_QUOT
+
+#define PV_A KC_C
+#define PV_O KC_V
+#define PV_E KC_N
+#define PV_U KC_M
+
+#endif
diff --git a/quantum/keymap_extras/keymap_russian.h b/quantum/keymap_extras/keymap_russian.h
new file mode 100644
index 0000000000..237e9abde3
--- /dev/null
+++ b/quantum/keymap_extras/keymap_russian.h
@@ -0,0 +1,77 @@
+#ifndef KEYMAP_RUSSIAN_H
+#define KEYMAP_RUSSIAN_H
+
+#include "keymap.h"
+
+// Normal Chracters // reg SHIFT
+#define RU_A KC_F // а and А
+#define RU_BE KC_COMM // б and Б
+#define RU_VE KC_D // в and В
+#define RU_GHE KC_U // г and Г
+#define RU_DE KC_L // д and Д
+#define RU_IE KC_T // е and Е
+#define RU_IO KC_GRV // ё and Ё
+#define RU_ZHE KC_SCLN // ж and Ж
+#define RU_ZE KC_P // з and З
+#define RU_I KC_B // и and И
+#define RU_SRT_I KC_Q // й and Й
+#define RU_KA KC_R // к and К
+#define RU_EL KC_K // л and Л
+#define RU_EM KC_V // м and М
+#define RU_EN KC_Y // н and Н
+#define RU_O KC_J // о and О
+#define RU_PE KC_G // п and П
+#define RU_ER KC_H // р and Р
+#define RU_ES KC_C // с and С
+#define RU_TE KC_N // т and Т
+#define RU_U KC_E // у and У
+#define RU_EF KC_A // ф and Ф
+#define RU_HA KC_LBRC // х and Х
+#define RU_TSE KC_W // ц and Ц
+#define RU_CHE KC_X // ч and Ч
+#define RU_SHA KC_I // ш and Ш
+#define RU_SHCHA KC_O // щ and Щ
+#define RU_HSIGN KC_RBRC // ъ and Ъ
+#define RU_YERU KC_S // ы and Ы
+#define RU_SSIGN KC_M // ь and Ь
+#define RU_E KC_QUOT // э and Э
+#define RU_YU KC_DOT // ю and Ю
+#define RU_YA KC_Z // я and Я
+
+#define RU_1 KC_1 // 1 and !
+#define RU_2 KC_2 // 2 and "
+#define RU_3 KC_3 // 3 and №
+#define RU_4 KC_4 // 4 and ;
+#define RU_5 KC_5 // 5 and %
+#define RU_6 KC_6 // 6 and :
+#define RU_7 KC_7 // 7 and ?
+#define RU_8 KC_8 // 8 and *
+#define RU_9 KC_9 // 9 and (
+#define RU_0 KC_0 // 0 and )
+
+#define RU_MINS KC_MINS // - and _
+#define RU_EQL KC_EQL // = and +
+#define RU_BSLS KC_BSLS // \ and /
+#define RU_DOT KC_SLSH // . and ,
+
+// Shifted Chracters
+#define RU_EXLM LSFT(RU_1) // !
+#define RU_DQUT LSFT(RU_2) // "
+#define RU_NMRO LSFT(RU_3) // №
+#define RU_SCLN LSFT(RU_4) // ;
+#define RU_PERC LSFT(RU_5) // %
+#define RU_COLN LSFT(RU_6) // :
+#define RU_QUES LSFT(RU_7) // ?
+#define RU_ASTR LSFT(RU_8) // *
+#define RU_LPRN LSFT(RU_9) // (
+#define RU_RPRN LSFT(RU_0) // )
+
+#define RU_UNDR LSFT(RU_MINS) // _
+#define RU_PLUS LSFT(RU_EQL) // +
+#define RU_SLSH LSFT(RU_BSLS) // /
+#define RU_COMM LSFT(RU_DOT) // ,
+
+// Alt Gr-ed characters
+#define RU_RUBL RALT(RU_8) // ₽
+
+#endif
diff --git a/quantum/keymap_extras/keymap_spanish.h b/quantum/keymap_extras/keymap_spanish.h
index 7f980afbcf..4ba568af23 100644
--- a/quantum/keymap_extras/keymap_spanish.h
+++ b/quantum/keymap_extras/keymap_spanish.h
@@ -1,10 +1,10 @@
#ifndef KEYMAP_SPANISH_H
#define KEYMAP_SPANISH_H
-#include "keymap_common.h"
+#include "keymap.h"
// Alt gr
-#define ALGR(kc) kc | 0x1400
+#define ALGR(kc) RALT(kc)
#define NO_ALGR KC_RALT
// Normal characters
@@ -49,7 +49,7 @@
#define ES_PIPE ALGR(KC_1)
#define ES_AT ALGR(KC_2)
#define ES_HASH ALGR(KC_3)
-#define ES_TILD ALGR(KC_4)
+#define ES_TILD ALGR(ES_NTIL)
#define ES_EURO ALGR(KC_5)
#define ES_NOT ALGR(KC_6)
@@ -59,4 +59,4 @@
#define ES_LCBR ALGR(ES_ACUT)
#define ES_RCRB ALGR(ES_CCED)
-#endif \ No newline at end of file
+#endif
diff --git a/quantum/keymap_extras/keymap_uk.h b/quantum/keymap_extras/keymap_uk.h
index 5b4bd3c0d4..00c87afc3e 100644
--- a/quantum/keymap_extras/keymap_uk.h
+++ b/quantum/keymap_extras/keymap_uk.h
@@ -1,10 +1,10 @@
#ifndef KEYMAP_UK_H
#define KEYMAP_UK_H
-#include "keymap_common.h"
+#include "keymap.h"
// Alt gr
-#define ALGR(kc) kc | 0x1400
+#define ALGR(kc) RALT(kc)
#define NO_ALGR KC_RALT
// Normal characters
@@ -33,4 +33,4 @@
#define UK_AACT ALGR(KC_A)
-#endif \ No newline at end of file
+#endif
diff --git a/quantum/keymap_extras/keymap_unicode_cyrillic.h b/quantum/keymap_extras/keymap_unicode_cyrillic.h
new file mode 100644
index 0000000000..a40626d911
--- /dev/null
+++ b/quantum/keymap_extras/keymap_unicode_cyrillic.h
@@ -0,0 +1,163 @@
+#ifndef KEYMAP_CYRILLIC_H
+#define KEYMAP_CYRILLIC_H
+
+#include "keymap.h"
+
+/*
+ * This is based off of
+ * https://en.wikipedia.org/wiki/Cyrillic_script
+ *
+ * Unicode is iffy, a software implementation is preferred
+ */
+
+// Capital Char russian/ukrainian/bulgarian
+#define CY_A UC(0x0410) // А rus ukr bul
+#define CY_BE UC(0x0411) // Б rus ukr bul
+#define CY_VE UC(0x0412) // В rus ukr bul
+#define CY_GHE UC(0x0413) // Г rus ukr bul
+#define CY_GHEUP UC(0x0490) // Ґ ukr
+#define CY_DE UC(0x0414) // Д rus ukr bul
+#define CY_DJE UC(0x0402) // Ђ
+#define CY_GJE UC(0x0403) // Ѓ
+#define CY_IE UC(0x0415) // Е rus ukr bul
+#define CY_IO UC(0x0401) // Ё rus
+#define CY_UIE UC(0x0404) // Є ukr
+#define CY_ZHE UC(0x0416) // Ж rus ukr bul
+#define CY_ZE UC(0x0417) // З rus ukr bul
+#define CY_DZE UC(0x0405) // Ѕ
+#define CY_I UC(0x0418) // И rus ukr bul
+#define CY_B_U_I UC(0x0406) // І ukr
+#define CY_YI UC(0x0407) // Ї ukr
+#define CY_SRT_I UC(0x0419) // Й rus ukr bul
+#define CY_JE UC(0x0408) // Ј
+#define CY_KA UC(0x041a) // К rus ukr bul
+#define CY_EL UC(0x041b) // Л rus ukr bul
+#define CY_LJE UC(0x0409) // Љ
+#define CY_EM UC(0x041c) // М rus ukr bul
+#define CY_EN UC(0x041d) // Н rus ukr bul
+#define CY_NJE UC(0x040a) // Њ
+#define CY_O UC(0x041e) // О rus ukr bul
+#define CY_PE UC(0x041f) // П rus ukr bul
+#define CY_ER UC(0x0420) // Р rus ukr bul
+#define CY_ES UC(0x0421) // С rus ukr bul
+#define CY_TE UC(0x0422) // Т rus ukr bul
+#define CY_TSHE UC(0x040b) // Ћ
+#define CY_KJE UC(0x040c) // Ќ
+#define CY_U UC(0x0423) // У rus ukr bul
+#define CY_SRT_U UC(0x040e) // Ў
+#define CY_EF UC(0x0424) // Ф rus ukr bul
+#define CY_HA UC(0x0425) // Х rus bul
+#define CY_TSE UC(0x0426) // Ц rus ukr bul
+#define CY_CHE UC(0x0427) // Ч rus ukr bul
+#define CY_DZHE UC(0x040f) // Џ
+#define CY_SHA UC(0x0428) // Ш rus ukr bul
+#define CY_SHCHA UC(0x0429) // Щ rus ukr bul
+#define CY_HSIGN UC(0x042a) // Ъ rus bul
+#define CY_YERU UC(0x042b) // Ы rus
+#define CY_SSIGN UC(0x042c) // Ь rus ukr bul
+#define CY_E UC(0x042d) // Э rus
+#define CY_YU UC(0x042e) // Ю rus ukr bul
+#define CY_YA UC(0x042f) // Я rus ukr bul
+// Important Cyrillic non-Slavic letters
+#define CY_PALOCHKA UC(0x04c0) // Ӏ
+#define CY_SCHWA UC(0x04d8) // Ә
+#define CY_GHE_S UC(0x0492) // Ғ
+#define CY_ZE_D UC(0x0498) // Ҙ
+#define CY_ES_D UC(0x04aa) // Ҫ
+#define CY_BR_KA UC(0x04a0) // Ҡ
+#define CY_ZHE_D UC(0x0496) // Җ
+#define CY_KA_D UC(0x049a) // Қ
+#define CY_EN_D UC(0x04a2) // Ң
+#define CY_ENGHE UC(0x04a4) // Ҥ
+#define CY_BRD_O UC(0x04e8) // Ө
+#define CY_STR_U UC(0x04ae) // Ү
+#define CY_S_U_S UC(0x04b0) // Ұ
+#define CY_SHHA UC(0x04ba) // Һ
+#define CY_HA_D UC(0x04b2) // Ҳ
+
+
+// Small
+#define CY_a UC(0x0430) // a rus ukr bul
+#define CY_be UC(0x0431) // б rus ukr bul
+#define CY_ve UC(0x0432) // в rus ukr bul
+#define CY_ghe UC(0x0433) // г rus ukr bul
+#define CY_gheup UC(0x0491) // ґ ukr
+#define CY_de UC(0x0434) // д rus ukr bul
+#define CY_dje UC(0x0452) // ђ
+#define CY_gje UC(0x0453) // ѓ
+#define CY_ie UC(0x0435) // е rus ukr bul
+#define CY_io UC(0x0451) // ё rus
+#define CY_uie UC(0x0454) // є ukr
+#define CY_zhe UC(0x0436) // ж rus ukr bul
+#define CY_ze UC(0x0437) // з rus ukr bul
+#define CY_dze UC(0x0455) // ѕ
+#define CY_i UC(0x0438) // и rus ukr bul
+#define CY_b_u_i UC(0x0456) // і ukr
+#define CY_yi UC(0x0457) // ї ukr
+#define CY_srt_i UC(0x0439) // й rus ukr bul
+#define CY_je UC(0x0458) // ј
+#define CY_ka UC(0x043a) // к rus ukr bul
+#define CY_el UC(0x043b) // л rus ukr bul
+#define CY_lje UC(0x0459) // љ
+#define CY_em UC(0x043c) // м rus ukr bul
+#define CY_en UC(0x043d) // н rus ukr bul
+#define CY_nje UC(0x045a) // њ
+#define CY_o UC(0x043e) // о rus ukr bul
+#define CY_pe UC(0x043f) // п rus ukr bul
+#define CY_er UC(0x0440) // р rus ukr bul
+#define CY_es UC(0x0441) // с rus ukr bul
+#define CY_te UC(0x0442) // т rus ukr bul
+#define CY_tshe UC(0x045b) // ћ
+#define CY_kje UC(0x045c) // ќ
+#define CY_u UC(0x0443) // у rus ukr bul
+#define CY_srt_u UC(0x045e) // ў
+#define CY_ef UC(0x0444) // ф rus ukr bul
+#define CY_ha UC(0x0445) // х rus ukr bul
+#define CY_tse UC(0x0446) // ц rus ukr bul
+#define CY_che UC(0x0447) // ч rus ukr bul
+#define CY_dzhe UC(0x045f) // џ
+#define CY_sha UC(0x0448) // ш rus ukr bul
+#define CY_shcha UC(0x0449) // щ rus ukr bul
+#define CY_hsign UC(0x044a) // ъ rus bul
+#define CY_yeru UC(0x044b) // ы rus
+#define CY_ssign UC(0x044c) // ь rus ukr bul
+#define CY_e UC(0x044d) // э rus
+#define CY_yu UC(0x044e) // ю rus ukr bul
+#define CY_ya UC(0x044f) // я rus ukr bul
+// Important Cyrillic non-Slavic letters
+#define CY_palochka UC(0x04cf) // ӏ
+#define CY_schwa UC(0x04d9) // ә
+#define CY_ghe_s UC(0x0493) // ғ
+#define CY_ze_d UC(0x0499) // ҙ
+#define CY_es_d UC(0x04ab) // ҫ
+#define CY_br_ka UC(0x04a1) // ҡ
+#define CY_zhe_d UC(0x0497) // җ
+#define CY_ka_d UC(0x049b) // қ
+#define CY_en_d UC(0x04a3) // ң
+#define CY_enghe UC(0x04a5) // ҥ
+#define CY_brd_o UC(0x04e9) // ө
+#define CY_str_u UC(0x04af) // ү
+#define CY_s_u_s UC(0x04b1) // ұ
+#define CY_shha UC(0x04bb) // һ
+#define CY_ha_d UC(0x04b3) // ҳ
+
+
+// Extra
+#define CY_slr_ve UC(0x1c80) // ᲀ CYRILLIC SMALL LETTER ROUNDED VE
+#define CY_ll_de UC(0x1c81) // ᲁ CYRILLIC SMALL LETTER LONG-LEGGED DE
+#define CY_ZEMLYA UC(0xa640) // Ꙁ CYRILLIC CAPITAL LETTER ZEMLYA
+#define CY_zemlya UC(0xa641) // ꙁ CYRILLIC SMALL LETTER ZEMLYA
+#define CY_RV_DZE UC(0xa644) // Ꙅ CYRILLIC CAPITAL LETTER REVERSED DZE
+#define CY_rv_DZE UC(0xa645) // ꙅ CYRILLIC SMALL LETTER REVERSED DZE
+#define CY_slw_es UC(0x1c83) // ᲃ CYRILLIC SMALL LETTER WIDE ES
+#define CY_st_te UC(0x1c84) // ᲄ CYRILLIC SMALL LETTER TALL TE
+#define CY_3l_te UC(0x1c85) // ᲅ CYRILLIC SMALL LETTER THREE-LEGGED TE
+#define CY_thsign UC(0x1c86) // ᲆ CYRILLIC SMALL LETTER TALL HARD SIGN
+#define CY_YERUBY UC(0xa650) // Ꙑ CYRILLIC CAPITAL LETTER YERU WITH BACK YER
+#define CY_yeruby UC(0xa651) // ꙑ CYRILLIC SMALL LETTER YERU WITH BACK YER
+#define CY_RUBL UC(0x20bd) // ₽
+#define CY_NMRO UC(0x2116) // №
+
+// The letters Zje and Sje are made for other letters and accent marks
+
+#endif
diff --git a/quantum/keymap_midi.c b/quantum/keymap_midi.c
deleted file mode 100644
index ac45d25892..0000000000
--- a/quantum/keymap_midi.c
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
-Copyright 2015 Jack Humbert <jack.humb@gmail.com>
-
-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 "keymap_common.h"
-#include "keymap_midi.h"
-
-uint8_t starting_note = 0x0C;
-int offset = 7;
-
-void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
-{
- if (id != 0) {
- if (record->event.pressed) {
- midi_send_noteon(&midi_device, opt, (id & 0xFF), 127);
- } else {
- midi_send_noteoff(&midi_device, opt, (id & 0xFF), 127);
- }
- }
-
- if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
- if (record->event.pressed) {
- starting_note++;
- play_note(((double)261.626)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
- midi_send_cc(&midi_device, 0, 0x7B, 0);
- midi_send_cc(&midi_device, 1, 0x7B, 0);
- midi_send_cc(&midi_device, 2, 0x7B, 0);
- midi_send_cc(&midi_device, 3, 0x7B, 0);
- midi_send_cc(&midi_device, 4, 0x7B, 0);
- return;
- } else {
- stop_note(((double)261.626)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)));
- stop_all_notes();
- return;
- }
- }
- if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
- if (record->event.pressed) {
- starting_note--;
- play_note(((double)261.626)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
- midi_send_cc(&midi_device, 0, 0x7B, 0);
- midi_send_cc(&midi_device, 1, 0x7B, 0);
- midi_send_cc(&midi_device, 2, 0x7B, 0);
- midi_send_cc(&midi_device, 3, 0x7B, 0);
- midi_send_cc(&midi_device, 4, 0x7B, 0);
- return;
- } else {
- stop_note(((double)261.626)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)));
- stop_all_notes();
- return;
- }
- }
-
- if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
- offset++;
- midi_send_cc(&midi_device, 0, 0x7B, 0);
- midi_send_cc(&midi_device, 1, 0x7B, 0);
- midi_send_cc(&midi_device, 2, 0x7B, 0);
- midi_send_cc(&midi_device, 3, 0x7B, 0);
- midi_send_cc(&midi_device, 4, 0x7B, 0);
- stop_all_notes();
- for (int i = 0; i <= 7; i++) {
- play_note(((double)261.626)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
- _delay_us(80000);
- stop_note(((double)261.626)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)));
- _delay_us(8000);
- }
- return;
- }
- if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
- offset--;
- midi_send_cc(&midi_device, 0, 0x7B, 0);
- midi_send_cc(&midi_device, 1, 0x7B, 0);
- midi_send_cc(&midi_device, 2, 0x7B, 0);
- midi_send_cc(&midi_device, 3, 0x7B, 0);
- midi_send_cc(&midi_device, 4, 0x7B, 0);
- stop_all_notes();
- for (int i = 0; i <= 7; i++) {
- play_note(((double)261.626)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
- _delay_us(80000);
- stop_note(((double)261.626)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)));
- _delay_us(8000);
- }
- return;
- }
-
- if (record->event.pressed) {
- // midi_send_noteon(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
- // midi_send_noteon(&midi_device, 0, (starting_note + SCALE[record->event.key.col + offset])+12*(MATRIX_ROWS - record->event.key.row), 127);
- play_note(((double)261.626)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)), 0xF);
- } else {
- // midi_send_noteoff(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
- // midi_send_noteoff(&midi_device, 0, (starting_note + SCALE[record->event.key.col + offset])+12*(MATRIX_ROWS - record->event.key.row), 127);
- stop_note(((double)261.626)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)));
- }
-} \ No newline at end of file
diff --git a/quantum/keymap_unicode.c b/quantum/keymap_unicode.c
deleted file mode 100644
index a44965e611..0000000000
--- a/quantum/keymap_unicode.c
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
-Copyright 2015 Jack Humbert <jack.humb@gmail.com>
-
-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 "keymap_common.h"
-
-uint16_t hextokeycode(int hex) {
- if (hex == 0x0) {
- return KC_0;
- } else if (hex < 0xA) {
- return KC_1 + (hex - 0x1);
- } else {
- return KC_A + (hex - 0xA);
- }
-}
-
-void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
-{
-
- // For more info on how this works per OS, see here: https://en.wikipedia.org/wiki/Unicode_input#Hexadecimal_code_input
-
- if (record->event.pressed) {
- uint16_t unicode = (opt << 8) | id;
- register_code(KC_LALT);
-
- register_code(hextokeycode((unicode & 0xF000) >> 12));
- unregister_code(hextokeycode((unicode & 0xF000) >> 12));
- register_code(hextokeycode((unicode & 0x0F00) >> 8));
- unregister_code(hextokeycode((unicode & 0x0F00) >> 8));
- register_code(hextokeycode((unicode & 0x00F0) >> 4));
- unregister_code(hextokeycode((unicode & 0x00F0) >> 4));
- register_code(hextokeycode((unicode & 0x000F)));
- unregister_code(hextokeycode((unicode & 0x000F)));
-
- /* Test 'a' */
- // register_code(hextokeycode(0x0));
- // unregister_code(hextokeycode(0x0));
- // register_code(hextokeycode(0x0));
- // unregister_code(hextokeycode(0x0));
- // register_code(hextokeycode(0x6));
- // unregister_code(hextokeycode(0x6));
- // register_code(hextokeycode(0x1));
- // unregister_code(hextokeycode(0x1));
-
- unregister_code(KC_LALT);
- }
- return;
-} \ No newline at end of file
diff --git a/quantum/led.c b/quantum/led.c
deleted file mode 100644
index 9cdb8a5c20..0000000000
--- a/quantum/led.c
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
-Copyright 2012 Jun Wako <wakojun@gmail.com>
-
-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 <avr/io.h>
-#include "stdint.h"
-#include "led.h"
-
-__attribute__ ((weak))
-void led_set_kb(uint8_t usb_led) {
-
-}
-
-void led_set(uint8_t usb_led)
-{
-
- // Example LED Code
- //
- // // Using PE6 Caps Lock LED
- // if (usb_led & (1<<USB_LED_CAPS_LOCK))
- // {
- // // Output high.
- // DDRE |= (1<<6);
- // PORTE |= (1<<6);
- // }
- // else
- // {
- // // Output low.
- // DDRE &= ~(1<<6);
- // PORTE &= ~(1<<6);
- // }
-
- led_set_kb(usb_led);
-}
diff --git a/quantum/light_ws2812.c b/quantum/light_ws2812.c
index f20043067e..401845e855 100755
--- a/quantum/light_ws2812.c
+++ b/quantum/light_ws2812.c
@@ -19,12 +19,16 @@
// Setleds for standard RGB
void inline ws2812_setleds(struct cRGB *ledarray, uint16_t leds)
{
- ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin));
+ // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin));
+ ws2812_setleds_pin(ledarray,leds, _BV(RGB_DI_PIN & 0xF));
}
void inline ws2812_setleds_pin(struct cRGB *ledarray, uint16_t leds, uint8_t pinmask)
{
- ws2812_DDRREG |= pinmask; // Enable DDR
+ // ws2812_DDRREG |= pinmask; // Enable DDR
+ // new universal format (DDR)
+ _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask;
+
ws2812_sendarray_mask((uint8_t*)ledarray,leds+leds+leds,pinmask);
_delay_us(50);
}
@@ -32,14 +36,17 @@ void inline ws2812_setleds_pin(struct cRGB *ledarray, uint16_t leds, uint8_t pin
// Setleds for SK6812RGBW
void inline ws2812_setleds_rgbw(struct cRGBW *ledarray, uint16_t leds)
{
- ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR
- ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(ws2812_pin));
+ // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR
+ // new universal format (DDR)
+ _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF);
+
+ ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF));
_delay_us(80);
}
void ws2812_sendarray(uint8_t *data,uint16_t datlen)
{
- ws2812_sendarray_mask(data,datlen,_BV(ws2812_pin));
+ ws2812_sendarray_mask(data,datlen,_BV(RGB_DI_PIN & 0xF));
}
/*
@@ -108,8 +115,10 @@ void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi)
uint8_t curbyte,ctr,masklo;
uint8_t sreg_prev;
- masklo =~maskhi&ws2812_PORTREG;
- maskhi |= ws2812_PORTREG;
+ // masklo =~maskhi&ws2812_PORTREG;
+ // maskhi |= ws2812_PORTREG;
+ masklo =~maskhi&_SFR_IO8((RGB_DI_PIN >> 4) + 2);
+ maskhi |= _SFR_IO8((RGB_DI_PIN >> 4) + 2);
sreg_prev=SREG;
cli();
@@ -173,7 +182,7 @@ w_nop16
" dec %0 \n\t" // '1' [+2] '0' [+2]
" brne loop%=\n\t" // '1' [+3] '0' [+4]
: "=&d" (ctr)
- : "r" (curbyte), "I" (_SFR_IO_ADDR(ws2812_PORTREG)), "r" (maskhi), "r" (masklo)
+ : "r" (curbyte), "I" (_SFR_IO_ADDR(_SFR_IO8((RGB_DI_PIN >> 4) + 2))), "r" (maskhi), "r" (masklo)
);
}
diff --git a/quantum/matrix.c b/quantum/matrix.c
index 2dab6ae941..3174e07390 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -1,6 +1,6 @@
/*
-Copyright 2012 Jun Wako
-Generated by planckkeyboard.com (2014 Jack Humbert)
+Copyright 2012 Jun Wako
+Copyright 2014 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
@@ -15,23 +15,26 @@ 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/>.
*/
-
-/*
- * scan matrix
- */
#include <stdint.h>
#include <stdbool.h>
+#if defined(__AVR__)
#include <avr/io.h>
-#include <util/delay.h>
+#endif
+#include "wait.h"
#include "print.h"
#include "debug.h"
#include "util.h"
#include "matrix.h"
-#ifndef DEBOUNCE
-# define DEBOUNCE 10
+/* Set 0 if debouncing isn't needed */
+
+#ifndef DEBOUNCING_DELAY
+# define DEBOUNCING_DELAY 5
#endif
-static uint8_t debouncing = DEBOUNCE;
+static uint8_t debouncing = DEBOUNCING_DELAY;
+
+static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
+static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
/* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS];
@@ -42,39 +45,85 @@ static matrix_row_t matrix_debouncing[MATRIX_ROWS];
static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
#endif
+#if MATRIX_COLS > 16
+ #define SHIFTER 1UL
+#else
+ #define SHIFTER 1
+#endif
+
static matrix_row_t read_cols(void);
static void init_cols(void);
static void unselect_rows(void);
static void select_row(uint8_t row);
__attribute__ ((weak))
-void matrix_init_kb(void) {
+void matrix_init_quantum(void) {
+ matrix_init_kb();
+}
+
+__attribute__ ((weak))
+void matrix_scan_quantum(void) {
+ matrix_scan_kb();
+}
+__attribute__ ((weak))
+void matrix_init_kb(void) {
+ matrix_init_user();
}
__attribute__ ((weak))
void matrix_scan_kb(void) {
+ matrix_scan_user();
+}
+__attribute__ ((weak))
+void matrix_init_user(void) {
+}
+
+__attribute__ ((weak))
+void matrix_scan_user(void) {
}
inline
-uint8_t matrix_rows(void)
-{
+uint8_t matrix_rows(void) {
return MATRIX_ROWS;
}
inline
-uint8_t matrix_cols(void)
-{
+uint8_t matrix_cols(void) {
return MATRIX_COLS;
}
-void matrix_init(void)
-{
+// void matrix_power_up(void) {
+// #if DIODE_DIRECTION == COL2ROW
+// for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
+// /* DDRxn */
+// _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
+// toggle_row(r);
+// }
+// for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
+// /* PORTxn */
+// _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
+// }
+// #else
+// for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
+// /* DDRxn */
+// _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
+// toggle_col(c);
+// }
+// for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
+// /* PORTxn */
+// _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
+// }
+// #endif
+// }
+
+void matrix_init(void) {
// To use PORTF disable JTAG with writing JTD bit twice within four cycles.
- MCUCR |= (1<<JTD);
- MCUCR |= (1<<JTD);
-
+ #ifdef __AVR_ATmega32U4__
+ MCUCR |= _BV(JTD);
+ MCUCR |= _BV(JTD);
+ #endif
// initialize row and col
unselect_rows();
@@ -86,31 +135,30 @@ void matrix_init(void)
matrix_debouncing[i] = 0;
}
- matrix_init_kb();
+ matrix_init_quantum();
}
-
uint8_t matrix_scan(void)
{
#if DIODE_DIRECTION == COL2ROW
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
select_row(i);
- _delay_us(30); // without this wait read unstable value.
+ wait_us(30); // without this wait read unstable value.
matrix_row_t cols = read_cols();
if (matrix_debouncing[i] != cols) {
matrix_debouncing[i] = cols;
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); debug("\n");
}
- debouncing = DEBOUNCE;
+ debouncing = DEBOUNCING_DELAY;
}
unselect_rows();
}
if (debouncing) {
if (--debouncing) {
- _delay_ms(1);
+ wait_ms(1);
} else {
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
matrix[i] = matrix_debouncing[i];
@@ -120,21 +168,21 @@ uint8_t matrix_scan(void)
#else
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
select_row(i);
- _delay_us(30); // without this wait read unstable value.
+ wait_us(30); // without this wait read unstable value.
matrix_row_t rows = read_cols();
if (matrix_reversed_debouncing[i] != rows) {
matrix_reversed_debouncing[i] = rows;
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); debug("\n");
}
- debouncing = DEBOUNCE;
+ debouncing = DEBOUNCING_DELAY;
}
unselect_rows();
}
if (debouncing) {
if (--debouncing) {
- _delay_ms(1);
+ wait_ms(1);
} else {
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
matrix_reversed[i] = matrix_reversed_debouncing[i];
@@ -150,7 +198,7 @@ uint8_t matrix_scan(void)
}
#endif
- matrix_scan_kb();
+ matrix_scan_quantum();
return 1;
}
@@ -194,32 +242,16 @@ uint8_t matrix_key_count(void)
static void init_cols(void)
{
- int B = 0, C = 0, D = 0, E = 0, F = 0;
-
#if DIODE_DIRECTION == COL2ROW
for(int x = 0; x < MATRIX_COLS; x++) {
- int col = COLS[x];
+ int pin = col_pins[x];
#else
for(int x = 0; x < MATRIX_ROWS; x++) {
- int col = ROWS[x];
+ int pin = row_pins[x];
#endif
- if ((col & 0xF0) == 0x20) {
- B |= (1<<(col & 0x0F));
- } else if ((col & 0xF0) == 0x30) {
- C |= (1<<(col & 0x0F));
- } else if ((col & 0xF0) == 0x40) {
- D |= (1<<(col & 0x0F));
- } else if ((col & 0xF0) == 0x50) {
- E |= (1<<(col & 0x0F));
- } else if ((col & 0xF0) == 0x60) {
- F |= (1<<(col & 0x0F));
- }
+ _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
+ _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
}
- DDRB &= ~(B); PORTB |= (B);
- DDRC &= ~(C); PORTC |= (C);
- DDRD &= ~(D); PORTD |= (D);
- DDRE &= ~(E); PORTE |= (E);
- DDRF &= ~(F); PORTF |= (F);
}
static matrix_row_t read_cols(void)
@@ -228,80 +260,38 @@ static matrix_row_t read_cols(void)
#if DIODE_DIRECTION == COL2ROW
for(int x = 0; x < MATRIX_COLS; x++) {
- int col = COLS[x];
+ int pin = col_pins[x];
#else
for(int x = 0; x < MATRIX_ROWS; x++) {
- int col = ROWS[x];
+ int pin = row_pins[x];
#endif
-
- if ((col & 0xF0) == 0x20) {
- result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x));
- } else if ((col & 0xF0) == 0x30) {
- result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x));
- } else if ((col & 0xF0) == 0x40) {
- result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x));
- } else if ((col & 0xF0) == 0x50) {
- result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x));
- } else if ((col & 0xF0) == 0x60) {
- result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x));
- }
+ result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (SHIFTER << x);
}
return result;
}
static void unselect_rows(void)
{
- int B = 0, C = 0, D = 0, E = 0, F = 0;
-
#if DIODE_DIRECTION == COL2ROW
for(int x = 0; x < MATRIX_ROWS; x++) {
- int row = ROWS[x];
+ int pin = row_pins[x];
#else
for(int x = 0; x < MATRIX_COLS; x++) {
- int row = COLS[x];
+ int pin = col_pins[x];
#endif
- if ((row & 0xF0) == 0x20) {
- B |= (1<<(row & 0x0F));
- } else if ((row & 0xF0) == 0x30) {
- C |= (1<<(row & 0x0F));
- } else if ((row & 0xF0) == 0x40) {
- D |= (1<<(row & 0x0F));
- } else if ((row & 0xF0) == 0x50) {
- E |= (1<<(row & 0x0F));
- } else if ((row & 0xF0) == 0x60) {
- F |= (1<<(row & 0x0F));
- }
+ _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
+ _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
}
- DDRB &= ~(B); PORTB |= (B);
- DDRC &= ~(C); PORTC |= (C);
- DDRD &= ~(D); PORTD |= (D);
- DDRE &= ~(E); PORTE |= (E);
- DDRF &= ~(F); PORTF |= (F);
}
static void select_row(uint8_t row)
{
#if DIODE_DIRECTION == COL2ROW
- int row_pin = ROWS[row];
+ int pin = row_pins[row];
#else
- int row_pin = COLS[row];
+ int pin = col_pins[row];
#endif
-
- if ((row_pin & 0xF0) == 0x20) {
- DDRB |= (1<<(row_pin & 0x0F));
- PORTB &= ~(1<<(row_pin & 0x0F));
- } else if ((row_pin & 0xF0) == 0x30) {
- DDRC |= (1<<(row_pin & 0x0F));
- PORTC &= ~(1<<(row_pin & 0x0F));
- } else if ((row_pin & 0xF0) == 0x40) {
- DDRD |= (1<<(row_pin & 0x0F));
- PORTD &= ~(1<<(row_pin & 0x0F));
- } else if ((row_pin & 0xF0) == 0x50) {
- DDRE |= (1<<(row_pin & 0x0F));
- PORTE &= ~(1<<(row_pin & 0x0F));
- } else if ((row_pin & 0xF0) == 0x60) {
- DDRF |= (1<<(row_pin & 0x0F));
- PORTF &= ~(1<<(row_pin & 0x0F));
- }
-} \ No newline at end of file
+ _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF);
+ _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF);
+}
diff --git a/quantum/process_keycode/process_chording.c b/quantum/process_keycode/process_chording.c
new file mode 100644
index 0000000000..d7814629f3
--- /dev/null
+++ b/quantum/process_keycode/process_chording.c
@@ -0,0 +1,60 @@
+#include "process_chording.h"
+
+bool keys_chord(uint8_t keys[]) {
+ uint8_t keys_size = sizeof(keys)/sizeof(keys[0]);
+ bool pass = true;
+ uint8_t in = 0;
+ for (uint8_t i = 0; i < chord_key_count; i++) {
+ bool found = false;
+ for (uint8_t j = 0; j < keys_size; j++) {
+ if (chord_keys[i] == (keys[j] & 0xFF)) {
+ in++; // detects key in chord
+ found = true;
+ break;
+ }
+ }
+ if (found)
+ continue;
+ if (chord_keys[i] != 0) {
+ pass = false; // makes sure rest are blank
+ }
+ }
+ return (pass && (in == keys_size));
+}
+
+bool process_chording(uint16_t keycode, keyrecord_t *record) {
+ if (keycode >= QK_CHORDING && keycode <= QK_CHORDING_MAX) {
+ if (record->event.pressed) {
+ if (!chording) {
+ chording = true;
+ for (uint8_t i = 0; i < CHORDING_MAX; i++)
+ chord_keys[i] = 0;
+ chord_key_count = 0;
+ chord_key_down = 0;
+ }
+ chord_keys[chord_key_count] = (keycode & 0xFF);
+ chord_key_count++;
+ chord_key_down++;
+ return false;
+ } else {
+ if (chording) {
+ chord_key_down--;
+ if (chord_key_down == 0) {
+ chording = false;
+ // Chord Dictionary
+ if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
+ register_code(KC_A);
+ unregister_code(KC_A);
+ return false;
+ }
+ for (uint8_t i = 0; i < chord_key_count; i++) {
+ register_code(chord_keys[i]);
+ unregister_code(chord_keys[i]);
+ return false;
+ }
+ }
+ }
+ }
+ }
+ return true;
+} \ No newline at end of file
diff --git a/quantum/process_keycode/process_chording.h b/quantum/process_keycode/process_chording.h
new file mode 100644
index 0000000000..49c97db3bc
--- /dev/null
+++ b/quantum/process_keycode/process_chording.h
@@ -0,0 +1,16 @@
+#ifndef PROCESS_CHORDING_H
+#define PROCESS_CHORDING_H
+
+#include "quantum.h"
+
+// Chording stuff
+#define CHORDING_MAX 4
+bool chording = false;
+
+uint8_t chord_keys[CHORDING_MAX] = {0};
+uint8_t chord_key_count = 0;
+uint8_t chord_key_down = 0;
+
+bool process_chording(uint16_t keycode, keyrecord_t *record);
+
+#endif \ No newline at end of file
diff --git a/quantum/process_keycode/process_leader.c b/quantum/process_keycode/process_leader.c
new file mode 100644
index 0000000000..e53d221e75
--- /dev/null
+++ b/quantum/process_keycode/process_leader.c
@@ -0,0 +1,38 @@
+#include "process_leader.h"
+
+__attribute__ ((weak))
+void leader_start(void) {}
+
+__attribute__ ((weak))
+void leader_end(void) {}
+
+// Leader key stuff
+bool leading = false;
+uint16_t leader_time = 0;
+
+uint16_t leader_sequence[5] = {0, 0, 0, 0, 0};
+uint8_t leader_sequence_size = 0;
+
+bool process_leader(uint16_t keycode, keyrecord_t *record) {
+ // Leader key set-up
+ if (record->event.pressed) {
+ if (!leading && keycode == KC_LEAD) {
+ leader_start();
+ leading = true;
+ leader_time = timer_read();
+ leader_sequence_size = 0;
+ leader_sequence[0] = 0;
+ leader_sequence[1] = 0;
+ leader_sequence[2] = 0;
+ leader_sequence[3] = 0;
+ leader_sequence[4] = 0;
+ return false;
+ }
+ if (leading && timer_elapsed(leader_time) < LEADER_TIMEOUT) {
+ leader_sequence[leader_sequence_size] = keycode;
+ leader_sequence_size++;
+ return false;
+ }
+ }
+ return true;
+} \ No newline at end of file
diff --git a/quantum/process_keycode/process_leader.h b/quantum/process_keycode/process_leader.h
new file mode 100644
index 0000000000..c83db8abbd
--- /dev/null
+++ b/quantum/process_keycode/process_leader.h
@@ -0,0 +1,23 @@
+#ifndef PROCESS_LEADER_H
+#define PROCESS_LEADER_H
+
+#include "quantum.h"
+
+bool process_leader(uint16_t keycode, keyrecord_t *record);
+
+void leader_start(void);
+void leader_end(void);
+
+#ifndef LEADER_TIMEOUT
+ #define LEADER_TIMEOUT 200
+#endif
+#define SEQ_ONE_KEY(key) if (leader_sequence[0] == (key) && leader_sequence[1] == 0 && leader_sequence[2] == 0 && leader_sequence[3] == 0 && leader_sequence[4] == 0)
+#define SEQ_TWO_KEYS(key1, key2) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == 0 && leader_sequence[3] == 0 && leader_sequence[4] == 0)
+#define SEQ_THREE_KEYS(key1, key2, key3) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == (key3) && leader_sequence[3] == 0 && leader_sequence[4] == 0)
+#define SEQ_FOUR_KEYS(key1, key2, key3, key4) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == (key3) && leader_sequence[3] == (key4) && leader_sequence[4] == 0)
+#define SEQ_FIVE_KEYS(key1, key2, key3, key4, key5) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == (key3) && leader_sequence[3] == (key4) && leader_sequence[4] == (key5))
+
+#define LEADER_EXTERNS() extern bool leading; extern uint16_t leader_time; extern uint16_t leader_sequence[5]; extern uint8_t leader_sequence_size
+#define LEADER_DICTIONARY() if (leading && timer_elapsed(leader_time) > LEADER_TIMEOUT)
+
+#endif \ No newline at end of file
diff --git a/quantum/process_keycode/process_midi.c b/quantum/process_keycode/process_midi.c
new file mode 100644
index 0000000000..8784e64f36
--- /dev/null
+++ b/quantum/process_keycode/process_midi.c
@@ -0,0 +1,66 @@
+#include "process_midi.h"
+
+bool midi_activated = false;
+uint8_t midi_starting_note = 0x0C;
+int midi_offset = 7;
+
+bool process_midi(uint16_t keycode, keyrecord_t *record) {
+ if (keycode == MI_ON && record->event.pressed) {
+ midi_activated = true;
+ music_scale_user();
+ return false;
+ }
+
+ if (keycode == MI_OFF && record->event.pressed) {
+ midi_activated = false;
+ midi_send_cc(&midi_device, 0, 0x7B, 0);
+ return false;
+ }
+
+ if (midi_activated) {
+ if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
+ if (record->event.pressed) {
+ midi_starting_note++; // Change key
+ midi_send_cc(&midi_device, 0, 0x7B, 0);
+ }
+ return false;
+ }
+ if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
+ if (record->event.pressed) {
+ midi_starting_note--; // Change key
+ midi_send_cc(&midi_device, 0, 0x7B, 0);
+ }
+ return false;
+ }
+ if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
+ midi_offset++; // Change scale
+ midi_send_cc(&midi_device, 0, 0x7B, 0);
+ return false;
+ }
+ if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
+ midi_offset--; // Change scale
+ midi_send_cc(&midi_device, 0, 0x7B, 0);
+ return false;
+ }
+ // basic
+ // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row);
+ // advanced
+ // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row);
+ // guitar
+ uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row);
+ // violin
+ // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+7*(MATRIX_ROWS - record->event.key.row);
+
+ if (record->event.pressed) {
+ // midi_send_noteon(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
+ midi_send_noteon(&midi_device, 0, note, 127);
+ } else {
+ // midi_send_noteoff(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
+ midi_send_noteoff(&midi_device, 0, note, 127);
+ }
+
+ if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
+ return false;
+ }
+ return true;
+} \ No newline at end of file
diff --git a/quantum/keymap_midi.h b/quantum/process_keycode/process_midi.h
index a89420ce2a..acd4fc1b16 100644
--- a/quantum/keymap_midi.h
+++ b/quantum/process_keycode/process_midi.h
@@ -1,35 +1,20 @@
-/*
-Copyright 2015 Jack Humbert <jack.humb@gmail.com>
+#ifndef PROCESS_MIDI_H
+#define PROCESS_MIDI_H
-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.
+#include "quantum.h"
-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.
+bool process_midi(uint16_t keycode, keyrecord_t *record);
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#ifndef KEYMAP_MIDI_H
-#define KEYMAP_MIDI_H
-
-#include <lufa.h>
-
-#define MIDI 0x6000
+#define MIDI(n) ((n) | 0x6000)
#define MIDI12 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000
#define CHNL(note, channel) (note + (channel << 8))
-#define SCALE (int []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \
- 0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \
- 0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \
- 0 + (12*3), 2 + (12*3), 4 + (12*3), 5 + (12*3), 7 + (12*3), 9 + (12*3), 11 + (12*3), \
- 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
+#define SCALE (int8_t []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \
+ 0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \
+ 0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \
+ 0 + (12*3), 2 + (12*3), 4 + (12*3), 5 + (12*3), 7 + (12*3), 9 + (12*3), 11 + (12*3), \
+ 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
#define N_CN1 (0x600C + (12 * -1) + 0 )
#define N_CN1S (0x600C + (12 * -1) + 1 )
diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c
new file mode 100644
index 0000000000..2d52e47a72
--- /dev/null
+++ b/quantum/process_keycode/process_music.c
@@ -0,0 +1,176 @@
+#include "process_music.h"
+
+bool music_activated = false;
+uint8_t starting_note = 0x0C;
+int offset = 7;
+
+// music sequencer
+static bool music_sequence_recording = false;
+static bool music_sequence_recorded = false;
+static bool music_sequence_playing = false;
+static float music_sequence[16] = {0};
+static uint8_t music_sequence_count = 0;
+static uint8_t music_sequence_position = 0;
+
+static uint16_t music_sequence_timer = 0;
+static uint16_t music_sequence_interval = 100;
+
+bool process_music(uint16_t keycode, keyrecord_t *record) {
+
+ if (keycode == AU_ON && record->event.pressed) {
+ audio_on();
+ return false;
+ }
+
+ if (keycode == AU_OFF && record->event.pressed) {
+ audio_off();
+ return false;
+ }
+
+ if (keycode == AU_TOG && record->event.pressed) {
+ if (is_audio_on())
+ {
+ audio_off();
+ }
+ else
+ {
+ audio_on();
+ }
+ return false;
+ }
+
+ if (keycode == MU_ON && record->event.pressed) {
+ music_on();
+ return false;
+ }
+
+ if (keycode == MU_OFF && record->event.pressed) {
+ music_off();
+ return false;
+ }
+
+ if (keycode == MU_TOG && record->event.pressed) {
+ if (music_activated)
+ {
+ music_off();
+ }
+ else
+ {
+ music_on();
+ }
+ return false;
+ }
+
+ if (keycode == MUV_IN && record->event.pressed) {
+ voice_iterate();
+ music_scale_user();
+ return false;
+ }
+
+ if (keycode == MUV_DE && record->event.pressed) {
+ voice_deiterate();
+ music_scale_user();
+ return false;
+ }
+
+ if (music_activated) {
+
+ if (keycode == KC_LCTL && record->event.pressed) { // Start recording
+ stop_all_notes();
+ music_sequence_recording = true;
+ music_sequence_recorded = false;
+ music_sequence_playing = false;
+ music_sequence_count = 0;
+ return false;
+ }
+
+ if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
+ stop_all_notes();
+ if (music_sequence_recording) { // was recording
+ music_sequence_recorded = true;
+ }
+ music_sequence_recording = false;
+ music_sequence_playing = false;
+ return false;
+ }
+
+ if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
+ stop_all_notes();
+ music_sequence_recording = false;
+ music_sequence_playing = true;
+ music_sequence_position = 0;
+ music_sequence_timer = 0;
+ return false;
+ }
+
+ if (keycode == KC_UP) {
+ if (record->event.pressed)
+ music_sequence_interval-=10;
+ return false;
+ }
+
+ if (keycode == KC_DOWN) {
+ if (record->event.pressed)
+ music_sequence_interval+=10;
+ return false;
+ }
+
+ float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row));
+ if (record->event.pressed) {
+ play_note(freq, 0xF);
+ if (music_sequence_recording) {
+ music_sequence[music_sequence_count] = freq;
+ music_sequence_count++;
+ }
+ } else {
+ stop_note(freq);
+ }
+
+ if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
+ return false;
+ }
+ return true;
+}
+
+bool is_music_on(void) {
+ return (music_activated != 0);
+}
+
+void music_toggle(void) {
+ if (!music_activated) {
+ music_on();
+ } else {
+ music_off();
+ }
+}
+
+void music_on(void) {
+ music_activated = 1;
+ music_on_user();
+}
+
+void music_off(void) {
+ music_activated = 0;
+ stop_all_notes();
+}
+
+
+__attribute__ ((weak))
+void music_on_user() {}
+
+__attribute__ ((weak))
+void audio_on_user() {}
+
+__attribute__ ((weak))
+void music_scale_user() {}
+
+void matrix_scan_music(void) {
+ if (music_sequence_playing) {
+ if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
+ music_sequence_timer = timer_read();
+ stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]);
+ play_note(music_sequence[music_sequence_position], 0xF);
+ music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
+ }
+ }
+}
diff --git a/quantum/process_keycode/process_music.h b/quantum/process_keycode/process_music.h
new file mode 100644
index 0000000000..318b3e3875
--- /dev/null
+++ b/quantum/process_keycode/process_music.h
@@ -0,0 +1,27 @@
+#ifndef PROCESS_MUSIC_H
+#define PROCESS_MUSIC_H
+
+#include "quantum.h"
+
+bool process_music(uint16_t keycode, keyrecord_t *record);
+
+bool is_music_on(void);
+void music_toggle(void);
+void music_on(void);
+void music_off(void);
+
+void audio_on_user(void);
+void music_on_user(void);
+void music_scale_user(void);
+
+void matrix_scan_music(void);
+
+#ifndef SCALE
+#define SCALE (int8_t []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \
+ 0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \
+ 0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \
+ 0 + (12*3), 2 + (12*3), 4 + (12*3), 5 + (12*3), 7 + (12*3), 9 + (12*3), 11 + (12*3), \
+ 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
+#endif
+
+#endif \ No newline at end of file
diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c
new file mode 100644
index 0000000000..79ade4d000
--- /dev/null
+++ b/quantum/process_keycode/process_tap_dance.c
@@ -0,0 +1,135 @@
+#include "quantum.h"
+#include "action_tapping.h"
+
+static uint16_t last_td;
+static int8_t highest_td = -1;
+
+void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data) {
+ qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
+
+ if (state->count == 1) {
+ register_code16 (pair->kc1);
+ } else if (state->count == 2) {
+ register_code16 (pair->kc2);
+ }
+}
+
+void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data) {
+ qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
+
+ if (state->count == 1) {
+ unregister_code16 (pair->kc1);
+ } else if (state->count == 2) {
+ unregister_code16 (pair->kc2);
+ }
+}
+
+static inline void _process_tap_dance_action_fn (qk_tap_dance_state_t *state,
+ void *user_data,
+ qk_tap_dance_user_fn_t fn)
+{
+ if (fn) {
+ fn(state, user_data);
+ }
+}
+
+static inline void process_tap_dance_action_on_each_tap (qk_tap_dance_action_t *action)
+{
+ _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_each_tap);
+}
+
+static inline void process_tap_dance_action_on_dance_finished (qk_tap_dance_action_t *action)
+{
+ if (action->state.finished)
+ return;
+ action->state.finished = true;
+ _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_dance_finished);
+}
+
+static inline void process_tap_dance_action_on_reset (qk_tap_dance_action_t *action)
+{
+ _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_reset);
+}
+
+bool process_tap_dance(uint16_t keycode, keyrecord_t *record) {
+ uint16_t idx = keycode - QK_TAP_DANCE;
+ qk_tap_dance_action_t *action;
+
+ if (last_td && last_td != keycode) {
+ (&tap_dance_actions[last_td - QK_TAP_DANCE])->state.interrupted = true;
+ }
+
+ switch(keycode) {
+ case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
+ if ((int16_t)idx > highest_td)
+ highest_td = idx;
+ action = &tap_dance_actions[idx];
+
+ action->state.pressed = record->event.pressed;
+ if (record->event.pressed) {
+ action->state.keycode = keycode;
+ action->state.count++;
+ action->state.timer = timer_read();
+
+ if (last_td && last_td != keycode) {
+ qk_tap_dance_action_t *paction = &tap_dance_actions[last_td - QK_TAP_DANCE];
+ paction->state.interrupted = true;
+ process_tap_dance_action_on_dance_finished (paction);
+ reset_tap_dance (&paction->state);
+ }
+
+ last_td = keycode;
+ }
+
+ break;
+
+ default:
+ if (!record->event.pressed)
+ return true;
+
+ if (highest_td == -1)
+ return true;
+
+ for (int i = 0; i <= highest_td; i++) {
+ action = &tap_dance_actions[i];
+ if (action->state.count == 0)
+ continue;
+ action->state.interrupted = true;
+ process_tap_dance_action_on_dance_finished (action);
+ reset_tap_dance (&action->state);
+ }
+ break;
+ }
+
+ return true;
+}
+
+void matrix_scan_tap_dance () {
+ if (highest_td == -1)
+ return;
+
+ for (int i = 0; i <= highest_td; i++) {
+ qk_tap_dance_action_t *action = &tap_dance_actions[i];
+
+ if (action->state.count && timer_elapsed (action->state.timer) > TAPPING_TERM) {
+ process_tap_dance_action_on_dance_finished (action);
+ reset_tap_dance (&action->state);
+ }
+ }
+}
+
+void reset_tap_dance (qk_tap_dance_state_t *state) {
+ qk_tap_dance_action_t *action;
+
+ if (state->pressed)
+ return;
+
+ action = &tap_dance_actions[state->keycode - QK_TAP_DANCE];
+
+ process_tap_dance_action_on_reset (action);
+
+ state->count = 0;
+ state->interrupted = false;
+ state->finished = false;
+ last_td = 0;
+}
diff --git a/quantum/process_keycode/process_tap_dance.h b/quantum/process_keycode/process_tap_dance.h
new file mode 100644
index 0000000000..d7b857bdc6
--- /dev/null
+++ b/quantum/process_keycode/process_tap_dance.h
@@ -0,0 +1,70 @@
+#ifndef PROCESS_TAP_DANCE_H
+#define PROCESS_TAP_DANCE_H
+
+#ifdef TAP_DANCE_ENABLE
+
+#include <stdbool.h>
+#include <inttypes.h>
+
+typedef struct
+{
+ uint8_t count;
+ uint16_t keycode;
+ uint16_t timer;
+ bool interrupted;
+ bool pressed;
+ bool finished;
+} qk_tap_dance_state_t;
+
+#define TD(n) (QK_TAP_DANCE + n)
+
+typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state, void *user_data);
+
+typedef struct
+{
+ struct {
+ qk_tap_dance_user_fn_t on_each_tap;
+ qk_tap_dance_user_fn_t on_dance_finished;
+ qk_tap_dance_user_fn_t on_reset;
+ } fn;
+ qk_tap_dance_state_t state;
+ void *user_data;
+} qk_tap_dance_action_t;
+
+typedef struct
+{
+ uint16_t kc1;
+ uint16_t kc2;
+} qk_tap_dance_pair_t;
+
+#define ACTION_TAP_DANCE_DOUBLE(kc1, kc2) { \
+ .fn = { NULL, qk_tap_dance_pair_finished, qk_tap_dance_pair_reset }, \
+ .user_data = (void *)&((qk_tap_dance_pair_t) { kc1, kc2 }) \
+ }
+
+#define ACTION_TAP_DANCE_FN(user_fn) { \
+ .fn = { NULL, user_fn, NULL } \
+ }
+
+#define ACTION_TAP_DANCE_FN_ADVANCED(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_reset) { \
+ .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_reset } \
+ }
+
+extern qk_tap_dance_action_t tap_dance_actions[];
+
+/* To be used internally */
+
+bool process_tap_dance(uint16_t keycode, keyrecord_t *record);
+void matrix_scan_tap_dance (void);
+void reset_tap_dance (qk_tap_dance_state_t *state);
+
+void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data);
+void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data);
+
+#else
+
+#define TD(n) KC_NO
+
+#endif
+
+#endif
diff --git a/quantum/process_keycode/process_unicode.c b/quantum/process_keycode/process_unicode.c
new file mode 100644
index 0000000000..a5d7dca21e
--- /dev/null
+++ b/quantum/process_keycode/process_unicode.c
@@ -0,0 +1,212 @@
+#include "process_unicode.h"
+
+static uint8_t input_mode;
+
+uint16_t hex_to_keycode(uint8_t hex)
+{
+ if (hex == 0x0) {
+ return KC_0;
+ } else if (hex < 0xA) {
+ return KC_1 + (hex - 0x1);
+ } else {
+ return KC_A + (hex - 0xA);
+ }
+}
+
+void set_unicode_input_mode(uint8_t os_target)
+{
+ input_mode = os_target;
+}
+
+uint8_t get_unicode_input_mode(void) {
+ return input_mode;
+}
+
+__attribute__((weak))
+void unicode_input_start (void) {
+ switch(input_mode) {
+ case UC_OSX:
+ register_code(KC_LALT);
+ break;
+ case UC_LNX:
+ register_code(KC_LCTL);
+ register_code(KC_LSFT);
+ register_code(KC_U);
+ unregister_code(KC_U);
+ unregister_code(KC_LSFT);
+ unregister_code(KC_LCTL);
+ break;
+ case UC_WIN:
+ register_code(KC_LALT);
+ register_code(KC_PPLS);
+ unregister_code(KC_PPLS);
+ break;
+ }
+ wait_ms(UNICODE_TYPE_DELAY);
+}
+
+__attribute__((weak))
+void unicode_input_finish (void) {
+ switch(input_mode) {
+ case UC_OSX:
+ case UC_WIN:
+ unregister_code(KC_LALT);
+ break;
+ case UC_LNX:
+ register_code(KC_SPC);
+ unregister_code(KC_SPC);
+ break;
+ }
+}
+
+void register_hex(uint16_t hex) {
+ for(int i = 3; i >= 0; i--) {
+ uint8_t digit = ((hex >> (i*4)) & 0xF);
+ register_code(hex_to_keycode(digit));
+ unregister_code(hex_to_keycode(digit));
+ }
+}
+
+bool process_unicode(uint16_t keycode, keyrecord_t *record) {
+ if (keycode > QK_UNICODE && record->event.pressed) {
+ uint16_t unicode = keycode & 0x7FFF;
+ unicode_input_start();
+ register_hex(unicode);
+ unicode_input_finish();
+ }
+ return true;
+}
+
+#ifdef UCIS_ENABLE
+qk_ucis_state_t qk_ucis_state;
+
+void qk_ucis_start(void) {
+ qk_ucis_state.count = 0;
+ qk_ucis_state.in_progress = true;
+
+ qk_ucis_start_user();
+}
+
+__attribute__((weak))
+void qk_ucis_start_user(void) {
+ unicode_input_start();
+ register_hex(0x2328);
+ unicode_input_finish();
+}
+
+static bool is_uni_seq(char *seq) {
+ uint8_t i;
+
+ for (i = 0; seq[i]; i++) {
+ uint16_t code;
+ if (('1' <= seq[i]) && (seq[i] <= '0'))
+ code = seq[i] - '1' + KC_1;
+ else
+ code = seq[i] - 'a' + KC_A;
+
+ if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
+ return false;
+ }
+
+ return (qk_ucis_state.codes[i] == KC_ENT ||
+ qk_ucis_state.codes[i] == KC_SPC);
+}
+
+__attribute__((weak))
+void qk_ucis_symbol_fallback (void) {
+ for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
+ uint8_t code = qk_ucis_state.codes[i];
+ register_code(code);
+ unregister_code(code);
+ wait_ms(UNICODE_TYPE_DELAY);
+ }
+}
+
+void register_ucis(const char *hex) {
+ for(int i = 0; hex[i]; i++) {
+ uint8_t kc = 0;
+ char c = hex[i];
+
+ switch (c) {
+ case '0':
+ kc = KC_0;
+ break;
+ case '1' ... '9':
+ kc = c - '1' + KC_1;
+ break;
+ case 'a' ... 'f':
+ kc = c - 'a' + KC_A;
+ break;
+ case 'A' ... 'F':
+ kc = c - 'A' + KC_A;
+ break;
+ }
+
+ if (kc) {
+ register_code (kc);
+ unregister_code (kc);
+ wait_ms (UNICODE_TYPE_DELAY);
+ }
+ }
+}
+
+bool process_ucis (uint16_t keycode, keyrecord_t *record) {
+ uint8_t i;
+
+ if (!qk_ucis_state.in_progress)
+ return true;
+
+ if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
+ !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
+ return false;
+ }
+
+ if (!record->event.pressed)
+ return true;
+
+ qk_ucis_state.codes[qk_ucis_state.count] = keycode;
+ qk_ucis_state.count++;
+
+ if (keycode == KC_BSPC) {
+ if (qk_ucis_state.count >= 2) {
+ qk_ucis_state.count -= 2;
+ return true;
+ } else {
+ qk_ucis_state.count--;
+ return false;
+ }
+ }
+
+ if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
+ bool symbol_found = false;
+
+ for (i = qk_ucis_state.count; i > 0; i--) {
+ register_code (KC_BSPC);
+ unregister_code (KC_BSPC);
+ wait_ms(UNICODE_TYPE_DELAY);
+ }
+
+ if (keycode == KC_ESC) {
+ qk_ucis_state.in_progress = false;
+ return false;
+ }
+
+ unicode_input_start();
+ for (i = 0; ucis_symbol_table[i].symbol; i++) {
+ if (is_uni_seq (ucis_symbol_table[i].symbol)) {
+ symbol_found = true;
+ register_ucis(ucis_symbol_table[i].code + 2);
+ break;
+ }
+ }
+ if (!symbol_found) {
+ qk_ucis_symbol_fallback();
+ }
+ unicode_input_finish();
+
+ qk_ucis_state.in_progress = false;
+ return false;
+ }
+ return true;
+}
+#endif
diff --git a/quantum/process_keycode/process_unicode.h b/quantum/process_keycode/process_unicode.h
new file mode 100644
index 0000000000..27f8072ee6
--- /dev/null
+++ b/quantum/process_keycode/process_unicode.h
@@ -0,0 +1,161 @@
+#ifndef PROCESS_UNICODE_H
+#define PROCESS_UNICODE_H
+
+#include "quantum.h"
+
+#define UC_OSX 0
+#define UC_LNX 1
+#define UC_WIN 2
+#define UC_BSD 3
+
+#ifndef UNICODE_TYPE_DELAY
+#define UNICODE_TYPE_DELAY 10
+#endif
+
+void set_unicode_input_mode(uint8_t os_target);
+uint8_t get_unicode_input_mode(void);
+void unicode_input_start(void);
+void unicode_input_finish(void);
+void register_hex(uint16_t hex);
+
+bool process_unicode(uint16_t keycode, keyrecord_t *record);
+
+#ifdef UCIS_ENABLE
+#ifndef UCIS_MAX_SYMBOL_LENGTH
+#define UCIS_MAX_SYMBOL_LENGTH 32
+#endif
+
+typedef struct {
+ char *symbol;
+ char *code;
+} qk_ucis_symbol_t;
+
+typedef struct {
+ uint8_t count;
+ uint16_t codes[UCIS_MAX_SYMBOL_LENGTH];
+ bool in_progress:1;
+} qk_ucis_state_t;
+
+extern qk_ucis_state_t qk_ucis_state;
+
+#define UCIS_TABLE(...) {__VA_ARGS__, {NULL, NULL}}
+#define UCIS_SYM(name, code) {name, #code}
+
+extern const qk_ucis_symbol_t ucis_symbol_table[];
+
+void qk_ucis_start(void);
+void qk_ucis_start_user(void);
+void qk_ucis_symbol_fallback (void);
+void register_ucis(const char *hex);
+bool process_ucis (uint16_t keycode, keyrecord_t *record);
+
+#endif
+
+#define UC_BSPC UC(0x0008)
+
+#define UC_SPC UC(0x0020)
+
+#define UC_EXLM UC(0x0021)
+#define UC_DQUT UC(0x0022)
+#define UC_HASH UC(0x0023)
+#define UC_DLR UC(0x0024)
+#define UC_PERC UC(0x0025)
+#define UC_AMPR UC(0x0026)
+#define UC_QUOT UC(0x0027)
+#define UC_LPRN UC(0x0028)
+#define UC_RPRN UC(0x0029)
+#define UC_ASTR UC(0x002A)
+#define UC_PLUS UC(0x002B)
+#define UC_COMM UC(0x002C)
+#define UC_DASH UC(0x002D)
+#define UC_DOT UC(0x002E)
+#define UC_SLSH UC(0x002F)
+
+#define UC_0 UC(0x0030)
+#define UC_1 UC(0x0031)
+#define UC_2 UC(0x0032)
+#define UC_3 UC(0x0033)
+#define UC_4 UC(0x0034)
+#define UC_5 UC(0x0035)
+#define UC_6 UC(0x0036)
+#define UC_7 UC(0x0037)
+#define UC_8 UC(0x0038)
+#define UC_9 UC(0x0039)
+
+#define UC_COLN UC(0x003A)
+#define UC_SCLN UC(0x003B)
+#define UC_LT UC(0x003C)
+#define UC_EQL UC(0x003D)
+#define UC_GT UC(0x003E)
+#define UC_QUES UC(0x003F)
+#define UC_AT UC(0x0040)
+
+#define UC_A UC(0x0041)
+#define UC_B UC(0x0042)
+#define UC_C UC(0x0043)
+#define UC_D UC(0x0044)
+#define UC_E UC(0x0045)
+#define UC_F UC(0x0046)
+#define UC_G UC(0x0047)
+#define UC_H UC(0x0048)
+#define UC_I UC(0x0049)
+#define UC_J UC(0x004A)
+#define UC_K UC(0x004B)
+#define UC_L UC(0x004C)
+#define UC_M UC(0x004D)
+#define UC_N UC(0x004E)
+#define UC_O UC(0x004F)
+#define UC_P UC(0x0050)
+#define UC_Q UC(0x0051)
+#define UC_R UC(0x0052)
+#define UC_S UC(0x0053)
+#define UC_T UC(0x0054)
+#define UC_U UC(0x0055)
+#define UC_V UC(0x0056)
+#define UC_W UC(0x0057)
+#define UC_X UC(0x0058)
+#define UC_Y UC(0x0059)
+#define UC_Z UC(0x005A)
+
+#define UC_LBRC UC(0x005B)
+#define UC_BSLS UC(0x005C)
+#define UC_RBRC UC(0x005D)
+#define UC_CIRM UC(0x005E)
+#define UC_UNDR UC(0x005F)
+
+#define UC_GRV UC(0x0060)
+
+#define UC_a UC(0x0061)
+#define UC_b UC(0x0062)
+#define UC_c UC(0x0063)
+#define UC_d UC(0x0064)
+#define UC_e UC(0x0065)
+#define UC_f UC(0x0066)
+#define UC_g UC(0x0067)
+#define UC_h UC(0x0068)
+#define UC_i UC(0x0069)
+#define UC_j UC(0x006A)
+#define UC_k UC(0x006B)
+#define UC_l UC(0x006C)
+#define UC_m UC(0x006D)
+#define UC_n UC(0x006E)
+#define UC_o UC(0x006F)
+#define UC_p UC(0x0070)
+#define UC_q UC(0x0071)
+#define UC_r UC(0x0072)
+#define UC_s UC(0x0073)
+#define UC_t UC(0x0074)
+#define UC_u UC(0x0075)
+#define UC_v UC(0x0076)
+#define UC_w UC(0x0077)
+#define UC_x UC(0x0078)
+#define UC_y UC(0x0079)
+#define UC_z UC(0x007A)
+
+#define UC_LCBR UC(0x007B)
+#define UC_PIPE UC(0x007C)
+#define UC_RCBR UC(0x007D)
+#define UC_TILD UC(0x007E)
+#define UC_DEL UC(0x007F)
+
+#endif
diff --git a/quantum/quantum.c b/quantum/quantum.c
new file mode 100644
index 0000000000..a16bd5443c
--- /dev/null
+++ b/quantum/quantum.c
@@ -0,0 +1,850 @@
+#include "quantum.h"
+
+static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
+ switch (code) {
+ case QK_MODS ... QK_MODS_MAX:
+ break;
+ default:
+ return;
+ }
+
+ if (code & QK_LCTL)
+ f(KC_LCTL);
+ if (code & QK_LSFT)
+ f(KC_LSFT);
+ if (code & QK_LALT)
+ f(KC_LALT);
+ if (code & QK_LGUI)
+ f(KC_LGUI);
+
+ if (code & QK_RCTL)
+ f(KC_RCTL);
+ if (code & QK_RSFT)
+ f(KC_RSFT);
+ if (code & QK_RALT)
+ f(KC_RALT);
+ if (code & QK_RGUI)
+ f(KC_RGUI);
+}
+
+void register_code16 (uint16_t code) {
+ do_code16 (code, register_code);
+ register_code (code);
+}
+
+void unregister_code16 (uint16_t code) {
+ unregister_code (code);
+ do_code16 (code, unregister_code);
+}
+
+__attribute__ ((weak))
+bool process_action_kb(keyrecord_t *record) {
+ return true;
+}
+
+__attribute__ ((weak))
+bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
+ return process_record_user(keycode, record);
+}
+
+__attribute__ ((weak))
+bool process_record_user(uint16_t keycode, keyrecord_t *record) {
+ return true;
+}
+
+void reset_keyboard(void) {
+ clear_keyboard();
+#ifdef AUDIO_ENABLE
+ stop_all_notes();
+ shutdown_user();
+#endif
+ wait_ms(250);
+#ifdef CATERINA_BOOTLOADER
+ *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
+#endif
+ bootloader_jump();
+}
+
+// Shift / paren setup
+
+#ifndef LSPO_KEY
+ #define LSPO_KEY KC_9
+#endif
+#ifndef RSPC_KEY
+ #define RSPC_KEY KC_0
+#endif
+
+static bool shift_interrupted[2] = {0, 0};
+
+bool process_record_quantum(keyrecord_t *record) {
+
+ /* This gets the keycode from the key pressed */
+ keypos_t key = record->event.key;
+ uint16_t keycode;
+
+ #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
+ /* TODO: Use store_or_get_action() or a similar function. */
+ if (!disable_action_cache) {
+ uint8_t layer;
+
+ if (record->event.pressed) {
+ layer = layer_switch_get_layer(key);
+ update_source_layers_cache(key, layer);
+ } else {
+ layer = read_source_layers_cache(key);
+ }
+ keycode = keymap_key_to_keycode(layer, key);
+ } else
+ #endif
+ keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
+
+ // This is how you use actions here
+ // if (keycode == KC_LEAD) {
+ // action_t action;
+ // action.code = ACTION_DEFAULT_LAYER_SET(0);
+ // process_action(record, action);
+ // return false;
+ // }
+
+ if (!(
+ process_record_kb(keycode, record) &&
+ #ifdef MIDI_ENABLE
+ process_midi(keycode, record) &&
+ #endif
+ #ifdef AUDIO_ENABLE
+ process_music(keycode, record) &&
+ #endif
+ #ifdef TAP_DANCE_ENABLE
+ process_tap_dance(keycode, record) &&
+ #endif
+ #ifndef DISABLE_LEADER
+ process_leader(keycode, record) &&
+ #endif
+ #ifndef DISABLE_CHORDING
+ process_chording(keycode, record) &&
+ #endif
+ #ifdef UNICODE_ENABLE
+ process_unicode(keycode, record) &&
+ #endif
+ #ifdef UCIS_ENABLE
+ process_ucis(keycode, record) &&
+ #endif
+ true)) {
+ return false;
+ }
+
+ // Shift / paren setup
+
+ switch(keycode) {
+ case RESET:
+ if (record->event.pressed) {
+ reset_keyboard();
+ }
+ return false;
+ break;
+ case DEBUG:
+ if (record->event.pressed) {
+ print("\nDEBUG: enabled.\n");
+ debug_enable = true;
+ }
+ return false;
+ break;
+ #ifdef RGBLIGHT_ENABLE
+ case RGB_TOG:
+ if (record->event.pressed) {
+ rgblight_toggle();
+ }
+ return false;
+ break;
+ case RGB_MOD:
+ if (record->event.pressed) {
+ rgblight_step();
+ }
+ return false;
+ break;
+ case RGB_HUI:
+ if (record->event.pressed) {
+ rgblight_increase_hue();
+ }
+ return false;
+ break;
+ case RGB_HUD:
+ if (record->event.pressed) {
+ rgblight_decrease_hue();
+ }
+ return false;
+ break;
+ case RGB_SAI:
+ if (record->event.pressed) {
+ rgblight_increase_sat();
+ }
+ return false;
+ break;
+ case RGB_SAD:
+ if (record->event.pressed) {
+ rgblight_decrease_sat();
+ }
+ return false;
+ break;
+ case RGB_VAI:
+ if (record->event.pressed) {
+ rgblight_increase_val();
+ }
+ return false;
+ break;
+ case RGB_VAD:
+ if (record->event.pressed) {
+ rgblight_decrease_val();
+ }
+ return false;
+ break;
+ #endif
+ case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
+ if (record->event.pressed) {
+ // MAGIC actions (BOOTMAGIC without the boot)
+ if (!eeconfig_is_enabled()) {
+ eeconfig_init();
+ }
+ /* keymap config */
+ keymap_config.raw = eeconfig_read_keymap();
+ switch (keycode)
+ {
+ case MAGIC_SWAP_CONTROL_CAPSLOCK:
+ keymap_config.swap_control_capslock = true;
+ break;
+ case MAGIC_CAPSLOCK_TO_CONTROL:
+ keymap_config.capslock_to_control = true;
+ break;
+ case MAGIC_SWAP_LALT_LGUI:
+ keymap_config.swap_lalt_lgui = true;
+ break;
+ case MAGIC_SWAP_RALT_RGUI:
+ keymap_config.swap_ralt_rgui = true;
+ break;
+ case MAGIC_NO_GUI:
+ keymap_config.no_gui = true;
+ break;
+ case MAGIC_SWAP_GRAVE_ESC:
+ keymap_config.swap_grave_esc = true;
+ break;
+ case MAGIC_SWAP_BACKSLASH_BACKSPACE:
+ keymap_config.swap_backslash_backspace = true;
+ break;
+ case MAGIC_HOST_NKRO:
+ keymap_config.nkro = true;
+ break;
+ case MAGIC_SWAP_ALT_GUI:
+ keymap_config.swap_lalt_lgui = true;
+ keymap_config.swap_ralt_rgui = true;
+ break;
+ case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
+ keymap_config.swap_control_capslock = false;
+ break;
+ case MAGIC_UNCAPSLOCK_TO_CONTROL:
+ keymap_config.capslock_to_control = false;
+ break;
+ case MAGIC_UNSWAP_LALT_LGUI:
+ keymap_config.swap_lalt_lgui = false;
+ break;
+ case MAGIC_UNSWAP_RALT_RGUI:
+ keymap_config.swap_ralt_rgui = false;
+ break;
+ case MAGIC_UNNO_GUI:
+ keymap_config.no_gui = false;
+ break;
+ case MAGIC_UNSWAP_GRAVE_ESC:
+ keymap_config.swap_grave_esc = false;
+ break;
+ case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
+ keymap_config.swap_backslash_backspace = false;
+ break;
+ case MAGIC_UNHOST_NKRO:
+ keymap_config.nkro = false;
+ break;
+ case MAGIC_UNSWAP_ALT_GUI:
+ keymap_config.swap_lalt_lgui = false;
+ keymap_config.swap_ralt_rgui = false;
+ break;
+ case MAGIC_TOGGLE_NKRO:
+ keymap_config.nkro = !keymap_config.nkro;
+ break;
+ default:
+ break;
+ }
+ eeconfig_update_keymap(keymap_config.raw);
+ clear_keyboard(); // clear to prevent stuck keys
+
+ return false;
+ }
+ break;
+ case KC_LSPO: {
+ if (record->event.pressed) {
+ shift_interrupted[0] = false;
+ register_mods(MOD_BIT(KC_LSFT));
+ }
+ else {
+ #ifdef DISABLE_SPACE_CADET_ROLLOVER
+ if (get_mods() & MOD_BIT(KC_RSFT)) {
+ shift_interrupted[0] = true;
+ shift_interrupted[1] = true;
+ }
+ #endif
+ if (!shift_interrupted[0]) {
+ register_code(LSPO_KEY);
+ unregister_code(LSPO_KEY);
+ }
+ unregister_mods(MOD_BIT(KC_LSFT));
+ }
+ return false;
+ // break;
+ }
+
+ case KC_RSPC: {
+ if (record->event.pressed) {
+ shift_interrupted[1] = false;
+ register_mods(MOD_BIT(KC_RSFT));
+ }
+ else {
+ #ifdef DISABLE_SPACE_CADET_ROLLOVER
+ if (get_mods() & MOD_BIT(KC_LSFT)) {
+ shift_interrupted[0] = true;
+ shift_interrupted[1] = true;
+ }
+ #endif
+ if (!shift_interrupted[1]) {
+ register_code(RSPC_KEY);
+ unregister_code(RSPC_KEY);
+ }
+ unregister_mods(MOD_BIT(KC_RSFT));
+ }
+ return false;
+ // break;
+ }
+ default: {
+ shift_interrupted[0] = true;
+ shift_interrupted[1] = true;
+ break;
+ }
+ }
+
+ return process_action_kb(record);
+}
+
+const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 1, 1, 1, 1, 1, 1, 0,
+ 1, 1, 1, 1, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 1, 0, 1, 0, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 0, 0, 0, 1, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 1, 1, 1, 1, 0
+};
+
+const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, KC_ESC, 0, 0, 0, 0,
+ KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
+ KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
+ KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
+ KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
+ KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
+ KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
+ KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
+ KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
+ KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
+ KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
+ KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
+ KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
+};
+
+/* for users whose OSes are set to Colemak */
+#if 0
+#include "keymap_colemak.h"
+
+const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 1, 1, 1, 1, 1, 1, 0,
+ 1, 1, 1, 1, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 1, 0, 1, 0, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 0, 0, 0, 1, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 1, 1, 1, 1, 0
+};
+
+const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, KC_ESC, 0, 0, 0, 0,
+ KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
+ KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
+ KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
+ KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
+ KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
+ CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
+ CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
+ CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
+ KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
+ CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
+ CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
+ CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
+};
+
+#endif
+
+void send_string(const char *str) {
+ while (1) {
+ uint8_t keycode;
+ uint8_t ascii_code = pgm_read_byte(str);
+ if (!ascii_code) break;
+ keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
+ if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
+ register_code(KC_LSFT);
+ register_code(keycode);
+ unregister_code(keycode);
+ unregister_code(KC_LSFT);
+ }
+ else {
+ register_code(keycode);
+ unregister_code(keycode);
+ }
+ ++str;
+ }
+}
+
+void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
+ if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
+ layer_on(layer3);
+ } else {
+ layer_off(layer3);
+ }
+}
+
+void tap_random_base64(void) {
+ #if defined(__AVR_ATmega32U4__)
+ uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
+ #else
+ uint8_t key = rand() % 64;
+ #endif
+ switch (key) {
+ case 0 ... 25:
+ register_code(KC_LSFT);
+ register_code(key + KC_A);
+ unregister_code(key + KC_A);
+ unregister_code(KC_LSFT);
+ break;
+ case 26 ... 51:
+ register_code(key - 26 + KC_A);
+ unregister_code(key - 26 + KC_A);
+ break;
+ case 52:
+ register_code(KC_0);
+ unregister_code(KC_0);
+ break;
+ case 53 ... 61:
+ register_code(key - 53 + KC_1);
+ unregister_code(key - 53 + KC_1);
+ break;
+ case 62:
+ register_code(KC_LSFT);
+ register_code(KC_EQL);
+ unregister_code(KC_EQL);
+ unregister_code(KC_LSFT);
+ break;
+ case 63:
+ register_code(KC_SLSH);
+ unregister_code(KC_SLSH);
+ break;
+ }
+}
+
+void matrix_init_quantum() {
+ #ifdef BACKLIGHT_ENABLE
+ backlight_init_ports();
+ #endif
+ matrix_init_kb();
+}
+
+void matrix_scan_quantum() {
+ #ifdef AUDIO_ENABLE
+ matrix_scan_music();
+ #endif
+
+ #ifdef TAP_DANCE_ENABLE
+ matrix_scan_tap_dance();
+ #endif
+ matrix_scan_kb();
+}
+
+#if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
+
+static const uint8_t backlight_pin = BACKLIGHT_PIN;
+
+#if BACKLIGHT_PIN == B7
+# define COM1x1 COM1C1
+# define OCR1x OCR1C
+#elif BACKLIGHT_PIN == B6
+# define COM1x1 COM1B1
+# define OCR1x OCR1B
+#elif BACKLIGHT_PIN == B5
+# define COM1x1 COM1A1
+# define OCR1x OCR1A
+#else
+# error "Backlight pin not supported - use B5, B6, or B7"
+#endif
+
+__attribute__ ((weak))
+void backlight_init_ports(void)
+{
+
+ // Setup backlight pin as output and output low.
+ // DDRx |= n
+ _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
+ // PORTx &= ~n
+ _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
+
+ // Use full 16-bit resolution.
+ ICR1 = 0xFFFF;
+
+ // I could write a wall of text here to explain... but TL;DW
+ // Go read the ATmega32u4 datasheet.
+ // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
+
+ // Pin PB7 = OCR1C (Timer 1, Channel C)
+ // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
+ // (i.e. start high, go low when counter matches.)
+ // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
+ // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
+
+ TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
+ TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
+
+ backlight_init();
+ #ifdef BACKLIGHT_BREATHING
+ breathing_defaults();
+ #endif
+}
+
+__attribute__ ((weak))
+void backlight_set(uint8_t level)
+{
+ // Prevent backlight blink on lowest level
+ // PORTx &= ~n
+ _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
+
+ if ( level == 0 ) {
+ // Turn off PWM control on backlight pin, revert to output low.
+ TCCR1A &= ~(_BV(COM1x1));
+ OCR1x = 0x0;
+ } else if ( level == BACKLIGHT_LEVELS ) {
+ // Turn on PWM control of backlight pin
+ TCCR1A |= _BV(COM1x1);
+ // Set the brightness
+ OCR1x = 0xFFFF;
+ } else {
+ // Turn on PWM control of backlight pin
+ TCCR1A |= _BV(COM1x1);
+ // Set the brightness
+ OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
+ }
+
+ #ifdef BACKLIGHT_BREATHING
+ breathing_intensity_default();
+ #endif
+}
+
+
+#ifdef BACKLIGHT_BREATHING
+
+#define BREATHING_NO_HALT 0
+#define BREATHING_HALT_OFF 1
+#define BREATHING_HALT_ON 2
+
+static uint8_t breath_intensity;
+static uint8_t breath_speed;
+static uint16_t breathing_index;
+static uint8_t breathing_halt;
+
+void breathing_enable(void)
+{
+ if (get_backlight_level() == 0)
+ {
+ breathing_index = 0;
+ }
+ else
+ {
+ // Set breathing_index to be at the midpoint (brightest point)
+ breathing_index = 0x20 << breath_speed;
+ }
+
+ breathing_halt = BREATHING_NO_HALT;
+
+ // Enable breathing interrupt
+ TIMSK1 |= _BV(OCIE1A);
+}
+
+void breathing_pulse(void)
+{
+ if (get_backlight_level() == 0)
+ {
+ breathing_index = 0;
+ }
+ else
+ {
+ // Set breathing_index to be at the midpoint + 1 (brightest point)
+ breathing_index = 0x21 << breath_speed;
+ }
+
+ breathing_halt = BREATHING_HALT_ON;
+
+ // Enable breathing interrupt
+ TIMSK1 |= _BV(OCIE1A);
+}
+
+void breathing_disable(void)
+{
+ // Disable breathing interrupt
+ TIMSK1 &= ~_BV(OCIE1A);
+ backlight_set(get_backlight_level());
+}
+
+void breathing_self_disable(void)
+{
+ if (get_backlight_level() == 0)
+ {
+ breathing_halt = BREATHING_HALT_OFF;
+ }
+ else
+ {
+ breathing_halt = BREATHING_HALT_ON;
+ }
+
+ //backlight_set(get_backlight_level());
+}
+
+void breathing_toggle(void)
+{
+ if (!is_breathing())
+ {
+ if (get_backlight_level() == 0)
+ {
+ breathing_index = 0;
+ }
+ else
+ {
+ // Set breathing_index to be at the midpoint + 1 (brightest point)
+ breathing_index = 0x21 << breath_speed;
+ }
+
+ breathing_halt = BREATHING_NO_HALT;
+ }
+
+ // Toggle breathing interrupt
+ TIMSK1 ^= _BV(OCIE1A);
+
+ // Restore backlight level
+ if (!is_breathing())
+ {
+ backlight_set(get_backlight_level());
+ }
+}
+
+bool is_breathing(void)
+{
+ return (TIMSK1 && _BV(OCIE1A));
+}
+
+void breathing_intensity_default(void)
+{
+ //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
+ breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
+}
+
+void breathing_intensity_set(uint8_t value)
+{
+ breath_intensity = value;
+}
+
+void breathing_speed_default(void)
+{
+ breath_speed = 4;
+}
+
+void breathing_speed_set(uint8_t value)
+{
+ bool is_breathing_now = is_breathing();
+ uint8_t old_breath_speed = breath_speed;
+
+ if (is_breathing_now)
+ {
+ // Disable breathing interrupt
+ TIMSK1 &= ~_BV(OCIE1A);
+ }
+
+ breath_speed = value;
+
+ if (is_breathing_now)
+ {
+ // Adjust index to account for new speed
+ breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
+
+ // Enable breathing interrupt
+ TIMSK1 |= _BV(OCIE1A);
+ }
+
+}
+
+void breathing_speed_inc(uint8_t value)
+{
+ if ((uint16_t)(breath_speed - value) > 10 )
+ {
+ breathing_speed_set(0);
+ }
+ else
+ {
+ breathing_speed_set(breath_speed - value);
+ }
+}
+
+void breathing_speed_dec(uint8_t value)
+{
+ if ((uint16_t)(breath_speed + value) > 10 )
+ {
+ breathing_speed_set(10);
+ }
+ else
+ {
+ breathing_speed_set(breath_speed + value);
+ }
+}
+
+void breathing_defaults(void)
+{
+ breathing_intensity_default();
+ breathing_speed_default();
+ breathing_halt = BREATHING_NO_HALT;
+}
+
+/* Breathing Sleep LED brighness(PWM On period) table
+ * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
+ *
+ * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
+ * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
+ */
+static const uint8_t breathing_table[64] PROGMEM = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
+ 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
+255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
+ 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+};
+
+ISR(TIMER1_COMPA_vect)
+{
+ // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
+
+
+ uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
+
+ if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
+ {
+ // Disable breathing interrupt
+ TIMSK1 &= ~_BV(OCIE1A);
+ }
+
+ OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
+
+}
+
+
+
+#endif // breathing
+
+#else // backlight
+
+__attribute__ ((weak))
+void backlight_init_ports(void)
+{
+
+}
+
+__attribute__ ((weak))
+void backlight_set(uint8_t level)
+{
+
+}
+
+#endif // backlight
+
+
+
+__attribute__ ((weak))
+void led_set_user(uint8_t usb_led) {
+
+}
+
+__attribute__ ((weak))
+void led_set_kb(uint8_t usb_led) {
+ led_set_user(usb_led);
+}
+
+__attribute__ ((weak))
+void led_init_ports(void)
+{
+
+}
+
+__attribute__ ((weak))
+void led_set(uint8_t usb_led)
+{
+
+ // Example LED Code
+ //
+ // // Using PE6 Caps Lock LED
+ // if (usb_led & (1<<USB_LED_CAPS_LOCK))
+ // {
+ // // Output high.
+ // DDRE |= (1<<6);
+ // PORTE |= (1<<6);
+ // }
+ // else
+ // {
+ // // Output low.
+ // DDRE &= ~(1<<6);
+ // PORTE &= ~(1<<6);
+ // }
+
+ led_set_kb(usb_led);
+}
+
+
+//------------------------------------------------------------------------------
+// Override these functions in your keymap file to play different tunes on
+// different events such as startup and bootloader jump
+
+__attribute__ ((weak))
+void startup_user() {}
+
+__attribute__ ((weak))
+void shutdown_user() {}
+
+//------------------------------------------------------------------------------
diff --git a/quantum/quantum.h b/quantum/quantum.h
new file mode 100644
index 0000000000..0c60466495
--- /dev/null
+++ b/quantum/quantum.h
@@ -0,0 +1,113 @@
+#ifndef QUANTUM_H
+#define QUANTUM_H
+
+#if defined(__AVR__)
+#include <avr/pgmspace.h>
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#endif
+#include "wait.h"
+#include "matrix.h"
+#include "keymap.h"
+#ifdef BACKLIGHT_ENABLE
+ #include "backlight.h"
+#endif
+#ifdef RGBLIGHT_ENABLE
+ #include "rgblight.h"
+#endif
+
+#include "action_layer.h"
+#include "eeconfig.h"
+#include <stddef.h>
+#include "bootloader.h"
+#include "timer.h"
+#include "config_common.h"
+#include "led.h"
+#include "action_util.h"
+#include <stdlib.h>
+#include "print.h"
+
+
+extern uint32_t default_layer_state;
+
+#ifndef NO_ACTION_LAYER
+ extern uint32_t layer_state;
+#endif
+
+#ifdef MIDI_ENABLE
+ #include <lufa.h>
+ #include "process_midi.h"
+#endif
+
+#ifdef AUDIO_ENABLE
+ #include "audio.h"
+ #include "process_music.h"
+#endif
+
+#ifndef DISABLE_LEADER
+ #include "process_leader.h"
+#endif
+
+#define DISABLE_CHORDING
+#ifndef DISABLE_CHORDING
+ #include "process_chording.h"
+#endif
+
+#ifdef UNICODE_ENABLE
+ #include "process_unicode.h"
+#endif
+
+#include "process_tap_dance.h"
+
+#define SEND_STRING(str) send_string(PSTR(str))
+void send_string(const char *str);
+
+// For tri-layer
+void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3);
+
+void tap_random_base64(void);
+
+#define IS_LAYER_ON(layer) (layer_state & (1UL << (layer)))
+#define IS_LAYER_OFF(layer) (~layer_state & (1UL << (layer)))
+
+void matrix_init_kb(void);
+void matrix_scan_kb(void);
+void matrix_init_user(void);
+void matrix_scan_user(void);
+bool process_action_kb(keyrecord_t *record);
+bool process_record_kb(uint16_t keycode, keyrecord_t *record);
+bool process_record_user(uint16_t keycode, keyrecord_t *record);
+
+void reset_keyboard(void);
+
+void startup_user(void);
+void shutdown_user(void);
+
+void register_code16 (uint16_t code);
+void unregister_code16 (uint16_t code);
+
+#ifdef BACKLIGHT_ENABLE
+void backlight_init_ports(void);
+
+#ifdef BACKLIGHT_BREATHING
+void breathing_enable(void);
+void breathing_pulse(void);
+void breathing_disable(void);
+void breathing_self_disable(void);
+void breathing_toggle(void);
+bool is_breathing(void);
+
+void breathing_defaults(void);
+void breathing_intensity_default(void);
+void breathing_speed_default(void);
+void breathing_speed_set(uint8_t value);
+void breathing_speed_inc(uint8_t value);
+void breathing_speed_dec(uint8_t value);
+#endif
+
+#endif
+
+void led_set_user(uint8_t usb_led);
+void led_set_kb(uint8_t usb_led);
+
+#endif
diff --git a/quantum/quantum.mk b/quantum/quantum.mk
deleted file mode 100644
index de93af7e8c..0000000000
--- a/quantum/quantum.mk
+++ /dev/null
@@ -1,53 +0,0 @@
-QUANTUM_DIR = quantum
-
-# # project specific files
-SRC += $(QUANTUM_DIR)/keymap_common.c \
- $(QUANTUM_DIR)/led.c
-
-# ifdef KEYMAP_FILE
-# ifneq (,$(shell grep USING_MIDI '$(KEYMAP_FILE)'))
-# MIDI_ENABLE=yes
-# $(info * Overriding MIDI_ENABLE setting - $(KEYMAP_FILE) requires it)
-# endif
-# ifneq (,$(shell grep USING_UNICODE '$(KEYMAP_FILE)'))
-# UNICODE_ENABLE=yes
-# $(info * Overriding UNICODE_ENABLE setting - $(KEYMAP_FILE) requires it)
-# endif
-# ifneq (,$(shell grep USING_BACKLIGHT '$(KEYMAP_FILE)'))
-# BACKLIGHT_ENABLE=yes
-# $(info * Overriding BACKLIGHT_ENABLE setting - $(KEYMAP_FILE) requires it)
-# endif
-# endif
-
-ifndef CUSTOM_MATRIX
- SRC += $(QUANTUM_DIR)/matrix.c
-endif
-
-ifdef MIDI_ENABLE
- SRC += $(QUANTUM_DIR)/keymap_midi.c
-endif
-
-ifdef AUDIO_ENABLE
- SRC += $(QUANTUM_DIR)/audio.c
-endif
-
-ifdef UNICODE_ENABLE
- SRC += $(QUANTUM_DIR)/keymap_unicode.c
-endif
-
-ifdef RGBLIGHT_ENABLE
- SRC += $(QUANTUM_DIR)/light_ws2812.c
- SRC += $(QUANTUM_DIR)/rgblight.c
- OPT_DEFS += -DRGBLIGHT_ENABLE
-endif
-
-# Optimize size but this may cause error "relocation truncated to fit"
-#EXTRALDFLAGS = -Wl,--relax
-
-# Search Path
-VPATH += $(TOP_DIR)/$(QUANTUM_DIR)
-
-include $(TMK_DIR)/protocol/lufa.mk
-
-include $(TMK_DIR)/common.mk
-include $(TMK_DIR)/rules.mk
diff --git a/quantum/rgblight.c b/quantum/rgblight.c
index 2215cf5cdf..f82e3ec558 100644
--- a/quantum/rgblight.c
+++ b/quantum/rgblight.c
@@ -7,24 +7,41 @@
#include "debug.h"
const uint8_t DIM_CURVE[] PROGMEM = {
- 0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
- 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
- 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
- 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11,
- 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
- 15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20,
- 20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
- 27, 27, 28, 28, 29, 29, 30, 30, 31, 32, 32, 33, 33, 34, 35, 35,
- 36, 36, 37, 38, 38, 39, 40, 40, 41, 42, 43, 43, 44, 45, 46, 47,
- 48, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
- 63, 64, 65, 66, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 81, 82,
- 83, 85, 86, 88, 90, 91, 93, 94, 96, 98, 99, 101, 103, 105, 107, 109,
- 110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144,
- 146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190,
- 193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255,
+ 0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
+ 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
+ 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
+ 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11,
+ 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
+ 15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20,
+ 20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
+ 27, 27, 28, 28, 29, 29, 30, 30, 31, 32, 32, 33, 33, 34, 35, 35,
+ 36, 36, 37, 38, 38, 39, 40, 40, 41, 42, 43, 43, 44, 45, 46, 47,
+ 48, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
+ 63, 64, 65, 66, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 81, 82,
+ 83, 85, 86, 88, 90, 91, 93, 94, 96, 98, 99, 101, 103, 105, 107, 109,
+ 110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144,
+ 146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190,
+ 193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255
+};
+const uint8_t RGBLED_BREATHING_TABLE[] PROGMEM = {
+ 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9,
+ 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35,
+ 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
+ 79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121, 124,
+ 127, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, 173,
+ 176, 179, 182, 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211, 213, 215,
+ 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 238, 240, 241, 243, 244,
+ 245, 246, 248, 249, 250, 250, 251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
+ 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251, 250, 250, 249, 248, 246,
+ 245, 244, 243, 241, 240, 238, 237, 235, 234, 232, 230, 228, 226, 224, 222, 220,
+ 218, 215, 213, 211, 208, 206, 203, 201, 198, 196, 193, 190, 188, 185, 182, 179,
+ 176, 173, 170, 167, 165, 162, 158, 155, 152, 149, 146, 143, 140, 137, 134, 131,
+ 128, 124, 121, 118, 115, 112, 109, 106, 103, 100, 97, 93, 90, 88, 85, 82,
+ 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40,
+ 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11,
+ 10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0
};
-const uint8_t RGBLED_BREATHING_TABLE[] PROGMEM = {0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,17,18,20,21,23,25,27,29,31,33,35,37,40,42,44,47,49,52,54,57,59,62,65,67,70,73,76,79,82,85,88,90,93,97,100,103,106,109,112,115,118,121,124,127,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173,176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215,218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244,245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255,255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,246,245,244,243,241,240,238,237,235,234,232,230,228,226,224,222,220,218,215,213,211,208,206,203,201,198,196,193,190,188,185,182,179,176,173,170,167,165,162,158,155,152,149,146,143,140,137,134,131,128,124,121,118,115,112,109,106,103,100,97,93,90,88,85,82,79,76,73,70,67,65,62,59,57,54,52,49,47,44,42,40,37,35,33,31,29,27,25,23,21,20,18,17,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0};
const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
@@ -38,63 +55,56 @@ uint8_t rgblight_inited = 0;
void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) {
- /* convert hue, saturation and brightness ( HSB/HSV ) to RGB
- The DIM_CURVE is used only on brightness/value and on saturation (inverted).
- This looks the most natural.
- */
- uint8_t r, g, b;
+ // Convert hue, saturation, and value (HSV/HSB) to RGB. DIM_CURVE is used only
+ // on value and saturation (inverted). This looks the most natural.
+ uint8_t r = 0, g = 0, b = 0, base, color;
val = pgm_read_byte(&DIM_CURVE[val]);
- sat = 255 - pgm_read_byte(&DIM_CURVE[255 - sat]);
-
- uint8_t base;
-
- if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
- r = val;
- g = val;
- b = val;
- } else {
- base = ((255 - sat) * val) >> 8;
-
- switch (hue / 60) {
- case 0:
- r = val;
- g = (((val - base)*hue) / 60) + base;
- b = base;
- break;
-
- case 1:
- r = (((val - base)*(60 - (hue % 60))) / 60) + base;
- g = val;
- b = base;
- break;
-
- case 2:
- r = base;
- g = val;
- b = (((val - base)*(hue % 60)) / 60) + base;
- break;
-
- case 3:
- r = base;
- g = (((val - base)*(60 - (hue % 60))) / 60) + base;
- b = val;
- break;
-
- case 4:
- r = (((val - base)*(hue % 60)) / 60) + base;
- g = base;
- b = val;
- break;
-
- case 5:
- r = val;
- g = base;
- b = (((val - base)*(60 - (hue % 60))) / 60) + base;
- break;
- }
- }
- setrgb(r,g,b, led1);
+ sat = 255 - pgm_read_byte(&DIM_CURVE[255 - sat]);
+
+ if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
+ r = val;
+ g = val;
+ b = val;
+ } else {
+ base = ((255 - sat) * val) >> 8;
+ color = (val - base) * (hue % 60) / 60;
+
+ switch (hue / 60) {
+ case 0:
+ r = val;
+ g = base + color;
+ b = base;
+ break;
+ case 1:
+ r = val - color;
+ g = val;
+ b = base;
+ break;
+ case 2:
+ r = base;
+ g = val;
+ b = base + color;
+ break;
+ case 3:
+ r = base;
+ g = val - color;
+ b = val;
+ break;
+ case 4:
+ r = base + color;
+ g = base;
+ b = val;
+ break;
+ case 5:
+ r = val;
+ g = base;
+ b = val - color;
+ break;
+ }
+ }
+
+ setrgb(r, g, b, led1);
}
void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) {
@@ -107,46 +117,48 @@ void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) {
uint32_t eeconfig_read_rgblight(void) {
return eeprom_read_dword(EECONFIG_RGBLIGHT);
}
-void eeconfig_write_rgblight(uint32_t val) {
- eeprom_write_dword(EECONFIG_RGBLIGHT, val);
+void eeconfig_update_rgblight(uint32_t val) {
+ eeprom_update_dword(EECONFIG_RGBLIGHT, val);
}
-void eeconfig_write_rgblight_default(void) {
- dprintf("eeconfig_write_rgblight_default\n");
- rgblight_config.enable = 1;
- rgblight_config.mode = 1;
- rgblight_config.hue = 200;
- rgblight_config.sat = 204;
- rgblight_config.val = 204;
- eeconfig_write_rgblight(rgblight_config.raw);
+void eeconfig_update_rgblight_default(void) {
+ dprintf("eeconfig_update_rgblight_default\n");
+ rgblight_config.enable = 1;
+ rgblight_config.mode = 1;
+ rgblight_config.hue = 200;
+ rgblight_config.sat = 204;
+ rgblight_config.val = 204;
+ eeconfig_update_rgblight(rgblight_config.raw);
}
void eeconfig_debug_rgblight(void) {
- dprintf("rgblight_config eprom\n");
- dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
- dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
- dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
- dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
- dprintf("rgblight_config.val = %d\n", rgblight_config.val);
+ dprintf("rgblight_config eprom\n");
+ dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
+ dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
+ dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
+ dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
+ dprintf("rgblight_config.val = %d\n", rgblight_config.val);
}
void rgblight_init(void) {
debug_enable = 1; // Debug ON!
- dprintf("rgblight_init called.\n");
+ dprintf("rgblight_init called.\n");
rgblight_inited = 1;
- dprintf("rgblight_init start!\n");
+ dprintf("rgblight_init start!\n");
if (!eeconfig_is_enabled()) {
- dprintf("rgblight_init eeconfig is not enabled.\n");
+ dprintf("rgblight_init eeconfig is not enabled.\n");
eeconfig_init();
- eeconfig_write_rgblight_default();
+ eeconfig_update_rgblight_default();
}
rgblight_config.raw = eeconfig_read_rgblight();
- if (!rgblight_config.mode) {
- dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
- eeconfig_write_rgblight_default();
- rgblight_config.raw = eeconfig_read_rgblight();
- }
- eeconfig_debug_rgblight(); // display current eeprom values
+ if (!rgblight_config.mode) {
+ dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
+ eeconfig_update_rgblight_default();
+ rgblight_config.raw = eeconfig_read_rgblight();
+ }
+ eeconfig_debug_rgblight(); // display current eeprom values
- rgblight_timer_init(); // setup the timer
+ #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
+ rgblight_timer_init(); // setup the timer
+ #endif
if (rgblight_config.enable) {
rgblight_mode(rgblight_config.mode);
@@ -154,352 +166,376 @@ void rgblight_init(void) {
}
void rgblight_increase(void) {
- uint8_t mode;
+ uint8_t mode = 0;
if (rgblight_config.mode < RGBLIGHT_MODES) {
mode = rgblight_config.mode + 1;
}
- rgblight_mode(mode);
+ rgblight_mode(mode);
}
-
void rgblight_decrease(void) {
- uint8_t mode;
- if (rgblight_config.mode > 1) { //mode will never < 1, if mode is less than 1, eeprom need to be initialized.
- mode = rgblight_config.mode-1;
+ uint8_t mode = 0;
+ // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
+ if (rgblight_config.mode > 1) {
+ mode = rgblight_config.mode - 1;
}
- rgblight_mode(mode);
+ rgblight_mode(mode);
}
-
void rgblight_step(void) {
- uint8_t mode;
+ uint8_t mode = 0;
mode = rgblight_config.mode + 1;
if (mode > RGBLIGHT_MODES) {
mode = 1;
}
- rgblight_mode(mode);
+ rgblight_mode(mode);
}
void rgblight_mode(uint8_t mode) {
- if (!rgblight_config.enable) {
- return;
- }
- if (mode<1) {
- rgblight_config.mode = 1;
- } else if (mode > RGBLIGHT_MODES) {
- rgblight_config.mode = RGBLIGHT_MODES;
- } else {
- rgblight_config.mode = mode;
- }
- eeconfig_write_rgblight(rgblight_config.raw);
- dprintf("rgblight mode: %u\n", rgblight_config.mode);
- if (rgblight_config.mode == 1) {
- rgblight_timer_disable();
- } else if (rgblight_config.mode >=2 && rgblight_config.mode <=23) {
- // MODE 2-5, breathing
- // MODE 6-8, rainbow mood
- // MODE 9-14, rainbow swirl
- // MODE 15-20, snake
- // MODE 21-23, knight
- rgblight_timer_enable();
- }
+ if (!rgblight_config.enable) {
+ return;
+ }
+ if (mode < 1) {
+ rgblight_config.mode = 1;
+ } else if (mode > RGBLIGHT_MODES) {
+ rgblight_config.mode = RGBLIGHT_MODES;
+ } else {
+ rgblight_config.mode = mode;
+ }
+ eeconfig_update_rgblight(rgblight_config.raw);
+ xprintf("rgblight mode: %u\n", rgblight_config.mode);
+ if (rgblight_config.mode == 1) {
+ #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
+ rgblight_timer_disable();
+ #endif
+ } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 23) {
+ // MODE 2-5, breathing
+ // MODE 6-8, rainbow mood
+ // MODE 9-14, rainbow swirl
+ // MODE 15-20, snake
+ // MODE 21-23, knight
+
+ #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
+ rgblight_timer_enable();
+ #endif
+ }
rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
}
void rgblight_toggle(void) {
rgblight_config.enable ^= 1;
- eeconfig_write_rgblight(rgblight_config.raw);
- dprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable);
- if (rgblight_config.enable) {
- rgblight_mode(rgblight_config.mode);
- } else {
- rgblight_timer_disable();
- _delay_ms(50);
- rgblight_set();
- }
+ eeconfig_update_rgblight(rgblight_config.raw);
+ xprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable);
+ if (rgblight_config.enable) {
+ rgblight_mode(rgblight_config.mode);
+ } else {
+ #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
+ rgblight_timer_disable();
+ #endif
+ _delay_ms(50);
+ rgblight_set();
+ }
}
-void rgblight_increase_hue(void){
- uint16_t hue;
+void rgblight_increase_hue(void) {
+ uint16_t hue;
hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
}
-void rgblight_decrease_hue(void){
- uint16_t hue;
- if (rgblight_config.hue-RGBLIGHT_HUE_STEP <0 ) {
- hue = (rgblight_config.hue+360-RGBLIGHT_HUE_STEP) % 360;
- } else {
- hue = (rgblight_config.hue-RGBLIGHT_HUE_STEP) % 360;
- }
+void rgblight_decrease_hue(void) {
+ uint16_t hue;
+ if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
+ hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
+ } else {
+ hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
+ }
rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
}
void rgblight_increase_sat(void) {
- uint8_t sat;
+ uint8_t sat;
if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
sat = 255;
} else {
- sat = rgblight_config.sat+RGBLIGHT_SAT_STEP;
+ sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
}
rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
}
-void rgblight_decrease_sat(void){
- uint8_t sat;
+void rgblight_decrease_sat(void) {
+ uint8_t sat;
if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
sat = 0;
} else {
- sat = rgblight_config.sat-RGBLIGHT_SAT_STEP;
+ sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
}
rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
}
-void rgblight_increase_val(void){
- uint8_t val;
+void rgblight_increase_val(void) {
+ uint8_t val;
if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) {
val = 255;
} else {
- val = rgblight_config.val+RGBLIGHT_VAL_STEP;
+ val = rgblight_config.val + RGBLIGHT_VAL_STEP;
}
rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
}
void rgblight_decrease_val(void) {
- uint8_t val;
+ uint8_t val;
if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
val = 0;
} else {
- val = rgblight_config.val-RGBLIGHT_VAL_STEP;
+ val = rgblight_config.val - RGBLIGHT_VAL_STEP;
}
rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
}
-void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val){
- inmem_config.raw = rgblight_config.raw;
+void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
+ inmem_config.raw = rgblight_config.raw;
if (rgblight_config.enable) {
struct cRGB tmp_led;
sethsv(hue, sat, val, &tmp_led);
- inmem_config.hue = hue;
- inmem_config.sat = sat;
- inmem_config.val = val;
+ inmem_config.hue = hue;
+ inmem_config.sat = sat;
+ inmem_config.val = val;
// dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
}
}
-void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val){
+void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
if (rgblight_config.enable) {
- if (rgblight_config.mode == 1) {
- // same static color
- rgblight_sethsv_noeeprom(hue, sat, val);
- } else {
- // all LEDs in same color
- if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
- // breathing mode, ignore the change of val, use in memory value instead
- val = rgblight_config.val;
- } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
- // rainbow mood and rainbow swirl, ignore the change of hue
- hue = rgblight_config.hue;
- }
- }
- rgblight_config.hue = hue;
- rgblight_config.sat = sat;
- rgblight_config.val = val;
- eeconfig_write_rgblight(rgblight_config.raw);
- dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
- }
-}
-
-void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b){
+ if (rgblight_config.mode == 1) {
+ // same static color
+ rgblight_sethsv_noeeprom(hue, sat, val);
+ } else {
+ // all LEDs in same color
+ if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
+ // breathing mode, ignore the change of val, use in memory value instead
+ val = rgblight_config.val;
+ } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
+ // rainbow mood and rainbow swirl, ignore the change of hue
+ hue = rgblight_config.hue;
+ }
+ }
+ rgblight_config.hue = hue;
+ rgblight_config.sat = sat;
+ rgblight_config.val = val;
+ eeconfig_update_rgblight(rgblight_config.raw);
+ xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
+ }
+}
+
+void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
// dprintf("rgblight set rgb: %u,%u,%u\n", r,g,b);
- for (uint8_t i=0;i<RGBLED_NUM;i++) {
+ for (uint8_t i = 0; i < RGBLED_NUM; i++) {
led[i].r = r;
led[i].g = g;
led[i].b = b;
}
rgblight_set();
-
}
void rgblight_set(void) {
- if (rgblight_config.enable) {
- ws2812_setleds(led, RGBLED_NUM);
- } else {
- for (uint8_t i=0;i<RGBLED_NUM;i++) {
- led[i].r = 0;
- led[i].g = 0;
- led[i].b = 0;
- }
- ws2812_setleds(led, RGBLED_NUM);
- }
+ if (rgblight_config.enable) {
+ ws2812_setleds(led, RGBLED_NUM);
+ } else {
+ for (uint8_t i = 0; i < RGBLED_NUM; i++) {
+ led[i].r = 0;
+ led[i].g = 0;
+ led[i].b = 0;
+ }
+ ws2812_setleds(led, RGBLED_NUM);
+ }
}
+#if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
+
// Animation timer -- AVR Timer3
void rgblight_timer_init(void) {
- static uint8_t rgblight_timer_is_init = 0;
- if (rgblight_timer_is_init) {
- return;
- }
- rgblight_timer_is_init = 1;
- /* Timer 3 setup */
- TCCR3B = _BV(WGM32) //CTC mode OCR3A as TOP
- | _BV(CS30); //Clock selelct: clk/1
- /* Set TOP value */
- uint8_t sreg = SREG;
- cli();
- OCR3AH = (RGBLED_TIMER_TOP>>8)&0xff;
- OCR3AL = RGBLED_TIMER_TOP&0xff;
- SREG = sreg;
+ static uint8_t rgblight_timer_is_init = 0;
+ if (rgblight_timer_is_init) {
+ return;
+ }
+ rgblight_timer_is_init = 1;
+ /* Timer 3 setup */
+ TCCR3B = _BV(WGM32) //CTC mode OCR3A as TOP
+ | _BV(CS30); //Clock selelct: clk/1
+ /* Set TOP value */
+ uint8_t sreg = SREG;
+ cli();
+ OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
+ OCR3AL = RGBLED_TIMER_TOP & 0xff;
+ SREG = sreg;
}
void rgblight_timer_enable(void) {
- TIMSK3 |= _BV(OCIE3A);
- dprintf("TIMER3 enabled.\n");
+ TIMSK3 |= _BV(OCIE3A);
+ dprintf("TIMER3 enabled.\n");
}
void rgblight_timer_disable(void) {
- TIMSK3 &= ~_BV(OCIE3A);
- dprintf("TIMER3 disabled.\n");
+ TIMSK3 &= ~_BV(OCIE3A);
+ dprintf("TIMER3 disabled.\n");
}
void rgblight_timer_toggle(void) {
- TIMSK3 ^= _BV(OCIE3A);
- dprintf("TIMER3 toggled.\n");
+ TIMSK3 ^= _BV(OCIE3A);
+ dprintf("TIMER3 toggled.\n");
}
ISR(TIMER3_COMPA_vect) {
- // Mode = 1, static light, do nothing here
- if (rgblight_config.mode>=2 && rgblight_config.mode<=5) {
- // mode = 2 to 5, breathing mode
- rgblight_effect_breathing(rgblight_config.mode-2);
-
- } else if (rgblight_config.mode>=6 && rgblight_config.mode<=8) {
- rgblight_effect_rainbow_mood(rgblight_config.mode-6);
- } else if (rgblight_config.mode>=9 && rgblight_config.mode<=14) {
- rgblight_effect_rainbow_swirl(rgblight_config.mode-9);
- } else if (rgblight_config.mode>=15 && rgblight_config.mode<=20) {
- rgblight_effect_snake(rgblight_config.mode-15);
- } else if (rgblight_config.mode>=21 && rgblight_config.mode<=23) {
- rgblight_effect_knight(rgblight_config.mode-21);
- }
-}
-
-// effects
+ // mode = 1, static light, do nothing here
+ if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
+ // mode = 2 to 5, breathing mode
+ rgblight_effect_breathing(rgblight_config.mode - 2);
+ } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
+ // mode = 6 to 8, rainbow mood mod
+ rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
+ } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
+ // mode = 9 to 14, rainbow swirl mode
+ rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
+ } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
+ // mode = 15 to 20, snake mode
+ rgblight_effect_snake(rgblight_config.mode - 15);
+ } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
+ // mode = 21 to 23, knight mode
+ rgblight_effect_knight(rgblight_config.mode - 21);
+ }
+}
+
+// Effects
void rgblight_effect_breathing(uint8_t interval) {
- static uint8_t pos = 0;
- static uint16_t last_timer = 0;
+ static uint8_t pos = 0;
+ static uint16_t last_timer = 0;
- if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) return;
- last_timer = timer_read();
+ if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
+ return;
+ }
+ last_timer = timer_read();
- rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos]));
- pos = (pos+1) % 256;
+ rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos]));
+ pos = (pos + 1) % 256;
}
-
void rgblight_effect_rainbow_mood(uint8_t interval) {
- static uint16_t current_hue=0;
- static uint16_t last_timer = 0;
+ static uint16_t current_hue = 0;
+ static uint16_t last_timer = 0;
- if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) return;
- last_timer = timer_read();
- rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
- current_hue = (current_hue+1) % 360;
+ if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
+ return;
+ }
+ last_timer = timer_read();
+ rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
+ current_hue = (current_hue + 1) % 360;
}
-
void rgblight_effect_rainbow_swirl(uint8_t interval) {
- static uint16_t current_hue=0;
- static uint16_t last_timer = 0;
- uint16_t hue;
- uint8_t i;
- if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval/2])) return;
- last_timer = timer_read();
- for (i=0; i<RGBLED_NUM; i++) {
- hue = (360/RGBLED_NUM*i+current_hue)%360;
- sethsv(hue, rgblight_config.sat, rgblight_config.val, &led[i]);
- }
- rgblight_set();
-
- if (interval % 2) {
- current_hue = (current_hue+1) % 360;
- } else {
- if (current_hue -1 < 0) {
- current_hue = 359;
- } else {
- current_hue = current_hue - 1;
- }
-
- }
+ static uint16_t current_hue = 0;
+ static uint16_t last_timer = 0;
+ uint16_t hue;
+ uint8_t i;
+ if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval / 2])) {
+ return;
+ }
+ last_timer = timer_read();
+ for (i = 0; i < RGBLED_NUM; i++) {
+ hue = (360 / RGBLED_NUM * i + current_hue) % 360;
+ sethsv(hue, rgblight_config.sat, rgblight_config.val, &led[i]);
+ }
+ rgblight_set();
+
+ if (interval % 2) {
+ current_hue = (current_hue + 1) % 360;
+ } else {
+ if (current_hue - 1 < 0) {
+ current_hue = 359;
+ } else {
+ current_hue = current_hue - 1;
+ }
+ }
}
void rgblight_effect_snake(uint8_t interval) {
- static uint8_t pos=0;
- static uint16_t last_timer = 0;
- uint8_t i,j;
- int8_t k;
- int8_t increament = 1;
- if (interval%2) increament = -1;
- if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval/2])) return;
- last_timer = timer_read();
- for (i=0;i<RGBLED_NUM;i++) {
- led[i].r=0;
- led[i].g=0;
- led[i].b=0;
- for (j=0;j<RGBLIGHT_EFFECT_SNAKE_LENGTH;j++) {
- k = pos+j*increament;
- if (k<0) k = k+RGBLED_NUM;
- if (i==k) {
- sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), &led[i]);
- }
- }
- }
- rgblight_set();
- if (increament == 1) {
- if (pos - 1 < 0) {
- pos = RGBLED_NUM-1;
- } else {
- pos -= 1;
- }
- } else {
- pos = (pos+1)%RGBLED_NUM;
- }
-
+ static uint8_t pos = 0;
+ static uint16_t last_timer = 0;
+ uint8_t i, j;
+ int8_t k;
+ int8_t increment = 1;
+ if (interval % 2) {
+ increment = -1;
+ }
+ if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
+ return;
+ }
+ last_timer = timer_read();
+ for (i = 0; i < RGBLED_NUM; i++) {
+ led[i].r = 0;
+ led[i].g = 0;
+ led[i].b = 0;
+ for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
+ k = pos + j * increment;
+ if (k < 0) {
+ k = k + RGBLED_NUM;
+ }
+ if (i == k) {
+ sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), &led[i]);
+ }
+ }
+ }
+ rgblight_set();
+ if (increment == 1) {
+ if (pos - 1 < 0) {
+ pos = RGBLED_NUM - 1;
+ } else {
+ pos -= 1;
+ }
+ } else {
+ pos = (pos + 1) % RGBLED_NUM;
+ }
}
-
void rgblight_effect_knight(uint8_t interval) {
- static int8_t pos=0;
- static uint16_t last_timer = 0;
- uint8_t i,j,cur;
- int8_t k;
- struct cRGB preled[RGBLED_NUM];
- static int8_t increament = -1;
- if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) return;
- last_timer = timer_read();
- for (i=0;i<RGBLED_NUM;i++) {
- preled[i].r=0;
- preled[i].g=0;
- preled[i].b=0;
- for (j=0;j<RGBLIGHT_EFFECT_KNIGHT_LENGTH;j++) {
- k = pos+j*increament;
- if (k<0) k = 0;
- if (k>=RGBLED_NUM) k=RGBLED_NUM-1;
- if (i==k) {
- sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, &preled[i]);
- }
- }
- }
- if (RGBLIGHT_EFFECT_KNIGHT_OFFSET) {
- for (i=0;i<RGBLED_NUM;i++) {
- cur = (i+RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
- led[i].r = preled[cur].r;
- led[i].g = preled[cur].g;
- led[i].b = preled[cur].b;
- }
- }
- rgblight_set();
- if (increament == 1) {
- if (pos - 1 < 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
- pos = 0- RGBLIGHT_EFFECT_KNIGHT_LENGTH;
- increament = -1;
- } else {
- pos -= 1;
- }
- } else {
- if (pos+1>RGBLED_NUM+RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
- pos = RGBLED_NUM+RGBLIGHT_EFFECT_KNIGHT_LENGTH-1;
- increament = 1;
- } else {
- pos += 1;
- }
- }
-
+ static int8_t pos = 0;
+ static uint16_t last_timer = 0;
+ uint8_t i, j, cur;
+ int8_t k;
+ struct cRGB preled[RGBLED_NUM];
+ static int8_t increment = -1;
+ if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
+ return;
+ }
+ last_timer = timer_read();
+ for (i = 0; i < RGBLED_NUM; i++) {
+ preled[i].r = 0;
+ preled[i].g = 0;
+ preled[i].b = 0;
+ for (j = 0; j < RGBLIGHT_EFFECT_KNIGHT_LENGTH; j++) {
+ k = pos + j * increment;
+ if (k < 0) {
+ k = 0;
+ }
+ if (k >= RGBLED_NUM) {
+ k = RGBLED_NUM - 1;
+ }
+ if (i == k) {
+ sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, &preled[i]);
+ }
+ }
+ }
+ if (RGBLIGHT_EFFECT_KNIGHT_OFFSET) {
+ for (i = 0; i < RGBLED_NUM; i++) {
+ cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
+ led[i].r = preled[cur].r;
+ led[i].g = preled[cur].g;
+ led[i].b = preled[cur].b;
+ }
+ }
+ rgblight_set();
+ if (increment == 1) {
+ if (pos - 1 < 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
+ pos = 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH;
+ increment = -1;
+ } else {
+ pos -= 1;
+ }
+ } else {
+ if (pos + 1 > RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
+ pos = RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
+ increment = 1;
+ } else {
+ pos += 1;
+ }
+ }
}
+
+#endif
diff --git a/quantum/rgblight.h b/quantum/rgblight.h
index 9e1562328f..def26c428c 100644
--- a/quantum/rgblight.h
+++ b/quantum/rgblight.h
@@ -1,8 +1,11 @@
#ifndef RGBLIGHT_H
#define RGBLIGHT_H
-#ifndef RGBLIGHT_MODES
-#define RGBLIGHT_MODES 23
+
+#if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
+ #define RGBLIGHT_MODES 23
+#else
+ #define RGBLIGHT_MODES 1
#endif
#ifndef RGBLIGHT_EFFECT_SNAKE_LENGTH
@@ -64,10 +67,9 @@ void rgblight_decrease_val(void);
void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val);
void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b);
-#define EECONFIG_RGBLIGHT (uint8_t *)7
uint32_t eeconfig_read_rgblight(void);
-void eeconfig_write_rgblight(uint32_t val);
-void eeconfig_write_rgblight_default(void);
+void eeconfig_update_rgblight(uint32_t val);
+void eeconfig_update_rgblight_default(void);
void eeconfig_debug_rgblight(void);
void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1);
diff --git a/quantum/serial_link/LICENSE b/quantum/serial_link/LICENSE
new file mode 100644
index 0000000000..d7cc3198cb
--- /dev/null
+++ b/quantum/serial_link/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/quantum/serial_link/README.md b/quantum/serial_link/README.md
new file mode 100644
index 0000000000..e8490e290d
--- /dev/null
+++ b/quantum/serial_link/README.md
@@ -0,0 +1 @@
+# qmk_serial_link \ No newline at end of file
diff --git a/quantum/serial_link/protocol/byte_stuffer.c b/quantum/serial_link/protocol/byte_stuffer.c
new file mode 100644
index 0000000000..2c87d64c29
--- /dev/null
+++ b/quantum/serial_link/protocol/byte_stuffer.c
@@ -0,0 +1,142 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "serial_link/protocol/byte_stuffer.h"
+#include "serial_link/protocol/frame_validator.h"
+#include "serial_link/protocol/physical.h"
+#include <stdbool.h>
+
+// This implements the "Consistent overhead byte stuffing protocol"
+// https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
+// http://www.stuartcheshire.org/papers/COBSforToN.pdf
+
+typedef struct byte_stuffer_state {
+ uint16_t next_zero;
+ uint16_t data_pos;
+ bool long_frame;
+ uint8_t data[MAX_FRAME_SIZE];
+}byte_stuffer_state_t;
+
+static byte_stuffer_state_t states[NUM_LINKS];
+
+void init_byte_stuffer_state(byte_stuffer_state_t* state) {
+ state->next_zero = 0;
+ state->data_pos = 0;
+ state->long_frame = false;
+}
+
+void init_byte_stuffer(void) {
+ int i;
+ for (i=0;i<NUM_LINKS;i++) {
+ init_byte_stuffer_state(&states[i]);
+ }
+}
+
+void byte_stuffer_recv_byte(uint8_t link, uint8_t data) {
+ byte_stuffer_state_t* state = &states[link];
+ // Start of a new frame
+ if (state->next_zero == 0) {
+ state->next_zero = data;
+ state->long_frame = data == 0xFF;
+ state->data_pos = 0;
+ return;
+ }
+
+ state->next_zero--;
+ if (data == 0) {
+ if (state->next_zero == 0) {
+ // The frame is completed
+ if (state->data_pos > 0) {
+ validator_recv_frame(link, state->data, state->data_pos);
+ }
+ }
+ else {
+ // The frame is invalid, so reset
+ init_byte_stuffer_state(state);
+ }
+ }
+ else {
+ if (state->data_pos == MAX_FRAME_SIZE) {
+ // We exceeded our maximum frame size
+ // therefore there's nothing else to do than reset to a new frame
+ state->next_zero = data;
+ state->long_frame = data == 0xFF;
+ state->data_pos = 0;
+ }
+ else if (state->next_zero == 0) {
+ if (state->long_frame) {
+ // This is part of a long frame, so continue
+ state->next_zero = data;
+ state->long_frame = data == 0xFF;
+ }
+ else {
+ // Special case for zeroes
+ state->next_zero = data;
+ state->data[state->data_pos++] = 0;
+ }
+ }
+ else {
+ state->data[state->data_pos++] = data;
+ }
+ }
+}
+
+static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_non_zero) {
+ send_data(link, &num_non_zero, 1);
+ if (end > start) {
+ send_data(link, start, end-start);
+ }
+}
+
+void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) {
+ const uint8_t zero = 0;
+ if (size > 0) {
+ uint16_t num_non_zero = 1;
+ uint8_t* end = data + size;
+ uint8_t* start = data;
+ while (data < end) {
+ if (num_non_zero == 0xFF) {
+ // There's more data after big non-zero block
+ // So send it, and start a new block
+ send_block(link, start, data, num_non_zero);
+ start = data;
+ num_non_zero = 1;
+ }
+ else {
+ if (*data == 0) {
+ // A zero encountered, so send the block
+ send_block(link, start, data, num_non_zero);
+ start = data + 1;
+ num_non_zero = 1;
+ }
+ else {
+ num_non_zero++;
+ }
+ ++data;
+ }
+ }
+ send_block(link, start, data, num_non_zero);
+ send_data(link, &zero, 1);
+ }
+}
diff --git a/quantum/serial_link/protocol/byte_stuffer.h b/quantum/serial_link/protocol/byte_stuffer.h
new file mode 100644
index 0000000000..97e8968564
--- /dev/null
+++ b/quantum/serial_link/protocol/byte_stuffer.h
@@ -0,0 +1,37 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef SERIAL_LINK_BYTE_STUFFER_H
+#define SERIAL_LINK_BYTE_STUFFER_H
+
+#include <stdint.h>
+
+#define MAX_FRAME_SIZE 1024
+#define NUM_LINKS 2
+
+void init_byte_stuffer(void);
+void byte_stuffer_recv_byte(uint8_t link, uint8_t data);
+void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size);
+
+#endif
diff --git a/quantum/serial_link/protocol/frame_router.c b/quantum/serial_link/protocol/frame_router.c
new file mode 100644
index 0000000000..04b8c2e75c
--- /dev/null
+++ b/quantum/serial_link/protocol/frame_router.c
@@ -0,0 +1,69 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "serial_link/protocol/frame_router.h"
+#include "serial_link/protocol/transport.h"
+#include "serial_link/protocol/frame_validator.h"
+
+static bool is_master;
+
+void router_set_master(bool master) {
+ is_master = master;
+}
+
+void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){
+ if (is_master) {
+ if (link == DOWN_LINK) {
+ transport_recv_frame(data[size-1], data, size - 1);
+ }
+ }
+ else {
+ if (link == UP_LINK) {
+ if (data[size-1] & 1) {
+ transport_recv_frame(0, data, size - 1);
+ }
+ data[size-1] >>= 1;
+ validator_send_frame(DOWN_LINK, data, size);
+ }
+ else {
+ data[size-1]++;
+ validator_send_frame(UP_LINK, data, size);
+ }
+ }
+}
+
+void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) {
+ if (destination == 0) {
+ if (!is_master) {
+ data[size] = 1;
+ validator_send_frame(UP_LINK, data, size + 1);
+ }
+ }
+ else {
+ if (is_master) {
+ data[size] = destination;
+ validator_send_frame(DOWN_LINK, data, size + 1);
+ }
+ }
+}
diff --git a/quantum/serial_link/protocol/frame_router.h b/quantum/serial_link/protocol/frame_router.h
new file mode 100644
index 0000000000..712250ff35
--- /dev/null
+++ b/quantum/serial_link/protocol/frame_router.h
@@ -0,0 +1,38 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef SERIAL_LINK_FRAME_ROUTER_H
+#define SERIAL_LINK_FRAME_ROUTER_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#define UP_LINK 0
+#define DOWN_LINK 1
+
+void router_set_master(bool master);
+void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size);
+void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size);
+
+#endif
diff --git a/quantum/serial_link/protocol/frame_validator.c b/quantum/serial_link/protocol/frame_validator.c
new file mode 100644
index 0000000000..474f80ee8e
--- /dev/null
+++ b/quantum/serial_link/protocol/frame_validator.c
@@ -0,0 +1,121 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "serial_link/protocol/frame_validator.h"
+#include "serial_link/protocol/frame_router.h"
+#include "serial_link/protocol/byte_stuffer.h"
+#include <string.h>
+
+const uint32_t poly8_lookup[256] =
+{
+ 0, 0x77073096, 0xEE0E612C, 0x990951BA,
+ 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
+ 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
+ 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
+ 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
+ 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
+ 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
+ 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
+ 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
+ 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
+ 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
+ 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
+ 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
+ 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
+ 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
+ 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
+ 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
+ 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
+ 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
+ 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
+ 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
+ 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
+ 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
+ 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
+ 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
+ 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
+ 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
+ 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
+ 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
+ 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
+ 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
+ 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
+ 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
+ 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
+ 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
+ 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
+ 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
+ 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
+ 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
+ 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
+ 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
+ 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
+ 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
+ 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
+ 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
+ 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
+ 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
+ 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
+ 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
+ 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
+ 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
+ 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
+ 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
+ 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
+ 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
+ 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
+ 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
+ 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
+ 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
+ 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
+ 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
+ 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
+ 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
+ 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
+};
+
+static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength)
+{
+ uint32_t crc = 0xffffffff;
+ while (bytelength-- !=0) crc = poly8_lookup[((uint8_t) crc ^ *(p++))] ^ (crc >> 8);
+ // return (~crc); also works
+ return (crc ^ 0xffffffff);
+}
+
+void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) {
+ if (size > 4) {
+ uint32_t frame_crc;
+ memcpy(&frame_crc, data + size -4, 4);
+ uint32_t expected_crc = crc32_byte(data, size - 4);
+ if (frame_crc == expected_crc) {
+ route_incoming_frame(link, data, size-4);
+ }
+ }
+}
+
+void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size) {
+ uint32_t crc = crc32_byte(data, size);
+ memcpy(data + size, &crc, 4);
+ byte_stuffer_send_frame(link, data, size + 4);
+}
diff --git a/quantum/serial_link/protocol/frame_validator.h b/quantum/serial_link/protocol/frame_validator.h
new file mode 100644
index 0000000000..4a910d510b
--- /dev/null
+++ b/quantum/serial_link/protocol/frame_validator.h
@@ -0,0 +1,34 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef SERIAL_LINK_FRAME_VALIDATOR_H
+#define SERIAL_LINK_FRAME_VALIDATOR_H
+
+#include <stdint.h>
+
+void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size);
+// The buffer pointed to by the data needs 4 additional bytes
+void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size);
+
+#endif
diff --git a/quantum/serial_link/protocol/physical.h b/quantum/serial_link/protocol/physical.h
new file mode 100644
index 0000000000..425e06cdd2
--- /dev/null
+++ b/quantum/serial_link/protocol/physical.h
@@ -0,0 +1,30 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef SERIAL_LINK_PHYSICAL_H
+#define SERIAL_LINK_PHYSICAL_H
+
+void send_data(uint8_t link, const uint8_t* data, uint16_t size);
+
+#endif
diff --git a/quantum/serial_link/protocol/transport.c b/quantum/serial_link/protocol/transport.c
new file mode 100644
index 0000000000..ff795fe201
--- /dev/null
+++ b/quantum/serial_link/protocol/transport.c
@@ -0,0 +1,128 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "serial_link/protocol/transport.h"
+#include "serial_link/protocol/frame_router.h"
+#include "serial_link/protocol/triple_buffered_object.h"
+#include <string.h>
+
+#define MAX_REMOTE_OBJECTS 16
+static remote_object_t* remote_objects[MAX_REMOTE_OBJECTS];
+static uint32_t num_remote_objects = 0;
+
+void reinitialize_serial_link_transport(void) {
+ num_remote_objects = 0;
+}
+
+void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_objects) {
+ unsigned int i;
+ for(i=0;i<_num_remote_objects;i++) {
+ remote_object_t* obj = _remote_objects[i];
+ remote_objects[num_remote_objects++] = obj;
+ if (obj->object_type == MASTER_TO_ALL_SLAVES) {
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
+ triple_buffer_init(tb);
+ uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
+ tb = (triple_buffer_object_t*)start;
+ triple_buffer_init(tb);
+ }
+ else if(obj->object_type == MASTER_TO_SINGLE_SLAVE) {
+ uint8_t* start = obj->buffer;
+ unsigned int j;
+ for (j=0;j<NUM_SLAVES;j++) {
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
+ triple_buffer_init(tb);
+ start += LOCAL_OBJECT_SIZE(obj->object_size);
+ }
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
+ triple_buffer_init(tb);
+ }
+ else {
+ uint8_t* start = obj->buffer;
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
+ triple_buffer_init(tb);
+ start += LOCAL_OBJECT_SIZE(obj->object_size);
+ unsigned int j;
+ for (j=0;j<NUM_SLAVES;j++) {
+ tb = (triple_buffer_object_t*)start;
+ triple_buffer_init(tb);
+ start += REMOTE_OBJECT_SIZE(obj->object_size);
+ }
+ }
+ }
+}
+
+void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) {
+ uint8_t id = data[size-1];
+ if (id < num_remote_objects) {
+ remote_object_t* obj = remote_objects[id];
+ if (obj->object_size == size - 1) {
+ uint8_t* start;
+ if (obj->object_type == MASTER_TO_ALL_SLAVES) {
+ start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
+ }
+ else if(obj->object_type == SLAVE_TO_MASTER) {
+ start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
+ start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size);
+ }
+ else {
+ start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);
+ }
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
+ void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb);
+ memcpy(ptr, data, size - 1);
+ triple_buffer_end_write_internal(tb);
+ }
+ }
+}
+
+void update_transport(void) {
+ unsigned int i;
+ for(i=0;i<num_remote_objects;i++) {
+ remote_object_t* obj = remote_objects[i];
+ if (obj->object_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) {
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
+ uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
+ if (ptr) {
+ ptr[obj->object_size] = i;
+ uint8_t dest = obj->object_type == MASTER_TO_ALL_SLAVES ? 0xFF : 0;
+ router_send_frame(dest, ptr, obj->object_size + 1);
+ }
+ }
+ else {
+ uint8_t* start = obj->buffer;
+ unsigned int j;
+ for (j=0;j<NUM_SLAVES;j++) {
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
+ uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
+ if (ptr) {
+ ptr[obj->object_size] = i;
+ uint8_t dest = j + 1;
+ router_send_frame(dest, ptr, obj->object_size + 1);
+ }
+ start += LOCAL_OBJECT_SIZE(obj->object_size);
+ }
+ }
+ }
+}
diff --git a/quantum/serial_link/protocol/transport.h b/quantum/serial_link/protocol/transport.h
new file mode 100644
index 0000000000..2c5d890b21
--- /dev/null
+++ b/quantum/serial_link/protocol/transport.h
@@ -0,0 +1,152 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef SERIAL_LINK_TRANSPORT_H
+#define SERIAL_LINK_TRANSPORT_H
+
+#include "serial_link/protocol/triple_buffered_object.h"
+#include "serial_link/system/serial_link.h"
+
+#define NUM_SLAVES 8
+#define LOCAL_OBJECT_EXTRA 16
+
+// master -> slave = 1 local(target all), 1 remote object
+// slave -> master = 1 local(target 0), multiple remote objects
+// master -> single slave (multiple local, target id), 1 remote object
+typedef enum {
+ MASTER_TO_ALL_SLAVES,
+ MASTER_TO_SINGLE_SLAVE,
+ SLAVE_TO_MASTER,
+} remote_object_type;
+
+typedef struct {
+ remote_object_type object_type;
+ uint16_t object_size;
+ uint8_t buffer[] __attribute__((aligned(4)));
+} remote_object_t;
+
+#define REMOTE_OBJECT_SIZE(objectsize) \
+ (sizeof(triple_buffer_object_t) + objectsize * 3)
+#define LOCAL_OBJECT_SIZE(objectsize) \
+ (sizeof(triple_buffer_object_t) + (objectsize + LOCAL_OBJECT_EXTRA) * 3)
+
+#define REMOTE_OBJECT_HELPER(name, type, num_local, num_remote) \
+typedef struct { \
+ remote_object_t object; \
+ uint8_t buffer[ \
+ num_remote * REMOTE_OBJECT_SIZE(sizeof(type)) + \
+ num_local * LOCAL_OBJECT_SIZE(sizeof(type))]; \
+} remote_object_##name##_t;
+
+#define MASTER_TO_ALL_SLAVES_OBJECT(name, type) \
+ REMOTE_OBJECT_HELPER(name, type, 1, 1) \
+ remote_object_##name##_t remote_object_##name = { \
+ .object = { \
+ .object_type = MASTER_TO_ALL_SLAVES, \
+ .object_size = sizeof(type), \
+ } \
+ }; \
+ type* begin_write_##name(void) { \
+ remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \
+ return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \
+ }\
+ void end_write_##name(void) { \
+ remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \
+ triple_buffer_end_write_internal(tb); \
+ signal_data_written(); \
+ }\
+ type* read_##name(void) { \
+ remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
+ uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
+ return (type*)triple_buffer_read_internal(obj->object_size, tb); \
+ }
+
+#define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \
+ REMOTE_OBJECT_HELPER(name, type, NUM_SLAVES, 1) \
+ remote_object_##name##_t remote_object_##name = { \
+ .object = { \
+ .object_type = MASTER_TO_SINGLE_SLAVE, \
+ .object_size = sizeof(type), \
+ } \
+ }; \
+ type* begin_write_##name(uint8_t slave) { \
+ remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
+ uint8_t* start = obj->buffer;\
+ start += slave * LOCAL_OBJECT_SIZE(obj->object_size); \
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
+ return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \
+ }\
+ void end_write_##name(uint8_t slave) { \
+ remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
+ uint8_t* start = obj->buffer;\
+ start += slave * LOCAL_OBJECT_SIZE(obj->object_size); \
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
+ triple_buffer_end_write_internal(tb); \
+ signal_data_written(); \
+ }\
+ type* read_##name() { \
+ remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
+ uint8_t* start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);\
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
+ return (type*)triple_buffer_read_internal(obj->object_size, tb); \
+ }
+
+#define SLAVE_TO_MASTER_OBJECT(name, type) \
+ REMOTE_OBJECT_HELPER(name, type, 1, NUM_SLAVES) \
+ remote_object_##name##_t remote_object_##name = { \
+ .object = { \
+ .object_type = SLAVE_TO_MASTER, \
+ .object_size = sizeof(type), \
+ } \
+ }; \
+ type* begin_write_##name(void) { \
+ remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \
+ return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \
+ }\
+ void end_write_##name(void) { \
+ remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \
+ triple_buffer_end_write_internal(tb); \
+ signal_data_written(); \
+ }\
+ type* read_##name(uint8_t slave) { \
+ remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
+ uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\
+ start+=slave * REMOTE_OBJECT_SIZE(obj->object_size); \
+ triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
+ return (type*)triple_buffer_read_internal(obj->object_size, tb); \
+ }
+
+#define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name
+
+void add_remote_objects(remote_object_t** remote_objects, uint32_t num_remote_objects);
+void reinitialize_serial_link_transport(void);
+void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size);
+void update_transport(void);
+
+#endif
diff --git a/quantum/serial_link/protocol/triple_buffered_object.c b/quantum/serial_link/protocol/triple_buffered_object.c
new file mode 100644
index 0000000000..e3e8989d30
--- /dev/null
+++ b/quantum/serial_link/protocol/triple_buffered_object.c
@@ -0,0 +1,78 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "serial_link/protocol/triple_buffered_object.h"
+#include "serial_link/system/serial_link.h"
+#include <stdbool.h>
+#include <stddef.h>
+
+#define GET_READ_INDEX() object->state & 3
+#define GET_WRITE_INDEX() (object->state >> 2) & 3
+#define GET_SHARED_INDEX() (object->state >> 4) & 3
+#define GET_DATA_AVAILABLE() (object->state >> 6) & 1
+
+#define SET_READ_INDEX(i) object->state = ((object->state & ~3) | i)
+#define SET_WRITE_INDEX(i) object->state = ((object->state & ~(3 << 2)) | (i << 2))
+#define SET_SHARED_INDEX(i) object->state = ((object->state & ~(3 << 4)) | (i << 4))
+#define SET_DATA_AVAILABLE(i) object->state = ((object->state & ~(1 << 6)) | (i << 6))
+
+void triple_buffer_init(triple_buffer_object_t* object) {
+ object->state = 0;
+ SET_WRITE_INDEX(0);
+ SET_READ_INDEX(1);
+ SET_SHARED_INDEX(2);
+ SET_DATA_AVAILABLE(0);
+}
+
+void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object) {
+ serial_link_lock();
+ if (GET_DATA_AVAILABLE()) {
+ uint8_t shared_index = GET_SHARED_INDEX();
+ uint8_t read_index = GET_READ_INDEX();
+ SET_READ_INDEX(shared_index);
+ SET_SHARED_INDEX(read_index);
+ SET_DATA_AVAILABLE(false);
+ serial_link_unlock();
+ return object->buffer + object_size * shared_index;
+ }
+ else {
+ serial_link_unlock();
+ return NULL;
+ }
+}
+
+void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object) {
+ uint8_t write_index = GET_WRITE_INDEX();
+ return object->buffer + object_size * write_index;
+}
+
+void triple_buffer_end_write_internal(triple_buffer_object_t* object) {
+ serial_link_lock();
+ uint8_t shared_index = GET_SHARED_INDEX();
+ uint8_t write_index = GET_WRITE_INDEX();
+ SET_SHARED_INDEX(write_index);
+ SET_WRITE_INDEX(shared_index);
+ SET_DATA_AVAILABLE(true);
+ serial_link_unlock();
+}
diff --git a/quantum/serial_link/protocol/triple_buffered_object.h b/quantum/serial_link/protocol/triple_buffered_object.h
new file mode 100644
index 0000000000..2e57db3f50
--- /dev/null
+++ b/quantum/serial_link/protocol/triple_buffered_object.h
@@ -0,0 +1,51 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef SERIAL_LINK_TRIPLE_BUFFERED_OBJECT_H
+#define SERIAL_LINK_TRIPLE_BUFFERED_OBJECT_H
+
+#include <stdint.h>
+
+typedef struct {
+ uint8_t state;
+ uint8_t buffer[] __attribute__((aligned(4)));
+}triple_buffer_object_t;
+
+void triple_buffer_init(triple_buffer_object_t* object);
+
+#define triple_buffer_begin_write(object) \
+ (typeof(*object.buffer[0])*)triple_buffer_begin_write_internal(sizeof(*object.buffer[0]), (triple_buffer_object_t*)object)
+
+#define triple_buffer_end_write(object) \
+ triple_buffer_end_write_internal((triple_buffer_object_t*)object)
+
+#define triple_buffer_read(object) \
+ (typeof(*object.buffer[0])*)triple_buffer_read_internal(sizeof(*object.buffer[0]), (triple_buffer_object_t*)object)
+
+void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object);
+void triple_buffer_end_write_internal(triple_buffer_object_t* object);
+void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object);
+
+
+#endif
diff --git a/quantum/serial_link/system/serial_link.c b/quantum/serial_link/system/serial_link.c
new file mode 100644
index 0000000000..75c7e77a76
--- /dev/null
+++ b/quantum/serial_link/system/serial_link.c
@@ -0,0 +1,265 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+#include "report.h"
+#include "host_driver.h"
+#include "serial_link/system/serial_link.h"
+#include "hal.h"
+#include "serial_link/protocol/byte_stuffer.h"
+#include "serial_link/protocol/transport.h"
+#include "serial_link/protocol/frame_router.h"
+#include "matrix.h"
+#include <stdbool.h>
+#include "print.h"
+#include "config.h"
+
+static event_source_t new_data_event;
+static bool serial_link_connected;
+static bool is_master = false;
+
+static uint8_t keyboard_leds(void);
+static void send_keyboard(report_keyboard_t *report);
+static void send_mouse(report_mouse_t *report);
+static void send_system(uint16_t data);
+static void send_consumer(uint16_t data);
+
+host_driver_t serial_driver = {
+ keyboard_leds,
+ send_keyboard,
+ send_mouse,
+ send_system,
+ send_consumer
+};
+
+// Define these in your Config.h file
+#ifndef SERIAL_LINK_BAUD
+#error "Serial link baud is not set"
+#endif
+
+#ifndef SERIAL_LINK_THREAD_PRIORITY
+#error "Serial link thread priority not set"
+#endif
+
+static SerialConfig config = {
+ .sc_speed = SERIAL_LINK_BAUD
+};
+
+//#define DEBUG_LINK_ERRORS
+
+static uint32_t read_from_serial(SerialDriver* driver, uint8_t link) {
+ const uint32_t buffer_size = 16;
+ uint8_t buffer[buffer_size];
+ uint32_t bytes_read = sdAsynchronousRead(driver, buffer, buffer_size);
+ uint8_t* current = buffer;
+ uint8_t* end = current + bytes_read;
+ while(current < end) {
+ byte_stuffer_recv_byte(link, *current);
+ current++;
+ }
+ return bytes_read;
+}
+
+static void print_error(char* str, eventflags_t flags, SerialDriver* driver) {
+#ifdef DEBUG_LINK_ERRORS
+ if (flags & SD_PARITY_ERROR) {
+ print(str);
+ print(" Parity error\n");
+ }
+ if (flags & SD_FRAMING_ERROR) {
+ print(str);
+ print(" Framing error\n");
+ }
+ if (flags & SD_OVERRUN_ERROR) {
+ print(str);
+ uint32_t size = qSpaceI(&(driver->iqueue));
+ xprintf(" Overrun error, queue size %d\n", size);
+
+ }
+ if (flags & SD_NOISE_ERROR) {
+ print(str);
+ print(" Noise error\n");
+ }
+ if (flags & SD_BREAK_DETECTED) {
+ print(str);
+ print(" Break detected\n");
+ }
+#else
+ (void)str;
+ (void)flags;
+ (void)driver;
+#endif
+}
+
+bool is_serial_link_master(void) {
+ return is_master;
+}
+
+// TODO: Optimize the stack size, this is probably way too big
+static THD_WORKING_AREA(serialThreadStack, 1024);
+static THD_FUNCTION(serialThread, arg) {
+ (void)arg;
+ event_listener_t new_data_listener;
+ event_listener_t sd1_listener;
+ event_listener_t sd2_listener;
+ chEvtRegister(&new_data_event, &new_data_listener, 0);
+ eventflags_t events = CHN_INPUT_AVAILABLE
+ | SD_PARITY_ERROR | SD_FRAMING_ERROR | SD_OVERRUN_ERROR | SD_NOISE_ERROR | SD_BREAK_DETECTED;
+ chEvtRegisterMaskWithFlags(chnGetEventSource(&SD1),
+ &sd1_listener,
+ EVENT_MASK(1),
+ events);
+ chEvtRegisterMaskWithFlags(chnGetEventSource(&SD2),
+ &sd2_listener,
+ EVENT_MASK(2),
+ events);
+ bool need_wait = false;
+ while(true) {
+ eventflags_t flags1 = 0;
+ eventflags_t flags2 = 0;
+ if (need_wait) {
+ eventmask_t mask = chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(1000));
+ if (mask & EVENT_MASK(1)) {
+ flags1 = chEvtGetAndClearFlags(&sd1_listener);
+ print_error("DOWNLINK", flags1, &SD1);
+ }
+ if (mask & EVENT_MASK(2)) {
+ flags2 = chEvtGetAndClearFlags(&sd2_listener);
+ print_error("UPLINK", flags2, &SD2);
+ }
+ }
+
+ // Always stay as master, even if the USB goes into sleep mode
+ is_master |= usbGetDriverStateI(&USBD1) == USB_ACTIVE;
+ router_set_master(is_master);
+
+ need_wait = true;
+ need_wait &= read_from_serial(&SD2, UP_LINK) == 0;
+ need_wait &= read_from_serial(&SD1, DOWN_LINK) == 0;
+ update_transport();
+ }
+}
+
+void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
+ if (link == DOWN_LINK) {
+ sdWrite(&SD1, data, size);
+ }
+ else {
+ sdWrite(&SD2, data, size);
+ }
+}
+
+static systime_t last_update = 0;
+
+typedef struct {
+ matrix_row_t rows[MATRIX_ROWS];
+} matrix_object_t;
+
+static matrix_object_t last_matrix = {};
+
+SLAVE_TO_MASTER_OBJECT(keyboard_matrix, matrix_object_t);
+MASTER_TO_ALL_SLAVES_OBJECT(serial_link_connected, bool);
+
+static remote_object_t* remote_objects[] = {
+ REMOTE_OBJECT(serial_link_connected),
+ REMOTE_OBJECT(keyboard_matrix),
+};
+
+void init_serial_link(void) {
+ serial_link_connected = false;
+ init_serial_link_hal();
+ add_remote_objects(remote_objects, sizeof(remote_objects)/sizeof(remote_object_t*));
+ init_byte_stuffer();
+ sdStart(&SD1, &config);
+ sdStart(&SD2, &config);
+ chEvtObjectInit(&new_data_event);
+ (void)chThdCreateStatic(serialThreadStack, sizeof(serialThreadStack),
+ SERIAL_LINK_THREAD_PRIORITY, serialThread, NULL);
+}
+
+void matrix_set_remote(matrix_row_t* rows, uint8_t index);
+
+void serial_link_update(void) {
+ if (read_serial_link_connected()) {
+ serial_link_connected = true;
+ }
+
+ matrix_object_t matrix;
+ bool changed = false;
+ for(uint8_t i=0;i<MATRIX_ROWS;i++) {
+ matrix.rows[i] = matrix_get_row(i);
+ changed |= matrix.rows[i] != last_matrix.rows[i];
+ }
+
+ systime_t current_time = chVTGetSystemTimeX();
+ systime_t delta = current_time - last_update;
+ if (changed || delta > US2ST(1000)) {
+ last_update = current_time;
+ last_matrix = matrix;
+ matrix_object_t* m = begin_write_keyboard_matrix();
+ for(uint8_t i=0;i<MATRIX_ROWS;i++) {
+ m->rows[i] = matrix.rows[i];
+ }
+ end_write_keyboard_matrix();
+ *begin_write_serial_link_connected() = true;
+ end_write_serial_link_connected();
+ }
+
+ matrix_object_t* m = read_keyboard_matrix(0);
+ if (m) {
+ matrix_set_remote(m->rows, 0);
+ }
+}
+
+void signal_data_written(void) {
+ chEvtBroadcast(&new_data_event);
+}
+
+bool is_serial_link_connected(void) {
+ return serial_link_connected;
+}
+
+host_driver_t* get_serial_link_driver(void) {
+ return &serial_driver;
+}
+
+// NOTE: The driver does nothing, because the master handles everything
+uint8_t keyboard_leds(void) {
+ return 0;
+}
+
+void send_keyboard(report_keyboard_t *report) {
+ (void)report;
+}
+
+void send_mouse(report_mouse_t *report) {
+ (void)report;
+}
+
+void send_system(uint16_t data) {
+ (void)data;
+}
+
+void send_consumer(uint16_t data) {
+ (void)data;
+}
+
diff --git a/quantum/serial_link/system/serial_link.h b/quantum/serial_link/system/serial_link.h
new file mode 100644
index 0000000000..351e03877b
--- /dev/null
+++ b/quantum/serial_link/system/serial_link.h
@@ -0,0 +1,63 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef SERIAL_LINK_H
+#define SERIAL_LINK_H
+
+#include "host_driver.h"
+#include <stdbool.h>
+
+void init_serial_link(void);
+void init_serial_link_hal(void);
+bool is_serial_link_connected(void);
+bool is_serial_link_master(void);
+host_driver_t* get_serial_link_driver(void);
+void serial_link_update(void);
+
+#if defined(PROTOCOL_CHIBIOS)
+#include "ch.h"
+
+static inline void serial_link_lock(void) {
+ chSysLock();
+}
+
+static inline void serial_link_unlock(void) {
+ chSysUnlock();
+}
+
+void signal_data_written(void);
+
+#else
+
+inline void serial_link_lock(void) {
+}
+
+inline void serial_link_unlock(void) {
+}
+
+void signal_data_written(void);
+
+#endif
+
+#endif
diff --git a/quantum/serial_link/tests/Makefile b/quantum/serial_link/tests/Makefile
new file mode 100644
index 0000000000..1b072c6f1d
--- /dev/null
+++ b/quantum/serial_link/tests/Makefile
@@ -0,0 +1,61 @@
+# The MIT License (MIT)
+#
+# Copyright (c) 2016 Fred Sundvik
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+CC = gcc
+CFLAGS =
+INCLUDES = -I. -I../../
+LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src -shared
+LDLIBS = -lcgreen
+UNITOBJ = $(BUILDDIR)/serialtest/unitobj
+DEPDIR = $(BUILDDIR)/serialtest/unit.d
+UNITTESTS = $(BUILDDIR)/serialtest/unittests
+DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
+EXT = .so
+UNAME := $(shell uname)
+ifneq (, $(findstring MINGW, $(UNAME)))
+ EXT = .dll
+endif
+ifneq (, $(findstring CYGWIN, $(UNAME)))
+ EXT = .dll
+endif
+
+SRC = $(wildcard *.c)
+TESTFILES = $(patsubst %.c, $(UNITTESTS)/%$(EXT), $(SRC))
+$(shell mkdir -p $(DEPDIR) >/dev/null)
+
+test: $(TESTFILES)
+ @$(BUILDDIR)/cgreen/build-c/tools/cgreen-runner --color $(TESTFILES)
+
+$(UNITTESTS)/%$(EXT): $(UNITOBJ)/%.o
+ @mkdir -p $(UNITTESTS)
+ $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+$(UNITOBJ)/%.o : %.c
+$(UNITOBJ)/%.o: %.c $(DEPDIR)/%.d
+ @mkdir -p $(UNITOBJ)
+ $(CC) $(CFLAGS) $(DEPFLAGS) $(INCLUDES) -c $< -o $@
+ @mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
+
+$(DEPDIR)/%.d: ;
+.PRECIOUS: $(DEPDIR)/%.d
+
+-include $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRC))) \ No newline at end of file
diff --git a/quantum/serial_link/tests/byte_stuffer_tests.cpp b/quantum/serial_link/tests/byte_stuffer_tests.cpp
new file mode 100644
index 0000000000..ff49d727bb
--- /dev/null
+++ b/quantum/serial_link/tests/byte_stuffer_tests.cpp
@@ -0,0 +1,483 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include <vector>
+#include <algorithm>
+extern "C" {
+#include "serial_link/protocol/byte_stuffer.h"
+#include "serial_link/protocol/frame_validator.h"
+#include "serial_link/protocol/physical.h"
+}
+
+using testing::_;
+using testing::ElementsAreArray;
+using testing::Args;
+
+class ByteStuffer : public ::testing::Test{
+public:
+ ByteStuffer() {
+ Instance = this;
+ init_byte_stuffer();
+ }
+
+ ~ByteStuffer() {
+ Instance = nullptr;
+ }
+
+ MOCK_METHOD3(validator_recv_frame, void (uint8_t link, uint8_t* data, uint16_t size));
+
+ void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
+ std::copy(data, data + size, std::back_inserter(sent_data));
+ }
+ std::vector<uint8_t> sent_data;
+
+ static ByteStuffer* Instance;
+};
+
+ByteStuffer* ByteStuffer::Instance = nullptr;
+
+extern "C" {
+ void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) {
+ ByteStuffer::Instance->validator_recv_frame(link, data, size);
+ }
+
+ void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
+ ByteStuffer::Instance->send_data(link, data, size);
+ }
+}
+
+TEST_F(ByteStuffer, receives_no_frame_for_a_single_zero_byte) {
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .Times(0);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, receives_no_frame_for_a_single_FF_byte) {
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .Times(0);
+ byte_stuffer_recv_byte(0, 0xFF);
+}
+
+TEST_F(ByteStuffer, receives_no_frame_for_a_single_random_byte) {
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .Times(0);
+ byte_stuffer_recv_byte(0, 0x4A);
+}
+
+TEST_F(ByteStuffer, receives_no_frame_for_a_zero_length_frame) {
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .Times(0);
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, receives_single_byte_valid_frame) {
+ uint8_t expected[] = {0x37};
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ byte_stuffer_recv_byte(0, 2);
+ byte_stuffer_recv_byte(0, 0x37);
+ byte_stuffer_recv_byte(0, 0);
+}
+TEST_F(ByteStuffer, receives_three_bytes_valid_frame) {
+ uint8_t expected[] = {0x37, 0x99, 0xFF};
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ byte_stuffer_recv_byte(0, 4);
+ byte_stuffer_recv_byte(0, 0x37);
+ byte_stuffer_recv_byte(0, 0x99);
+ byte_stuffer_recv_byte(0, 0xFF);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, receives_single_zero_valid_frame) {
+ uint8_t expected[] = {0};
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, receives_valid_frame_with_zeroes) {
+ uint8_t expected[] = {5, 0, 3, 0};
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ byte_stuffer_recv_byte(0, 2);
+ byte_stuffer_recv_byte(0, 5);
+ byte_stuffer_recv_byte(0, 2);
+ byte_stuffer_recv_byte(0, 3);
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+
+TEST_F(ByteStuffer, receives_two_valid_frames) {
+ uint8_t expected1[] = {5, 0};
+ uint8_t expected2[] = {3};
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected1)));
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected2)));
+ byte_stuffer_recv_byte(1, 2);
+ byte_stuffer_recv_byte(1, 5);
+ byte_stuffer_recv_byte(1, 1);
+ byte_stuffer_recv_byte(1, 0);
+ byte_stuffer_recv_byte(1, 2);
+ byte_stuffer_recv_byte(1, 3);
+ byte_stuffer_recv_byte(1, 0);
+}
+
+TEST_F(ByteStuffer, receives_valid_frame_after_unexpected_zero) {
+ uint8_t expected[] = {5, 7};
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ byte_stuffer_recv_byte(1, 3);
+ byte_stuffer_recv_byte(1, 1);
+ byte_stuffer_recv_byte(1, 0);
+ byte_stuffer_recv_byte(1, 3);
+ byte_stuffer_recv_byte(1, 5);
+ byte_stuffer_recv_byte(1, 7);
+ byte_stuffer_recv_byte(1, 0);
+}
+
+TEST_F(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) {
+ uint8_t expected[] = {5, 7};
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ byte_stuffer_recv_byte(0, 2);
+ byte_stuffer_recv_byte(0, 9);
+ byte_stuffer_recv_byte(0, 4); // This should have been zero
+ byte_stuffer_recv_byte(0, 0);
+ byte_stuffer_recv_byte(0, 3);
+ byte_stuffer_recv_byte(0, 5);
+ byte_stuffer_recv_byte(0, 7);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) {
+ uint8_t expected[254];
+ int i;
+ for (i=0;i<254;i++) {
+ expected[i] = i + 1;
+ }
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ byte_stuffer_recv_byte(0, 0xFF);
+ for (i=0;i<254;i++) {
+ byte_stuffer_recv_byte(0, i+1);
+ }
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) {
+ uint8_t expected[255];
+ int i;
+ for (i=0;i<254;i++) {
+ expected[i] = i + 1;
+ }
+ expected[254] = 7;
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ byte_stuffer_recv_byte(0, 0xFF);
+ for (i=0;i<254;i++) {
+ byte_stuffer_recv_byte(0, i+1);
+ }
+ byte_stuffer_recv_byte(0, 2);
+ byte_stuffer_recv_byte(0, 7);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) {
+ uint8_t expected[255];
+ int i;
+ for (i=0;i<254;i++) {
+ expected[i] = i + 1;
+ }
+ expected[254] = 0;
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ byte_stuffer_recv_byte(0, 0xFF);
+ for (i=0;i<254;i++) {
+ byte_stuffer_recv_byte(0, i+1);
+ }
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, receives_two_long_frames_and_some_more) {
+ uint8_t expected[515];
+ int i;
+ int j;
+ for (j=0;j<2;j++) {
+ for (i=0;i<254;i++) {
+ expected[i+254*j] = i + 1;
+ }
+ }
+ for (i=0;i<7;i++) {
+ expected[254*2+i] = i + 1;
+ }
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ byte_stuffer_recv_byte(0, 0xFF);
+ for (i=0;i<254;i++) {
+ byte_stuffer_recv_byte(0, i+1);
+ }
+ byte_stuffer_recv_byte(0, 0xFF);
+ for (i=0;i<254;i++) {
+ byte_stuffer_recv_byte(0, i+1);
+ }
+ byte_stuffer_recv_byte(0, 8);
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 2);
+ byte_stuffer_recv_byte(0, 3);
+ byte_stuffer_recv_byte(0, 4);
+ byte_stuffer_recv_byte(0, 5);
+ byte_stuffer_recv_byte(0, 6);
+ byte_stuffer_recv_byte(0, 7);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) {
+ uint8_t expected[MAX_FRAME_SIZE] = {};
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ int i;
+ byte_stuffer_recv_byte(0, 1);
+ for(i=0;i<MAX_FRAME_SIZE;i++) {
+ byte_stuffer_recv_byte(0, 1);
+ }
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) {
+ uint8_t expected[1] = {0};
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .Times(0);
+ int i;
+ byte_stuffer_recv_byte(0, 1);
+ for(i=0;i<MAX_FRAME_SIZE;i++) {
+ byte_stuffer_recv_byte(0, 1);
+ }
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, received_frame_is_aborted_when_its_too_long) {
+ uint8_t expected[1] = {1};
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ int i;
+ byte_stuffer_recv_byte(0, 1);
+ for(i=0;i<MAX_FRAME_SIZE;i++) {
+ byte_stuffer_recv_byte(0, 1);
+ }
+ byte_stuffer_recv_byte(0, 2);
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+TEST_F(ByteStuffer, does_nothing_when_sending_zero_size_frame) {
+ EXPECT_EQ(sent_data.size(), 0);
+ byte_stuffer_send_frame(0, NULL, 0);
+}
+
+TEST_F(ByteStuffer, send_one_byte_frame) {
+ uint8_t data[] = {5};
+ byte_stuffer_send_frame(1, data, 1);
+ uint8_t expected[] = {2, 5, 0};
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_two_byte_frame) {
+ uint8_t data[] = {5, 0x77};
+ byte_stuffer_send_frame(0, data, 2);
+ uint8_t expected[] = {3, 5, 0x77, 0};
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_one_byte_frame_with_zero) {
+ uint8_t data[] = {0};
+ byte_stuffer_send_frame(0, data, 1);
+ uint8_t expected[] = {1, 1, 0};
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_two_byte_frame_starting_with_zero) {
+ uint8_t data[] = {0, 9};
+ byte_stuffer_send_frame(1, data, 2);
+ uint8_t expected[] = {1, 2, 9, 0};
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_two_byte_frame_starting_with_non_zero) {
+ uint8_t data[] = {9, 0};
+ byte_stuffer_send_frame(1, data, 2);
+ uint8_t expected[] = {2, 9, 1, 0};
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_three_byte_frame_zero_in_the_middle) {
+ uint8_t data[] = {9, 0, 0x68};
+ byte_stuffer_send_frame(0, data, 3);
+ uint8_t expected[] = {2, 9, 2, 0x68, 0};
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_three_byte_frame_data_in_the_middle) {
+ uint8_t data[] = {0, 0x55, 0};
+ byte_stuffer_send_frame(0, data, 3);
+ uint8_t expected[] = {1, 2, 0x55, 1, 0};
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_three_byte_frame_with_all_zeroes) {
+ uint8_t data[] = {0, 0, 0};
+ byte_stuffer_send_frame(0, data, 3);
+ uint8_t expected[] = {1, 1, 1, 1, 0};
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_frame_with_254_non_zeroes) {
+ uint8_t data[254];
+ int i;
+ for(i=0;i<254;i++) {
+ data[i] = i + 1;
+ }
+ byte_stuffer_send_frame(0, data, 254);
+ uint8_t expected[256];
+ expected[0] = 0xFF;
+ for(i=1;i<255;i++) {
+ expected[i] = i;
+ }
+ expected[255] = 0;
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_frame_with_255_non_zeroes) {
+ uint8_t data[255];
+ int i;
+ for(i=0;i<255;i++) {
+ data[i] = i + 1;
+ }
+ byte_stuffer_send_frame(0, data, 255);
+ uint8_t expected[258];
+ expected[0] = 0xFF;
+ for(i=1;i<255;i++) {
+ expected[i] = i;
+ }
+ expected[255] = 2;
+ expected[256] = 255;
+ expected[257] = 0;
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) {
+ uint8_t data[255];
+ int i;
+ for(i=0;i<254;i++) {
+ data[i] = i + 1;
+ }
+ data[254] = 0;
+ byte_stuffer_send_frame(0, data, 255);
+ uint8_t expected[258];
+ expected[0] = 0xFF;
+ for(i=1;i<255;i++) {
+ expected[i] = i;
+ }
+ expected[255] = 1;
+ expected[256] = 1;
+ expected[257] = 0;
+ EXPECT_THAT(sent_data, ElementsAreArray(expected));
+}
+
+TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_small_packet) {
+ uint8_t original_data[] = { 1, 2, 3};
+ byte_stuffer_send_frame(0, original_data, sizeof(original_data));
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(original_data)));
+ int i;
+ for(auto& d : sent_data) {
+ byte_stuffer_recv_byte(1, d);
+ }
+}
+
+TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_small_packet_with_zeros) {
+ uint8_t original_data[] = { 1, 0, 3, 0, 0, 9};
+ byte_stuffer_send_frame(1, original_data, sizeof(original_data));
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(original_data)));
+ int i;
+ for(auto& d : sent_data) {
+ byte_stuffer_recv_byte(1, d);
+ }
+}
+
+TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes) {
+ uint8_t original_data[254];
+ int i;
+ for(i=0;i<254;i++) {
+ original_data[i] = i + 1;
+ }
+ byte_stuffer_send_frame(0, original_data, sizeof(original_data));
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(original_data)));
+ for(auto& d : sent_data) {
+ byte_stuffer_recv_byte(1, d);
+ }
+}
+
+TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_256_bytes) {
+ uint8_t original_data[256];
+ int i;
+ for(i=0;i<254;i++) {
+ original_data[i] = i + 1;
+ }
+ original_data[254] = 22;
+ original_data[255] = 23;
+ byte_stuffer_send_frame(0, original_data, sizeof(original_data));
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(original_data)));
+ for(auto& d : sent_data) {
+ byte_stuffer_recv_byte(1, d);
+ }
+}
+
+TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes_and_then_zero) {
+ uint8_t original_data[255];
+ int i;
+ for(i=0;i<254;i++) {
+ original_data[i] = i + 1;
+ }
+ original_data[254] = 0;
+ byte_stuffer_send_frame(0, original_data, sizeof(original_data));
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(original_data)));
+ for(auto& d : sent_data) {
+ byte_stuffer_recv_byte(1, d);
+ }
+}
diff --git a/quantum/serial_link/tests/frame_router_tests.cpp b/quantum/serial_link/tests/frame_router_tests.cpp
new file mode 100644
index 0000000000..2bd5bf830d
--- /dev/null
+++ b/quantum/serial_link/tests/frame_router_tests.cpp
@@ -0,0 +1,229 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include <array>
+extern "C" {
+ #include "serial_link/protocol/transport.h"
+ #include "serial_link/protocol/byte_stuffer.h"
+ #include "serial_link/protocol/frame_router.h"
+}
+
+using testing::_;
+using testing::ElementsAreArray;
+using testing::Args;
+
+class FrameRouter : public testing::Test {
+public:
+ FrameRouter() :
+ current_router_buffer(nullptr)
+ {
+ Instance = this;
+ init_byte_stuffer();
+ }
+
+ ~FrameRouter() {
+ Instance = nullptr;
+ }
+
+ void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
+ auto& buffer = current_router_buffer->send_buffers[link];
+ std::copy(data, data + size, std::back_inserter(buffer));
+ }
+
+ void receive_data(uint8_t link, uint8_t* data, uint16_t size) {
+ int i;
+ for(i=0;i<size;i++) {
+ byte_stuffer_recv_byte(link, data[i]);
+ }
+ }
+
+ void activate_router(uint8_t num) {
+ current_router_buffer = router_buffers + num;
+ router_set_master(num==0);
+ }
+
+ void simulate_transport(uint8_t from, uint8_t to) {
+ activate_router(to);
+ if (from > to) {
+ receive_data(DOWN_LINK,
+ router_buffers[from].send_buffers[UP_LINK].data(),
+ router_buffers[from].send_buffers[UP_LINK].size());
+ }
+ else if(to > from) {
+ receive_data(UP_LINK,
+ router_buffers[from].send_buffers[DOWN_LINK].data(),
+ router_buffers[from].send_buffers[DOWN_LINK].size());
+ }
+ }
+
+ MOCK_METHOD3(transport_recv_frame, void (uint8_t from, uint8_t* data, uint16_t size));
+
+ std::vector<uint8_t> received_data;
+
+ struct router_buffer {
+ std::vector<uint8_t> send_buffers[2];
+ };
+
+ router_buffer router_buffers[8];
+ router_buffer* current_router_buffer;
+
+ static FrameRouter* Instance;
+};
+
+FrameRouter* FrameRouter::Instance = nullptr;
+
+
+typedef struct {
+ std::array<uint8_t, 4> data;
+ uint8_t extra[16];
+} frame_buffer_t;
+
+
+extern "C" {
+ void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
+ FrameRouter::Instance->send_data(link, data, size);
+ }
+
+
+ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) {
+ FrameRouter::Instance->transport_recv_frame(from, data, size);
+ }
+}
+
+TEST_F(FrameRouter, master_broadcast_is_received_by_everyone) {
+ frame_buffer_t data;
+ data.data = {0xAB, 0x70, 0x55, 0xBB};
+ activate_router(0);
+ router_send_frame(0xFF, (uint8_t*)&data, 4);
+ EXPECT_GT(router_buffers[0].send_buffers[DOWN_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0);
+ EXPECT_CALL(*this, transport_recv_frame(0, _, _))
+ .With(Args<1, 2>(ElementsAreArray(data.data)));
+ simulate_transport(0, 1);
+ EXPECT_GT(router_buffers[1].send_buffers[DOWN_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[1].send_buffers[UP_LINK].size(), 0);
+
+ EXPECT_CALL(*this, transport_recv_frame(0, _, _))
+ .With(Args<1, 2>(ElementsAreArray(data.data)));
+ simulate_transport(1, 2);
+ EXPECT_GT(router_buffers[2].send_buffers[DOWN_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[2].send_buffers[UP_LINK].size(), 0);
+}
+
+TEST_F(FrameRouter, master_send_is_received_by_targets) {
+ frame_buffer_t data;
+ data.data = {0xAB, 0x70, 0x55, 0xBB};
+ activate_router(0);
+ router_send_frame((1 << 1) | (1 << 2), (uint8_t*)&data, 4);
+ EXPECT_GT(router_buffers[0].send_buffers[DOWN_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0);
+
+ simulate_transport(0, 1);
+ EXPECT_GT(router_buffers[1].send_buffers[DOWN_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[1].send_buffers[UP_LINK].size(), 0);
+
+ EXPECT_CALL(*this, transport_recv_frame(0, _, _))
+ .With(Args<1, 2>(ElementsAreArray(data.data)));
+ simulate_transport(1, 2);
+ EXPECT_GT(router_buffers[2].send_buffers[DOWN_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[2].send_buffers[UP_LINK].size(), 0);
+
+ EXPECT_CALL(*this, transport_recv_frame(0, _, _))
+ .With(Args<1, 2>(ElementsAreArray(data.data)));
+ simulate_transport(2, 3);
+ EXPECT_GT(router_buffers[3].send_buffers[DOWN_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[3].send_buffers[UP_LINK].size(), 0);
+}
+
+TEST_F(FrameRouter, first_link_sends_to_master) {
+ frame_buffer_t data;
+ data.data = {0xAB, 0x70, 0x55, 0xBB};
+ activate_router(1);
+ router_send_frame(0, (uint8_t*)&data, 4);
+ EXPECT_GT(router_buffers[1].send_buffers[UP_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[1].send_buffers[DOWN_LINK].size(), 0);
+
+ EXPECT_CALL(*this, transport_recv_frame(1, _, _))
+ .With(Args<1, 2>(ElementsAreArray(data.data)));
+ simulate_transport(1, 0);
+ EXPECT_EQ(router_buffers[0].send_buffers[DOWN_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0);
+}
+
+TEST_F(FrameRouter, second_link_sends_to_master) {
+ frame_buffer_t data;
+ data.data = {0xAB, 0x70, 0x55, 0xBB};
+ activate_router(2);
+ router_send_frame(0, (uint8_t*)&data, 4);
+ EXPECT_GT(router_buffers[2].send_buffers[UP_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[2].send_buffers[DOWN_LINK].size(), 0);
+
+ simulate_transport(2, 1);
+ EXPECT_GT(router_buffers[1].send_buffers[UP_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[1].send_buffers[DOWN_LINK].size(), 0);
+
+ EXPECT_CALL(*this, transport_recv_frame(2, _, _))
+ .With(Args<1, 2>(ElementsAreArray(data.data)));
+ simulate_transport(1, 0);
+ EXPECT_EQ(router_buffers[0].send_buffers[DOWN_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0);
+}
+
+TEST_F(FrameRouter, master_sends_to_master_does_nothing) {
+ frame_buffer_t data;
+ data.data = {0xAB, 0x70, 0x55, 0xBB};
+ activate_router(0);
+ router_send_frame(0, (uint8_t*)&data, 4);
+ EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[0].send_buffers[DOWN_LINK].size(), 0);
+}
+
+TEST_F(FrameRouter, link_sends_to_other_link_does_nothing) {
+ frame_buffer_t data;
+ data.data = {0xAB, 0x70, 0x55, 0xBB};
+ activate_router(1);
+ router_send_frame(2, (uint8_t*)&data, 4);
+ EXPECT_EQ(router_buffers[1].send_buffers[UP_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[1].send_buffers[DOWN_LINK].size(), 0);
+}
+
+TEST_F(FrameRouter, master_receives_on_uplink_does_nothing) {
+ frame_buffer_t data;
+ data.data = {0xAB, 0x70, 0x55, 0xBB};
+ activate_router(1);
+ router_send_frame(0, (uint8_t*)&data, 4);
+ EXPECT_GT(router_buffers[1].send_buffers[UP_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[1].send_buffers[DOWN_LINK].size(), 0);
+
+ EXPECT_CALL(*this, transport_recv_frame(_, _, _))
+ .Times(0);
+ activate_router(0);
+ receive_data(UP_LINK,
+ router_buffers[1].send_buffers[UP_LINK].data(),
+ router_buffers[1].send_buffers[UP_LINK].size());
+ EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0);
+ EXPECT_EQ(router_buffers[0].send_buffers[DOWN_LINK].size(), 0);
+}
diff --git a/quantum/serial_link/tests/frame_validator_tests.cpp b/quantum/serial_link/tests/frame_validator_tests.cpp
new file mode 100644
index 0000000000..9223af83b0
--- /dev/null
+++ b/quantum/serial_link/tests/frame_validator_tests.cpp
@@ -0,0 +1,115 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+extern "C" {
+#include "serial_link/protocol/frame_validator.h"
+}
+
+using testing::_;
+using testing::ElementsAreArray;
+using testing::Args;
+
+class FrameValidator : public testing::Test {
+public:
+ FrameValidator() {
+ Instance = this;
+ }
+
+ ~FrameValidator() {
+ Instance = nullptr;
+ }
+
+ MOCK_METHOD3(route_incoming_frame, void (uint8_t link, uint8_t* data, uint16_t size));
+ MOCK_METHOD3(byte_stuffer_send_frame, void (uint8_t link, uint8_t* data, uint16_t size));
+
+ static FrameValidator* Instance;
+};
+
+FrameValidator* FrameValidator::Instance = nullptr;
+
+extern "C" {
+void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size) {
+ FrameValidator::Instance->route_incoming_frame(link, data, size);
+}
+
+void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) {
+ FrameValidator::Instance->byte_stuffer_send_frame(link, data, size);
+}
+}
+
+TEST_F(FrameValidator, doesnt_validate_frames_under_5_bytes) {
+ EXPECT_CALL(*this, route_incoming_frame(_, _, _))
+ .Times(0);
+ uint8_t data[] = {1, 2};
+ validator_recv_frame(0, 0, 1);
+ validator_recv_frame(0, data, 2);
+ validator_recv_frame(0, data, 3);
+ validator_recv_frame(0, data, 4);
+}
+
+TEST_F(FrameValidator, validates_one_byte_frame_with_correct_crc) {
+ uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3};
+ EXPECT_CALL(*this, route_incoming_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(data, 1)));
+ validator_recv_frame(0, data, 5);
+}
+
+TEST_F(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) {
+ uint8_t data[] = {0x44, 0, 0, 0, 0};
+ EXPECT_CALL(*this, route_incoming_frame(_, _, _))
+ .Times(0);
+ validator_recv_frame(1, data, 5);
+}
+
+TEST_F(FrameValidator, validates_four_byte_frame_with_correct_crc) {
+ uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA};
+ EXPECT_CALL(*this, route_incoming_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(data, 4)));
+ validator_recv_frame(1, data, 8);
+}
+
+TEST_F(FrameValidator, validates_five_byte_frame_with_correct_crc) {
+ uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47};
+ EXPECT_CALL(*this, route_incoming_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(data, 5)));
+ validator_recv_frame(0, data, 9);
+}
+
+TEST_F(FrameValidator, sends_one_byte_with_correct_crc) {
+ uint8_t original[] = {0x44, 0, 0, 0, 0};
+ uint8_t expected[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3};
+ EXPECT_CALL(*this, byte_stuffer_send_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ validator_send_frame(0, original, 1);
+}
+
+TEST_F(FrameValidator, sends_five_bytes_with_correct_crc) {
+ uint8_t original[] = {1, 2, 3, 4, 5, 0, 0, 0, 0};
+ uint8_t expected[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47};
+ EXPECT_CALL(*this, byte_stuffer_send_frame(_, _, _))
+ .With(Args<1, 2>(ElementsAreArray(expected)));
+ validator_send_frame(0, original, 5);
+}
diff --git a/quantum/serial_link/tests/rules.mk b/quantum/serial_link/tests/rules.mk
new file mode 100644
index 0000000000..b81515bc55
--- /dev/null
+++ b/quantum/serial_link/tests/rules.mk
@@ -0,0 +1,22 @@
+serial_link_byte_stuffer_SRC :=\
+ $(SERIAL_PATH)/tests/byte_stuffer_tests.cpp \
+ $(SERIAL_PATH)/protocol/byte_stuffer.c
+
+serial_link_frame_validator_SRC := \
+ $(SERIAL_PATH)/tests/frame_validator_tests.cpp \
+ $(SERIAL_PATH)/protocol/frame_validator.c
+
+serial_link_frame_router_SRC := \
+ $(SERIAL_PATH)/tests/frame_router_tests.cpp \
+ $(SERIAL_PATH)/protocol/byte_stuffer.c \
+ $(SERIAL_PATH)/protocol/frame_validator.c \
+ $(SERIAL_PATH)/protocol/frame_router.c
+
+serial_link_triple_buffered_object_SRC := \
+ $(SERIAL_PATH)/tests/triple_buffered_object_tests.cpp \
+ $(SERIAL_PATH)/protocol/triple_buffered_object.c
+
+serial_link_transport_SRC := \
+ $(SERIAL_PATH)/tests/transport_tests.cpp \
+ $(SERIAL_PATH)/protocol/transport.c \
+ $(SERIAL_PATH)/protocol/triple_buffered_object.c
diff --git a/quantum/serial_link/tests/testlist.mk b/quantum/serial_link/tests/testlist.mk
new file mode 100644
index 0000000000..a80e888849
--- /dev/null
+++ b/quantum/serial_link/tests/testlist.mk
@@ -0,0 +1,6 @@
+TEST_LIST +=\
+ serial_link_byte_stuffer\
+ serial_link_frame_validator\
+ serial_link_frame_router\
+ serial_link_triple_buffered_object\
+ serial_link_transport \ No newline at end of file
diff --git a/quantum/serial_link/tests/transport_tests.cpp b/quantum/serial_link/tests/transport_tests.cpp
new file mode 100644
index 0000000000..21b7b165f6
--- /dev/null
+++ b/quantum/serial_link/tests/transport_tests.cpp
@@ -0,0 +1,188 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+
+using testing::_;
+using testing::ElementsAreArray;
+using testing::Args;
+
+extern "C" {
+#include "serial_link/protocol/transport.h"
+}
+
+struct test_object1 {
+ uint32_t test;
+};
+
+struct test_object2 {
+ uint32_t test1;
+ uint32_t test2;
+};
+
+MASTER_TO_ALL_SLAVES_OBJECT(master_to_slave, test_object1);
+MASTER_TO_SINGLE_SLAVE_OBJECT(master_to_single_slave, test_object1);
+SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1);
+
+static remote_object_t* test_remote_objects[] = {
+ REMOTE_OBJECT(master_to_slave),
+ REMOTE_OBJECT(master_to_single_slave),
+ REMOTE_OBJECT(slave_to_master),
+};
+
+class Transport : public testing::Test {
+public:
+ Transport() {
+ Instance = this;
+ add_remote_objects(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*));
+ }
+
+ ~Transport() {
+ Instance = nullptr;
+ reinitialize_serial_link_transport();
+ }
+
+ MOCK_METHOD0(signal_data_written, void ());
+ MOCK_METHOD1(router_send_frame, void (uint8_t destination));
+
+ void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) {
+ router_send_frame(destination);
+ std::copy(data, data + size, std::back_inserter(sent_data));
+ }
+
+ static Transport* Instance;
+
+ std::vector<uint8_t> sent_data;
+};
+
+Transport* Transport::Instance = nullptr;
+
+extern "C" {
+void signal_data_written(void) {
+ Transport::Instance->signal_data_written();
+}
+
+void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) {
+ Transport::Instance->router_send_frame(destination, data, size);
+}
+}
+
+TEST_F(Transport, write_to_local_signals_an_event) {
+ begin_write_master_to_slave();
+ EXPECT_CALL(*this, signal_data_written());
+ end_write_master_to_slave();
+ begin_write_slave_to_master();
+ EXPECT_CALL(*this, signal_data_written());
+ end_write_slave_to_master();
+ begin_write_master_to_single_slave(1);
+ EXPECT_CALL(*this, signal_data_written());
+ end_write_master_to_single_slave(1);
+}
+
+TEST_F(Transport, writes_from_master_to_all_slaves) {
+ update_transport();
+ test_object1* obj = begin_write_master_to_slave();
+ obj->test = 5;
+ EXPECT_CALL(*this, signal_data_written());
+ end_write_master_to_slave();
+ EXPECT_CALL(*this, router_send_frame(0xFF));
+ update_transport();
+ transport_recv_frame(0, sent_data.data(), sent_data.size());
+ test_object1* obj2 = read_master_to_slave();
+ EXPECT_NE(obj2, nullptr);
+ EXPECT_EQ(obj2->test, 5);
+}
+
+TEST_F(Transport, writes_from_slave_to_master) {
+ update_transport();
+ test_object1* obj = begin_write_slave_to_master();
+ obj->test = 7;
+ EXPECT_CALL(*this, signal_data_written());
+ end_write_slave_to_master();
+ EXPECT_CALL(*this, router_send_frame(0));
+ update_transport();
+ transport_recv_frame(3, sent_data.data(), sent_data.size());
+ test_object1* obj2 = read_slave_to_master(2);
+ EXPECT_EQ(read_slave_to_master(0), nullptr);
+ EXPECT_NE(obj2, nullptr);
+ EXPECT_EQ(obj2->test, 7);
+}
+
+TEST_F(Transport, writes_from_master_to_single_slave) {
+ update_transport();
+ test_object1* obj = begin_write_master_to_single_slave(3);
+ obj->test = 7;
+ EXPECT_CALL(*this, signal_data_written());
+ end_write_master_to_single_slave(3);
+ EXPECT_CALL(*this, router_send_frame(4));
+ update_transport();
+ transport_recv_frame(0, sent_data.data(), sent_data.size());
+ test_object1* obj2 = read_master_to_single_slave();
+ EXPECT_NE(obj2, nullptr);
+ EXPECT_EQ(obj2->test, 7);
+}
+
+TEST_F(Transport, ignores_object_with_invalid_id) {
+ update_transport();
+ test_object1* obj = begin_write_master_to_single_slave(3);
+ obj->test = 7;
+ EXPECT_CALL(*this, signal_data_written());
+ end_write_master_to_single_slave(3);
+ EXPECT_CALL(*this, router_send_frame(4));
+ update_transport();
+ sent_data[sent_data.size() - 1] = 44;
+ transport_recv_frame(0, sent_data.data(), sent_data.size());
+ test_object1* obj2 = read_master_to_single_slave();
+ EXPECT_EQ(obj2, nullptr);
+}
+
+TEST_F(Transport, ignores_object_with_size_too_small) {
+ update_transport();
+ test_object1* obj = begin_write_master_to_slave();
+ obj->test = 7;
+ EXPECT_CALL(*this, signal_data_written());
+ end_write_master_to_slave();
+ EXPECT_CALL(*this, router_send_frame(_));
+ update_transport();
+ sent_data[sent_data.size() - 2] = 0;
+ transport_recv_frame(0, sent_data.data(), sent_data.size() - 1);
+ test_object1* obj2 = read_master_to_slave();
+ EXPECT_EQ(obj2, nullptr);
+}
+
+TEST_F(Transport, ignores_object_with_size_too_big) {
+ update_transport();
+ test_object1* obj = begin_write_master_to_slave();
+ obj->test = 7;
+ EXPECT_CALL(*this, signal_data_written());
+ end_write_master_to_slave();
+ EXPECT_CALL(*this, router_send_frame(_));
+ update_transport();
+ sent_data.resize(sent_data.size() + 22);
+ sent_data[sent_data.size() - 1] = 0;
+ transport_recv_frame(0, sent_data.data(), sent_data.size());
+ test_object1* obj2 = read_master_to_slave();
+ EXPECT_EQ(obj2, nullptr);
+}
diff --git a/quantum/serial_link/tests/triple_buffered_object_tests.cpp b/quantum/serial_link/tests/triple_buffered_object_tests.cpp
new file mode 100644
index 0000000000..7724bbee9c
--- /dev/null
+++ b/quantum/serial_link/tests/triple_buffered_object_tests.cpp
@@ -0,0 +1,84 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "gtest/gtest.h"
+extern "C" {
+#include "serial_link/protocol/triple_buffered_object.h"
+}
+
+struct test_object{
+ uint8_t state;
+ uint32_t buffer[3];
+};
+
+test_object test_object;
+
+class TripleBufferedObject : public testing::Test {
+public:
+ TripleBufferedObject() {
+ triple_buffer_init((triple_buffer_object_t*)&test_object);
+ }
+};
+
+TEST_F(TripleBufferedObject, writes_and_reads_object) {
+ *triple_buffer_begin_write(&test_object) = 0x3456ABCC;
+ triple_buffer_end_write(&test_object);
+ EXPECT_EQ(*triple_buffer_read(&test_object), 0x3456ABCC);
+}
+
+TEST_F(TripleBufferedObject, does_not_read_empty) {
+ EXPECT_EQ(triple_buffer_read(&test_object), nullptr);
+}
+
+TEST_F(TripleBufferedObject, writes_twice_and_reads_object) {
+ *triple_buffer_begin_write(&test_object) = 0x3456ABCC;
+ triple_buffer_end_write(&test_object);
+ *triple_buffer_begin_write(&test_object) = 0x44778899;
+ triple_buffer_end_write(&test_object);
+ EXPECT_EQ(*triple_buffer_read(&test_object), 0x44778899);
+}
+
+TEST_F(TripleBufferedObject, performs_another_write_in_the_middle_of_read) {
+ *triple_buffer_begin_write(&test_object) = 1;
+ triple_buffer_end_write(&test_object);
+ uint32_t* read = triple_buffer_read(&test_object);
+ *triple_buffer_begin_write(&test_object) = 2;
+ triple_buffer_end_write(&test_object);
+ EXPECT_EQ(*read, 1);
+ EXPECT_EQ(*triple_buffer_read(&test_object), 2);
+ EXPECT_EQ(triple_buffer_read(&test_object), nullptr);
+}
+
+TEST_F(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) {
+ *triple_buffer_begin_write(&test_object) = 1;
+ triple_buffer_end_write(&test_object);
+ uint32_t* read = triple_buffer_read(&test_object);
+ *triple_buffer_begin_write(&test_object) = 2;
+ triple_buffer_end_write(&test_object);
+ *triple_buffer_begin_write(&test_object) = 3;
+ triple_buffer_end_write(&test_object);
+ EXPECT_EQ(*read, 1);
+ EXPECT_EQ(*triple_buffer_read(&test_object), 3);
+ EXPECT_EQ(triple_buffer_read(&test_object), nullptr);
+}
diff --git a/quantum/template/Makefile b/quantum/template/Makefile
index 4fa195468d..4e2a6f00fd 100644
--- a/quantum/template/Makefile
+++ b/quantum/template/Makefile
@@ -1,140 +1,3 @@
-#----------------------------------------------------------------------------
-# On command line:
-#
-# make all = Make software.
-#
-# make clean = Clean out built project files.
-#
-# make coff = Convert ELF to AVR COFF.
-#
-# make extcoff = Convert ELF to AVR Extended COFF.
-#
-# make program = Download the hex file to the device.
-# Please customize your programmer settings(PROGRAM_CMD)
-#
-# make teensy = Download the hex file to the device, using teensy_loader_cli.
-# (must have teensy_loader_cli installed).
-#
-# make dfu = Download the hex file to the device, using dfu-programmer (must
-# have dfu-programmer installed).
-#
-# make flip = Download the hex file to the device, using Atmel FLIP (must
-# have Atmel FLIP installed).
-#
-# make dfu-ee = Download the eeprom file to the device, using dfu-programmer
-# (must have dfu-programmer installed).
-#
-# make flip-ee = Download the eeprom file to the device, using Atmel FLIP
-# (must have Atmel FLIP installed).
-#
-# make debug = Start either simulavr or avarice as specified for debugging,
-# with avr-gdb or avr-insight as the front end for debugging.
-#
-# make filename.s = Just compile filename.c into the assembler code only.
-#
-# make filename.i = Create a preprocessed source file for use in submitting
-# bug reports to the GCC project.
-#
-# To rebuild project do "make clean" then "make all".
-#----------------------------------------------------------------------------
-
-# Target file name (without extension).
-TARGET = %KEYBOARD%
-
-
-# Directory common source filess exist
-TOP_DIR = ../..
-TMK_DIR = ../../tmk_core
-
-# Directory keyboard dependent files exist
-TARGET_DIR = .
-
-# # project specific files
-SRC = %KEYBOARD%.c
-
-ifdef KEYMAP
- SRC := keymaps/$(KEYMAP).c $(SRC)
-else
- SRC := keymaps/default.c $(SRC)
-endif
-
-CONFIG_H = config.h
-
-# MCU name
-#MCU = at90usb1287
-MCU = atmega32u4
-
-# Processor frequency.
-# This will define a symbol, F_CPU, in all source code files equal to the
-# processor frequency in Hz. You can then use this symbol in your source code to
-# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
-# automatically to create a 32-bit value in your source code.
-#
-# This will be an integer division of F_USB below, as it is sourced by
-# F_USB after it has run through any CPU prescalers. Note that this value
-# does not *change* the processor frequency - it should merely be updated to
-# reflect the processor speed set externally so that the code can use accurate
-# software delays.
-F_CPU = 16000000
-
-
-#
-# LUFA specific
-#
-# Target architecture (see library "Board Types" documentation).
-ARCH = AVR8
-
-# Input clock frequency.
-# This will define a symbol, F_USB, in all source code files equal to the
-# input clock frequency (before any prescaling is performed) in Hz. This value may
-# differ from F_CPU if prescaling is used on the latter, and is required as the
-# raw input clock is fed directly to the PLL sections of the AVR for high speed
-# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
-# at the end, this will be done automatically to create a 32-bit value in your
-# source code.
-#
-# If no clock division is performed on the input clock inside the AVR (via the
-# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
-F_USB = $(F_CPU)
-
-# Interrupt driven control endpoint task(+60)
-OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
-
-
-# Boot Section Size in *bytes*
-# Teensy halfKay 512
-# Teensy++ halfKay 1024
-# Atmel DFU loader 4096
-# LUFA bootloader 4096
-# USBaspLoader 2048
-OPT_DEFS += -DBOOTLOADER_SIZE=512
-
-
-# Build Options
-# comment out to disable the options.
-#
-BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000)
-MOUSEKEY_ENABLE = yes # Mouse keys(+4700)
-EXTRAKEY_ENABLE = yes # Audio control and System control(+450)
-CONSOLE_ENABLE = yes # Console for debug(+400)
-COMMAND_ENABLE = yes # Commands for debug and configuration
-KEYBOARD_LOCK_ENABLE = yes # Allow locking of keyboard via magic key
-# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
-# SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend
-#NKRO_ENABLE = yes # USB Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
-# BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality
-# MIDI_ENABLE = YES # MIDI controls
-# UNICODE_ENABLE = YES # Unicode
-# BLUETOOTH_ENABLE = yes # Enable Bluetooth with the Adafruit EZ-Key HID
-
-
-# Optimize size but this may cause error "relocation truncated to fit"
-#EXTRALDFLAGS = -Wl,--relax
-
-# Search Path
-VPATH += $(TARGET_DIR)
-VPATH += $(TOP_DIR)
-VPATH += $(TMK_DIR)
-
-include $(TOP_DIR)/quantum/quantum.mk
-
+ifndef MAKEFILE_INCLUDED
+ include ../../Makefile
+endif \ No newline at end of file
diff --git a/quantum/template/config.h b/quantum/template/config.h
index e6fb7866c6..b02f0c7ebc 100644
--- a/quantum/template/config.h
+++ b/quantum/template/config.h
@@ -41,38 +41,43 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
* DIODE_DIRECTION: COL2ROW = COL = Anode (+), ROW = Cathode (-, marked on diode)
* ROW2COL = ROW = Anode (+), COL = Cathode (-, marked on diode)
*
-*/
-#define COLS (int []){ F1, F0, B0 }
-#define ROWS (int []){ D0, D5 }
+*/
+#define MATRIX_ROW_PINS { D0, D5 }
+#define MATRIX_COL_PINS { F1, F0, B0 }
+#define UNUSED_PINS
/* COL2ROW or ROW2COL */
#define DIODE_DIRECTION COL2ROW
+
+// #define BACKLIGHT_PIN B7
+// #define BACKLIGHT_BREATHING
+// #define BACKLIGHT_LEVELS 3
+
/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
-#define DEBOUNCE 5
+#define DEBOUNCING_DELAY 5
/* define if matrix has ghost (lacks anti-ghosting diodes) */
//#define MATRIX_HAS_GHOST
/* number of backlight levels */
-#define BACKLIGHT_LEVELS 3
/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE
/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE
-/*
+/*
* Force NKRO
*
- * Force NKRO (nKey Rollover) to be enabled by default, regardless of the saved
+ * Force NKRO (nKey Rollover) to be enabled by default, regardless of the saved
* state in the bootmagic EEPROM settings. (Note that NKRO must be enabled in the
* makefile for this to work.)
*
* If forced on, NKRO can be disabled via magic key (default = LShift+RShift+N)
* until the next keyboard reset.
*
- * NKRO may prevent your keystrokes from being detected in the BIOS, but it is
+ * NKRO may prevent your keystrokes from being detected in the BIOS, but it is
* fully operational during normal computer usage.
*
* For a less heavy-handed approach, enable NKRO via magic key (LShift+RShift+N)
@@ -90,7 +95,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
* the keyboard. They are best used in combination with the HID Listen program,
* found here: https://www.pjrc.com/teensy/hid_listen.html
*
- * The options below allow the magic key functionality to be changed. This is
+ * The options below allow the magic key functionality to be changed. This is
* useful if your keyboard/keypad is missing keys and you want magic key support.
*
*/
diff --git a/quantum/template/keymaps/default/Makefile b/quantum/template/keymaps/default/Makefile
new file mode 100644
index 0000000000..f4671a9d11
--- /dev/null
+++ b/quantum/template/keymaps/default/Makefile
@@ -0,0 +1,21 @@
+# Build Options
+# change to "no" to disable the options, or define them in the Makefile in
+# the appropriate keymap folder that will get included automatically
+#
+BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration(+1000)
+MOUSEKEY_ENABLE = yes # Mouse keys(+4700)
+EXTRAKEY_ENABLE = yes # Audio control and System control(+450)
+CONSOLE_ENABLE = no # Console for debug(+400)
+COMMAND_ENABLE = yes # Commands for debug and configuration
+NKRO_ENABLE = yes # Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
+BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality
+MIDI_ENABLE = no # MIDI controls
+AUDIO_ENABLE = no # Audio output on port C6
+UNICODE_ENABLE = no # Unicode
+BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
+RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight. Do not enable this with audio at the same time.
+SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
+
+ifndef QUANTUM_DIR
+ include ../../../../Makefile
+endif \ No newline at end of file
diff --git a/quantum/template/keymaps/default/config.h b/quantum/template/keymaps/default/config.h
new file mode 100644
index 0000000000..df06a26206
--- /dev/null
+++ b/quantum/template/keymaps/default/config.h
@@ -0,0 +1,8 @@
+#ifndef CONFIG_USER_H
+#define CONFIG_USER_H
+
+#include "../../config.h"
+
+// place overrides here
+
+#endif \ No newline at end of file
diff --git a/quantum/template/keymaps/default.c b/quantum/template/keymaps/default/keymap.c
index 4121fd860c..e28a4723e9 100644
--- a/quantum/template/keymaps/default.c
+++ b/quantum/template/keymaps/default/keymap.c
@@ -1,6 +1,3 @@
-// This is the canonical layout file for the Quantum project. If you want to add another keyboard,
-// this is the style you want to emulate.
-
#include "%KEYBOARD%.h"
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
@@ -28,3 +25,20 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
}
return MACRO_NONE;
};
+
+
+void matrix_init_user(void) {
+
+}
+
+void matrix_scan_user(void) {
+
+}
+
+bool process_record_user(uint16_t keycode, keyrecord_t *record) {
+ return true;
+}
+
+void led_set_user(uint8_t usb_led) {
+
+} \ No newline at end of file
diff --git a/quantum/template/keymaps/default/readme.md b/quantum/template/keymaps/default/readme.md
new file mode 100644
index 0000000000..21aa663d55
--- /dev/null
+++ b/quantum/template/keymaps/default/readme.md
@@ -0,0 +1 @@
+# The default keymap for %KEYBOARD% \ No newline at end of file
diff --git a/quantum/template/README.md b/quantum/template/readme.md
index 39f9f59d10..b16f4cd768 100644
--- a/quantum/template/README.md
+++ b/quantum/template/readme.md
@@ -3,22 +3,26 @@
## Quantum MK Firmware
-For the full Quantum feature list, see [the parent README.md](/README.md).
+For the full Quantum feature list, see [the parent readme](/).
## Building
-Download or clone the whole firmware and navigate to the keyboard/%KEYBOARD% folder. Once your dev env is setup, you'll be able to type `make` to generate your .hex - you can then use the Teensy Loader to program your .hex file.
+Download or clone the whole firmware and navigate to the keyboards/%KEYBOARD% folder. Once your dev env is setup, you'll be able to type `make` to generate your .hex - you can then use the Teensy Loader to program your .hex file.
Depending on which keymap you would like to use, you will have to compile slightly differently.
### Default
-To build with the default keymap, simply run `make`.
+
+To build with the default keymap, simply run `make default`.
### Other Keymaps
-Several version of keymap are available in advance but you are recommended to define your favorite layout yourself. To define your own keymap create file named `<name>.c` in the keymaps folder, and see keymap document (you can find in top README.md) and existent keymap files.
-To build the firmware binary hex file with a keymap just do `make` with `KEYMAP` option like:
+Several version of keymap are available in advance but you are recommended to define your favorite layout yourself. To define your own keymap create a folder with the name of your keymap in the keymaps folder, and see keymap documentation (you can find in top readme.md) and existant keymap files.
+
+To build the firmware binary hex file with a keymap just do `make` with a keymap like this:
+
```
-$ make KEYMAP=[default|jack|<name>]
+$ make [default|jack|<name>]
```
-Keymaps follow the format **__\<name\>.c__** and are stored in the `keymaps` folder. \ No newline at end of file
+
+Keymaps follow the format **__\<name\>.c__** and are stored in the `keymaps` folder.
diff --git a/quantum/template/rules.mk b/quantum/template/rules.mk
new file mode 100644
index 0000000000..55898147dd
--- /dev/null
+++ b/quantum/template/rules.mk
@@ -0,0 +1,67 @@
+# MCU name
+#MCU = at90usb1287
+MCU = atmega32u4
+
+# Processor frequency.
+# This will define a symbol, F_CPU, in all source code files equal to the
+# processor frequency in Hz. You can then use this symbol in your source code to
+# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
+# automatically to create a 32-bit value in your source code.
+#
+# This will be an integer division of F_USB below, as it is sourced by
+# F_USB after it has run through any CPU prescalers. Note that this value
+# does not *change* the processor frequency - it should merely be updated to
+# reflect the processor speed set externally so that the code can use accurate
+# software delays.
+F_CPU = 16000000
+
+
+#
+# LUFA specific
+#
+# Target architecture (see library "Board Types" documentation).
+ARCH = AVR8
+
+# Input clock frequency.
+# This will define a symbol, F_USB, in all source code files equal to the
+# input clock frequency (before any prescaling is performed) in Hz. This value may
+# differ from F_CPU if prescaling is used on the latter, and is required as the
+# raw input clock is fed directly to the PLL sections of the AVR for high speed
+# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
+# at the end, this will be done automatically to create a 32-bit value in your
+# source code.
+#
+# If no clock division is performed on the input clock inside the AVR (via the
+# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
+F_USB = $(F_CPU)
+
+# Interrupt driven control endpoint task(+60)
+OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
+
+
+# Boot Section Size in *bytes*
+# Teensy halfKay 512
+# Teensy++ halfKay 1024
+# Atmel DFU loader 4096
+# LUFA bootloader 4096
+# USBaspLoader 2048
+OPT_DEFS += -DBOOTLOADER_SIZE=512
+
+
+# Build Options
+# change yes to no to disable
+#
+BOOTMAGIC_ENABLE ?= no # Virtual DIP switch configuration(+1000)
+MOUSEKEY_ENABLE ?= yes # Mouse keys(+4700)
+EXTRAKEY_ENABLE ?= yes # Audio control and System control(+450)
+CONSOLE_ENABLE ?= yes # Console for debug(+400)
+COMMAND_ENABLE ?= yes # Commands for debug and configuration
+# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
+SLEEP_LED_ENABLE ?= no # Breathing sleep LED during USB suspend
+# if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
+NKRO_ENABLE ?= no # USB Nkey Rollover
+BACKLIGHT_ENABLE ?= no # Enable keyboard backlight functionality on B7 by default
+MIDI_ENABLE ?= no # MIDI controls
+UNICODE_ENABLE ?= no # Unicode
+BLUETOOTH_ENABLE ?= no # Enable Bluetooth with the Adafruit EZ-Key HID
+AUDIO_ENABLE ?= no # Audio output on port C6
diff --git a/quantum/template/template.c b/quantum/template/template.c
index cc52e496ff..5ef349583c 100644
--- a/quantum/template/template.c
+++ b/quantum/template/template.c
@@ -1,25 +1,5 @@
#include "%KEYBOARD%.h"
-__attribute__ ((weak))
-void matrix_init_user(void) {
- // leave this function blank - it can be defined in a keymap file
-};
-
-__attribute__ ((weak))
-void matrix_scan_user(void) {
- // leave this function blank - it can be defined in a keymap file
-}
-
-__attribute__ ((weak))
-void process_action_user(keyrecord_t *record) {
- // leave this function blank - it can be defined in a keymap file
-}
-
-__attribute__ ((weak))
-void led_set_user(uint8_t usb_led) {
- // leave this function blank - it can be defined in a keymap file
-}
-
void matrix_init_kb(void) {
// put your keyboard start-up code here
// runs once when the firmware starts up
@@ -34,11 +14,11 @@ void matrix_scan_kb(void) {
matrix_scan_user();
}
-void process_action_kb(keyrecord_t *record) {
+bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
// put your per-action keyboard code here
// runs for every action, just before processing by the firmware
- process_action_user(record);
+ return process_record_user(keycode, record);
}
void led_set_kb(uint8_t usb_led) {
diff --git a/quantum/template/template.h b/quantum/template/template.h
index b1c34d3cbe..cd78a54e3e 100644
--- a/quantum/template/template.h
+++ b/quantum/template/template.h
@@ -1,10 +1,7 @@
#ifndef %KEYBOARD_UPPERCASE%_H
#define %KEYBOARD_UPPERCASE%_H
-#include "matrix.h"
-#include "keymap_common.h"
-#include "backlight.h"
-#include <stddef.h>
+#include "quantum.h"
// This a shortcut to help you visually see your layout.
// The following is an example using the Planck MIT layout
@@ -19,9 +16,4 @@
{ k10, KC_NO, k11 }, \
}
-void matrix_init_user(void);
-void matrix_scan_user(void);
-void process_action_user(keyrecord_t *record);
-void led_set_user(uint8_t usb_led);
-
#endif
diff --git a/quantum/tools/eeprom_reset.hex b/quantum/tools/eeprom_reset.hex
new file mode 100644
index 0000000000..a8a75389fe
--- /dev/null
+++ b/quantum/tools/eeprom_reset.hex
@@ -0,0 +1,9 @@
+:10000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00
+:10001000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0
+:10002000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE0
+:10003000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD0
+:10004000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC0
+:10005000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB0
+:10006000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA0
+:10007000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF90
+:00000001FF
diff --git a/quantum/tools/readme.md b/quantum/tools/readme.md
new file mode 100644
index 0000000000..5f355256de
--- /dev/null
+++ b/quantum/tools/readme.md
@@ -0,0 +1,6 @@
+`eeprom_reset.hex` is to reset the eeprom on the Atmega32u4, like this:
+
+ dfu-programmer atmega32u4 erase
+ dfu-programmer atmega32u4 flash --eeprom eeprom_reset.hex
+
+ You'll need to reflash afterwards, because DFU requires the flash to be erased before messing with the eeprom.
diff --git a/quantum/visualizer/LICENSE.md b/quantum/visualizer/LICENSE.md
new file mode 100644
index 0000000000..22d4c3f08b
--- /dev/null
+++ b/quantum/visualizer/LICENSE.md
@@ -0,0 +1,29 @@
+The files in this project are licensed under the MIT license
+It uses the following libraries
+uGFX - with it's own license, see the license.html file in the uGFX subfolder for more information
+tmk_core - is indirectly used and not included in the repository. It's licensed under the GPLv2 license
+Chibios - which is used by tmk_core is licensed under GPLv3.
+
+Therefore the effective license for any project using the library is GPLv3
+
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/quantum/visualizer/example_integration/callbacks.c b/quantum/visualizer/example_integration/callbacks.c
new file mode 100644
index 0000000000..2539615d65
--- /dev/null
+++ b/quantum/visualizer/example_integration/callbacks.c
@@ -0,0 +1,36 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "keyboard.h"
+#include "action_layer.h"
+#include "visualizer.h"
+#include "host.h"
+
+void post_keyboard_init(void) {
+ visualizer_init();
+}
+
+void post_keyboard_task() {
+ visualizer_set_state(default_layer_state, layer_state, host_keyboard_leds());
+}
diff --git a/quantum/visualizer/example_integration/gfxconf.h b/quantum/visualizer/example_integration/gfxconf.h
new file mode 100644
index 0000000000..304c5d187a
--- /dev/null
+++ b/quantum/visualizer/example_integration/gfxconf.h
@@ -0,0 +1,325 @@
+/**
+ * This file has a different license to the rest of the uGFX system.
+ * You can copy, modify and distribute this file as you see fit.
+ * You do not need to publish your source modifications to this file.
+ * The only thing you are not permitted to do is to relicense it
+ * under a different license.
+ */
+
+/**
+ * Copy this file into your project directory and rename it as gfxconf.h
+ * Edit your copy to turn on the uGFX features you want to use.
+ * The values below are the defaults.
+ *
+ * Only remove the comments from lines where you want to change the
+ * default value. This allows definitions to be included from
+ * driver makefiles when required and provides the best future
+ * compatibility for your project.
+ *
+ * Please use spaces instead of tabs in this file.
+ */
+
+#ifndef _GFXCONF_H
+#define _GFXCONF_H
+
+
+///////////////////////////////////////////////////////////////////////////
+// GOS - One of these must be defined, preferably in your Makefile //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_OS_CHIBIOS TRUE
+//#define GFX_USE_OS_FREERTOS FALSE
+// #define GFX_FREERTOS_USE_TRACE FALSE
+//#define GFX_USE_OS_WIN32 FALSE
+//#define GFX_USE_OS_LINUX FALSE
+//#define GFX_USE_OS_OSX FALSE
+//#define GFX_USE_OS_ECOS FALSE
+//#define GFX_USE_OS_RAWRTOS FALSE
+//#define GFX_USE_OS_ARDUINO FALSE
+//#define GFX_USE_OS_KEIL FALSE
+//#define GFX_USE_OS_CMSIS FALSE
+//#define GFX_USE_OS_RAW32 FALSE
+// #define INTERRUPTS_OFF() optional_code
+// #define INTERRUPTS_ON() optional_code
+// These are not defined by default for some reason
+#define GOS_NEED_X_THREADS FALSE
+#define GOS_NEED_X_HEAP FALSE
+
+// Options that (should where relevant) apply to all operating systems
+ #define GFX_NO_INLINE FALSE
+// #define GFX_COMPILER GFX_COMPILER_UNKNOWN
+// #define GFX_CPU GFX_CPU_UNKNOWN
+// #define GFX_OS_HEAP_SIZE 0
+// #define GFX_OS_NO_INIT FALSE
+// #define GFX_OS_INIT_NO_WARNING FALSE
+// #define GFX_OS_PRE_INIT_FUNCTION myHardwareInitRoutine
+// #define GFX_OS_EXTRA_INIT_FUNCTION myOSInitRoutine
+// #define GFX_OS_EXTRA_DEINIT_FUNCTION myOSDeInitRoutine
+
+
+///////////////////////////////////////////////////////////////////////////
+// GDISP //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_GDISP TRUE
+
+//#define GDISP_NEED_AUTOFLUSH FALSE
+//#define GDISP_NEED_TIMERFLUSH FALSE
+//#define GDISP_NEED_VALIDATION TRUE
+//#define GDISP_NEED_CLIP TRUE
+//#define GDISP_NEED_CIRCLE FALSE
+//#define GDISP_NEED_ELLIPSE FALSE
+//#define GDISP_NEED_ARC FALSE
+//#define GDISP_NEED_ARCSECTORS FALSE
+//#define GDISP_NEED_CONVEX_POLYGON FALSE
+//#define GDISP_NEED_SCROLL FALSE
+//#define GDISP_NEED_PIXELREAD FALSE
+//#define GDISP_NEED_CONTROL FALSE
+//#define GDISP_NEED_QUERY FALSE
+//#define GDISP_NEED_MULTITHREAD FALSE
+//#define GDISP_NEED_STREAMING FALSE
+#define GDISP_NEED_TEXT TRUE
+// #define GDISP_NEED_TEXT_WORDWRAP FALSE
+// #define GDISP_NEED_ANTIALIAS FALSE
+// #define GDISP_NEED_UTF8 FALSE
+ #define GDISP_NEED_TEXT_KERNING TRUE
+// #define GDISP_INCLUDE_FONT_UI1 FALSE
+// #define GDISP_INCLUDE_FONT_UI2 FALSE // The smallest preferred font.
+// #define GDISP_INCLUDE_FONT_LARGENUMBERS FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS10 FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS12 FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS16 FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS20 FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS24 FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS32 FALSE
+ #define GDISP_INCLUDE_FONT_DEJAVUSANSBOLD12 TRUE
+// #define GDISP_INCLUDE_FONT_FIXED_10X20 FALSE
+// #define GDISP_INCLUDE_FONT_FIXED_7X14 FALSE
+ #define GDISP_INCLUDE_FONT_FIXED_5X8 TRUE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS12_AA FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS16_AA FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS20_AA FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS24_AA FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANS32_AA FALSE
+// #define GDISP_INCLUDE_FONT_DEJAVUSANSBOLD12_AA FALSE
+// #define GDISP_INCLUDE_USER_FONTS FALSE
+
+//#define GDISP_NEED_IMAGE FALSE
+// #define GDISP_NEED_IMAGE_NATIVE FALSE
+// #define GDISP_NEED_IMAGE_GIF FALSE
+// #define GDISP_NEED_IMAGE_BMP FALSE
+// #define GDISP_NEED_IMAGE_BMP_1 FALSE
+// #define GDISP_NEED_IMAGE_BMP_4 FALSE
+// #define GDISP_NEED_IMAGE_BMP_4_RLE FALSE
+// #define GDISP_NEED_IMAGE_BMP_8 FALSE
+// #define GDISP_NEED_IMAGE_BMP_8_RLE FALSE
+// #define GDISP_NEED_IMAGE_BMP_16 FALSE
+// #define GDISP_NEED_IMAGE_BMP_24 FALSE
+// #define GDISP_NEED_IMAGE_BMP_32 FALSE
+// #define GDISP_NEED_IMAGE_JPG FALSE
+// #define GDISP_NEED_IMAGE_PNG FALSE
+// #define GDISP_NEED_IMAGE_ACCOUNTING FALSE
+
+//#define GDISP_NEED_PIXMAP FALSE
+// #define GDISP_NEED_PIXMAP_IMAGE FALSE
+
+//#define GDISP_DEFAULT_ORIENTATION GDISP_ROTATE_LANDSCAPE // If not defined the native hardware orientation is used.
+//#define GDISP_LINEBUF_SIZE 128
+//#define GDISP_STARTUP_COLOR Black
+#define GDISP_NEED_STARTUP_LOGO FALSE
+
+//#define GDISP_TOTAL_DISPLAYS 1
+
+//#define GDISP_DRIVER_LIST GDISPVMT_Win32, GDISPVMT_Win32
+// #ifdef GDISP_DRIVER_LIST
+// // For code and speed optimization define as TRUE or FALSE if all controllers have the same capability
+// #define GDISP_HARDWARE_STREAM_WRITE FALSE
+// #define GDISP_HARDWARE_STREAM_READ FALSE
+// #define GDISP_HARDWARE_STREAM_POS FALSE
+// #define GDISP_HARDWARE_DRAWPIXEL FALSE
+// #define GDISP_HARDWARE_CLEARS FALSE
+// #define GDISP_HARDWARE_FILLS FALSE
+// #define GDISP_HARDWARE_BITFILLS FALSE
+// #define GDISP_HARDWARE_SCROLL FALSE
+// #define GDISP_HARDWARE_PIXELREAD FALSE
+// #define GDISP_HARDWARE_CONTROL FALSE
+// #define GDISP_HARDWARE_QUERY FALSE
+// #define GDISP_HARDWARE_CLIP FALSE
+
+ #define GDISP_PIXELFORMAT GDISP_PIXELFORMAT_RGB888
+// #endif
+
+// The custom format is not defined for some reason, so define it as error
+// so we don't get compiler warnings
+#define GDISP_PIXELFORMAT_CUSTOM GDISP_PIXELFORMAT_ERROR
+
+#define GDISP_USE_GFXNET FALSE
+// #define GDISP_GFXNET_PORT 13001
+// #define GDISP_GFXNET_CUSTOM_LWIP_STARTUP FALSE
+// #define GDISP_DONT_WAIT_FOR_NET_DISPLAY FALSE
+// #define GDISP_GFXNET_UNSAFE_SOCKETS FALSE
+
+
+///////////////////////////////////////////////////////////////////////////
+// GWIN //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_GWIN FALSE
+
+//#define GWIN_NEED_WINDOWMANAGER FALSE
+// #define GWIN_REDRAW_IMMEDIATE FALSE
+// #define GWIN_REDRAW_SINGLEOP FALSE
+// #define GWIN_NEED_FLASHING FALSE
+// #define GWIN_FLASHING_PERIOD 250
+
+//#define GWIN_NEED_CONSOLE FALSE
+// #define GWIN_CONSOLE_USE_HISTORY FALSE
+// #define GWIN_CONSOLE_HISTORY_AVERAGING FALSE
+// #define GWIN_CONSOLE_HISTORY_ATCREATE FALSE
+// #define GWIN_CONSOLE_ESCSEQ FALSE
+// #define GWIN_CONSOLE_USE_BASESTREAM FALSE
+// #define GWIN_CONSOLE_USE_FLOAT FALSE
+//#define GWIN_NEED_GRAPH FALSE
+//#define GWIN_NEED_GL3D FALSE
+
+//#define GWIN_NEED_WIDGET FALSE
+//#define GWIN_FOCUS_HIGHLIGHT_WIDTH 1
+// #define GWIN_NEED_LABEL FALSE
+// #define GWIN_LABEL_ATTRIBUTE FALSE
+// #define GWIN_NEED_BUTTON FALSE
+// #define GWIN_BUTTON_LAZY_RELEASE FALSE
+// #define GWIN_NEED_SLIDER FALSE
+// #define GWIN_SLIDER_NOSNAP FALSE
+// #define GWIN_SLIDER_DEAD_BAND 5
+// #define GWIN_SLIDER_TOGGLE_INC 20
+// #define GWIN_NEED_CHECKBOX FALSE
+// #define GWIN_NEED_IMAGE FALSE
+// #define GWIN_NEED_IMAGE_ANIMATION FALSE
+// #define GWIN_NEED_RADIO FALSE
+// #define GWIN_NEED_LIST FALSE
+// #define GWIN_NEED_LIST_IMAGES FALSE
+// #define GWIN_NEED_PROGRESSBAR FALSE
+// #define GWIN_PROGRESSBAR_AUTO FALSE
+// #define GWIN_NEED_KEYBOARD FALSE
+// #define GWIN_KEYBOARD_DEFAULT_LAYOUT VirtualKeyboard_English1
+// #define GWIN_NEED_KEYBOARD_ENGLISH1 TRUE
+// #define GWIN_NEED_TEXTEDIT FALSE
+// #define GWIN_FLAT_STYLING FALSE
+// #define GWIN_WIDGET_TAGS FALSE
+
+//#define GWIN_NEED_CONTAINERS FALSE
+// #define GWIN_NEED_CONTAINER FALSE
+// #define GWIN_NEED_FRAME FALSE
+// #define GWIN_NEED_TABSET FALSE
+// #define GWIN_TABSET_TABHEIGHT 18
+
+
+///////////////////////////////////////////////////////////////////////////
+// GEVENT //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_GEVENT FALSE
+
+//#define GEVENT_ASSERT_NO_RESOURCE FALSE
+//#define GEVENT_MAXIMUM_SIZE 32
+//#define GEVENT_MAX_SOURCE_LISTENERS 32
+
+
+///////////////////////////////////////////////////////////////////////////
+// GTIMER //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_GTIMER FALSE
+
+//#define GTIMER_THREAD_PRIORITY HIGH_PRIORITY
+//#define GTIMER_THREAD_WORKAREA_SIZE 2048
+
+
+///////////////////////////////////////////////////////////////////////////
+// GQUEUE //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_GQUEUE FALSE
+
+//#define GQUEUE_NEED_ASYNC FALSE
+//#define GQUEUE_NEED_GSYNC FALSE
+//#define GQUEUE_NEED_FSYNC FALSE
+//#define GQUEUE_NEED_BUFFERS FALSE
+
+///////////////////////////////////////////////////////////////////////////
+// GINPUT //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_GINPUT FALSE
+
+//#define GINPUT_NEED_MOUSE FALSE
+// #define GINPUT_TOUCH_STARTRAW FALSE
+// #define GINPUT_TOUCH_NOTOUCH FALSE
+// #define GINPUT_TOUCH_NOCALIBRATE FALSE
+// #define GINPUT_TOUCH_NOCALIBRATE_GUI FALSE
+// #define GINPUT_MOUSE_POLL_PERIOD 25
+// #define GINPUT_MOUSE_CLICK_TIME 300
+// #define GINPUT_TOUCH_CXTCLICK_TIME 700
+// #define GINPUT_TOUCH_USER_CALIBRATION_LOAD FALSE
+// #define GINPUT_TOUCH_USER_CALIBRATION_SAVE FALSE
+// #define GMOUSE_DRIVER_LIST GMOUSEVMT_Win32, GMOUSEVMT_Win32
+//#define GINPUT_NEED_KEYBOARD FALSE
+// #define GINPUT_KEYBOARD_POLL_PERIOD 200
+// #define GKEYBOARD_DRIVER_LIST GKEYBOARDVMT_Win32, GKEYBOARDVMT_Win32
+// #define GKEYBOARD_LAYOUT_OFF FALSE
+// #define GKEYBOARD_LAYOUT_SCANCODE2_US FALSE
+//#define GINPUT_NEED_TOGGLE FALSE
+//#define GINPUT_NEED_DIAL FALSE
+
+
+///////////////////////////////////////////////////////////////////////////
+// GFILE //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_GFILE FALSE
+
+//#define GFILE_NEED_PRINTG FALSE
+//#define GFILE_NEED_SCANG FALSE
+//#define GFILE_NEED_STRINGS FALSE
+//#define GFILE_NEED_FILELISTS FALSE
+//#define GFILE_NEED_STDIO FALSE
+//#define GFILE_NEED_NOAUTOMOUNT FALSE
+//#define GFILE_NEED_NOAUTOSYNC FALSE
+
+//#define GFILE_NEED_MEMFS FALSE
+//#define GFILE_NEED_ROMFS FALSE
+//#define GFILE_NEED_RAMFS FALSE
+//#define GFILE_NEED_FATFS FALSE
+//#define GFILE_NEED_NATIVEFS FALSE
+//#define GFILE_NEED_CHBIOSFS FALSE
+
+//#define GFILE_ALLOW_FLOATS FALSE
+//#define GFILE_ALLOW_DEVICESPECIFIC FALSE
+//#define GFILE_MAX_GFILES 3
+
+///////////////////////////////////////////////////////////////////////////
+// GADC //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_GADC FALSE
+
+//#define GADC_MAX_LOWSPEED_DEVICES 4
+
+
+///////////////////////////////////////////////////////////////////////////
+// GAUDIO //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_GAUDIO FALSE
+// There seems to be a bug in the ugfx code, the wrong define is used
+// So define it in order to avoid warnings
+#define GFX_USE_GAUDIN GFX_USE_GAUDIO
+// #define GAUDIO_NEED_PLAY FALSE
+// #define GAUDIO_NEED_RECORD FALSE
+
+
+///////////////////////////////////////////////////////////////////////////
+// GMISC //
+///////////////////////////////////////////////////////////////////////////
+#define GFX_USE_GMISC FALSE
+
+//#define GMISC_NEED_ARRAYOPS FALSE
+//#define GMISC_NEED_FASTTRIG FALSE
+//#define GMISC_NEED_FIXEDTRIG FALSE
+//#define GMISC_NEED_INVSQRT FALSE
+// #define GMISC_INVSQRT_MIXED_ENDIAN FALSE
+// #define GMISC_INVSQRT_REAL_SLOW FALSE
+//#define GMISC_NEED_MATRIXFLOAT2D FALSE
+//#define GMISC_NEED_MATRIXFIXED2D FALSE
+
+#endif /* _GFXCONF_H */
diff --git a/quantum/visualizer/example_integration/lcd_backlight_hal.c b/quantum/visualizer/example_integration/lcd_backlight_hal.c
new file mode 100644
index 0000000000..913131b169
--- /dev/null
+++ b/quantum/visualizer/example_integration/lcd_backlight_hal.c
@@ -0,0 +1,91 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "lcd_backlight.h"
+#include "hal.h"
+
+#define RED_PIN 1
+#define GREEN_PIN 2
+#define BLUE_PIN 3
+#define CHANNEL_RED FTM0->CHANNEL[0]
+#define CHANNEL_GREEN FTM0->CHANNEL[1]
+#define CHANNEL_BLUE FTM0->CHANNEL[2]
+
+#define RGB_PORT PORTC
+#define RGB_PORT_GPIO GPIOC
+
+// Base FTM clock selection (72 MHz system clock)
+// @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
+// Higher pre-scalar will use the most power (also look the best)
+// Pre-scalar calculations
+// 0 - 72 MHz -> 549 Hz
+// 1 - 36 MHz -> 275 Hz
+// 2 - 18 MHz -> 137 Hz
+// 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
+// 4 - 4 500 kHz -> 34 Hz (Visible flickering)
+// 5 - 2 250 kHz -> 17 Hz
+// 6 - 1 125 kHz -> 9 Hz
+// 7 - 562 500 Hz -> 4 Hz
+// Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
+// Which will reduce the brightness range
+#define PRESCALAR_DEFINE 0
+
+void lcd_backlight_hal_init(void) {
+ // Setup Backlight
+ SIM->SCGC6 |= SIM_SCGC6_FTM0;
+ FTM0->CNT = 0; // Reset counter
+
+ // PWM Period
+ // 16-bit maximum
+ FTM0->MOD = 0xFFFF;
+
+ // Set FTM to PWM output - Edge Aligned, Low-true pulses
+#define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0)
+ CHANNEL_RED.CnSC = CNSC_MODE;
+ CHANNEL_GREEN.CnSC = CNSC_MODE;
+ CHANNEL_BLUE.CnSC = CNSC_MODE;
+
+ // System clock, /w prescalar setting
+ FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);
+
+ CHANNEL_RED.CnV = 0;
+ CHANNEL_GREEN.CnV = 0;
+ CHANNEL_BLUE.CnV = 0;
+
+ RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
+ RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
+ RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);
+
+#define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4)
+ RGB_PORT->PCR[RED_PIN] = RGB_MODE;
+ RGB_PORT->PCR[GREEN_PIN] = RGB_MODE;
+ RGB_PORT->PCR[BLUE_PIN] = RGB_MODE;
+}
+
+void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) {
+ CHANNEL_RED.CnV = r;
+ CHANNEL_GREEN.CnV = g;
+ CHANNEL_BLUE.CnV = b;
+}
+
diff --git a/quantum/visualizer/example_integration/visualizer_user.c b/quantum/visualizer/example_integration/visualizer_user.c
new file mode 100644
index 0000000000..fc09fe2eaf
--- /dev/null
+++ b/quantum/visualizer/example_integration/visualizer_user.c
@@ -0,0 +1,121 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+// Currently we are assuming that both the backlight and LCD are enabled
+// But it's entirely possible to write a custom visualizer that use only
+// one of them
+#ifndef LCD_BACKLIGHT_ENABLE
+#error This visualizer needs that LCD backlight is enabled
+#endif
+
+#ifndef LCD_ENABLE
+#error This visualizer needs that LCD is enabled
+#endif
+
+#include "visualizer.h"
+
+static const char* welcome_text[] = {"TMK", "Infinity Ergodox"};
+
+// Just an example how to write custom keyframe functions, we could have moved
+// all this into the init function
+bool display_welcome(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)animation;
+ // Read the uGFX documentation for information how to use the displays
+ // http://wiki.ugfx.org/index.php/Main_Page
+ gdispClear(White);
+ // You can use static variables for things that can't be found in the animation
+ // or state structs
+ gdispDrawString(0, 3, welcome_text[0], state->font_dejavusansbold12, Black);
+ gdispDrawString(0, 15, welcome_text[1], state->font_dejavusansbold12, Black);
+ // Always remember to flush the display
+ gdispFlush();
+ // you could set the backlight color as well, but we won't do it here, since
+ // it's part of the following animation
+ // lcd_backlight_color(hue, saturation, intensity);
+ // We don't need constant updates, just drawing the screen once is enough
+ return false;
+}
+
+// Feel free to modify the animations below, or even add new ones if needed
+
+// Don't worry, if the startup animation is long, you can use the keyboard like normal
+// during that time
+static keyframe_animation_t startup_animation = {
+ .num_frames = 4,
+ .loop = false,
+ .frame_lengths = {0, MS2ST(1000), MS2ST(5000), 0},
+ .frame_functions = {display_welcome, keyframe_animate_backlight_color, keyframe_no_operation, enable_visualization},
+};
+
+// The color animation animates the LCD color when you change layers
+static keyframe_animation_t color_animation = {
+ .num_frames = 2,
+ .loop = false,
+ // Note that there's a 200 ms no-operation frame,
+ // this prevents the color from changing when activating the layer
+ // momentarily
+ .frame_lengths = {MS2ST(200), MS2ST(500)},
+ .frame_functions = {keyframe_no_operation, keyframe_animate_backlight_color},
+};
+
+// The LCD animation alternates between the layer name display and a
+// bitmap that displays all active layers
+static keyframe_animation_t lcd_animation = {
+ .num_frames = 2,
+ .loop = true,
+ .frame_lengths = {MS2ST(2000), MS2ST(2000)},
+ .frame_functions = {keyframe_display_layer_text, keyframe_display_layer_bitmap},
+};
+
+void initialize_user_visualizer(visualizer_state_t* state) {
+ // The brightness will be dynamically adjustable in the future
+ // But for now, change it here.
+ lcd_backlight_brightness(0x50);
+ state->current_lcd_color = LCD_COLOR(0x00, 0x00, 0xFF);
+ state->target_lcd_color = LCD_COLOR(0x10, 0xFF, 0xFF);
+ start_keyframe_animation(&startup_animation);
+}
+
+void update_user_visualizer_state(visualizer_state_t* state) {
+ // Add more tests, change the colors and layer texts here
+ // Usually you want to check the high bits (higher layers first)
+ // because that's the order layers are processed for keypresses
+ // You can for check for example:
+ // state->status.layer
+ // state->status.default_layer
+ // state->status.leds (see led.h for available statuses)
+ if (state->status.layer & 0x2) {
+ state->target_lcd_color = LCD_COLOR(0xA0, 0xB0, 0xFF);
+ state->layer_text = "Layer 2";
+ }
+ else {
+ state->target_lcd_color = LCD_COLOR(0x50, 0xB0, 0xFF);
+ state->layer_text = "Layer 1";
+ }
+ // You can also stop existing animations, and start your custom ones here
+ // remember that you should normally have only one animation for the LCD
+ // and one for the background. But you can also combine them if you want.
+ start_keyframe_animation(&lcd_animation);
+ start_keyframe_animation(&color_animation);
+}
diff --git a/quantum/visualizer/lcd_backlight.c b/quantum/visualizer/lcd_backlight.c
new file mode 100644
index 0000000000..70187d1e00
--- /dev/null
+++ b/quantum/visualizer/lcd_backlight.c
@@ -0,0 +1,85 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "lcd_backlight.h"
+#include <math.h>
+
+static uint8_t current_hue = 0x00;
+static uint8_t current_saturation = 0x00;
+static uint8_t current_intensity = 0xFF;
+static uint8_t current_brightness = 0x7F;
+
+void lcd_backlight_init(void) {
+ lcd_backlight_hal_init();
+ lcd_backlight_color(current_hue, current_saturation, current_intensity);
+}
+
+// This code is based on Brian Neltner's blogpost and example code
+// "Why every LED light should be using HSI colorspace".
+// http://blog.saikoled.com/post/43693602826/why-every-led-light-should-be-using-hsi
+static void hsi_to_rgb(float h, float s, float i, uint16_t* r_out, uint16_t* g_out, uint16_t* b_out) {
+ unsigned int r, g, b;
+ h = fmodf(h, 360.0f); // cycle h around to 0-360 degrees
+ h = 3.14159f * h / 180.0f; // Convert to radians.
+ s = s > 0.0f ? (s < 1.0f ? s : 1.0f) : 0.0f; // clamp s and i to interval [0,1]
+ i = i > 0.0f ? (i < 1.0f ? i : 1.0f) : 0.0f;
+
+ // Math! Thanks in part to Kyle Miller.
+ if(h < 2.09439f) {
+ r = 65535.0f * i/3.0f *(1.0f + s * cos(h) / cosf(1.047196667f - h));
+ g = 65535.0f * i/3.0f *(1.0f + s *(1.0f - cosf(h) / cos(1.047196667f - h)));
+ b = 65535.0f * i/3.0f *(1.0f - s);
+ } else if(h < 4.188787) {
+ h = h - 2.09439;
+ g = 65535.0f * i/3.0f *(1.0f + s * cosf(h) / cosf(1.047196667f - h));
+ b = 65535.0f * i/3.0f *(1.0f + s * (1.0f - cosf(h) / cosf(1.047196667f - h)));
+ r = 65535.0f * i/3.0f *(1.0f - s);
+ } else {
+ h = h - 4.188787;
+ b = 65535.0f*i/3.0f * (1.0f + s * cosf(h) / cosf(1.047196667f - h));
+ r = 65535.0f*i/3.0f * (1.0f + s * (1.0f - cosf(h) / cosf(1.047196667f - h)));
+ g = 65535.0f*i/3.0f * (1.0f - s);
+ }
+ *r_out = r > 65535 ? 65535 : r;
+ *g_out = g > 65535 ? 65535 : g;
+ *b_out = b > 65535 ? 65535 : b;
+}
+
+void lcd_backlight_color(uint8_t hue, uint8_t saturation, uint8_t intensity) {
+ uint16_t r, g, b;
+ float hue_f = 360.0f * (float)hue / 255.0f;
+ float saturation_f = (float)saturation / 255.0f;
+ float intensity_f = (float)intensity / 255.0f;
+ intensity_f *= (float)current_brightness / 255.0f;
+ hsi_to_rgb(hue_f, saturation_f, intensity_f, &r, &g, &b);
+ current_hue = hue;
+ current_saturation = saturation;
+ current_intensity = intensity;
+ lcd_backlight_hal_color(r, g, b);
+}
+
+void lcd_backlight_brightness(uint8_t b) {
+ current_brightness = b;
+ lcd_backlight_color(current_hue, current_saturation, current_intensity);
+}
diff --git a/quantum/visualizer/lcd_backlight.h b/quantum/visualizer/lcd_backlight.h
new file mode 100644
index 0000000000..dd3e37a064
--- /dev/null
+++ b/quantum/visualizer/lcd_backlight.h
@@ -0,0 +1,42 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef LCD_BACKLIGHT_H_
+#define LCD_BACKLIGHT_H_
+#include "stdint.h"
+
+// Helper macros for storing hue, staturation and intensity as unsigned integers
+#define LCD_COLOR(hue, saturation, intensity) (hue << 16 | saturation << 8 | intensity)
+#define LCD_HUE(color) ((color >> 16) & 0xFF)
+#define LCD_SAT(color) ((color >> 8) & 0xFF)
+#define LCD_INT(color) (color & 0xFF)
+
+void lcd_backlight_init(void);
+void lcd_backlight_color(uint8_t hue, uint8_t saturation, uint8_t intensity);
+void lcd_backlight_brightness(uint8_t b);
+
+void lcd_backlight_hal_init(void);
+void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b);
+
+#endif /* LCD_BACKLIGHT_H_ */
diff --git a/quantum/visualizer/led_test.c b/quantum/visualizer/led_test.c
new file mode 100644
index 0000000000..a9abace8df
--- /dev/null
+++ b/quantum/visualizer/led_test.c
@@ -0,0 +1,170 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+#include "led_test.h"
+#include "gfx.h"
+#include "math.h"
+
+#define CROSSFADE_TIME 1000
+#define GRADIENT_TIME 3000
+
+keyframe_animation_t led_test_animation = {
+ .num_frames = 14,
+ .loop = true,
+ .frame_lengths = {
+ gfxMillisecondsToTicks(1000), // fade in
+ gfxMillisecondsToTicks(1000), // no op (leds on)
+ gfxMillisecondsToTicks(1000), // fade out
+ gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade
+ gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
+ gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade
+ gfxMillisecondsToTicks(GRADIENT_TIME), // top_to_bottom
+ 0, // mirror leds
+ gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade
+ gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
+ gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade
+ gfxMillisecondsToTicks(GRADIENT_TIME), // top_to_bottom
+ 0, // normal leds
+ gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade
+
+ },
+ .frame_functions = {
+ keyframe_fade_in_all_leds,
+ keyframe_no_operation,
+ keyframe_fade_out_all_leds,
+ keyframe_led_crossfade,
+ keyframe_led_left_to_right_gradient,
+ keyframe_led_crossfade,
+ keyframe_led_top_to_bottom_gradient,
+ keyframe_mirror_led_orientation,
+ keyframe_led_crossfade,
+ keyframe_led_left_to_right_gradient,
+ keyframe_led_crossfade,
+ keyframe_led_top_to_bottom_gradient,
+ keyframe_normal_led_orientation,
+ keyframe_led_crossfade,
+ },
+};
+
+static uint8_t fade_led_color(keyframe_animation_t* animation, int from, int to) {
+ int frame_length = animation->frame_lengths[animation->current_frame];
+ int current_pos = frame_length - animation->time_left_in_frame;
+ int delta = to - from;
+ int luma = (delta * current_pos) / frame_length;
+ luma += from;
+ return luma;
+}
+
+static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint8_t from, uint8_t to) {
+ uint8_t luma = fade_led_color(animation, from, to);
+ color_t color = LUMA2COLOR(luma);
+ gdispGClear(LED_DISPLAY, color);
+}
+
+// TODO: Should be customizable per keyboard
+#define NUM_ROWS 7
+#define NUM_COLS 7
+
+static uint8_t crossfade_start_frame[NUM_ROWS][NUM_COLS];
+static uint8_t crossfade_end_frame[NUM_ROWS][NUM_COLS];
+
+static uint8_t compute_gradient_color(float t, float index, float num) {
+ const float two_pi = M_2_PI;
+ float normalized_index = (1.0f - index / (num - 1.0f)) * two_pi;
+ float x = t * two_pi + normalized_index;
+ float v = 0.5 * (cosf(x) + 1.0f);
+ return (uint8_t)(255.0f * v);
+}
+
+bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)state;
+ keyframe_fade_all_leds_from_to(animation, 0, 255);
+ return true;
+}
+
+bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)state;
+ keyframe_fade_all_leds_from_to(animation, 255, 0);
+ return true;
+}
+
+bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)state;
+ float frame_length = animation->frame_lengths[animation->current_frame];
+ float current_pos = frame_length - animation->time_left_in_frame;
+ float t = current_pos / frame_length;
+ for (int i=0; i< NUM_COLS; i++) {
+ uint8_t color = compute_gradient_color(t, i, NUM_COLS);
+ gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color));
+ }
+ return true;
+}
+
+bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)state;
+ float frame_length = animation->frame_lengths[animation->current_frame];
+ float current_pos = frame_length - animation->time_left_in_frame;
+ float t = current_pos / frame_length;
+ for (int i=0; i< NUM_ROWS; i++) {
+ uint8_t color = compute_gradient_color(t, i, NUM_ROWS);
+ gdispGDrawLine(LED_DISPLAY, 0, i, NUM_COLS - 1, i, LUMA2COLOR(color));
+ }
+ return true;
+}
+
+static void copy_current_led_state(uint8_t* dest) {
+ for (int i=0;i<NUM_ROWS;i++) {
+ for (int j=0;j<NUM_COLS;j++) {
+ dest[i*NUM_COLS + j] = gdispGGetPixelColor(LED_DISPLAY, j, i);
+ }
+ }
+}
+bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)state;
+ if (animation->first_update_of_frame) {
+ copy_current_led_state(&crossfade_start_frame[0][0]);
+ run_next_keyframe(animation, state);
+ copy_current_led_state(&crossfade_end_frame[0][0]);
+ }
+ for (int i=0;i<NUM_ROWS;i++) {
+ for (int j=0;j<NUM_COLS;j++) {
+ color_t color = LUMA2COLOR(fade_led_color(animation, crossfade_start_frame[i][j], crossfade_end_frame[i][j]));
+ gdispGDrawPixel(LED_DISPLAY, j, i, color);
+ }
+ }
+ return true;
+}
+
+bool keyframe_mirror_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)state;
+ (void)animation;
+ gdispGSetOrientation(LED_DISPLAY, GDISP_ROTATE_180);
+ return false;
+}
+
+bool keyframe_normal_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)state;
+ (void)animation;
+ gdispGSetOrientation(LED_DISPLAY, GDISP_ROTATE_0);
+ return false;
+}
diff --git a/quantum/visualizer/led_test.h b/quantum/visualizer/led_test.h
new file mode 100644
index 0000000000..5e2325753e
--- /dev/null
+++ b/quantum/visualizer/led_test.h
@@ -0,0 +1,41 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef TMK_VISUALIZER_LED_TEST_H_
+#define TMK_VISUALIZER_LED_TEST_H_
+
+#include "visualizer.h"
+
+bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state);
+bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_state_t* state);
+bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visualizer_state_t* state);
+bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visualizer_state_t* state);
+bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* state);
+bool keyframe_mirror_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state);
+bool keyframe_normal_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state);
+
+extern keyframe_animation_t led_test_animation;
+
+
+#endif /* TMK_VISUALIZER_LED_TEST_H_ */
diff --git a/quantum/visualizer/readme.md b/quantum/visualizer/readme.md
new file mode 100644
index 0000000000..545ba22707
--- /dev/null
+++ b/quantum/visualizer/readme.md
@@ -0,0 +1,18 @@
+# A visualization library for the TMK keyboard firmware
+
+This library is designed to work together with the [TMK keyboard firmware](https://github.com/tmk/tmk_keyboard). Currently it only works for [Chibios](http://www.chibios.org/)
+ flavors, but it would be possible to add support for other configurations as well. The LCD display functionality is provided by the [uGFX library](http://www.ugfx.org/).
+
+## To use this library as a user
+You can and should modify the visualizer\_user.c file. Check the comments in the file for more information.
+
+## To add this library to custom keyboard projects
+
+1. Add tmk_visualizer as a submodule to your project
+1. Set VISUALIZER_DIR in the main keyboard project makefile to point to the submodule
+1. Define LCD\_ENABLE and/or LCD\_BACKLIGHT\_ENABLE, to enable support
+1. Include the visualizer.mk make file
+1. Copy the files in the example\_integration folder to your keyboard project
+1. All other files than the callback.c file are included automatically, so you will need to add callback.c to your makefile manually. If you already have a similar file in your project, you can just copy the functions instead of the whole file.
+1. Edit the files to match your hardware. You might might want to read the Chibios and UGfx documentation, for more information.
+1. If you enable LCD support you might also have to write a custom uGFX display driver, check the uGFX documentation for that. You probably also want to enable SPI support in your Chibios configuration.
diff --git a/quantum/visualizer/visualizer.c b/quantum/visualizer/visualizer.c
new file mode 100644
index 0000000000..54f6faaa42
--- /dev/null
+++ b/quantum/visualizer/visualizer.c
@@ -0,0 +1,545 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "visualizer.h"
+#include "config.h"
+#include <string.h>
+#ifdef PROTOCOL_CHIBIOS
+#include "ch.h"
+#endif
+
+#include "gfx.h"
+
+#ifdef LCD_BACKLIGHT_ENABLE
+#include "lcd_backlight.h"
+#endif
+
+//#define DEBUG_VISUALIZER
+
+#ifdef DEBUG_VISUALIZER
+#include "debug.h"
+#else
+#include "nodebug.h"
+#endif
+
+#ifdef SERIAL_LINK_ENABLE
+#include "serial_link/protocol/transport.h"
+#include "serial_link/system/serial_link.h"
+#endif
+
+// Define this in config.h
+#ifndef VISUALIZER_THREAD_PRIORITY
+#define "Visualizer thread priority not defined"
+#endif
+
+
+static visualizer_keyboard_status_t current_status = {
+ .layer = 0xFFFFFFFF,
+ .default_layer = 0xFFFFFFFF,
+ .leds = 0xFFFFFFFF,
+ .suspended = false,
+};
+
+static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboard_status_t* status2) {
+ return status1->layer == status2->layer &&
+ status1->default_layer == status2->default_layer &&
+ status1->leds == status2->leds &&
+ status1->suspended == status2->suspended;
+}
+
+static bool visualizer_enabled = false;
+
+#define MAX_SIMULTANEOUS_ANIMATIONS 4
+static keyframe_animation_t* animations[MAX_SIMULTANEOUS_ANIMATIONS] = {};
+
+#ifdef SERIAL_LINK_ENABLE
+MASTER_TO_ALL_SLAVES_OBJECT(current_status, visualizer_keyboard_status_t);
+
+static remote_object_t* remote_objects[] = {
+ REMOTE_OBJECT(current_status),
+};
+
+#endif
+
+GDisplay* LCD_DISPLAY = 0;
+GDisplay* LED_DISPLAY = 0;
+
+__attribute__((weak))
+GDisplay* get_lcd_display(void) {
+ return gdispGetDisplay(0);
+}
+
+__attribute__((weak))
+GDisplay* get_led_display(void) {
+ return gdispGetDisplay(1);
+}
+
+void start_keyframe_animation(keyframe_animation_t* animation) {
+ animation->current_frame = -1;
+ animation->time_left_in_frame = 0;
+ animation->need_update = true;
+ int free_index = -1;
+ for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
+ if (animations[i] == animation) {
+ return;
+ }
+ if (free_index == -1 && animations[i] == NULL) {
+ free_index=i;
+ }
+ }
+ if (free_index!=-1) {
+ animations[free_index] = animation;
+ }
+}
+
+void stop_keyframe_animation(keyframe_animation_t* animation) {
+ animation->current_frame = animation->num_frames;
+ animation->time_left_in_frame = 0;
+ animation->need_update = true;
+ animation->first_update_of_frame = false;
+ animation->last_update_of_frame = false;
+ for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
+ if (animations[i] == animation) {
+ animations[i] = NULL;
+ return;
+ }
+ }
+}
+
+void stop_all_keyframe_animations(void) {
+ for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
+ if (animations[i]) {
+ animations[i]->current_frame = animations[i]->num_frames;
+ animations[i]->time_left_in_frame = 0;
+ animations[i]->need_update = true;
+ animations[i]->first_update_of_frame = false;
+ animations[i]->last_update_of_frame = false;
+ animations[i] = NULL;
+ }
+ }
+}
+
+static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systemticks_t delta, systemticks_t* sleep_time) {
+ // TODO: Clean up this messy code
+ dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame,
+ animation->time_left_in_frame, delta);
+ if (animation->current_frame == animation->num_frames) {
+ animation->need_update = false;
+ return false;
+ }
+ if (animation->current_frame == -1) {
+ animation->current_frame = 0;
+ animation->time_left_in_frame = animation->frame_lengths[0];
+ animation->need_update = true;
+ animation->first_update_of_frame = true;
+ } else {
+ animation->time_left_in_frame -= delta;
+ while (animation->time_left_in_frame <= 0) {
+ int left = animation->time_left_in_frame;
+ if (animation->need_update) {
+ animation->time_left_in_frame = 0;
+ animation->last_update_of_frame = true;
+ (*animation->frame_functions[animation->current_frame])(animation, state);
+ animation->last_update_of_frame = false;
+ }
+ animation->current_frame++;
+ animation->need_update = true;
+ animation->first_update_of_frame = true;
+ if (animation->current_frame == animation->num_frames) {
+ if (animation->loop) {
+ animation->current_frame = 0;
+ }
+ else {
+ stop_keyframe_animation(animation);
+ return false;
+ }
+ }
+ delta = -left;
+ animation->time_left_in_frame = animation->frame_lengths[animation->current_frame];
+ animation->time_left_in_frame -= delta;
+ }
+ }
+ if (animation->need_update) {
+ animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state);
+ animation->first_update_of_frame = false;
+ }
+
+ systemticks_t wanted_sleep = animation->need_update ? gfxMillisecondsToTicks(10) : (unsigned)animation->time_left_in_frame;
+ if (wanted_sleep < *sleep_time) {
+ *sleep_time = wanted_sleep;
+ }
+
+ return true;
+}
+
+void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state) {
+ int next_frame = animation->current_frame + 1;
+ if (next_frame == animation->num_frames) {
+ next_frame = 0;
+ }
+ keyframe_animation_t temp_animation = *animation;
+ temp_animation.current_frame = next_frame;
+ temp_animation.time_left_in_frame = animation->frame_lengths[next_frame];
+ temp_animation.first_update_of_frame = true;
+ temp_animation.last_update_of_frame = false;
+ temp_animation.need_update = false;
+ visualizer_state_t temp_state = *state;
+ (*temp_animation.frame_functions[next_frame])(&temp_animation, &temp_state);
+}
+
+bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)animation;
+ (void)state;
+ return false;
+}
+
+#ifdef LCD_BACKLIGHT_ENABLE
+bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) {
+ int frame_length = animation->frame_lengths[animation->current_frame];
+ int current_pos = frame_length - animation->time_left_in_frame;
+ uint8_t t_h = LCD_HUE(state->target_lcd_color);
+ uint8_t t_s = LCD_SAT(state->target_lcd_color);
+ uint8_t t_i = LCD_INT(state->target_lcd_color);
+ uint8_t p_h = LCD_HUE(state->prev_lcd_color);
+ uint8_t p_s = LCD_SAT(state->prev_lcd_color);
+ uint8_t p_i = LCD_INT(state->prev_lcd_color);
+
+ uint8_t d_h1 = t_h - p_h; //Modulo arithmetic since we want to wrap around
+ int d_h2 = t_h - p_h;
+ // Chose the shortest way around
+ int d_h = abs(d_h2) < d_h1 ? d_h2 : d_h1;
+ int d_s = t_s - p_s;
+ int d_i = t_i - p_i;
+
+ int hue = (d_h * current_pos) / frame_length;
+ int sat = (d_s * current_pos) / frame_length;
+ int intensity = (d_i * current_pos) / frame_length;
+ //dprintf("%X -> %X = %X\n", p_h, t_h, hue);
+ hue += p_h;
+ sat += p_s;
+ intensity += p_i;
+ state->current_lcd_color = LCD_COLOR(hue, sat, intensity);
+ lcd_backlight_color(
+ LCD_HUE(state->current_lcd_color),
+ LCD_SAT(state->current_lcd_color),
+ LCD_INT(state->current_lcd_color));
+
+ return true;
+}
+
+bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)animation;
+ state->prev_lcd_color = state->target_lcd_color;
+ state->current_lcd_color = state->target_lcd_color;
+ lcd_backlight_color(
+ LCD_HUE(state->current_lcd_color),
+ LCD_SAT(state->current_lcd_color),
+ LCD_INT(state->current_lcd_color));
+ return false;
+}
+#endif // LCD_BACKLIGHT_ENABLE
+
+#ifdef LCD_ENABLE
+bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)animation;
+ gdispClear(White);
+ gdispDrawString(0, 10, state->layer_text, state->font_dejavusansbold12, Black);
+ gdispFlush();
+ return false;
+}
+
+static void format_layer_bitmap_string(uint16_t default_layer, uint16_t layer, char* buffer) {
+ for (int i=0; i<16;i++)
+ {
+ uint32_t mask = (1u << i);
+ if (default_layer & mask) {
+ if (layer & mask) {
+ *buffer = 'B';
+ } else {
+ *buffer = 'D';
+ }
+ } else if (layer & mask) {
+ *buffer = '1';
+ } else {
+ *buffer = '0';
+ }
+ ++buffer;
+
+ if (i==3 || i==7 || i==11) {
+ *buffer = ' ';
+ ++buffer;
+ }
+ }
+ *buffer = 0;
+}
+
+bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)animation;
+ const char* layer_help = "1=On D=Default B=Both";
+ char layer_buffer[16 + 4]; // 3 spaces and one null terminator
+ gdispClear(White);
+ gdispDrawString(0, 0, layer_help, state->font_fixed5x8, Black);
+ format_layer_bitmap_string(state->status.default_layer, state->status.layer, layer_buffer);
+ gdispDrawString(0, 10, layer_buffer, state->font_fixed5x8, Black);
+ format_layer_bitmap_string(state->status.default_layer >> 16, state->status.layer >> 16, layer_buffer);
+ gdispDrawString(0, 20, layer_buffer, state->font_fixed5x8, Black);
+ gdispFlush();
+ return false;
+}
+#endif // LCD_ENABLE
+
+bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)animation;
+ (void)state;
+#ifdef LCD_ENABLE
+ gdispSetPowerMode(powerOff);
+#endif
+#ifdef LCD_BACKLIGHT_ENABLE
+ lcd_backlight_hal_color(0, 0, 0);
+#endif
+ return false;
+}
+
+bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)animation;
+ (void)state;
+#ifdef LCD_ENABLE
+ gdispSetPowerMode(powerOn);
+#endif
+ return false;
+}
+
+bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state) {
+ (void)animation;
+ (void)state;
+ dprint("User visualizer inited\n");
+ visualizer_enabled = true;
+ return false;
+}
+
+// TODO: Optimize the stack size, this is probably way too big
+static DECLARE_THREAD_STACK(visualizerThreadStack, 1024);
+static DECLARE_THREAD_FUNCTION(visualizerThread, arg) {
+ (void)arg;
+
+ GListener event_listener;
+ geventListenerInit(&event_listener);
+ geventAttachSource(&event_listener, (GSourceHandle)&current_status, 0);
+
+ visualizer_keyboard_status_t initial_status = {
+ .default_layer = 0xFFFFFFFF,
+ .layer = 0xFFFFFFFF,
+ .leds = 0xFFFFFFFF,
+ .suspended = false,
+ };
+
+ visualizer_state_t state = {
+ .status = initial_status,
+ .current_lcd_color = 0,
+#ifdef LCD_ENABLE
+ .font_fixed5x8 = gdispOpenFont("fixed_5x8"),
+ .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12")
+#endif
+ };
+ initialize_user_visualizer(&state);
+ state.prev_lcd_color = state.current_lcd_color;
+
+#ifdef LCD_BACKLIGHT_ENABLE
+ lcd_backlight_color(
+ LCD_HUE(state.current_lcd_color),
+ LCD_SAT(state.current_lcd_color),
+ LCD_INT(state.current_lcd_color));
+#endif
+
+ systemticks_t sleep_time = TIME_INFINITE;
+ systemticks_t current_time = gfxSystemTicks();
+
+ while(true) {
+ systemticks_t new_time = gfxSystemTicks();
+ systemticks_t delta = new_time - current_time;
+ current_time = new_time;
+ bool enabled = visualizer_enabled;
+ if (!same_status(&state.status, &current_status)) {
+ if (visualizer_enabled) {
+ if (current_status.suspended) {
+ stop_all_keyframe_animations();
+ visualizer_enabled = false;
+ state.status = current_status;
+ user_visualizer_suspend(&state);
+ }
+ else {
+ state.status = current_status;
+ update_user_visualizer_state(&state);
+ }
+ state.prev_lcd_color = state.current_lcd_color;
+ }
+ }
+ if (!enabled && state.status.suspended && current_status.suspended == false) {
+ // Setting the status to the initial status will force an update
+ // when the visualizer is enabled again
+ state.status = initial_status;
+ state.status.suspended = false;
+ stop_all_keyframe_animations();
+ user_visualizer_resume(&state);
+ state.prev_lcd_color = state.current_lcd_color;
+ }
+ sleep_time = TIME_INFINITE;
+ for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
+ if (animations[i]) {
+ update_keyframe_animation(animations[i], &state, delta, &sleep_time);
+ }
+ }
+#ifdef LED_ENABLE
+ gdispGFlush(LED_DISPLAY);
+#endif
+
+#ifdef EMULATOR
+ draw_emulator();
+#endif
+ // The animation can enable the visualizer
+ // And we might need to update the state when that happens
+ // so don't sleep
+ if (enabled != visualizer_enabled) {
+ sleep_time = 0;
+ }
+
+ systemticks_t after_update = gfxSystemTicks();
+ unsigned update_delta = after_update - current_time;
+ if (sleep_time != TIME_INFINITE) {
+ if (sleep_time > update_delta) {
+ sleep_time -= update_delta;
+ }
+ else {
+ sleep_time = 0;
+ }
+ }
+ dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time);
+#ifdef PROTOCOL_CHIBIOS
+ // The gEventWait function really takes milliseconds, even if the documentation says ticks.
+ // Unfortunately there's no generic ugfx conversion from system time to milliseconds,
+ // so let's do it in a platform dependent way.
+
+ // On windows the system ticks is the same as milliseconds anyway
+ if (sleep_time != TIME_INFINITE) {
+ sleep_time = ST2MS(sleep_time);
+ }
+#endif
+ geventEventWait(&event_listener, sleep_time);
+ }
+#ifdef LCD_ENABLE
+ gdispCloseFont(state.font_fixed5x8);
+ gdispCloseFont(state.font_dejavusansbold12);
+#endif
+
+ return 0;
+}
+
+void visualizer_init(void) {
+ gfxInit();
+
+#ifdef LCD_BACKLIGHT_ENABLE
+ lcd_backlight_init();
+#endif
+
+#ifdef SERIAL_LINK_ENABLE
+ add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) );
+#endif
+
+#ifdef LCD_ENABLE
+ LCD_DISPLAY = get_lcd_display();
+#endif
+#ifdef LED_ENABLE
+ LED_DISPLAY = get_led_display();
+#endif
+
+ // We are using a low priority thread, the idea is to have it run only
+ // when the main thread is sleeping during the matrix scanning
+ gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack),
+ VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL);
+}
+
+void update_status(bool changed) {
+ if (changed) {
+ GSourceListener* listener = geventGetSourceListener((GSourceHandle)&current_status, NULL);
+ if (listener) {
+ geventSendEvent(listener);
+ }
+ }
+#ifdef SERIAL_LINK_ENABLE
+ static systime_t last_update = 0;
+ systime_t current_update = chVTGetSystemTimeX();
+ systime_t delta = current_update - last_update;
+ if (changed || delta > MS2ST(10)) {
+ last_update = current_update;
+ visualizer_keyboard_status_t* r = begin_write_current_status();
+ *r = current_status;
+ end_write_current_status();
+ }
+#endif
+}
+
+void visualizer_update(uint32_t default_state, uint32_t state, uint32_t leds) {
+ // Note that there's a small race condition here, the thread could read
+ // a state where one of these are set but not the other. But this should
+ // not really matter as it will be fixed during the next loop step.
+ // Alternatively a mutex could be used instead of the volatile variables
+
+ bool changed = false;
+#ifdef SERIAL_LINK_ENABLE
+ if (is_serial_link_connected ()) {
+ visualizer_keyboard_status_t* new_status = read_current_status();
+ if (new_status) {
+ if (!same_status(&current_status, new_status)) {
+ changed = true;
+ current_status = *new_status;
+ }
+ }
+ }
+ else {
+#else
+ {
+#endif
+ visualizer_keyboard_status_t new_status = {
+ .layer = state,
+ .default_layer = default_state,
+ .leds = leds,
+ .suspended = current_status.suspended,
+ };
+ if (!same_status(&current_status, &new_status)) {
+ changed = true;
+ current_status = new_status;
+ }
+ }
+ update_status(changed);
+}
+
+void visualizer_suspend(void) {
+ current_status.suspended = true;
+ update_status(true);
+}
+
+void visualizer_resume(void) {
+ current_status.suspended = false;
+ update_status(true);
+}
diff --git a/quantum/visualizer/visualizer.h b/quantum/visualizer/visualizer.h
new file mode 100644
index 0000000000..53e250725c
--- /dev/null
+++ b/quantum/visualizer/visualizer.h
@@ -0,0 +1,147 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Fred Sundvik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef VISUALIZER_H
+#define VISUALIZER_H
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "gfx.h"
+
+#ifdef LCD_BACKLIGHT_ENABLE
+#include "lcd_backlight.h"
+#endif
+
+// This need to be called once at the start
+void visualizer_init(void);
+// This should be called at every matrix scan
+void visualizer_update(uint32_t default_state, uint32_t state, uint32_t leds);
+// This should be called when the keyboard goes to suspend state
+void visualizer_suspend(void);
+// This should be called when the keyboard wakes up from suspend state
+void visualizer_resume(void);
+
+// These functions are week, so they can be overridden by the keyboard
+// if needed
+GDisplay* get_lcd_display(void);
+GDisplay* get_led_display(void);
+
+// For emulator builds, this function need to be implemented
+#ifdef EMULATOR
+void draw_emulator(void);
+#endif
+
+// If you need support for more than 16 keyframes per animation, you can change this
+#define MAX_VISUALIZER_KEY_FRAMES 16
+
+struct keyframe_animation_t;
+
+typedef struct {
+ uint32_t layer;
+ uint32_t default_layer;
+ uint32_t leds; // See led.h for available statuses
+ bool suspended;
+} visualizer_keyboard_status_t;
+
+// The state struct is used by the various keyframe functions
+// It's also used for setting the LCD color and layer text
+// from the user customized code
+typedef struct visualizer_state_t {
+ // The user code should primarily be modifying these
+ uint32_t target_lcd_color;
+ const char* layer_text;
+
+ // The user visualizer(and animation functions) can read these
+ visualizer_keyboard_status_t status;
+
+ // These are used by the animation functions
+ uint32_t current_lcd_color;
+ uint32_t prev_lcd_color;
+#ifdef LCD_ENABLE
+ font_t font_fixed5x8;
+ font_t font_dejavusansbold12;
+#endif
+} visualizer_state_t;
+
+// Any custom keyframe function should have this signature
+// return true to get continuous updates, otherwise you will only get one
+// update per frame
+typedef bool (*frame_func)(struct keyframe_animation_t*, visualizer_state_t*);
+
+// Represents a keyframe animation, so fields are internal to the system
+// while others are meant to be initialized by the user code
+typedef struct keyframe_animation_t {
+ // These should be initialized
+ int num_frames;
+ bool loop;
+ int frame_lengths[MAX_VISUALIZER_KEY_FRAMES];
+ frame_func frame_functions[MAX_VISUALIZER_KEY_FRAMES];
+
+ // Used internally by the system, and can also be read by
+ // keyframe update functions
+ int current_frame;
+ int time_left_in_frame;
+ bool first_update_of_frame;
+ bool last_update_of_frame;
+ bool need_update;
+
+} keyframe_animation_t;
+
+extern GDisplay* LCD_DISPLAY;
+extern GDisplay* LED_DISPLAY;
+
+void start_keyframe_animation(keyframe_animation_t* animation);
+void stop_keyframe_animation(keyframe_animation_t* animation);
+// This runs the next keyframe, but does not update the animation state
+// Useful for crossfades for example
+void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state);
+
+// Some predefined keyframe functions that can be used by the user code
+// Does nothing, useful for adding delays
+bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state);
+// Animates the LCD backlight color between the current color and the target color (of the state)
+bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state);
+// Sets the backlight color to the target color
+bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state);
+// Displays the layer text centered vertically on the screen
+bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state);
+// Displays a bitmap (0/1) of all the currently active layers
+bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state);
+
+bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state);
+bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state);
+
+// Call this once, when the initial animation has finished, alternatively you can call it
+// directly from the initalize_user_visualizer function (the animation can be null)
+bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state);
+
+// These functions have to be implemented by the user
+void initialize_user_visualizer(visualizer_state_t* state);
+void update_user_visualizer_state(visualizer_state_t* state);
+void user_visualizer_suspend(visualizer_state_t* state);
+void user_visualizer_resume(visualizer_state_t* state);
+
+
+#endif /* VISUALIZER_H */
diff --git a/quantum/visualizer/visualizer.mk b/quantum/visualizer/visualizer.mk
new file mode 100644
index 0000000000..2f4a41d66f
--- /dev/null
+++ b/quantum/visualizer/visualizer.mk
@@ -0,0 +1,61 @@
+# The MIT License (MIT)
+#
+# Copyright (c) 2016 Fred Sundvik
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+SRC += $(VISUALIZER_DIR)/visualizer.c
+EXTRAINCDIRS += $(GFXINC) $(VISUALIZER_DIR)
+GFXLIB = $(LIB_PATH)/ugfx
+VPATH += $(VISUALIZER_PATH)
+
+OPT_DEFS += -DVISUALIZER_ENABLE
+
+ifdef LCD_ENABLE
+OPT_DEFS += -DLCD_ENABLE
+ULIBS += -lm
+endif
+
+ifdef LCD_BACKLIGHT_ENABLE
+SRC += $(VISUALIZER_DIR)/lcd_backlight.c
+OPT_DEFS += -DLCD_BACKLIGHT_ENABLE
+endif
+
+ifdef LED_ENABLE
+SRC += $(VISUALIZER_DIR)/led_test.c
+OPT_DEFS += -DLED_ENABLE
+endif
+
+include $(GFXLIB)/gfx.mk
+SRC += $(patsubst $(TOP_DIR)/%,%,$(GFXSRC))
+OPT_DEFS += $(patsubst %,-D%,$(patsubst -D%,%,$(GFXDEFS)))
+
+ifneq ("$(wildcard $(KEYMAP_PATH)/visualizer.c)","")
+ SRC += keyboards/$(KEYBOARD)/keymaps/$(KEYMAP)/visualizer.c
+else
+ ifeq ("$(wildcard $(SUBPROJECT_PATH)/keymaps/$(KEYMAP)/visualizer.c)","")
+$(error "$(KEYMAP_PATH)/visualizer.c" does not exist)
+ else
+ SRC += keyboards/$(KEYBOARD)/$(SUBPROJECT)/keymaps/$(KEYMAP)/visualizer.c
+ endif
+endif
+
+ifdef EMULATOR
+UINCDIR += $(TMK_DIR)/common
+endif \ No newline at end of file