]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/api.c
Improved dynamic keymaps (#3972)
[qmk_firmware.git] / quantum / api.c
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
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; 
25 }
26
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];
29 }
30
31 __attribute__ ((weak))
32 bool process_api_quantum(uint8_t length, uint8_t * data) {
33     return process_api_keyboard(length, data);
34 }
35
36 __attribute__ ((weak))
37 bool process_api_keyboard(uint8_t length, uint8_t * data) {
38     return process_api_user(length, data);
39 }
40
41 __attribute__ ((weak))
42 bool process_api_user(uint8_t length, uint8_t * data) {
43     return true;
44 }
45
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]);
50     //     SEND_STRING(" ");
51     // }
52     if (!process_api_quantum(length, data))
53         return;
54
55     switch (data[0]) {
56         case MT_SET_DATA:
57             switch (data[1]) {
58                 case DT_DEFAULT_LAYER: {
59                     eeconfig_update_default_layer(data[2]);
60                     default_layer_set((uint32_t)(data[2]));
61                     break;
62                 }
63                 case DT_KEYMAP_OPTIONS: {
64                     eeconfig_update_keymap(data[2]);
65                     break;
66                 }
67                 case DT_RGBLIGHT: {
68                     #ifdef RGBLIGHT_ENABLE
69                         uint32_t rgblight = bytes_to_dword(data, 2);
70                         rgblight_update_dword(rgblight);
71                     #endif
72                     break;
73                 }
74             }
75         case MT_GET_DATA:
76             switch (data[1]) {
77                 case DT_HANDSHAKE: {
78                     MT_GET_DATA_ACK(DT_HANDSHAKE, NULL, 0);
79                     break;
80                 }
81                 case DT_DEBUG: {
82                     uint8_t debug_bytes[1] = { eeprom_read_byte(EECONFIG_DEBUG) };
83                     MT_GET_DATA_ACK(DT_DEBUG, debug_bytes, 1);
84                     break;
85                 }
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);
89                     break;
90                 }
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);
95                     break;
96                 }
97                 case DT_AUDIO: {
98                     #ifdef AUDIO_ENABLE
99                         uint8_t audio_bytes[1] = { eeprom_read_byte(EECONFIG_AUDIO) };
100                         MT_GET_DATA_ACK(DT_AUDIO, audio_bytes, 1);
101                     #else
102                         MT_GET_DATA_ACK(DT_AUDIO, NULL, 0);
103                     #endif
104                     break;
105                 }
106                 case DT_BACKLIGHT: {
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);
110                     #else
111                         MT_GET_DATA_ACK(DT_BACKLIGHT, NULL, 0);
112                     #endif
113                     break;
114                 }
115                 case DT_RGBLIGHT: {
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);
120                     #else
121                         MT_GET_DATA_ACK(DT_RGBLIGHT, NULL, 0);
122                     #endif
123                     break;
124                 }
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);
128                     break;
129                 }
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);
133                     break;
134                 }
135                 // This may be too much
136                 // case DT_KEYMAP: {
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;
145                 //         }
146                 //     }
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;
154
155                 //     // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, 5);
156                 //     break;
157                 // }
158                 default:
159                     break;
160             }
161             break;
162         case MT_SET_DATA_ACK:
163         case MT_GET_DATA_ACK:
164             break;
165         case MT_SEND_DATA:
166             break;
167         case MT_SEND_DATA_ACK:
168             break;
169         case MT_EXE_ACTION:
170             break;
171         case MT_EXE_ACTION_ACK:
172             break;
173         case MT_TYPE_ERROR:
174             break;
175         default: ; // command not recognised
176             SEND_BYTES(MT_TYPE_ERROR, DT_NONE, data, length);
177             break;
178
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]);
184         //             break;
185         //         case 0x01: ; // Update RGB
186         //             break;
187         //         case 0x02: ; // Update mode
188         //             rgblight_mode(data[0]);
189         //             break;
190         //     }
191         //     break;
192         // #endif
193     }
194
195 }