You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
7.4 KiB

  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. #include "api.h"
  17. #include "quantum.h"
  18. void dword_to_bytes(uint32_t dword, uint8_t * bytes) {
  19. bytes[0] = (dword >> 24) & 0xFF;
  20. bytes[1] = (dword >> 16) & 0xFF;
  21. bytes[2] = (dword >> 8) & 0xFF;
  22. bytes[3] = (dword >> 0) & 0xFF;
  23. }
  24. uint32_t bytes_to_dword(uint8_t * bytes, uint8_t index) {
  25. return ((uint32_t)bytes[index + 0] << 24) | ((uint32_t)bytes[index + 1] << 16) | ((uint32_t)bytes[index + 2] << 8) | (uint32_t)bytes[index + 3];
  26. }
  27. __attribute__ ((weak))
  28. bool process_api_quantum(uint8_t length, uint8_t * data) {
  29. return process_api_keyboard(length, data);
  30. }
  31. __attribute__ ((weak))
  32. bool process_api_keyboard(uint8_t length, uint8_t * data) {
  33. return process_api_user(length, data);
  34. }
  35. __attribute__ ((weak))
  36. bool process_api_user(uint8_t length, uint8_t * data) {
  37. return true;
  38. }
  39. void process_api(uint16_t length, uint8_t * data) {
  40. // SEND_STRING("\nRX: ");
  41. // for (uint8_t i = 0; i < length; i++) {
  42. // send_byte(data[i]);
  43. // SEND_STRING(" ");
  44. // }
  45. if (!process_api_quantum(length, data))
  46. return;
  47. switch (data[0]) {
  48. case MT_SET_DATA:
  49. switch (data[1]) {
  50. case DT_DEFAULT_LAYER: {
  51. eeconfig_update_default_layer(data[2]);
  52. default_layer_set((uint32_t)(data[2]));
  53. break;
  54. }
  55. case DT_KEYMAP_OPTIONS: {
  56. eeconfig_update_keymap(data[2]);
  57. break;
  58. }
  59. case DT_RGBLIGHT: {
  60. #ifdef RGBLIGHT_ENABLE
  61. uint32_t rgblight = bytes_to_dword(data, 2);
  62. rgblight_update_dword(rgblight);
  63. #endif
  64. break;
  65. }
  66. }
  67. case MT_GET_DATA:
  68. switch (data[1]) {
  69. case DT_HANDSHAKE: {
  70. MT_GET_DATA_ACK(DT_HANDSHAKE, NULL, 0);
  71. break;
  72. }
  73. case DT_DEBUG: {
  74. uint8_t debug_bytes[1] = { eeprom_read_byte(EECONFIG_DEBUG) };
  75. MT_GET_DATA_ACK(DT_DEBUG, debug_bytes, 1);
  76. break;
  77. }
  78. case DT_DEFAULT_LAYER: {
  79. uint8_t default_bytes[1] = { eeprom_read_byte(EECONFIG_DEFAULT_LAYER) };
  80. MT_GET_DATA_ACK(DT_DEFAULT_LAYER, default_bytes, 1);
  81. break;
  82. }
  83. case DT_CURRENT_LAYER: {
  84. uint8_t layer_state_bytes[4];
  85. dword_to_bytes(layer_state, layer_state_bytes);
  86. MT_GET_DATA_ACK(DT_CURRENT_LAYER, layer_state_bytes, 4);
  87. break;
  88. }
  89. case DT_AUDIO: {
  90. #ifdef AUDIO_ENABLE
  91. uint8_t audio_bytes[1] = { eeprom_read_byte(EECONFIG_AUDIO) };
  92. MT_GET_DATA_ACK(DT_AUDIO, audio_bytes, 1);
  93. #else
  94. MT_GET_DATA_ACK(DT_AUDIO, NULL, 0);
  95. #endif
  96. break;
  97. }
  98. case DT_BACKLIGHT: {
  99. #ifdef BACKLIGHT_ENABLE
  100. uint8_t backlight_bytes[1] = { eeprom_read_byte(EECONFIG_BACKLIGHT) };
  101. MT_GET_DATA_ACK(DT_BACKLIGHT, backlight_bytes, 1);
  102. #else
  103. MT_GET_DATA_ACK(DT_BACKLIGHT, NULL, 0);
  104. #endif
  105. break;
  106. }
  107. case DT_RGBLIGHT: {
  108. #ifdef RGBLIGHT_ENABLE
  109. uint8_t rgblight_bytes[4];
  110. dword_to_bytes(eeconfig_read_rgblight(), rgblight_bytes);
  111. MT_GET_DATA_ACK(DT_RGBLIGHT, rgblight_bytes, 4);
  112. #else
  113. MT_GET_DATA_ACK(DT_RGBLIGHT, NULL, 0);
  114. #endif
  115. break;
  116. }
  117. case DT_KEYMAP_OPTIONS: {
  118. uint8_t keymap_bytes[1] = { eeconfig_read_keymap() };
  119. MT_GET_DATA_ACK(DT_KEYMAP_OPTIONS, keymap_bytes, 1);
  120. break;
  121. }
  122. case DT_KEYMAP_SIZE: {
  123. uint8_t keymap_size[2] = {MATRIX_ROWS, MATRIX_COLS};
  124. MT_GET_DATA_ACK(DT_KEYMAP_SIZE, keymap_size, 2);
  125. break;
  126. }
  127. // This may be too much
  128. // case DT_KEYMAP: {
  129. // uint8_t keymap_data[MATRIX_ROWS * MATRIX_COLS * 4 + 3];
  130. // keymap_data[0] = data[2];
  131. // keymap_data[1] = MATRIX_ROWS;
  132. // keymap_data[2] = MATRIX_COLS;
  133. // for (int i = 0; i < MATRIX_ROWS; i++) {
  134. // for (int j = 0; j < MATRIX_COLS; j++) {
  135. // keymap_data[3 + (i*MATRIX_COLS*2) + (j*2)] = pgm_read_word(&keymaps[data[2]][i][j]) >> 8;
  136. // keymap_data[3 + (i*MATRIX_COLS*2) + (j*2) + 1] = pgm_read_word(&keymaps[data[2]][i][j]) & 0xFF;
  137. // }
  138. // }
  139. // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, MATRIX_ROWS * MATRIX_COLS * 4 + 3);
  140. // // uint8_t keymap_data[5];
  141. // // keymap_data[0] = data[2];
  142. // // keymap_data[1] = data[3];
  143. // // keymap_data[2] = data[4];
  144. // // keymap_data[3] = pgm_read_word(&keymaps[data[2]][data[3]][data[4]]) >> 8;
  145. // // keymap_data[4] = pgm_read_word(&keymaps[data[2]][data[3]][data[4]]) & 0xFF;
  146. // // MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, 5);
  147. // break;
  148. // }
  149. default:
  150. break;
  151. }
  152. break;
  153. case MT_SET_DATA_ACK:
  154. case MT_GET_DATA_ACK:
  155. break;
  156. case MT_SEND_DATA:
  157. break;
  158. case MT_SEND_DATA_ACK:
  159. break;
  160. case MT_EXE_ACTION:
  161. break;
  162. case MT_EXE_ACTION_ACK:
  163. break;
  164. case MT_TYPE_ERROR:
  165. break;
  166. default: ; // command not recognised
  167. SEND_BYTES(MT_TYPE_ERROR, DT_NONE, data, length);
  168. break;
  169. // #ifdef RGBLIGHT_ENABLE
  170. // case 0x27: ; // RGB LED functions
  171. // switch (*data++) {
  172. // case 0x00: ; // Update HSV
  173. // rgblight_sethsv((data[0] << 8 | data[1]) % 360, data[2], data[3]);
  174. // break;
  175. // case 0x01: ; // Update RGB
  176. // break;
  177. // case 0x02: ; // Update mode
  178. // rgblight_mode(data[0]);
  179. // break;
  180. // }
  181. // break;
  182. // #endif
  183. }
  184. }