diff options
Diffstat (limited to 'tmk_core')
| -rw-r--r-- | tmk_core/common/host_driver.h | 9 | ||||
| -rw-r--r-- | tmk_core/protocol/lufa/lufa.c | 217 | ||||
| -rw-r--r-- | tmk_core/protocol/lufa/lufa.h | 14 |
3 files changed, 224 insertions, 16 deletions
diff --git a/tmk_core/common/host_driver.h b/tmk_core/common/host_driver.h index edb9e5dd9..588d1c0be 100644 --- a/tmk_core/common/host_driver.h +++ b/tmk_core/common/host_driver.h | |||
| @@ -20,7 +20,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 20 | 20 | ||
| 21 | #include <stdint.h> | 21 | #include <stdint.h> |
| 22 | #include "report.h" | 22 | #include "report.h" |
| 23 | 23 | #ifdef MIDI_ENABLE | |
| 24 | #include "midi.h" | ||
| 25 | #endif | ||
| 24 | 26 | ||
| 25 | typedef struct { | 27 | typedef struct { |
| 26 | uint8_t (*keyboard_leds)(void); | 28 | uint8_t (*keyboard_leds)(void); |
| @@ -28,6 +30,11 @@ typedef struct { | |||
| 28 | void (*send_mouse)(report_mouse_t *); | 30 | void (*send_mouse)(report_mouse_t *); |
| 29 | void (*send_system)(uint16_t); | 31 | void (*send_system)(uint16_t); |
| 30 | void (*send_consumer)(uint16_t); | 32 | void (*send_consumer)(uint16_t); |
| 33 | #ifdef MIDI_ENABLE | ||
| 34 | void (*usb_send_func)(MidiDevice *, uint16_t, uint8_t, uint8_t, uint8_t); | ||
| 35 | void (*usb_get_midi)(MidiDevice *); | ||
| 36 | void (*midi_usb_init)(MidiDevice *); | ||
| 37 | #endif | ||
| 31 | } host_driver_t; | 38 | } host_driver_t; |
| 32 | 39 | ||
| 33 | #endif | 40 | #endif |
diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 01c0e45b0..a33a16599 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c | |||
| @@ -51,6 +51,7 @@ | |||
| 51 | 51 | ||
| 52 | #include "descriptor.h" | 52 | #include "descriptor.h" |
| 53 | #include "lufa.h" | 53 | #include "lufa.h" |
| 54 | #include "quantum.h" | ||
| 54 | 55 | ||
| 55 | #ifdef NKRO_ENABLE | 56 | #ifdef NKRO_ENABLE |
| 56 | #include "keycode_config.h" | 57 | #include "keycode_config.h" |
| @@ -71,6 +72,10 @@ | |||
| 71 | #include "virtser.h" | 72 | #include "virtser.h" |
| 72 | #endif | 73 | #endif |
| 73 | 74 | ||
| 75 | #ifdef RGB_MIDI | ||
| 76 | #include "rgblight.h" | ||
| 77 | #endif | ||
| 78 | |||
| 74 | uint8_t keyboard_idle = 0; | 79 | uint8_t keyboard_idle = 0; |
| 75 | /* 0: Boot Protocol, 1: Report Protocol(default) */ | 80 | /* 0: Boot Protocol, 1: Report Protocol(default) */ |
| 76 | uint8_t keyboard_protocol = 1; | 81 | uint8_t keyboard_protocol = 1; |
| @@ -79,9 +84,9 @@ static uint8_t keyboard_led_stats = 0; | |||
| 79 | static report_keyboard_t keyboard_report_sent; | 84 | static report_keyboard_t keyboard_report_sent; |
| 80 | 85 | ||
| 81 | #ifdef MIDI_ENABLE | 86 | #ifdef MIDI_ENABLE |
| 82 | void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2); | 87 | static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2); |
| 83 | void usb_get_midi(MidiDevice * device); | 88 | static void usb_get_midi(MidiDevice * device); |
| 84 | void midi_usb_init(MidiDevice * device); | 89 | static void midi_usb_init(MidiDevice * device); |
| 85 | #endif | 90 | #endif |
| 86 | 91 | ||
| 87 | /* Host driver */ | 92 | /* Host driver */ |
| @@ -709,7 +714,7 @@ int8_t sendchar(uint8_t c) | |||
| 709 | ******************************************************************************/ | 714 | ******************************************************************************/ |
| 710 | 715 | ||
| 711 | #ifdef MIDI_ENABLE | 716 | #ifdef MIDI_ENABLE |
| 712 | void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { | 717 | static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { |
| 713 | MIDI_EventPacket_t event; | 718 | MIDI_EventPacket_t event; |
| 714 | event.Data1 = byte0; | 719 | event.Data1 = byte0; |
| 715 | event.Data2 = byte1; | 720 | event.Data2 = byte1; |
| @@ -769,7 +774,7 @@ void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byt | |||
| 769 | USB_USBTask(); | 774 | USB_USBTask(); |
| 770 | } | 775 | } |
| 771 | 776 | ||
| 772 | void usb_get_midi(MidiDevice * device) { | 777 | static void usb_get_midi(MidiDevice * device) { |
| 773 | MIDI_EventPacket_t event; | 778 | MIDI_EventPacket_t event; |
| 774 | while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) { | 779 | while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) { |
| 775 | 780 | ||
| @@ -799,12 +804,12 @@ void usb_get_midi(MidiDevice * device) { | |||
| 799 | USB_USBTask(); | 804 | USB_USBTask(); |
| 800 | } | 805 | } |
| 801 | 806 | ||
| 802 | void midi_usb_init(MidiDevice * device){ | 807 | static void midi_usb_init(MidiDevice * device){ |
| 803 | midi_device_init(device); | 808 | midi_device_init(device); |
| 804 | midi_device_set_send_func(device, usb_send_func); | 809 | midi_device_set_send_func(device, usb_send_func); |
| 805 | midi_device_set_pre_input_process_func(device, usb_get_midi); | 810 | midi_device_set_pre_input_process_func(device, usb_get_midi); |
| 806 | 811 | ||
| 807 | SetupHardware(); | 812 | // SetupHardware(); |
| 808 | sei(); | 813 | sei(); |
| 809 | } | 814 | } |
| 810 | 815 | ||
| @@ -1039,11 +1044,16 @@ int main(void) | |||
| 1039 | } | 1044 | } |
| 1040 | #endif | 1045 | #endif |
| 1041 | 1046 | ||
| 1047 | keyboard_task(); | ||
| 1048 | |||
| 1042 | #ifdef MIDI_ENABLE | 1049 | #ifdef MIDI_ENABLE |
| 1043 | midi_device_process(&midi_device); | 1050 | midi_device_process(&midi_device); |
| 1044 | // MIDI_Task(); | 1051 | // MIDI_Task(); |
| 1045 | #endif | 1052 | #endif |
| 1046 | keyboard_task(); | 1053 | |
| 1054 | #ifdef RGBLIGHT_ANIMATIONS | ||
| 1055 | rgblight_task(); | ||
| 1056 | #endif | ||
| 1047 | 1057 | ||
| 1048 | #ifdef VIRTSER_ENABLE | 1058 | #ifdef VIRTSER_ENABLE |
| 1049 | virtser_task(); | 1059 | virtser_task(); |
| @@ -1077,15 +1087,196 @@ void fallthrough_callback(MidiDevice * device, | |||
| 1077 | #endif | 1087 | #endif |
| 1078 | } | 1088 | } |
| 1079 | 1089 | ||
| 1090 | #ifdef RGB_MIDI | ||
| 1091 | rgblight_config_t rgblight_config; | ||
| 1092 | #endif | ||
| 1093 | |||
| 1080 | void cc_callback(MidiDevice * device, | 1094 | void cc_callback(MidiDevice * device, |
| 1081 | uint8_t chan, uint8_t num, uint8_t val) { | 1095 | uint8_t chan, uint8_t num, uint8_t val) { |
| 1082 | //sending it back on the next channel | 1096 | //sending it back on the next channel |
| 1083 | midi_send_cc(device, (chan + 1) % 16, num, val); | 1097 | // midi_send_cc(device, (chan + 1) % 16, num, val); |
| 1098 | #ifdef RGB_MIDI | ||
| 1099 | rgblight_config.raw = eeconfig_read_rgblight(); | ||
| 1100 | switch (num) { | ||
| 1101 | case 14: | ||
| 1102 | rgblight_config.hue = val * 360 / 127; | ||
| 1103 | break; | ||
| 1104 | case 15: | ||
| 1105 | rgblight_config.sat = val << 1; | ||
| 1106 | break; | ||
| 1107 | case 16: | ||
| 1108 | rgblight_config.val = val << 1; | ||
| 1109 | break; | ||
| 1110 | } | ||
| 1111 | rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); | ||
| 1112 | #endif | ||
| 1084 | } | 1113 | } |
| 1085 | 1114 | ||
| 1086 | void sysex_callback(MidiDevice * device, | 1115 | uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0}; |
| 1087 | uint16_t start, uint8_t length, uint8_t * data) { | 1116 | |
| 1088 | for (int i = 0; i < length; i++) | 1117 | void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data) { |
| 1089 | midi_send_cc(device, 15, 0x7F & data[i], 0x7F & (start + i)); | 1118 | // for (int i = 0; i < length; i++) |
| 1119 | // midi_send_cc(device, 15, 0x7F & data[i], 0x7F & (start + i)); | ||
| 1120 | // if (start == 0x27) { | ||
| 1121 | // SEND_STRING("\n"); | ||
| 1122 | // send_word(start); | ||
| 1123 | // SEND_STRING(": "); | ||
| 1124 | for (uint8_t place = 0; place < length; place++) { | ||
| 1125 | // send_byte(*data); | ||
| 1126 | midi_buffer[start + place] = *data; | ||
| 1127 | if (*data == 0xF7 && midi_buffer[0] == 0xF0) | ||
| 1128 | sysex_buffer_callback(device, start + place, midi_buffer); | ||
| 1129 | // SEND_STRING(" "); | ||
| 1130 | data++; | ||
| 1131 | } | ||
| 1132 | // } | ||
| 1133 | |||
| 1134 | } | ||
| 1135 | |||
| 1136 | uint32_t decode_uint32_chunk(uint8_t * data) { | ||
| 1137 | uint32_t part1 = *data++; | ||
| 1138 | uint32_t part2 = *data++; | ||
| 1139 | uint32_t part3 = *data++; | ||
| 1140 | uint32_t part4 = *data++; | ||
| 1141 | uint32_t part5 = *data++; | ||
| 1142 | return ((part1 & 0x1FUL) << 28) | (part2 << 21) | (part3 << 14) | (part4 << 7) | part5; | ||
| 1143 | } | ||
| 1144 | |||
| 1145 | uint32_t decode_uint8_chunk(uint8_t * data) { | ||
| 1146 | uint32_t part4 = *data++; | ||
| 1147 | uint32_t part5 = *data++; | ||
| 1148 | return (part4 << 7) | part5; | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | void encode_uint32_chunk(uint32_t data, uint8_t * pointer) { | ||
| 1152 | *pointer++ = (data >> 28) & 0x7F; | ||
| 1153 | *pointer++ = (data >> 21) & 0x7F; | ||
| 1154 | *pointer++ = (data >> 14) & 0x7F; | ||
| 1155 | *pointer++ = (data >> 7) & 0x7F; | ||
| 1156 | *pointer++ = (data) & 0x7F; | ||
| 1157 | } | ||
| 1158 | |||
| 1159 | void encode_uint8_chunk(uint8_t data, uint8_t * pointer) { | ||
| 1160 | *pointer++ = (data >> 7) & 0x7F; | ||
| 1161 | *pointer++ = (data) & 0x7F; | ||
| 1162 | } | ||
| 1163 | |||
| 1164 | void sysex_buffer_callback(MidiDevice * device, uint8_t length, uint8_t * data) { | ||
| 1165 | // uint8_t * pointer_copy = data; // use for debugging | ||
| 1166 | |||
| 1167 | //data++; // i'm 98% sure there's a better way to do this | ||
| 1168 | data++; | ||
| 1169 | data++; | ||
| 1170 | data++; | ||
| 1171 | data++; | ||
| 1172 | |||
| 1173 | switch (*data++) { | ||
| 1174 | case 0x07: ; // Quantum action | ||
| 1175 | break; | ||
| 1176 | case 0x08: ; // Keyboard acion | ||
| 1177 | break; | ||
| 1178 | case 0x09: ; // User action | ||
| 1179 | break; | ||
| 1180 | case 0x12: ; // Set info on keyboard | ||
| 1181 | switch (*data++) { | ||
| 1182 | case 0x02: ; // set default layer | ||
| 1183 | uint8_t default_layer = decode_uint8_chunk(data); | ||
| 1184 | eeconfig_update_default_layer(default_layer); | ||
| 1185 | default_layer_set((uint32_t)default_layer); | ||
| 1186 | break; | ||
| 1187 | case 0x08: ; // set keymap options | ||
| 1188 | uint8_t keymap_options = decode_uint8_chunk(data); | ||
| 1189 | eeconfig_update_keymap(keymap_options); | ||
| 1190 | break; | ||
| 1191 | } | ||
| 1192 | break; | ||
| 1193 | case 0x13: ; // Get info from keyboard | ||
| 1194 | switch (*data++) { | ||
| 1195 | case 0x00: ; // Handshake | ||
| 1196 | send_bytes_sysex(0x00, NULL, 0); | ||
| 1197 | break; | ||
| 1198 | case 0x01: ; // Get debug state | ||
| 1199 | uint8_t debug[2]; | ||
| 1200 | encode_uint8_chunk(eeprom_read_byte(EECONFIG_DEBUG), debug); | ||
| 1201 | send_bytes_sysex(0x01, debug, 2); | ||
| 1202 | break; | ||
| 1203 | case 0x02: ; // Get default layer | ||
| 1204 | uint8_t default_layer[2]; | ||
| 1205 | encode_uint8_chunk(eeprom_read_byte(EECONFIG_DEFAULT_LAYER), default_layer); | ||
| 1206 | send_bytes_sysex(0x02, default_layer, 2); | ||
| 1207 | break; | ||
| 1208 | #ifdef AUDIO_ENABLE | ||
| 1209 | case 0x03: ; // Get backlight state | ||
| 1210 | uint8_t audio[2]; | ||
| 1211 | encode_uint8_chunk(eeprom_read_byte(EECONFIG_AUDIO), audio); | ||
| 1212 | send_bytes_sysex(0x03, audio, 2); | ||
| 1213 | #endif | ||
| 1214 | case 0x04: ; // Get layer state | ||
| 1215 | uint8_t layers[5]; | ||
| 1216 | encode_uint32_chunk(layer_state, layers); | ||
| 1217 | send_bytes_sysex(0x04, layers, 5); | ||
| 1218 | break; | ||
| 1219 | #ifdef BACKLIGHT_ENABLE | ||
| 1220 | case 0x06: ; // Get backlight state | ||
| 1221 | uint8_t backlight[2]; | ||
| 1222 | encode_uint8_chunk(eeprom_read_byte(EECONFIG_BACKLIGHT), backlight); | ||
| 1223 | send_bytes_sysex(0x06, backlight, 2); | ||
| 1224 | #endif | ||
| 1225 | #ifdef RGBLIGHT_ENABLE | ||
| 1226 | case 0x07: ; // Get rgblight state | ||
| 1227 | uint8_t rgblight[2]; | ||
| 1228 | encode_uint32_chunk(eeprom_read_dword(EECONFIG_RGBLIGHT), rgblight); | ||
| 1229 | send_bytes_sysex(0x07, rgblight, 5); | ||
| 1230 | #endif | ||
| 1231 | case 0x08: ; // Keymap options | ||
| 1232 | uint8_t keymap_options[2]; | ||
| 1233 | encode_uint8_chunk(eeconfig_read_keymap(), keymap_options); | ||
| 1234 | send_bytes_sysex(0x08, keymap_options, 2); | ||
| 1235 | break; | ||
| 1236 | } | ||
| 1237 | break; | ||
| 1238 | #ifdef RGBLIGHT_ENABLE | ||
| 1239 | case 0x27: ; // RGB LED functions | ||
| 1240 | switch (*data++) { | ||
| 1241 | case 0x00: ; // Update HSV | ||
| 1242 | uint32_t hsv = decode_uint32_chunk(data); | ||
| 1243 | rgblight_sethsv(((hsv >> 16) & 0xFFFF) % 360, (hsv >> 8) & 0xFF, hsv & 0xFF); | ||
| 1244 | break; | ||
| 1245 | case 0x01: ; // Update RGB | ||
| 1246 | break; | ||
| 1247 | case 0x02: ; // Update mode | ||
| 1248 | uint8_t rgb_mode = decode_uint8_chunk(data); | ||
| 1249 | rgblight_mode(rgb_mode); | ||
| 1250 | break; | ||
| 1251 | } | ||
| 1252 | break; | ||
| 1253 | #endif | ||
| 1254 | } | ||
| 1255 | |||
| 1256 | // SEND_STRING("\nDATA:\n"); | ||
| 1257 | // while (*pointer_copy != 0xF7) { | ||
| 1258 | // send_byte(*pointer_copy++); | ||
| 1259 | // SEND_STRING(" "); | ||
| 1260 | // } | ||
| 1261 | |||
| 1262 | } | ||
| 1263 | |||
| 1264 | void send_unicode_midi(uint32_t unicode) { | ||
| 1265 | uint8_t chunk[5]; | ||
| 1266 | encode_uint32_chunk(unicode, chunk); | ||
| 1267 | send_bytes_sysex(0x05, chunk, 5); | ||
| 1090 | } | 1268 | } |
| 1269 | |||
| 1270 | void send_bytes_sysex(uint8_t type, uint8_t * bytes, uint8_t length) { | ||
| 1271 | uint8_t * array = malloc(sizeof(uint8_t) * (length + 6)); | ||
| 1272 | array[0] = 0xF0; | ||
| 1273 | array[1] = 0x00; | ||
| 1274 | array[2] = 0x00; | ||
| 1275 | array[3] = 0x00; | ||
| 1276 | array[4] = type; | ||
| 1277 | array[length + 5] = 0xF7; | ||
| 1278 | memcpy(array + 5, bytes, length); | ||
| 1279 | midi_send_array(&midi_device, length + 6, array); | ||
| 1280 | } | ||
| 1281 | |||
| 1091 | #endif | 1282 | #endif |
diff --git a/tmk_core/protocol/lufa/lufa.h b/tmk_core/protocol/lufa/lufa.h index aad08d640..198964f90 100644 --- a/tmk_core/protocol/lufa/lufa.h +++ b/tmk_core/protocol/lufa/lufa.h | |||
| @@ -68,8 +68,18 @@ typedef struct { | |||
| 68 | } __attribute__ ((packed)) report_extra_t; | 68 | } __attribute__ ((packed)) report_extra_t; |
| 69 | 69 | ||
| 70 | #ifdef MIDI_ENABLE | 70 | #ifdef MIDI_ENABLE |
| 71 | void MIDI_Task(void); | 71 | #define MIDI_SYSEX_BUFFER 16 |
| 72 | MidiDevice midi_device; | 72 | void MIDI_Task(void); |
| 73 | MidiDevice midi_device; | ||
| 74 | |||
| 75 | void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data); | ||
| 76 | uint32_t decode_uint32_chunk(uint8_t * data); | ||
| 77 | uint32_t decode_uint8_chunk(uint8_t * data); | ||
| 78 | void encode_uint32_chunk(uint32_t data, uint8_t * pointer); | ||
| 79 | void encode_uint8_chunk(uint8_t data, uint8_t * pointer); | ||
| 80 | void sysex_buffer_callback(MidiDevice * device, uint8_t length, uint8_t * data); | ||
| 81 | void send_unicode_midi(uint32_t unicode); | ||
| 82 | void send_bytes_sysex(uint8_t type, uint8_t * bytes, uint8_t length); | ||
| 73 | #endif | 83 | #endif |
| 74 | 84 | ||
| 75 | // #if LUFA_VERSION_INTEGER < 0x120730 | 85 | // #if LUFA_VERSION_INTEGER < 0x120730 |
