summaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
Diffstat (limited to 'quantum')
-rw-r--r--quantum/audio/song_list.h8
-rw-r--r--quantum/dynamic_macro.h226
-rw-r--r--quantum/keymap.h12
-rw-r--r--quantum/keymap_common.c4
-rw-r--r--quantum/keymap_extras/keymap_bepo.h4
-rw-r--r--quantum/keymap_extras/keymap_dvorak.h15
-rw-r--r--quantum/keymap_extras/keymap_fr_ch.h2
-rw-r--r--quantum/keymap_extras/keymap_french.h4
-rw-r--r--quantum/keymap_extras/keymap_german.h2
-rw-r--r--quantum/keymap_extras/keymap_german_ch.h2
-rw-r--r--quantum/keymap_extras/keymap_nordic.h4
-rw-r--r--quantum/keymap_extras/keymap_norwegian.h2
-rw-r--r--quantum/keymap_extras/keymap_russian.h77
-rw-r--r--quantum/keymap_extras/keymap_spanish.h6
-rw-r--r--quantum/keymap_extras/keymap_uk.h4
-rw-r--r--quantum/keymap_extras/keymap_unicode_cyrillic.h163
-rwxr-xr-xquantum/light_ws2812.c25
-rw-r--r--quantum/matrix.c280
-rw-r--r--quantum/process_keycode/process_midi.c24
-rw-r--r--quantum/process_keycode/process_music.c7
-rw-r--r--quantum/process_keycode/process_tap_dance.c142
-rw-r--r--quantum/process_keycode/process_tap_dance.h50
-rw-r--r--quantum/process_keycode/process_unicode.c205
-rw-r--r--quantum/process_keycode/process_unicode.h38
-rw-r--r--quantum/quantum.c152
-rw-r--r--quantum/quantum.h6
-rw-r--r--quantum/rgblight.c722
-rw-r--r--quantum/rgblight.h7
-rw-r--r--quantum/serial_link/LICENSE21
-rw-r--r--quantum/serial_link/README.md1
-rw-r--r--quantum/serial_link/protocol/byte_stuffer.c145
-rw-r--r--quantum/serial_link/protocol/byte_stuffer.h34
-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.c124
-rw-r--r--quantum/serial_link/protocol/transport.h151
-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.c506
-rw-r--r--quantum/serial_link/tests/frame_router_tests.c231
-rw-r--r--quantum/serial_link/tests/frame_validator_tests.c101
-rw-r--r--quantum/serial_link/tests/transport_tests.c168
-rw-r--r--quantum/serial_link/tests/triple_buffered_object_tests.c82
-rw-r--r--quantum/template/template.c2
-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
63 files changed, 5641 insertions, 639 deletions
diff --git a/quantum/audio/song_list.h b/quantum/audio/song_list.h
index fc6fcdeef1..8022ca6729 100644
--- a/quantum/audio/song_list.h
+++ b/quantum/audio/song_list.h
@@ -28,6 +28,14 @@
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), \
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/keymap.h b/quantum/keymap.h
index 73f99f8211..f2d94d75c3 100644
--- a/quantum/keymap.h
+++ b/quantum/keymap.h
@@ -157,6 +157,16 @@ enum quantum_keycodes {
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,
@@ -299,7 +309,7 @@ enum quantum_keycodes {
#define OSL(layer) (layer | QK_ONE_SHOT_LAYER)
// One-shot mod
-#define OSM(layer) (layer | QK_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))
diff --git a/quantum/keymap_common.c b/quantum/keymap_common.c
index 76872ac592..833e5a8f8d 100644
--- a/quantum/keymap_common.c
+++ b/quantum/keymap_common.c
@@ -30,7 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "quantum.h"
#ifdef MIDI_ENABLE
- #include "keymap_midi.h"
+ #include "process_midi.h"
#endif
extern keymap_config_t keymap_config;
@@ -62,7 +62,7 @@ action_t action_for_key(uint8_t layer, keypos_t key)
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:
diff --git a/quantum/keymap_extras/keymap_bepo.h b/quantum/keymap_extras/keymap_bepo.h
index 4c30960547..e5ef39552a 100644
--- a/quantum/keymap_extras/keymap_bepo.h
+++ b/quantum/keymap_extras/keymap_bepo.h
@@ -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_dvorak.h b/quantum/keymap_extras/keymap_dvorak.h
index e855056e83..17f2059774 100644
--- a/quantum/keymap_extras/keymap_dvorak.h
+++ b/quantum/keymap_extras/keymap_dvorak.h
@@ -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 3fd9713575..87d4bb24cf 100644
--- a/quantum/keymap_extras/keymap_fr_ch.h
+++ b/quantum/keymap_extras/keymap_fr_ch.h
@@ -4,7 +4,7 @@
#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 2a44c80b14..834c69650e 100644
--- a/quantum/keymap_extras/keymap_french.h
+++ b/quantum/keymap_extras/keymap_french.h
@@ -4,7 +4,7 @@
#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_german.h b/quantum/keymap_extras/keymap_german.h
index 3f9ae8bade..7e2e0ed44e 100644
--- a/quantum/keymap_extras/keymap_german.h
+++ b/quantum/keymap_extras/keymap_german.h
@@ -4,7 +4,7 @@
#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
index 6a782bcd7b..b66d582a44 100644
--- a/quantum/keymap_extras/keymap_german_ch.h
+++ b/quantum/keymap_extras/keymap_german_ch.h
@@ -4,7 +4,7 @@
#include "keymap.h"
// Alt gr
-#define ALGR(kc) kc | 0x1400
+#define ALGR(kc) RALT(kc)
#define CH_ALGR KC_RALT
// normal characters
diff --git a/quantum/keymap_extras/keymap_nordic.h b/quantum/keymap_extras/keymap_nordic.h
index 3acb8b6983..da5c829757 100644
--- a/quantum/keymap_extras/keymap_nordic.h
+++ b/quantum/keymap_extras/keymap_nordic.h
@@ -4,7 +4,7 @@
#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.h b/quantum/keymap_extras/keymap_norwegian.h
index 018bfeae59..5c4e8c4955 100644
--- a/quantum/keymap_extras/keymap_norwegian.h
+++ 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_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 512d8a374b..4ba568af23 100644
--- a/quantum/keymap_extras/keymap_spanish.h
+++ b/quantum/keymap_extras/keymap_spanish.h
@@ -4,7 +4,7 @@
#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 5c5d951791..00c87afc3e 100644
--- a/quantum/keymap_extras/keymap_uk.h
+++ b/quantum/keymap_extras/keymap_uk.h
@@ -4,7 +4,7 @@
#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/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 a38c13f15b..3174e07390 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -26,32 +26,36 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "util.h"
#include "matrix.h"
-#ifdef MATRIX_HAS_GHOST
-# error "The universal matrix.c file cannot be used for this keyboard."
-#endif
+/* Set 0 if debouncing isn't needed */
#ifndef DEBOUNCING_DELAY
# define DEBOUNCING_DELAY 5
#endif
+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 */
-#if DIODE_DIRECTION == COL2ROW
+
+/* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS];
-#else
-static matrix_col_t matrix[MATRIX_COLS];
+static matrix_row_t matrix_debouncing[MATRIX_ROWS];
+
+#if DIODE_DIRECTION == ROW2COL
+ static matrix_row_t matrix_reversed[MATRIX_COLS];
+ static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
#endif
-static int8_t debouncing_delay = -1;
-#if DIODE_DIRECTION == COL2ROW
-static void toggle_row(uint8_t row);
-static matrix_row_t read_cols(void);
+#if MATRIX_COLS > 16
+ #define SHIFTER 1UL
#else
-static void toggle_col(uint8_t col);
-static matrix_col_t read_rows(void);
+ #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_quantum(void) {
matrix_init_kb();
@@ -80,10 +84,12 @@ __attribute__ ((weak))
void matrix_scan_user(void) {
}
+inline
uint8_t matrix_rows(void) {
return MATRIX_ROWS;
}
+inline
uint8_t matrix_cols(void) {
return MATRIX_COLS;
}
@@ -113,161 +119,179 @@ uint8_t matrix_cols(void) {
// }
void matrix_init(void) {
- /* frees PORTF by setting the JTD bit twice within four cycles */
+ // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
#ifdef __AVR_ATmega32U4__
MCUCR |= _BV(JTD);
MCUCR |= _BV(JTD);
#endif
- /* initializes the I/O pins */
-#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);
+
+ // initialize row and col
+ unselect_rows();
+ init_cols();
+
+ // initialize matrix state: all keys off
+ for (uint8_t i=0; i < MATRIX_ROWS; i++) {
+ matrix[i] = 0;
+ matrix_debouncing[i] = 0;
}
-#endif
+
matrix_init_quantum();
}
+uint8_t matrix_scan(void)
+{
+
#if DIODE_DIRECTION == COL2ROW
-uint8_t matrix_scan(void) {
- static matrix_row_t debouncing_matrix[MATRIX_ROWS];
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- toggle_row(r);
- matrix_row_t state = read_cols();
- if (debouncing_matrix[r] != state) {
- debouncing_matrix[r] = state;
- debouncing_delay = DEBOUNCING_DELAY;
- }
- toggle_row(r);
- }
- if (debouncing_delay >= 0) {
- dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
- --debouncing_delay;
- if (debouncing_delay >= 0) {
- wait_ms(1);
- }
- else {
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- matrix[r] = debouncing_matrix[r];
+ for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
+ select_row(i);
+ 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 = DEBOUNCING_DELAY;
}
+ unselect_rows();
}
- matrix_scan_quantum();
- return 1;
-}
-
-static void toggle_row(uint8_t row) {
- /* PINxn */
- _SFR_IO8((row_pins[row] >> 4)) = _BV(row_pins[row] & 0xF);
-}
-static matrix_row_t read_cols(void) {
- matrix_row_t state = 0;
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- /* PINxn */
- if (!(_SFR_IO8((col_pins[c] >> 4)) & _BV(col_pins[c] & 0xF))) {
- state |= (matrix_row_t)1 << c;
+ if (debouncing) {
+ if (--debouncing) {
+ wait_ms(1);
+ } else {
+ for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
+ matrix[i] = matrix_debouncing[i];
+ }
}
}
- return state;
-}
-
-matrix_row_t matrix_get_row(uint8_t row) {
- return matrix[row];
-}
-
#else
-uint8_t matrix_scan(void) {
- static matrix_col_t debouncing_matrix[MATRIX_COLS];
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- toggle_col(c);
- matrix_col_t state = read_rows();
- if (debouncing_matrix[c] != state) {
- debouncing_matrix[c] = state;
- debouncing_delay = DEBOUNCING_DELAY;
+ for (uint8_t i = 0; i < MATRIX_COLS; i++) {
+ select_row(i);
+ 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 = DEBOUNCING_DELAY;
}
- toggle_col(c);
+ unselect_rows();
}
- if (debouncing_delay >= 0) {
- dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
- --debouncing_delay;
- if (debouncing_delay >= 0) {
+
+ if (debouncing) {
+ if (--debouncing) {
wait_ms(1);
- }
- else {
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- matrix[c] = debouncing_matrix[c];
+ } else {
+ for (uint8_t i = 0; i < MATRIX_COLS; i++) {
+ matrix_reversed[i] = matrix_reversed_debouncing[i];
}
}
}
+ for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
+ matrix_row_t row = 0;
+ for (uint8_t x = 0; x < MATRIX_COLS; x++) {
+ row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
+ }
+ matrix[y] = row;
+ }
+#endif
+
matrix_scan_quantum();
+
return 1;
}
-static void toggle_col(uint8_t col) {
- /* PINxn */
- _SFR_IO8((col_pins[col] >> 4)) = _BV(col_pins[col] & 0xF);
+bool matrix_is_modified(void)
+{
+ if (debouncing) return false;
+ return true;
}
-static matrix_col_t read_rows(void) {
- matrix_col_t state = 0;
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- /* PINxn */
- if (!(_SFR_IO8((row_pins[r] >> 4)) & _BV(row_pins[r] & 0xF))) {
- state |= (matrix_col_t)1 << r;
- }
+inline
+bool matrix_is_on(uint8_t row, uint8_t col)
+{
+ return (matrix[row] & ((matrix_row_t)1<col));
+}
+
+inline
+matrix_row_t matrix_get_row(uint8_t row)
+{
+ return matrix[row];
+}
+
+void matrix_print(void)
+{
+ print("\nr/c 0123456789ABCDEF\n");
+ for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
+ phex(row); print(": ");
+ pbin_reverse16(matrix_get_row(row));
+ print("\n");
}
- return state;
}
-matrix_row_t matrix_get_row(uint8_t row) {
- matrix_row_t state = 0;
- matrix_col_t mask = (matrix_col_t)1 << row;
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- if (matrix[c] & mask) {
- state |= (matrix_row_t)1 << c;
- }
+uint8_t matrix_key_count(void)
+{
+ uint8_t count = 0;
+ for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
+ count += bitpop16(matrix[i]);
}
- return state;
+ return count;
}
+static void init_cols(void)
+{
+#if DIODE_DIRECTION == COL2ROW
+ for(int x = 0; x < MATRIX_COLS; x++) {
+ int pin = col_pins[x];
+#else
+ for(int x = 0; x < MATRIX_ROWS; x++) {
+ int pin = row_pins[x];
#endif
-
-bool matrix_is_modified(void) {
- if (debouncing_delay >= 0) return false;
- return true;
+ _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
+ _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
+ }
}
-bool matrix_is_on(uint8_t row, uint8_t col) {
- return matrix_get_row(row) & (matrix_row_t)1 << col;
-}
+static matrix_row_t read_cols(void)
+{
+ matrix_row_t result = 0;
-void matrix_print(void) {
- dprintln("Human-readable matrix state:");
- for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
- dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
+#if DIODE_DIRECTION == COL2ROW
+ for(int x = 0; x < MATRIX_COLS; x++) {
+ int pin = col_pins[x];
+#else
+ for(int x = 0; x < MATRIX_ROWS; x++) {
+ int pin = row_pins[x];
+#endif
+ result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (SHIFTER << x);
}
+ return result;
}
-uint8_t matrix_key_count(void) {
- uint8_t count = 0;
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- count += bitpop16(matrix_get_row(r));
+static void unselect_rows(void)
+{
+#if DIODE_DIRECTION == COL2ROW
+ for(int x = 0; x < MATRIX_ROWS; x++) {
+ int pin = row_pins[x];
+#else
+ for(int x = 0; x < MATRIX_COLS; x++) {
+ int pin = col_pins[x];
+#endif
+ _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
+ _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
}
- return count;
+}
+
+static void select_row(uint8_t row)
+{
+
+#if DIODE_DIRECTION == COL2ROW
+ int pin = row_pins[row];
+#else
+ int pin = col_pins[row];
+#endif
+ _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF);
+ _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF);
}
diff --git a/quantum/process_keycode/process_midi.c b/quantum/process_keycode/process_midi.c
index d6ab9c6264..8784e64f36 100644
--- a/quantum/process_keycode/process_midi.c
+++ b/quantum/process_keycode/process_midi.c
@@ -1,8 +1,8 @@
#include "process_midi.h"
bool midi_activated = false;
-uint8_t starting_note = 0x0C;
-int offset = 7;
+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) {
@@ -20,42 +20,42 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) {
if (midi_activated) {
if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
if (record->event.pressed) {
- starting_note++; // Change key
+ 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) {
- starting_note--; // Change key
+ 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) {
- offset++; // Change scale
+ 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) {
- offset--; // Change scale
+ midi_offset--; // Change scale
midi_send_cc(&midi_device, 0, 0x7B, 0);
return false;
}
// basic
- // uint8_t note = (starting_note + SCALE[record->event.key.col + offset])+12*(MATRIX_ROWS - record->event.key.row);
+ // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row);
// advanced
- // uint8_t note = (starting_note + record->event.key.col + offset)+12*(MATRIX_ROWS - record->event.key.row);
+ // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row);
// guitar
- uint8_t note = (starting_note + record->event.key.col + offset)+5*(MATRIX_ROWS - record->event.key.row);
+ uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row);
// violin
- // uint8_t note = (starting_note + record->event.key.col + offset)+7*(MATRIX_ROWS - record->event.key.row);
+ // 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, starting_note + SCALE[record->event.key.col], 127);
+ // 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, starting_note + SCALE[record->event.key.col], 127);
+ // 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);
}
diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c
index c8f3ddb900..2d52e47a72 100644
--- a/quantum/process_keycode/process_music.c
+++ b/quantum/process_keycode/process_music.c
@@ -6,6 +6,7 @@ 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;
@@ -77,6 +78,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
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;
@@ -84,12 +86,15 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
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) { // Start playing
+ if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
stop_all_notes();
music_sequence_recording = false;
music_sequence_playing = true;
diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c
index 9b172e1b6c..07de3ecb8f 100644
--- a/quantum/process_keycode/process_tap_dance.c
+++ b/quantum/process_keycode/process_tap_dance.c
@@ -1,90 +1,134 @@
#include "quantum.h"
+#include "action_tapping.h"
-static qk_tap_dance_state_t qk_tap_dance_state;
+static uint16_t last_td;
+static int8_t highest_td = -1;
-static void _process_tap_dance_action_pair (qk_tap_dance_state_t *state,
- uint16_t kc1, uint16_t kc2) {
- uint16_t kc;
+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 == 0)
- return;
-
- kc = (state->count == 1) ? kc1 : kc2;
+ if (state->count == 1) {
+ register_code16 (pair->kc1);
+ } else if (state->count == 2) {
+ register_code16 (pair->kc2);
+ }
+}
- register_code (kc);
- unregister_code (kc);
+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 >= 2) {
- reset_tap_dance (state);
+ if (state->count == 1) {
+ unregister_code16 (pair->kc1);
+ } else if (state->count == 2) {
+ unregister_code16 (pair->kc2);
}
}
-static void _process_tap_dance_action_fn (qk_tap_dance_state_t *state,
- qk_tap_dance_user_fn_t fn)
+static inline void _process_tap_dance_action_fn (qk_tap_dance_state_t *state,
+ void *user_data,
+ qk_tap_dance_user_fn_t fn)
{
- fn(state);
+ if (fn) {
+ fn(state, user_data);
+ }
}
-void process_tap_dance_action (uint16_t keycode)
+static inline void process_tap_dance_action_on_each_tap (qk_tap_dance_action_t *action)
{
- uint16_t idx = keycode - QK_TAP_DANCE;
- qk_tap_dance_action_t action;
-
- action = tap_dance_actions[idx];
+ _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_each_tap);
+}
- switch (action.type) {
- case QK_TAP_DANCE_TYPE_PAIR:
- _process_tap_dance_action_pair (&qk_tap_dance_state,
- action.pair.kc1, action.pair.kc2);
- break;
- case QK_TAP_DANCE_TYPE_FN:
- _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn);
- break;
+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);
+}
- default:
- break;
- }
+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) {
- bool r = true;
+ 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 (qk_tap_dance_state.keycode && qk_tap_dance_state.keycode != keycode) {
- process_tap_dance_action (qk_tap_dance_state.keycode);
- } else {
- r = false;
- }
+ if ((int16_t)idx > highest_td)
+ highest_td = idx;
+ action = &tap_dance_actions[idx];
+ action->state.keycode = keycode;
+ action->state.pressed = record->event.pressed;
if (record->event.pressed) {
- qk_tap_dance_state.keycode = keycode;
- qk_tap_dance_state.timer = timer_read ();
- qk_tap_dance_state.count++;
+ 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 (qk_tap_dance_state.keycode) {
- process_tap_dance_action (qk_tap_dance_state.keycode);
-
- reset_tap_dance (&qk_tap_dance_state);
+ 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 r;
+ return true;
}
void matrix_scan_tap_dance () {
- if (qk_tap_dance_state.keycode && timer_elapsed (qk_tap_dance_state.timer) > TAPPING_TERM) {
- process_tap_dance_action (qk_tap_dance_state.keycode);
+ if (highest_td == -1)
+ return;
- reset_tap_dance (&qk_tap_dance_state);
+ 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) {
- state->keycode = 0;
+ 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
index b9d7c7fcf4..d7b857bdc6 100644
--- a/quantum/process_keycode/process_tap_dance.h
+++ b/quantum/process_keycode/process_tap_dance.h
@@ -11,41 +11,46 @@ 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 enum
-{
- QK_TAP_DANCE_TYPE_PAIR,
- QK_TAP_DANCE_TYPE_FN,
-} qk_tap_dance_type_t;
-
-typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state);
+typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state, void *user_data);
typedef struct
{
- qk_tap_dance_type_t type;
- union {
- struct {
- uint16_t kc1;
- uint16_t kc2;
- } pair;
- qk_tap_dance_user_fn_t fn;
- };
+ 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) { \
- .type = QK_TAP_DANCE_TYPE_PAIR, \
- .pair = { 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(user_fn) { \
- .type = QK_TAP_DANCE_TYPE_FN, \
- .fn = user_fn \
+#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 const qk_tap_dance_action_t tap_dance_actions[];
+extern qk_tap_dance_action_t tap_dance_actions[];
/* To be used internally */
@@ -53,6 +58,9 @@ 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
diff --git a/quantum/process_keycode/process_unicode.c b/quantum/process_keycode/process_unicode.c
index ad5d7f86b7..72c809c30e 100644
--- a/quantum/process_keycode/process_unicode.c
+++ b/quantum/process_keycode/process_unicode.c
@@ -13,45 +13,192 @@ uint16_t hex_to_keycode(uint8_t hex)
}
}
-void set_unicode_mode(uint8_t os_target)
+void set_unicode_input_mode(uint8_t os_target)
{
input_mode = os_target;
}
+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);
+}
+
+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;
- 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);
- break;
- case UC_WIN:
- register_code(KC_LALT);
- register_code(KC_PPLS);
- unregister_code(KC_PPLS);
- break;
+ unicode_input_start();
+ register_hex(unicode);
+ unicode_input_finish();
+ }
+ return true;
+}
+
+#ifdef UCIS_ENABLE
+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;
}
- for(int i = 3; i >= 0; i--) {
- uint8_t digit = ((unicode >> (i*4)) & 0xF);
- register_code(hex_to_keycode(digit));
- unregister_code(hex_to_keycode(digit));
+
+ if (kc) {
+ register_code (kc);
+ unregister_code (kc);
+ wait_ms (UNICODE_TYPE_DELAY);
}
- switch(input_mode) {
- case UC_OSX:
- case UC_WIN:
- unregister_code(KC_LALT);
- break;
- case UC_LNX:
- unregister_code(KC_LCTL);
- unregister_code(KC_LSFT);
+ }
+}
+
+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;
-} \ No newline at end of file
+}
+#endif
diff --git a/quantum/process_keycode/process_unicode.h b/quantum/process_keycode/process_unicode.h
index ca17f8f669..85364e8eb3 100644
--- a/quantum/process_keycode/process_unicode.h
+++ b/quantum/process_keycode/process_unicode.h
@@ -8,10 +8,46 @@
#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);
+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;
+
+struct {
+ uint8_t count;
+ uint16_t codes[UCIS_MAX_SYMBOL_LENGTH];
+ bool in_progress:1;
+} 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)
@@ -119,4 +155,4 @@ bool process_unicode(uint16_t keycode, keyrecord_t *record);
#define UC_TILD UC(0x007E)
#define UC_DEL UC(0x007F)
-#endif \ No newline at end of file
+#endif
diff --git a/quantum/quantum.c b/quantum/quantum.c
index d59bd5a3f8..e3a20f43e0 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -1,5 +1,42 @@
#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;
@@ -15,6 +52,19 @@ 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
@@ -33,18 +83,20 @@ bool process_record_quantum(keyrecord_t *record) {
uint16_t keycode;
#if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
- uint8_t layer;
+ /* 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
- keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
+ 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) {
@@ -74,6 +126,9 @@ bool process_record_quantum(keyrecord_t *record) {
#ifdef UNICODE_ENABLE
process_unicode(keycode, record) &&
#endif
+ #ifdef UCIS_ENABLE
+ process_ucis(keycode, record) &&
+ #endif
true)) {
return false;
}
@@ -83,26 +138,67 @@ bool process_record_quantum(keyrecord_t *record) {
switch(keycode) {
case RESET:
if (record->event.pressed) {
- clear_keyboard();
- #ifdef AUDIO_ENABLE
- stop_all_notes();
- shutdown_user();
- #endif
- wait_ms(250);
- #ifdef ATREUS_ASTAR
- *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
- #endif
- bootloader_jump();
- return false;
+ reset_keyboard();
}
+ return false;
break;
case DEBUG:
if (record->event.pressed) {
print("\nDEBUG: enabled.\n");
debug_enable = true;
- return false;
}
+ 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_UNSWAP_ALT_GUI:
if (record->event.pressed) {
// MAGIC actions (BOOTMAGIC without the boot)
@@ -162,6 +258,12 @@ bool process_record_quantum(keyrecord_t *record) {
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);
@@ -178,6 +280,12 @@ bool process_record_quantum(keyrecord_t *record) {
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);
diff --git a/quantum/quantum.h b/quantum/quantum.h
index 3a0b742028..0c60466495 100644
--- a/quantum/quantum.h
+++ b/quantum/quantum.h
@@ -25,6 +25,7 @@
#include "led.h"
#include "action_util.h"
#include <stdlib.h>
+#include "print.h"
extern uint32_t default_layer_state;
@@ -77,9 +78,14 @@ 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);
diff --git a/quantum/rgblight.c b/quantum/rgblight.c
index c29ffedc38..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 = 0, g = 0, b = 0;
+ // 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) {
@@ -111,42 +121,44 @@ void eeconfig_update_rgblight(uint32_t val) {
eeprom_update_dword(EECONFIG_RGBLIGHT, val);
}
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);
+ 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_update_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_update_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,53 +166,57 @@ void rgblight_init(void) {
}
void rgblight_increase(void) {
- uint8_t mode = 0;
+ 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 = 0;
- 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 = 0;
+ 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;
- }
+ 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) {
- 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.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);
}
@@ -208,298 +224,318 @@ void rgblight_toggle(void) {
rgblight_config.enable ^= 1;
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 {
- rgblight_timer_disable();
- _delay_ms(50);
- rgblight_set();
- }
+ 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_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){
+ 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 64f92523e0..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
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..fb4c45a8dc
--- /dev/null
+++ b/quantum/serial_link/protocol/byte_stuffer.c
@@ -0,0 +1,145 @@
+/*
+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
+
+#define MAX_FRAME_SIZE 1024
+#define NUM_LINKS 2
+
+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..2cc88beb42
--- /dev/null
+++ b/quantum/serial_link/protocol/byte_stuffer.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_BYTE_STUFFER_H
+#define SERIAL_LINK_BYTE_STUFFER_H
+
+#include <stdint.h>
+
+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..f418d11ceb
--- /dev/null
+++ b/quantum/serial_link/protocol/transport.c
@@ -0,0 +1,124 @@
+/*
+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 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..9a052d8809
--- /dev/null
+++ b/quantum/serial_link/protocol/transport.h
@@ -0,0 +1,151 @@
+/*
+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 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 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 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 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.c b/quantum/serial_link/tests/byte_stuffer_tests.c
new file mode 100644
index 0000000000..64b170e8c1
--- /dev/null
+++ b/quantum/serial_link/tests/byte_stuffer_tests.c
@@ -0,0 +1,506 @@
+/*
+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 <cgreen/cgreen.h>
+#include <cgreen/mocks.h>
+#include "serial_link/protocol/byte_stuffer.h"
+#include "serial_link/protocol/byte_stuffer.c"
+#include "serial_link/protocol/frame_validator.h"
+#include "serial_link/protocol/physical.h"
+
+static uint8_t sent_data[MAX_FRAME_SIZE*2];
+static uint16_t sent_data_size;
+
+Describe(ByteStuffer);
+BeforeEach(ByteStuffer) {
+ init_byte_stuffer();
+ sent_data_size = 0;
+}
+AfterEach(ByteStuffer) {}
+
+void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) {
+ mock(data, size);
+}
+
+void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
+ memcpy(sent_data + sent_data_size, data, size);
+ sent_data_size += size;
+}
+
+Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) {
+ never_expect(validator_recv_frame);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) {
+ never_expect(validator_recv_frame);
+ byte_stuffer_recv_byte(0, 0xFF);
+}
+
+Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) {
+ never_expect(validator_recv_frame);
+ byte_stuffer_recv_byte(0, 0x4A);
+}
+
+Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) {
+ never_expect(validator_recv_frame);
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+Ensure(ByteStuffer, receives_single_byte_valid_frame) {
+ uint8_t expected[] = {0x37};
+ expect(validator_recv_frame,
+ when(size, is_equal_to(1)),
+ when(data, is_equal_to_contents_of(expected, 1))
+ );
+ byte_stuffer_recv_byte(0, 2);
+ byte_stuffer_recv_byte(0, 0x37);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+Ensure(ByteStuffer, receives_three_bytes_valid_frame) {
+ uint8_t expected[] = {0x37, 0x99, 0xFF};
+ expect(validator_recv_frame,
+ when(size, is_equal_to(3)),
+ when(data, is_equal_to_contents_of(expected, 3))
+ );
+ 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);
+}
+
+Ensure(ByteStuffer, receives_single_zero_valid_frame) {
+ uint8_t expected[] = {0};
+ expect(validator_recv_frame,
+ when(size, is_equal_to(1)),
+ when(data, is_equal_to_contents_of(expected, 1))
+ );
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 1);
+ byte_stuffer_recv_byte(0, 0);
+}
+
+Ensure(ByteStuffer, receives_valid_frame_with_zeroes) {
+ uint8_t expected[] = {5, 0, 3, 0};
+ expect(validator_recv_frame,
+ when(size, is_equal_to(4)),
+ when(data, is_equal_to_contents_of(expected, 4))
+ );
+ 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);
+}
+
+Ensure(ByteStuffer, receives_two_valid_frames) {
+ uint8_t expected1[] = {5, 0};
+ uint8_t expected2[] = {3};
+ expect(validator_recv_frame,
+ when(size, is_equal_to(2)),
+ when(data, is_equal_to_contents_of(expected1, 2))
+ );
+ expect(validator_recv_frame,
+ when(size, is_equal_to(1)),
+ when(data, is_equal_to_contents_of(expected2, 1))
+ );
+ 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);
+}
+
+Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) {
+ uint8_t expected[] = {5, 7};
+ expect(validator_recv_frame,
+ when(size, is_equal_to(2)),
+ when(data, is_equal_to_contents_of(expected, 2))
+ );
+ 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);
+}
+
+Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) {
+ uint8_t expected[] = {5, 7};
+ expect(validator_recv_frame,
+ when(size, is_equal_to(2)),
+ when(data, is_equal_to_contents_of(expected, 2))
+ );
+ 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);
+}
+
+Ensure(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(validator_recv_frame,
+ when(size, is_equal_to(254)),
+ when(data, is_equal_to_contents_of(expected, 254))
+ );
+ 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);
+}
+
+Ensure(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(validator_recv_frame,
+ when(size, is_equal_to(255)),
+ when(data, is_equal_to_contents_of(expected, 255))
+ );
+ 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);
+}
+
+Ensure(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(validator_recv_frame,
+ when(size, is_equal_to(255)),
+ when(data, is_equal_to_contents_of(expected, 255))
+ );
+ 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);
+}
+
+Ensure(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(validator_recv_frame,
+ when(size, is_equal_to(515)),
+ when(data, is_equal_to_contents_of(expected, 510))
+ );
+ 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);
+}
+
+Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) {
+ uint8_t expected[MAX_FRAME_SIZE] = {};
+ expect(validator_recv_frame,
+ when(size, is_equal_to(MAX_FRAME_SIZE)),
+ when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE))
+ );
+ 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);
+}
+
+Ensure(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) {
+ uint8_t expected[1] = {0};
+ never_expect(validator_recv_frame);
+ 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);
+}
+
+Ensure(ByteStuffer, received_frame_is_aborted_when_its_too_long) {
+ uint8_t expected[1] = {1};
+ expect(validator_recv_frame,
+ when(size, is_equal_to(1)),
+ when(data, is_equal_to_contents_of(expected, 1))
+ );
+ 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);
+}
+
+Ensure(ByteStuffer, does_nothing_when_sending_zero_size_frame) {
+ assert_that(sent_data_size, is_equal_to(0));
+ byte_stuffer_send_frame(0, NULL, 0);
+}
+
+Ensure(ByteStuffer, send_one_byte_frame) {
+ uint8_t data[] = {5};
+ byte_stuffer_send_frame(1, data, 1);
+ uint8_t expected[] = {2, 5, 0};
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(ByteStuffer, sends_two_byte_frame) {
+ uint8_t data[] = {5, 0x77};
+ byte_stuffer_send_frame(0, data, 2);
+ uint8_t expected[] = {3, 5, 0x77, 0};
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(ByteStuffer, sends_one_byte_frame_with_zero) {
+ uint8_t data[] = {0};
+ byte_stuffer_send_frame(0, data, 1);
+ uint8_t expected[] = {1, 1, 0};
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(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};
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(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};
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(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};
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(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};
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(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};
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(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;
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(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;
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(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[255] = 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;
+ assert_that(sent_data_size, is_equal_to(sizeof(expected)));
+ assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
+}
+
+Ensure(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(validator_recv_frame,
+ when(size, is_equal_to(sizeof(original_data))),
+ when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
+ );
+ int i;
+ for(i=0;i<sent_data_size;i++) {
+ byte_stuffer_recv_byte(1, sent_data[i]);
+ }
+}
+
+Ensure(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(validator_recv_frame,
+ when(size, is_equal_to(sizeof(original_data))),
+ when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
+ );
+ int i;
+ for(i=0;i<sent_data_size;i++) {
+ byte_stuffer_recv_byte(0, sent_data[i]);
+ }
+}
+
+Ensure(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(validator_recv_frame,
+ when(size, is_equal_to(sizeof(original_data))),
+ when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
+ );
+ for(i=0;i<sent_data_size;i++) {
+ byte_stuffer_recv_byte(1, sent_data[i]);
+ }
+}
+
+Ensure(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(validator_recv_frame,
+ when(size, is_equal_to(sizeof(original_data))),
+ when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
+ );
+ for(i=0;i<sent_data_size;i++) {
+ byte_stuffer_recv_byte(1, sent_data[i]);
+ }
+}
+
+Ensure(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(validator_recv_frame,
+ when(size, is_equal_to(sizeof(original_data))),
+ when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
+ );
+ for(i=0;i<sent_data_size;i++) {
+ byte_stuffer_recv_byte(1, sent_data[i]);
+ }
+}
diff --git a/quantum/serial_link/tests/frame_router_tests.c b/quantum/serial_link/tests/frame_router_tests.c
new file mode 100644
index 0000000000..6c806fa939
--- /dev/null
+++ b/quantum/serial_link/tests/frame_router_tests.c
@@ -0,0 +1,231 @@
+/*
+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 <cgreen/cgreen.h>
+#include <cgreen/mocks.h>
+#include "serial_link/protocol/byte_stuffer.c"
+#include "serial_link/protocol/frame_validator.c"
+#include "serial_link/protocol/frame_router.c"
+#include "serial_link/protocol/transport.h"
+
+static uint8_t received_data[256];
+static uint16_t received_data_size;
+
+typedef struct {
+ uint8_t sent_data[256];
+ uint16_t sent_data_size;
+} receive_buffer_t;
+
+typedef struct {
+ receive_buffer_t send_buffers[2];
+} router_buffer_t;
+
+router_buffer_t router_buffers[8];
+
+router_buffer_t* current_router_buffer;
+
+
+Describe(FrameRouter);
+BeforeEach(FrameRouter) {
+ init_byte_stuffer();
+ memset(router_buffers, 0, sizeof(router_buffers));
+ current_router_buffer = 0;
+}
+AfterEach(FrameRouter) {}
+
+typedef struct {
+ uint32_t data;
+ uint8_t extra[16];
+} frame_buffer_t;
+
+
+void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
+ receive_buffer_t* buffer = &current_router_buffer->send_buffers[link];
+ memcpy(buffer->sent_data + buffer->sent_data_size, data, size);
+ buffer->sent_data_size += size;
+}
+
+static 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]);
+ }
+}
+
+static void activate_router(uint8_t num) {
+ current_router_buffer = router_buffers + num;
+ router_set_master(num==0);
+}
+
+static 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].sent_data,
+ router_buffers[from].send_buffers[UP_LINK].sent_data_size);
+ }
+ else if(to > from) {
+ receive_data(UP_LINK,
+ router_buffers[from].send_buffers[DOWN_LINK].sent_data,
+ router_buffers[from].send_buffers[DOWN_LINK].sent_data_size);
+ }
+}
+
+void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) {
+ mock(from, data, size);
+}
+
+
+Ensure(FrameRouter, master_broadcast_is_received_by_everyone) {
+ frame_buffer_t data;
+ data.data = 0xAB7055BB;
+ activate_router(0);
+ router_send_frame(0xFF, (uint8_t*)&data, 4);
+ assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+
+ expect(transport_recv_frame,
+ when(from, is_equal_to(0)),
+ when(size, is_equal_to(4)),
+ when(data, is_equal_to_contents_of(&data.data, 4))
+ );
+ simulate_transport(0, 1);
+ assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+
+ expect(transport_recv_frame,
+ when(from, is_equal_to(0)),
+ when(size, is_equal_to(4)),
+ when(data, is_equal_to_contents_of(&data.data, 4))
+ );
+ simulate_transport(1, 2);
+ assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+}
+
+Ensure(FrameRouter, master_send_is_received_by_targets) {
+ frame_buffer_t data;
+ data.data = 0xAB7055BB;
+ activate_router(0);
+ router_send_frame((1 << 1) | (1 << 2), (uint8_t*)&data, 4);
+ assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+
+ simulate_transport(0, 1);
+ assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+
+ expect(transport_recv_frame,
+ when(from, is_equal_to(0)),
+ when(size, is_equal_to(4)),
+ when(data, is_equal_to_contents_of(&data.data, 4))
+ );
+ simulate_transport(1, 2);
+ assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+
+ expect(transport_recv_frame,
+ when(from, is_equal_to(0)),
+ when(size, is_equal_to(4)),
+ when(data, is_equal_to_contents_of(&data.data, 4))
+ );
+ simulate_transport(2, 3);
+ assert_that(router_buffers[3].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[3].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+}
+
+Ensure(FrameRouter, first_link_sends_to_master) {
+ frame_buffer_t data;
+ data.data = 0xAB7055BB;
+ activate_router(1);
+ router_send_frame(0, (uint8_t*)&data, 4);
+ assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0));
+
+ expect(transport_recv_frame,
+ when(from, is_equal_to(1)),
+ when(size, is_equal_to(4)),
+ when(data, is_equal_to_contents_of(&data.data, 4))
+ );
+ simulate_transport(1, 0);
+ assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0));
+ assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+}
+
+Ensure(FrameRouter, second_link_sends_to_master) {
+ frame_buffer_t data;
+ data.data = 0xAB7055BB;
+ activate_router(2);
+ router_send_frame(0, (uint8_t*)&data, 4);
+ assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0));
+
+ simulate_transport(2, 1);
+ assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0));
+
+ expect(transport_recv_frame,
+ when(from, is_equal_to(2)),
+ when(size, is_equal_to(4)),
+ when(data, is_equal_to_contents_of(&data.data, 4))
+ );
+ simulate_transport(1, 0);
+ assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0));
+ assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+}
+
+Ensure(FrameRouter, master_sends_to_master_does_nothing) {
+ frame_buffer_t data;
+ data.data = 0xAB7055BB;
+ activate_router(0);
+ router_send_frame(0, (uint8_t*)&data, 4);
+ assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+ assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0));
+}
+
+Ensure(FrameRouter, link_sends_to_other_link_does_nothing) {
+ frame_buffer_t data;
+ data.data = 0xAB7055BB;
+ activate_router(1);
+ router_send_frame(2, (uint8_t*)&data, 4);
+ assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+ assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0));
+}
+
+Ensure(FrameRouter, master_receives_on_uplink_does_nothing) {
+ frame_buffer_t data;
+ data.data = 0xAB7055BB;
+ activate_router(1);
+ router_send_frame(0, (uint8_t*)&data, 4);
+ assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0));
+
+ never_expect(transport_recv_frame);
+ activate_router(0);
+ receive_data(UP_LINK,
+ router_buffers[1].send_buffers[UP_LINK].sent_data,
+ router_buffers[1].send_buffers[UP_LINK].sent_data_size);
+ assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+ assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0));
+}
diff --git a/quantum/serial_link/tests/frame_validator_tests.c b/quantum/serial_link/tests/frame_validator_tests.c
new file mode 100644
index 0000000000..d20947e2c9
--- /dev/null
+++ b/quantum/serial_link/tests/frame_validator_tests.c
@@ -0,0 +1,101 @@
+/*
+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 <cgreen/cgreen.h>
+#include <cgreen/mocks.h>
+#include "serial_link/protocol/frame_validator.c"
+
+void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size) {
+ mock(data, size);
+}
+
+void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) {
+ mock(data, size);
+}
+
+Describe(FrameValidator);
+BeforeEach(FrameValidator) {}
+AfterEach(FrameValidator) {}
+
+Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) {
+ never_expect(route_incoming_frame);
+ 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);
+}
+
+Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) {
+ uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3};
+ expect(route_incoming_frame,
+ when(size, is_equal_to(1)),
+ when(data, is_equal_to_contents_of(data, 1))
+ );
+ validator_recv_frame(0, data, 5);
+}
+
+Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) {
+ uint8_t data[] = {0x44, 0, 0, 0, 0};
+ never_expect(route_incoming_frame);
+ validator_recv_frame(1, data, 5);
+}
+
+Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) {
+ uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA};
+ expect(route_incoming_frame,
+ when(size, is_equal_to(4)),
+ when(data, is_equal_to_contents_of(data, 4))
+ );
+ validator_recv_frame(1, data, 8);
+}
+
+Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) {
+ uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47};
+ expect(route_incoming_frame,
+ when(size, is_equal_to(5)),
+ when(data, is_equal_to_contents_of(data, 5))
+ );
+ validator_recv_frame(0, data, 9);
+}
+
+Ensure(FrameValidator, sends_one_byte_with_correct_crc) {
+ uint8_t original[] = {0x44, 0, 0, 0, 0};
+ uint8_t expected[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3};
+ expect(byte_stuffer_send_frame,
+ when(size, is_equal_to(sizeof(expected))),
+ when(data, is_equal_to_contents_of(expected, sizeof(expected)))
+ );
+ validator_send_frame(0, original, 1);
+}
+
+Ensure(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(byte_stuffer_send_frame,
+ when(size, is_equal_to(sizeof(expected))),
+ when(data, is_equal_to_contents_of(expected, sizeof(expected)))
+ );
+ validator_send_frame(0, original, 5);
+}
diff --git a/quantum/serial_link/tests/transport_tests.c b/quantum/serial_link/tests/transport_tests.c
new file mode 100644
index 0000000000..358e1b9fd4
--- /dev/null
+++ b/quantum/serial_link/tests/transport_tests.c
@@ -0,0 +1,168 @@
+/*
+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 <cgreen/cgreen.h>
+#include <cgreen/mocks.h>
+#include "serial_link/protocol/transport.c"
+#include "serial_link/protocol/triple_buffered_object.c"
+
+void signal_data_written(void) {
+ mock();
+}
+
+static uint8_t sent_data[2048];
+static uint16_t sent_data_size;
+
+void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) {
+ mock(destination);
+ memcpy(sent_data + sent_data_size, data, size);
+ sent_data_size += size;
+}
+
+typedef struct {
+ uint32_t test;
+} test_object1_t;
+
+typedef struct {
+ uint32_t test1;
+ uint32_t test2;
+} test_object2_t;
+
+MASTER_TO_ALL_SLAVES_OBJECT(master_to_slave, test_object1_t);
+MASTER_TO_SINGLE_SLAVE_OBJECT(master_to_single_slave, test_object1_t);
+SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t);
+
+static remote_object_t* test_remote_objects[] = {
+ REMOTE_OBJECT(master_to_slave),
+ REMOTE_OBJECT(master_to_single_slave),
+ REMOTE_OBJECT(slave_to_master),
+};
+
+Describe(Transport);
+BeforeEach(Transport) {
+ add_remote_objects(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*));
+ sent_data_size = 0;
+}
+AfterEach(Transport) {}
+
+Ensure(Transport, write_to_local_signals_an_event) {
+ begin_write_master_to_slave();
+ expect(signal_data_written);
+ end_write_master_to_slave();
+ begin_write_slave_to_master();
+ expect(signal_data_written);
+ end_write_slave_to_master();
+ begin_write_master_to_single_slave(1);
+ expect(signal_data_written);
+ end_write_master_to_single_slave(1);
+}
+
+Ensure(Transport, writes_from_master_to_all_slaves) {
+ update_transport();
+ test_object1_t* obj = begin_write_master_to_slave();
+ obj->test = 5;
+ expect(signal_data_written);
+ end_write_master_to_slave();
+ expect(router_send_frame,
+ when(destination, is_equal_to(0xFF)));
+ update_transport();
+ transport_recv_frame(0, sent_data, sent_data_size);
+ test_object1_t* obj2 = read_master_to_slave();
+ assert_that(obj2, is_not_equal_to(NULL));
+ assert_that(obj2->test, is_equal_to(5));
+}
+
+Ensure(Transport, writes_from_slave_to_master) {
+ update_transport();
+ test_object1_t* obj = begin_write_slave_to_master();
+ obj->test = 7;
+ expect(signal_data_written);
+ end_write_slave_to_master();
+ expect(router_send_frame,
+ when(destination, is_equal_to(0)));
+ update_transport();
+ transport_recv_frame(3, sent_data, sent_data_size);
+ test_object1_t* obj2 = read_slave_to_master(2);
+ assert_that(read_slave_to_master(0), is_equal_to(NULL));
+ assert_that(obj2, is_not_equal_to(NULL));
+ assert_that(obj2->test, is_equal_to(7));
+}
+
+Ensure(Transport, writes_from_master_to_single_slave) {
+ update_transport();
+ test_object1_t* obj = begin_write_master_to_single_slave(3);
+ obj->test = 7;
+ expect(signal_data_written);
+ end_write_master_to_single_slave(3);
+ expect(router_send_frame,
+ when(destination, is_equal_to(4)));
+ update_transport();
+ transport_recv_frame(0, sent_data, sent_data_size);
+ test_object1_t* obj2 = read_master_to_single_slave();
+ assert_that(obj2, is_not_equal_to(NULL));
+ assert_that(obj2->test, is_equal_to(7));
+}
+
+Ensure(Transport, ignores_object_with_invalid_id) {
+ update_transport();
+ test_object1_t* obj = begin_write_master_to_single_slave(3);
+ obj->test = 7;
+ expect(signal_data_written);
+ end_write_master_to_single_slave(3);
+ expect(router_send_frame,
+ when(destination, is_equal_to(4)));
+ update_transport();
+ sent_data[sent_data_size - 1] = 44;
+ transport_recv_frame(0, sent_data, sent_data_size);
+ test_object1_t* obj2 = read_master_to_single_slave();
+ assert_that(obj2, is_equal_to(NULL));
+}
+
+Ensure(Transport, ignores_object_with_size_too_small) {
+ update_transport();
+ test_object1_t* obj = begin_write_master_to_slave();
+ obj->test = 7;
+ expect(signal_data_written);
+ end_write_master_to_slave();
+ expect(router_send_frame);
+ update_transport();
+ sent_data[sent_data_size - 2] = 0;
+ transport_recv_frame(0, sent_data, sent_data_size - 1);
+ test_object1_t* obj2 = read_master_to_slave();
+ assert_that(obj2, is_equal_to(NULL));
+}
+
+Ensure(Transport, ignores_object_with_size_too_big) {
+ update_transport();
+ test_object1_t* obj = begin_write_master_to_slave();
+ obj->test = 7;
+ expect(signal_data_written);
+ end_write_master_to_slave();
+ expect(router_send_frame);
+ update_transport();
+ sent_data[sent_data_size + 21] = 0;
+ transport_recv_frame(0, sent_data, sent_data_size + 22);
+ test_object1_t* obj2 = read_master_to_slave();
+ assert_that(obj2, is_equal_to(NULL));
+}
diff --git a/quantum/serial_link/tests/triple_buffered_object_tests.c b/quantum/serial_link/tests/triple_buffered_object_tests.c
new file mode 100644
index 0000000000..6f7c82b468
--- /dev/null
+++ b/quantum/serial_link/tests/triple_buffered_object_tests.c
@@ -0,0 +1,82 @@
+/*
+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 <cgreen/cgreen.h>
+#include "serial_link/protocol/triple_buffered_object.c"
+
+typedef struct {
+ uint8_t state;
+ uint32_t buffer[3];
+}test_object_t;
+
+test_object_t test_object;
+
+Describe(TripleBufferedObject);
+BeforeEach(TripleBufferedObject) {
+ triple_buffer_init((triple_buffer_object_t*)&test_object);
+}
+AfterEach(TripleBufferedObject) {}
+
+
+Ensure(TripleBufferedObject, writes_and_reads_object) {
+ *triple_buffer_begin_write(&test_object) = 0x3456ABCC;
+ triple_buffer_end_write(&test_object);
+ assert_that(*triple_buffer_read(&test_object), is_equal_to(0x3456ABCC));
+}
+
+Ensure(TripleBufferedObject, does_not_read_empty) {
+ assert_that(triple_buffer_read(&test_object), is_equal_to(NULL));
+}
+
+Ensure(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);
+ assert_that(*triple_buffer_read(&test_object), is_equal_to(0x44778899));
+}
+
+Ensure(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);
+ assert_that(*read, is_equal_to(1));
+ assert_that(*triple_buffer_read(&test_object), is_equal_to(2));
+ assert_that(triple_buffer_read(&test_object), is_equal_to(NULL));
+}
+
+Ensure(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);
+ assert_that(*read, is_equal_to(1));
+ assert_that(*triple_buffer_read(&test_object), is_equal_to(3));
+ assert_that(triple_buffer_read(&test_object), is_equal_to(NULL));
+}
diff --git a/quantum/template/template.c b/quantum/template/template.c
index dcc4b0a221..5ef349583c 100644
--- a/quantum/template/template.c
+++ b/quantum/template/template.c
@@ -18,7 +18,7 @@ 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
- return process_action_user(record);
+ return process_record_user(keycode, record);
}
void led_set_kb(uint8_t usb_led) {
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