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
) { 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 __attribute__((weak
)) bool process_api_quantum(uint8_t length
, uint8_t* data
) { return process_api_keyboard(length
, data
); }
31 __attribute__((weak
)) bool process_api_keyboard(uint8_t length
, uint8_t* data
) { return process_api_user(length
, data
); }
33 __attribute__((weak
)) bool process_api_user(uint8_t length
, uint8_t* data
) { return true; }
35 void 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]);
41 if (!process_api_quantum(length
, data
)) return;
46 case DT_DEFAULT_LAYER
: {
47 eeconfig_update_default_layer(data
[2]);
48 default_layer_set((uint32_t)(data
[2]));
51 case DT_KEYMAP_OPTIONS
: {
52 eeconfig_update_keymap(data
[2]);
56 #ifdef RGBLIGHT_ENABLE
57 uint32_t rgblight
= bytes_to_dword(data
, 2);
58 eeconfig_update_rgblight(rgblight
);
66 MT_GET_DATA_ACK(DT_HANDSHAKE
, NULL
, 0);
70 uint8_t debug_bytes
[1] = {eeprom_read_byte(EECONFIG_DEBUG
)};
71 MT_GET_DATA_ACK(DT_DEBUG
, debug_bytes
, 1);
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);
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);
87 uint8_t audio_bytes
[1] = {eeprom_read_byte(EECONFIG_AUDIO
)};
88 MT_GET_DATA_ACK(DT_AUDIO
, audio_bytes
, 1);
90 MT_GET_DATA_ACK(DT_AUDIO
, NULL
, 0);
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);
99 MT_GET_DATA_ACK(DT_BACKLIGHT
, NULL
, 0);
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);
109 MT_GET_DATA_ACK(DT_RGBLIGHT
, NULL
, 0);
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);
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);
123 // This may be too much
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;
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;
143 // // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, 5);
150 case MT_SET_DATA_ACK
:
151 case MT_GET_DATA_ACK
:
155 case MT_SEND_DATA_ACK
:
159 case MT_EXE_ACTION_ACK
:
163 default:; // command not recognised
164 SEND_BYTES(MT_TYPE_ERROR
, DT_NONE
, data
, length
);
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]);
173 // case 0x01: ; // Update RGB
175 // case 0x02: ; // Update mode
176 // rgblight_mode(data[0]);