aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
Diffstat (limited to 'quantum')
-rw-r--r--quantum/api.c182
-rw-r--r--quantum/api.h55
-rw-r--r--quantum/api/api_sysex.c72
-rw-r--r--quantum/api/api_sysex.h25
-rw-r--r--quantum/quantum.c23
-rw-r--r--quantum/quantum.h2
6 files changed, 6 insertions, 353 deletions
diff --git a/quantum/api.c b/quantum/api.c
deleted file mode 100644
index 168574458..000000000
--- a/quantum/api.c
+++ /dev/null
@@ -1,182 +0,0 @@
1/* Copyright 2016 Jack Humbert
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "api.h"
18#include "quantum.h"
19
20void dword_to_bytes(uint32_t dword, uint8_t* bytes) {
21 bytes[0] = (dword >> 24) & 0xFF;
22 bytes[1] = (dword >> 16) & 0xFF;
23 bytes[2] = (dword >> 8) & 0xFF;
24 bytes[3] = (dword >> 0) & 0xFF;
25}
26
27uint32_t bytes_to_dword(uint8_t* bytes, uint8_t index) { return ((uint32_t)bytes[index + 0] << 24) | ((uint32_t)bytes[index + 1] << 16) | ((uint32_t)bytes[index + 2] << 8) | (uint32_t)bytes[index + 3]; }
28
29__attribute__((weak)) bool process_api_quantum(uint8_t length, uint8_t* data) { return process_api_keyboard(length, data); }
30
31__attribute__((weak)) bool process_api_keyboard(uint8_t length, uint8_t* data) { return process_api_user(length, data); }
32
33__attribute__((weak)) bool process_api_user(uint8_t length, uint8_t* data) { return true; }
34
35void process_api(uint16_t length, uint8_t* data) {
36 // SEND_STRING("\nRX: ");
37 // for (uint8_t i = 0; i < length; i++) {
38 // send_byte(data[i]);
39 // SEND_STRING(" ");
40 // }
41 if (!process_api_quantum(length, data)) return;
42
43 switch (data[0]) {
44 case MT_SET_DATA:
45 switch (data[1]) {
46 case DT_DEFAULT_LAYER: {
47 eeconfig_update_default_layer(data[2]);
48 default_layer_set((uint32_t)(data[2]));
49 break;
50 }
51 case DT_KEYMAP_OPTIONS: {
52 eeconfig_update_keymap(data[2]);
53 break;
54 }
55 case DT_RGBLIGHT: {
56#ifdef RGBLIGHT_ENABLE
57 uint32_t rgblight = bytes_to_dword(data, 2);
58 eeconfig_update_rgblight(rgblight);
59#endif
60 break;
61 }
62 }
63 case MT_GET_DATA:
64 switch (data[1]) {
65 case DT_HANDSHAKE: {
66 MT_GET_DATA_ACK(DT_HANDSHAKE, NULL, 0);
67 break;
68 }
69 case DT_DEBUG: {
70 uint8_t debug_bytes[1] = {eeprom_read_byte(EECONFIG_DEBUG)};
71 MT_GET_DATA_ACK(DT_DEBUG, debug_bytes, 1);
72 break;
73 }
74 case DT_DEFAULT_LAYER: {
75 uint8_t default_bytes[1] = {eeprom_read_byte(EECONFIG_DEFAULT_LAYER)};
76 MT_GET_DATA_ACK(DT_DEFAULT_LAYER, default_bytes, 1);
77 break;
78 }
79 case DT_CURRENT_LAYER: {
80 uint8_t layer_state_bytes[4];
81 dword_to_bytes(layer_state, layer_state_bytes);
82 MT_GET_DATA_ACK(DT_CURRENT_LAYER, layer_state_bytes, 4);
83 break;
84 }
85 case DT_AUDIO: {
86#ifdef AUDIO_ENABLE
87 uint8_t audio_bytes[1] = {eeprom_read_byte(EECONFIG_AUDIO)};
88 MT_GET_DATA_ACK(DT_AUDIO, audio_bytes, 1);
89#else
90 MT_GET_DATA_ACK(DT_AUDIO, NULL, 0);
91#endif
92 break;
93 }
94 case DT_BACKLIGHT: {
95#ifdef BACKLIGHT_ENABLE
96 uint8_t backlight_bytes[1] = {eeprom_read_byte(EECONFIG_BACKLIGHT)};
97 MT_GET_DATA_ACK(DT_BACKLIGHT, backlight_bytes, 1);
98#else
99 MT_GET_DATA_ACK(DT_BACKLIGHT, NULL, 0);
100#endif
101 break;
102 }
103 case DT_RGBLIGHT: {
104#ifdef RGBLIGHT_ENABLE
105 uint8_t rgblight_bytes[4];
106 dword_to_bytes(eeconfig_read_rgblight(), rgblight_bytes);
107 MT_GET_DATA_ACK(DT_RGBLIGHT, rgblight_bytes, 4);
108#else
109 MT_GET_DATA_ACK(DT_RGBLIGHT, NULL, 0);
110#endif
111 break;
112 }
113 case DT_KEYMAP_OPTIONS: {
114 uint8_t keymap_bytes[1] = {eeconfig_read_keymap()};
115 MT_GET_DATA_ACK(DT_KEYMAP_OPTIONS, keymap_bytes, 1);
116 break;
117 }
118 case DT_KEYMAP_SIZE: {
119 uint8_t keymap_size[2] = {MATRIX_ROWS, MATRIX_COLS};
120 MT_GET_DATA_ACK(DT_KEYMAP_SIZE, keymap_size, 2);
121 break;
122 }
123 // This may be too much
124 // case DT_KEYMAP: {
125 // uint8_t keymap_data[MATRIX_ROWS * MATRIX_COLS * 4 + 3];
126 // keymap_data[0] = data[2];
127 // keymap_data[1] = MATRIX_ROWS;
128 // keymap_data[2] = MATRIX_COLS;
129 // for (int i = 0; i < MATRIX_ROWS; i++) {
130 // for (int j = 0; j < MATRIX_COLS; j++) {
131 // keymap_data[3 + (i*MATRIX_COLS*2) + (j*2)] = pgm_read_word(&keymaps[data[2]][i][j]) >> 8;
132 // keymap_data[3 + (i*MATRIX_COLS*2) + (j*2) + 1] = pgm_read_word(&keymaps[data[2]][i][j]) & 0xFF;
133 // }
134 // }
135 // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, MATRIX_ROWS * MATRIX_COLS * 4 + 3);
136 // // uint8_t keymap_data[5];
137 // // keymap_data[0] = data[2];
138 // // keymap_data[1] = data[3];
139 // // keymap_data[2] = data[4];
140 // // keymap_data[3] = pgm_read_word(&keymaps[data[2]][data[3]][data[4]]) >> 8;
141 // // keymap_data[4] = pgm_read_word(&keymaps[data[2]][data[3]][data[4]]) & 0xFF;
142
143 // // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, 5);
144 // break;
145 // }
146 default:
147 break;
148 }
149 break;
150 case MT_SET_DATA_ACK:
151 case MT_GET_DATA_ACK:
152 break;
153 case MT_SEND_DATA:
154 break;
155 case MT_SEND_DATA_ACK:
156 break;
157 case MT_EXE_ACTION:
158 break;
159 case MT_EXE_ACTION_ACK:
160 break;
161 case MT_TYPE_ERROR:
162 break;
163 default:; // command not recognised
164 SEND_BYTES(MT_TYPE_ERROR, DT_NONE, data, length);
165 break;
166
167 // #ifdef RGBLIGHT_ENABLE
168 // case 0x27: ; // RGB LED functions
169 // switch (*data++) {
170 // case 0x00: ; // Update HSV
171 // rgblight_sethsv((data[0] << 8 | data[1]) % 360, data[2], data[3]);
172 // break;
173 // case 0x01: ; // Update RGB
174 // break;
175 // case 0x02: ; // Update mode
176 // rgblight_mode(data[0]);
177 // break;
178 // }
179 // break;
180 // #endif
181 }
182}
diff --git a/quantum/api.h b/quantum/api.h
deleted file mode 100644
index 0a30e9d6c..000000000
--- a/quantum/api.h
+++ /dev/null
@@ -1,55 +0,0 @@
1/* Copyright 2016 Jack Humbert
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#pragma once
18
19#ifdef __AVR__
20# include "lufa.h"
21#endif
22
23enum MESSAGE_TYPE {
24 MT_GET_DATA = 0x10, // Get data from keyboard
25 MT_GET_DATA_ACK = 0x11, // returned data to process (ACK)
26 MT_SET_DATA = 0x20, // Set data on keyboard
27 MT_SET_DATA_ACK = 0x21, // returned data to confirm (ACK)
28 MT_SEND_DATA = 0x30, // Sending data/action from keyboard
29 MT_SEND_DATA_ACK = 0x31, // returned data/action confirmation (ACK)
30 MT_EXE_ACTION = 0x40, // executing actions on keyboard
31 MT_EXE_ACTION_ACK = 0x41, // return confirmation/value (ACK)
32 MT_TYPE_ERROR = 0x80 // type not recognised (ACK)
33};
34
35enum DATA_TYPE { DT_NONE = 0x00, DT_HANDSHAKE, DT_DEFAULT_LAYER, DT_CURRENT_LAYER, DT_KEYMAP_OPTIONS, DT_BACKLIGHT, DT_RGBLIGHT, DT_UNICODE, DT_DEBUG, DT_AUDIO, DT_QUANTUM_ACTION, DT_KEYBOARD_ACTION, DT_USER_ACTION, DT_KEYMAP_SIZE, DT_KEYMAP };
36
37void dword_to_bytes(uint32_t dword, uint8_t* bytes);
38uint32_t bytes_to_dword(uint8_t* bytes, uint8_t index);
39
40#define MT_GET_DATA(data_type, data, length) SEND_BYTES(MT_GET_DATA, data_type, data, length)
41#define MT_GET_DATA_ACK(data_type, data, length) SEND_BYTES(MT_GET_DATA_ACK, data_type, data, length)
42#define MT_SET_DATA(data_type, data, length) SEND_BYTES(MT_SET_DATA, data_type, data, length)
43#define MT_SET_DATA_ACK(data_type, data, length) SEND_BYTES(MT_SET_DATA_ACK, data_type, data, length)
44#define MT_SEND_DATA(data_type, data, length) SEND_BYTES(MT_SEND_DATA, data_type, data, length)
45#define MT_SEND_DATA_ACK(data_type, data, length) SEND_BYTES(MT_SEND_DATA_ACK, data_type, data, length)
46#define MT_EXE_ACTION(data_type, data, length) SEND_BYTES(MT_EXE_ACTION, data_type, data, length)
47#define MT_EXE_ACTION_ACK(data_type, data, length) SEND_BYTES(MT_EXE_ACTION_ACK, data_type, data, length)
48
49void process_api(uint16_t length, uint8_t* data);
50
51__attribute__((weak)) bool process_api_quantum(uint8_t length, uint8_t* data);
52
53__attribute__((weak)) bool process_api_keyboard(uint8_t length, uint8_t* data);
54
55__attribute__((weak)) bool process_api_user(uint8_t length, uint8_t* data);
diff --git a/quantum/api/api_sysex.c b/quantum/api/api_sysex.c
deleted file mode 100644
index 07c90cf80..000000000
--- a/quantum/api/api_sysex.c
+++ /dev/null
@@ -1,72 +0,0 @@
1/* Copyright 2016 Jack Humbert, Fred Sundvik
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#include "api_sysex.h"
17#include "sysex_tools.h"
18#include "print.h"
19#include "qmk_midi.h"
20
21void send_bytes_sysex(uint8_t message_type, uint8_t data_type, uint8_t* bytes, uint16_t length) {
22 // SEND_STRING("\nTX: ");
23 // for (uint8_t i = 0; i < length; i++) {
24 // send_byte(bytes[i]);
25 // SEND_STRING(" ");
26 // }
27 if (length > API_SYSEX_MAX_SIZE) {
28 xprintf("Sysex msg too big %d %d %d", message_type, data_type, length);
29 return;
30 }
31
32 // The buffer size required is calculated as the following
33 // API_SYSEX_MAX_SIZE is the maximum length
34 // In addition to that we have a two byte message header consisting of the message_type and data_type
35 // This has to be encoded with an additional overhead of one byte for every starting 7 bytes
36 // We just add one extra byte in case it's not divisible by 7
37 // Then we have an unencoded header consisting of 4 bytes
38 // Plus a one byte terminator
39 const unsigned message_header = 2;
40 const unsigned unencoded_message = API_SYSEX_MAX_SIZE + message_header;
41 const unsigned encoding_overhead = unencoded_message / 7 + 1;
42 const unsigned encoded_size = unencoded_message + encoding_overhead;
43 const unsigned unencoded_header = 4;
44 const unsigned terminator = 1;
45 const unsigned buffer_size = encoded_size + unencoded_header + terminator;
46 uint8_t buffer[encoded_size + unencoded_header + terminator];
47 // The unencoded header
48 buffer[0] = 0xF0;
49 buffer[1] = 0x00;
50 buffer[2] = 0x00;
51 buffer[3] = 0x00;
52
53 // We copy the message to the end of the array, this way we can do an inplace encoding, using the same
54 // buffer for both input and output
55 const unsigned message_size = length + message_header;
56 uint8_t* unencoded_start = buffer + buffer_size - message_size;
57 uint8_t* ptr = unencoded_start;
58 *(ptr++) = message_type;
59 *(ptr++) = data_type;
60 memcpy(ptr, bytes, length);
61
62 unsigned encoded_length = sysex_encode(buffer + unencoded_header, unencoded_start, message_size);
63 unsigned final_size = unencoded_header + encoded_length + terminator;
64 buffer[final_size - 1] = 0xF7;
65 midi_send_array(&midi_device, final_size, buffer);
66
67 // SEND_STRING("\nTD: ");
68 // for (uint8_t i = 0; i < encoded_length + 5; i++) {
69 // send_byte(buffer[i]);
70 // SEND_STRING(" ");
71 // }
72}
diff --git a/quantum/api/api_sysex.h b/quantum/api/api_sysex.h
deleted file mode 100644
index eb0a18848..000000000
--- a/quantum/api/api_sysex.h
+++ /dev/null
@@ -1,25 +0,0 @@
1/* Copyright 2016 Jack Humbert
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#pragma once
18
19#include "api.h"
20
21#define API_SYSEX_MAX_SIZE 32
22
23void send_bytes_sysex(uint8_t message_type, uint8_t data_type, uint8_t* bytes, uint16_t length);
24
25#define SEND_BYTES(mt, dt, b, l) send_bytes_sysex(mt, dt, b, l)
diff --git a/quantum/quantum.c b/quantum/quantum.c
index 326c8370b..ac8857df8 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -25,10 +25,6 @@
25# include "backlight.h" 25# include "backlight.h"
26#endif 26#endif
27 27
28#ifdef API_ENABLE
29# include "api.h"
30#endif
31
32#ifdef MIDI_ENABLE 28#ifdef MIDI_ENABLE
33# include "process_midi.h" 29# include "process_midi.h"
34#endif 30#endif
@@ -145,12 +141,13 @@ void reset_keyboard(void) {
145/* Convert record into usable keycode via the contained event. */ 141/* Convert record into usable keycode via the contained event. */
146uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) { 142uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) {
147#ifdef COMBO_ENABLE 143#ifdef COMBO_ENABLE
148 if (record->keycode) { return record->keycode; } 144 if (record->keycode) {
145 return record->keycode;
146 }
149#endif 147#endif
150 return get_event_keycode(record->event, update_layer_cache); 148 return get_event_keycode(record->event, update_layer_cache);
151} 149}
152 150
153
154/* Convert event into usable keycode. Checks the layer cache to ensure that it 151/* Convert event into usable keycode. Checks the layer cache to ensure that it
155 * retains the correct keycode after a layer change, if the key is still pressed. 152 * retains the correct keycode after a layer change, if the key is still pressed.
156 * "update_layer_cache" is to ensure that it only updates the layer cache when 153 * "update_layer_cache" is to ensure that it only updates the layer cache when
@@ -179,12 +176,12 @@ uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
179bool pre_process_record_quantum(keyrecord_t *record) { 176bool pre_process_record_quantum(keyrecord_t *record) {
180 if (!( 177 if (!(
181#ifdef COMBO_ENABLE 178#ifdef COMBO_ENABLE
182 process_combo(get_record_keycode(record, true), record) && 179 process_combo(get_record_keycode(record, true), record) &&
183#endif 180#endif
184 true)) { 181 true)) {
185 return false; 182 return false;
186 } 183 }
187 return true; // continue processing 184 return true; // continue processing
188} 185}
189 186
190/* Get keycode, and then call keyboard function */ 187/* Get keycode, and then call keyboard function */
@@ -468,14 +465,6 @@ void matrix_scan_quantum() {
468# include "hd44780.h" 465# include "hd44780.h"
469#endif 466#endif
470 467
471void api_send_unicode(uint32_t unicode) {
472#ifdef API_ENABLE
473 uint8_t chunk[4];
474 dword_to_bytes(unicode, chunk);
475 MT_SEND_DATA(DT_UNICODE, chunk, 5);
476#endif
477}
478
479//------------------------------------------------------------------------------ 468//------------------------------------------------------------------------------
480// Override these functions in your keymap file to play different tunes on 469// Override these functions in your keymap file to play different tunes on
481// different events such as startup and bootloader jump 470// different events such as startup and bootloader jump
diff --git a/quantum/quantum.h b/quantum/quantum.h
index 5cbe84d0c..9250f5acc 100644
--- a/quantum/quantum.h
+++ b/quantum/quantum.h
@@ -237,5 +237,3 @@ void led_set_user(uint8_t usb_led);
237void led_set_kb(uint8_t usb_led); 237void led_set_kb(uint8_t usb_led);
238bool led_update_user(led_t led_state); 238bool led_update_user(led_t led_state);
239bool led_update_kb(led_t led_state); 239bool led_update_kb(led_t led_state);
240
241void api_send_unicode(uint32_t unicode);