1 /* Copyright 2016 Jack Humbert
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.
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.
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/>.
20 void 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;
27 uint32_t bytes_to_dword(uint8_t * bytes, uint8_t index) {
28 return ((uint32_t)bytes[index + 0] << 24) | ((uint32_t)bytes[index + 1] << 16) | ((uint32_t)bytes[index + 2] << 8) | (uint32_t)bytes[index + 3];
31 __attribute__ ((weak))
32 bool process_api_quantum(uint8_t length, uint8_t * data) {
33 return process_api_keyboard(length, data);
36 __attribute__ ((weak))
37 bool process_api_keyboard(uint8_t length, uint8_t * data) {
38 return process_api_user(length, data);
41 __attribute__ ((weak))
42 bool process_api_user(uint8_t length, uint8_t * data) {
46 void process_api(uint16_t length, uint8_t * data) {
47 // SEND_STRING("\nRX: ");
48 // for (uint8_t i = 0; i < length; i++) {
49 // send_byte(data[i]);
52 if (!process_api_quantum(length, data))
58 case DT_DEFAULT_LAYER: {
59 eeconfig_update_default_layer(data[2]);
60 default_layer_set((uint32_t)(data[2]));
63 case DT_KEYMAP_OPTIONS: {
64 eeconfig_update_keymap(data[2]);
68 #ifdef RGBLIGHT_ENABLE
69 uint32_t rgblight = bytes_to_dword(data, 2);
70 rgblight_update_dword(rgblight);
78 MT_GET_DATA_ACK(DT_HANDSHAKE, NULL, 0);
82 uint8_t debug_bytes[1] = { eeprom_read_byte(EECONFIG_DEBUG) };
83 MT_GET_DATA_ACK(DT_DEBUG, debug_bytes, 1);
86 case DT_DEFAULT_LAYER: {
87 uint8_t default_bytes[1] = { eeprom_read_byte(EECONFIG_DEFAULT_LAYER) };
88 MT_GET_DATA_ACK(DT_DEFAULT_LAYER, default_bytes, 1);
91 case DT_CURRENT_LAYER: {
92 uint8_t layer_state_bytes[4];
93 dword_to_bytes(layer_state, layer_state_bytes);
94 MT_GET_DATA_ACK(DT_CURRENT_LAYER, layer_state_bytes, 4);
99 uint8_t audio_bytes[1] = { eeprom_read_byte(EECONFIG_AUDIO) };
100 MT_GET_DATA_ACK(DT_AUDIO, audio_bytes, 1);
102 MT_GET_DATA_ACK(DT_AUDIO, NULL, 0);
107 #ifdef BACKLIGHT_ENABLE
108 uint8_t backlight_bytes[1] = { eeprom_read_byte(EECONFIG_BACKLIGHT) };
109 MT_GET_DATA_ACK(DT_BACKLIGHT, backlight_bytes, 1);
111 MT_GET_DATA_ACK(DT_BACKLIGHT, NULL, 0);
116 #ifdef RGBLIGHT_ENABLE
117 uint8_t rgblight_bytes[4];
118 dword_to_bytes(eeconfig_read_rgblight(), rgblight_bytes);
119 MT_GET_DATA_ACK(DT_RGBLIGHT, rgblight_bytes, 4);
121 MT_GET_DATA_ACK(DT_RGBLIGHT, NULL, 0);
125 case DT_KEYMAP_OPTIONS: {
126 uint8_t keymap_bytes[1] = { eeconfig_read_keymap() };
127 MT_GET_DATA_ACK(DT_KEYMAP_OPTIONS, keymap_bytes, 1);
130 case DT_KEYMAP_SIZE: {
131 uint8_t keymap_size[2] = {MATRIX_ROWS, MATRIX_COLS};
132 MT_GET_DATA_ACK(DT_KEYMAP_SIZE, keymap_size, 2);
135 // This may be too much
137 // uint8_t keymap_data[MATRIX_ROWS * MATRIX_COLS * 4 + 3];
138 // keymap_data[0] = data[2];
139 // keymap_data[1] = MATRIX_ROWS;
140 // keymap_data[2] = MATRIX_COLS;
141 // for (int i = 0; i < MATRIX_ROWS; i++) {
142 // for (int j = 0; j < MATRIX_COLS; j++) {
143 // keymap_data[3 + (i*MATRIX_COLS*2) + (j*2)] = pgm_read_word(&keymaps[data[2]][i][j]) >> 8;
144 // keymap_data[3 + (i*MATRIX_COLS*2) + (j*2) + 1] = pgm_read_word(&keymaps[data[2]][i][j]) & 0xFF;
147 // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, MATRIX_ROWS * MATRIX_COLS * 4 + 3);
148 // // uint8_t keymap_data[5];
149 // // keymap_data[0] = data[2];
150 // // keymap_data[1] = data[3];
151 // // keymap_data[2] = data[4];
152 // // keymap_data[3] = pgm_read_word(&keymaps[data[2]][data[3]][data[4]]) >> 8;
153 // // keymap_data[4] = pgm_read_word(&keymaps[data[2]][data[3]][data[4]]) & 0xFF;
155 // // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, 5);
162 case MT_SET_DATA_ACK:
163 case MT_GET_DATA_ACK:
167 case MT_SEND_DATA_ACK:
171 case MT_EXE_ACTION_ACK:
175 default: ; // command not recognised
176 SEND_BYTES(MT_TYPE_ERROR, DT_NONE, data, length);
179 // #ifdef RGBLIGHT_ENABLE
180 // case 0x27: ; // RGB LED functions
181 // switch (*data++) {
182 // case 0x00: ; // Update HSV
183 // rgblight_sethsv((data[0] << 8 | data[1]) % 360, data[2], data[3]);
185 // case 0x01: ; // Update RGB
187 // case 0x02: ; // Update mode
188 // rgblight_mode(data[0]);