From 9b46f4021e6c96ed4697924a90a8b2b6dd0b0acc Mon Sep 17 00:00:00 2001
From: Jack Humbert <jack.humb@gmail.com>
Date: Tue, 25 Aug 2015 17:06:38 -0400
Subject: [PATCH] midi on planck

---
 keyboard/planck/Makefile                      |  2 +-
 keyboard/planck/keymap_midi.c                 | 31 +++++++++++++++++--
 keyboard/planck/keymap_midi.h                 |  5 +++
 keyboard/planck/keymaps/keymap_lock.c         | 16 +++++-----
 .../extended_keymaps/extended_keymap_lock.c   |  2 +-
 keyboard/preonic/keymap_midi.c                |  4 +--
 6 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/keyboard/planck/Makefile b/keyboard/planck/Makefile
index 8c357f4b0..580dc25b2 100644
--- a/keyboard/planck/Makefile
+++ b/keyboard/planck/Makefile
@@ -124,7 +124,7 @@ COMMAND_ENABLE = yes    # Commands for debug and configuration
 #SLEEP_LED_ENABLE = yes  # Breathing sleep LED during USB suspend
 # NKRO_ENABLE = yes		# USB Nkey Rollover - not yet supported in LUFA
 BACKLIGHT_ENABLE = yes  # Enable keyboard backlight functionality
-# MIDI_ENABLE = YES 		# MIDI controls
+MIDI_ENABLE = YES 		# MIDI controls
 BACKLIGHT_ENABLE = yes
 
 ifdef MIDI_ENABLE
diff --git a/keyboard/planck/keymap_midi.c b/keyboard/planck/keymap_midi.c
index 81f84bcd7..a8913dc1d 100644
--- a/keyboard/planck/keymap_midi.c
+++ b/keyboard/planck/keymap_midi.c
@@ -19,11 +19,38 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include "keymap_midi.h"
 #include <lufa.h>
 
+uint8_t starting_note = 0x0C;
+
 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
 {
+	if (id != 0) {
+	    if (record->event.pressed) {
+	    	midi_send_noteon(&midi_device, opt, (id & 0xFF), 127);
+	    } else {
+	        midi_send_noteoff(&midi_device, opt, (id & 0xFF), 127);
+	    }
+	}
+
+    if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
+        starting_note++;
+        midi_send_cc(&midi_device, 0, 0x7B, 0);
+        midi_send_cc(&midi_device, 1, 0x7B, 0);
+        midi_send_cc(&midi_device, 2, 0x7B, 0);
+        midi_send_cc(&midi_device, 3, 0x7B, 0);
+        midi_send_cc(&midi_device, 4, 0x7B, 0);
+    }
+    if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
+        starting_note--;
+        midi_send_cc(&midi_device, 0, 0x7B, 0);
+        midi_send_cc(&midi_device, 1, 0x7B, 0);
+        midi_send_cc(&midi_device, 2, 0x7B, 0);
+        midi_send_cc(&midi_device, 3, 0x7B, 0);
+        midi_send_cc(&midi_device, 4, 0x7B, 0);
+    }
+
     if (record->event.pressed) {
-    	midi_send_noteon(&midi_device, opt, (id & 0xFF), 127);
+    	midi_send_noteon(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
     } else {
-        midi_send_noteoff(&midi_device, opt, (id & 0xFF), 127);
+        midi_send_noteoff(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
     }
 }
\ No newline at end of file
diff --git a/keyboard/planck/keymap_midi.h b/keyboard/planck/keymap_midi.h
index b4c874eee..02bf35c24 100644
--- a/keyboard/planck/keymap_midi.h
+++ b/keyboard/planck/keymap_midi.h
@@ -18,8 +18,13 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #ifndef KEYMAP_MIDI_H
 #define KEYMAP_MIDI_H
 
+#define MIDI 0x6000
+#define MIDI12 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000
+
 #define CHNL(note, channel) (note + (channel << 8))
 
+#define SCALE (int []){ 0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19, 21, 23, 24, 26, 28, 29, 31, 33, 35, 36}
+
 #define N_CN1  (0x600C + (12 * -1) + 0 )
 #define N_CN1S (0x600C + (12 * -1) + 1 )
 #define N_DN1F (0x600C + (12 * -1) + 1 )
diff --git a/keyboard/planck/keymaps/keymap_lock.c b/keyboard/planck/keymaps/keymap_lock.c
index 689670cca..98a88eb39 100644
--- a/keyboard/planck/keymaps/keymap_lock.c
+++ b/keyboard/planck/keymaps/keymap_lock.c
@@ -35,11 +35,11 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
   {KC_LSFT, KC_Z,    KC_X,    KC_C,    KC_V,    KC_B,    KC_N,    KC_KP_1,    KC_KP_2,    KC_KP_3,  KC_SLSH, KC_ENT},
   {KC_TRNS, KC_LCTL, KC_LALT, KC_LGUI, KC_TRNS,    KC_SPC,   KC_SPC, KC_KP_0,   KC_LEFT, KC_DOWN, KC_UP,  KC_RGHT}
 },
-[5] = { /* MIDI */
-  {CHNL(N_C5, 0), CHNL(N_D5, 0),CHNL(N_E5, 0),CHNL(N_F5, 0),CHNL(N_G5, 0),CHNL(N_A5, 0),CHNL(N_B5, 0),CHNL(N_C6, 0), CHNL(N_D6, 0), CHNL(N_E6, 0),  CHNL(N_F6, 0),CHNL(N_G6, 0)},
-  {CHNL(N_C5, 2), CHNL(N_D5, 2),CHNL(N_E5, 2),CHNL(N_F5, 2),CHNL(N_G5, 2),CHNL(N_A5, 2),CHNL(N_B5, 2),CHNL(N_C6, 2), CHNL(N_D6, 2), CHNL(N_E6, 2),  CHNL(N_F6, 2),CHNL(N_G6, 2)},
-  {CHNL(N_C5, 4), CHNL(N_D5, 4),CHNL(N_E5, 4),CHNL(N_F5, 4),CHNL(N_G5, 4),CHNL(N_A5, 4),CHNL(N_B5, 4),CHNL(N_C6, 4), CHNL(N_D6, 4), CHNL(N_E6, 4),  CHNL(N_F6, 4),CHNL(N_G6, 4)},
-  {KC_TRNS, KC_LCTL, KC_LALT, KC_LGUI, KC_TRNS,    KC_SPC,   KC_SPC, KC_KP_0,   KC_LEFT, KC_DOWN, KC_UP,  KC_RGHT}
+[5] = {
+  { MIDI12 },
+  { MIDI12 },
+  { MIDI12 },
+  {M(0), KC_LCTL, KC_LALT, KC_LGUI, FUNC(2),    KC_SPC,   KC_SPC,    FUNC(1),   MIDI, MIDI, MIDI,  MIDI}
 }
 };
 
@@ -60,11 +60,13 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
         if (record->event.pressed) {
           // register_code(KC_RSFT);
           backlight_set(BACKLIGHT_LEVELS);
-          layer_on(5);
+          default_layer_and(0); 
+          default_layer_or((1<<5));
         } else {
           // unregister_code(KC_RSFT);
           backlight_set(0);
-          layer_clear();
+          default_layer_and(0); 
+          default_layer_or(0);
         }
         break;
       } 
diff --git a/keyboard/preonic/extended_keymaps/extended_keymap_lock.c b/keyboard/preonic/extended_keymaps/extended_keymap_lock.c
index 70d4551a7..8edf8a283 100644
--- a/keyboard/preonic/extended_keymaps/extended_keymap_lock.c
+++ b/keyboard/preonic/extended_keymaps/extended_keymap_lock.c
@@ -46,7 +46,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
   { MIDI12 },
   { MIDI12 },
   { MIDI12 },
-  {M(0), KC_LCTL, KC_LALT, KC_LGUI, FUNC(2),    KC_SPC,   KC_SPC,    FUNC(1),   KC_LEFT, KC_DOWN, KC_UP,  KC_RGHT}
+  {M(0), KC_LCTL, KC_LALT, KC_LGUI, FUNC(2),    KC_SPC,   KC_SPC,    FUNC(1),   MIDI, MIDI, MIDI,  MIDI}
 }
 
 };
diff --git a/keyboard/preonic/keymap_midi.c b/keyboard/preonic/keymap_midi.c
index ec380fd9c..c7937bb9b 100644
--- a/keyboard/preonic/keymap_midi.c
+++ b/keyboard/preonic/keymap_midi.c
@@ -31,7 +31,7 @@ void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
 	    }
 	}
 
-    if (record->event.key.col == 11 && record->event.key.row == 4) {
+    if (record->event.key.col == 11 && record->event.key.row == 4 && record->event.pressed) {
         starting_note++;
         midi_send_cc(&midi_device, 0, 0x7B, 0);
         midi_send_cc(&midi_device, 1, 0x7B, 0);
@@ -39,7 +39,7 @@ void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
         midi_send_cc(&midi_device, 3, 0x7B, 0);
         midi_send_cc(&midi_device, 4, 0x7B, 0);
     }
-    if (record->event.key.col == 8 && record->event.key.row == 4) {
+    if (record->event.key.col == 8 && record->event.key.row == 4 && record->event.pressed) {
         starting_note--;
         midi_send_cc(&midi_device, 0, 0x7B, 0);
         midi_send_cc(&midi_device, 1, 0x7B, 0);
-- 
2.39.5