format code according to conventions [skip ci]
[jackhill/qmk/firmware.git] / quantum / api.c
CommitLineData
23839b8c 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
7edac212
JH
17#include "api.h"
18#include "quantum.h"
19
b624f32f 20void dword_to_bytes(uint32_t dword, uint8_t* bytes) {
7edac212 21 bytes[0] = (dword >> 24) & 0xFF;
b624f32f 22 bytes[1] = (dword >> 16) & 0xFF;
23 bytes[2] = (dword >> 8) & 0xFF;
24 bytes[3] = (dword >> 0) & 0xFF;
7edac212
JH
25}
26
b624f32f 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]; }
7edac212 28
b624f32f 29__attribute__((weak)) bool process_api_quantum(uint8_t length, uint8_t* data) { return process_api_keyboard(length, data); }
7edac212 30
b624f32f 31__attribute__((weak)) bool process_api_keyboard(uint8_t length, uint8_t* data) { return process_api_user(length, data); }
7edac212 32
b624f32f 33__attribute__((weak)) bool process_api_user(uint8_t length, uint8_t* data) { return true; }
7edac212 34
b624f32f 35void process_api(uint16_t length, uint8_t* data) {
7edac212
JH
36 // SEND_STRING("\nRX: ");
37 // for (uint8_t i = 0; i < length; i++) {
38 // send_byte(data[i]);
39 // SEND_STRING(" ");
40 // }
b624f32f 41 if (!process_api_quantum(length, data)) return;
7edac212
JH
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: {
b624f32f 56#ifdef RGBLIGHT_ENABLE
57 uint32_t rgblight = bytes_to_dword(data, 2);
58 eeconfig_update_rgblight(rgblight);
59#endif
7edac212
JH
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: {
b624f32f 70 uint8_t debug_bytes[1] = {eeprom_read_byte(EECONFIG_DEBUG)};
7edac212
JH
71 MT_GET_DATA_ACK(DT_DEBUG, debug_bytes, 1);
72 break;
73 }
74 case DT_DEFAULT_LAYER: {
b624f32f 75 uint8_t default_bytes[1] = {eeprom_read_byte(EECONFIG_DEFAULT_LAYER)};
7edac212
JH
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: {
b624f32f 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
7edac212
JH
92 break;
93 }
94 case DT_BACKLIGHT: {
b624f32f 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
7edac212
JH
101 break;
102 }
103 case DT_RGBLIGHT: {
b624f32f 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
7edac212
JH
111 break;
112 }
113 case DT_KEYMAP_OPTIONS: {
b624f32f 114 uint8_t keymap_bytes[1] = {eeconfig_read_keymap()};
7edac212
JH
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 }
06c64bbf
JH
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;
7edac212 142
06c64bbf
JH
143 // // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, 5);
144 // break;
145 // }
7edac212
JH
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;
b624f32f 163 default:; // command not recognised
7edac212
JH
164 SEND_BYTES(MT_TYPE_ERROR, DT_NONE, data, length);
165 break;
166
b624f32f 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
7edac212 181 }
23839b8c 182}