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.

453 lines
12 KiB

2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
  1. #include "satisfaction75.h"
  2. #include "print.h"
  3. #include "debug.h"
  4. #include "ch.h"
  5. #include "hal.h"
  6. #ifdef QWIIC_MICRO_OLED_ENABLE
  7. #include "micro_oled.h"
  8. #include "qwiic.h"
  9. #endif
  10. #include "timer.h"
  11. #include "raw_hid.h"
  12. #include "dynamic_keymap.h"
  13. #include "tmk_core/common/eeprom.h"
  14. #include "version.h" // for QMK_BUILDDATE used in EEPROM magic
  15. /* Artificial delay added to get media keys to work in the encoder*/
  16. #define MEDIA_KEY_DELAY 10
  17. uint16_t last_flush;
  18. volatile uint8_t led_numlock = false;
  19. volatile uint8_t led_capslock = false;
  20. volatile uint8_t led_scrolllock = false;
  21. uint8_t layer;
  22. bool queue_for_send = false;
  23. bool clock_set_mode = false;
  24. uint8_t oled_mode = OLED_DEFAULT;
  25. bool oled_sleeping = false;
  26. uint8_t encoder_value = 32;
  27. uint8_t encoder_mode = ENC_MODE_VOLUME;
  28. uint8_t enabled_encoder_modes = 0x1F;
  29. RTCDateTime last_timespec;
  30. uint16_t last_minute = 0;
  31. uint8_t time_config_idx = 0;
  32. int8_t hour_config = 0;
  33. int16_t minute_config = 0;
  34. int8_t year_config = 0;
  35. int8_t month_config = 0;
  36. int8_t day_config = 0;
  37. uint8_t previous_encoder_mode = 0;
  38. backlight_config_t kb_backlight_config = {
  39. .enable = true,
  40. .breathing = true,
  41. .level = BACKLIGHT_LEVELS
  42. };
  43. void board_init(void) {
  44. SYSCFG->CFGR1 |= SYSCFG_CFGR1_I2C1_DMA_RMP;
  45. SYSCFG->CFGR1 &= ~(SYSCFG_CFGR1_SPI2_DMA_RMP);
  46. }
  47. #ifdef VIA_ENABLE
  48. void backlight_get_value( uint8_t *data )
  49. {
  50. uint8_t *value_id = &(data[0]);
  51. uint8_t *value_data = &(data[1]);
  52. switch (*value_id)
  53. {
  54. case id_qmk_backlight_brightness:
  55. {
  56. // level / BACKLIGHT_LEVELS * 255
  57. value_data[0] = ((uint16_t)kb_backlight_config.level) * 255 / BACKLIGHT_LEVELS;
  58. break;
  59. }
  60. case id_qmk_backlight_effect:
  61. {
  62. value_data[0] = kb_backlight_config.breathing ? 1 : 0;
  63. break;
  64. }
  65. }
  66. }
  67. void backlight_set_value( uint8_t *data )
  68. {
  69. uint8_t *value_id = &(data[0]);
  70. uint8_t *value_data = &(data[1]);
  71. switch (*value_id)
  72. {
  73. case id_qmk_backlight_brightness:
  74. {
  75. // level / 255 * BACKLIGHT_LEVELS
  76. kb_backlight_config.level = ((uint16_t)value_data[0]) * BACKLIGHT_LEVELS / 255;
  77. backlight_set(kb_backlight_config.level);
  78. break;
  79. }
  80. case id_qmk_backlight_effect:
  81. {
  82. if ( value_data[0] == 0 ) {
  83. kb_backlight_config.breathing = false;
  84. breathing_disable();
  85. } else {
  86. kb_backlight_config.breathing = true;
  87. breathing_enable();
  88. }
  89. break;
  90. }
  91. }
  92. }
  93. void raw_hid_receive_kb( uint8_t *data, uint8_t length )
  94. {
  95. uint8_t *command_id = &(data[0]);
  96. uint8_t *command_data = &(data[1]);
  97. switch ( *command_id )
  98. {
  99. case id_get_keyboard_value:
  100. {
  101. switch( command_data[0])
  102. {
  103. case id_oled_default_mode:
  104. {
  105. uint8_t default_oled = eeprom_read_byte((uint8_t*)EEPROM_DEFAULT_OLED);
  106. command_data[1] = default_oled;
  107. break;
  108. }
  109. case id_oled_mode:
  110. {
  111. command_data[1] = oled_mode;
  112. break;
  113. }
  114. case id_encoder_modes:
  115. {
  116. command_data[1] = enabled_encoder_modes;
  117. break;
  118. }
  119. case id_encoder_custom:
  120. {
  121. uint8_t custom_encoder_idx = command_data[1];
  122. uint16_t keycode = retrieve_custom_encoder_config(custom_encoder_idx, ENC_CUSTOM_CW);
  123. command_data[2] = keycode >> 8;
  124. command_data[3] = keycode & 0xFF;
  125. keycode = retrieve_custom_encoder_config(custom_encoder_idx, ENC_CUSTOM_CCW);
  126. command_data[4] = keycode >> 8;
  127. command_data[5] = keycode & 0xFF;
  128. keycode = retrieve_custom_encoder_config(custom_encoder_idx, ENC_CUSTOM_PRESS);
  129. command_data[6] = keycode >> 8;
  130. command_data[7] = keycode & 0xFF;
  131. break;
  132. }
  133. default:
  134. {
  135. *command_id = id_unhandled;
  136. break;
  137. }
  138. }
  139. break;
  140. }
  141. case id_set_keyboard_value:
  142. {
  143. switch(command_data[0]){
  144. case id_oled_default_mode:
  145. {
  146. eeprom_update_byte((uint8_t*)EEPROM_DEFAULT_OLED, command_data[1]);
  147. break;
  148. }
  149. case id_oled_mode:
  150. {
  151. oled_mode = command_data[1];
  152. draw_ui();
  153. break;
  154. }
  155. case id_encoder_modes:
  156. {
  157. enabled_encoder_modes = command_data[1];
  158. eeprom_update_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES, enabled_encoder_modes);
  159. break;
  160. }
  161. case id_encoder_custom:
  162. {
  163. uint8_t custom_encoder_idx = command_data[1];
  164. uint8_t encoder_behavior = command_data[2];
  165. uint16_t keycode = (command_data[3] << 8) | command_data[4];
  166. set_custom_encoder_config(custom_encoder_idx, encoder_behavior, keycode);
  167. break;
  168. }
  169. default:
  170. {
  171. *command_id = id_unhandled;
  172. break;
  173. }
  174. }
  175. break;
  176. }
  177. case id_lighting_set_value:
  178. {
  179. backlight_set_value(command_data);
  180. break;
  181. }
  182. case id_lighting_get_value:
  183. {
  184. backlight_get_value(command_data);
  185. break;
  186. }
  187. case id_lighting_save:
  188. {
  189. backlight_config_save();
  190. break;
  191. }
  192. default:
  193. {
  194. // Unhandled message.
  195. *command_id = id_unhandled;
  196. break;
  197. }
  198. }
  199. // DO NOT call raw_hid_send(data,length) here, let caller do this
  200. }
  201. #endif
  202. void read_host_led_state(void) {
  203. uint8_t leds = host_keyboard_leds();
  204. if (leds & (1 << USB_LED_NUM_LOCK)) {
  205. if (led_numlock == false){
  206. led_numlock = true;}
  207. } else {
  208. if (led_numlock == true){
  209. led_numlock = false;}
  210. }
  211. if (leds & (1 << USB_LED_CAPS_LOCK)) {
  212. if (led_capslock == false){
  213. led_capslock = true;}
  214. } else {
  215. if (led_capslock == true){
  216. led_capslock = false;}
  217. }
  218. if (leds & (1 << USB_LED_SCROLL_LOCK)) {
  219. if (led_scrolllock == false){
  220. led_scrolllock = true;}
  221. } else {
  222. if (led_scrolllock == true){
  223. led_scrolllock = false;}
  224. }
  225. }
  226. uint32_t layer_state_set_kb(uint32_t state) {
  227. state = layer_state_set_user(state);
  228. layer = biton32(state);
  229. queue_for_send = true;
  230. return state;
  231. }
  232. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  233. queue_for_send = true;
  234. switch (keycode) {
  235. case OLED_TOGG:
  236. if(!clock_set_mode){
  237. if (record->event.pressed) {
  238. oled_mode = (oled_mode + 1) % _NUM_OLED_MODES;
  239. draw_ui();
  240. }
  241. }
  242. return false;
  243. case CLOCK_SET:
  244. if (record->event.pressed) {
  245. if(clock_set_mode){
  246. pre_encoder_mode_change();
  247. clock_set_mode = false;
  248. encoder_mode = previous_encoder_mode;
  249. post_encoder_mode_change();
  250. }else{
  251. previous_encoder_mode = encoder_mode;
  252. pre_encoder_mode_change();
  253. clock_set_mode = true;
  254. encoder_mode = ENC_MODE_CLOCK_SET;
  255. post_encoder_mode_change();
  256. }
  257. }
  258. return false;
  259. case ENC_PRESS:
  260. if (record->event.pressed) {
  261. uint16_t mapped_code = handle_encoder_press();
  262. uint16_t held_keycode_timer = timer_read();
  263. if(mapped_code != 0){
  264. register_code16(mapped_code);
  265. while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
  266. unregister_code16(mapped_code);
  267. }
  268. } else {
  269. // Do something else when release
  270. }
  271. return false;
  272. default:
  273. break;
  274. }
  275. return process_record_user(keycode, record);
  276. }
  277. void encoder_update_kb(uint8_t index, bool clockwise) {
  278. encoder_value = (encoder_value + (clockwise ? 1 : -1)) % 64;
  279. queue_for_send = true;
  280. if (index == 0) {
  281. if (layer == 0){
  282. uint16_t mapped_code = 0;
  283. if (clockwise) {
  284. mapped_code = handle_encoder_clockwise();
  285. } else {
  286. mapped_code = handle_encoder_ccw();
  287. }
  288. uint16_t held_keycode_timer = timer_read();
  289. if(mapped_code != 0){
  290. register_code16(mapped_code);
  291. while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
  292. unregister_code16(mapped_code);
  293. }
  294. } else {
  295. if(clockwise){
  296. change_encoder_mode(false);
  297. } else {
  298. change_encoder_mode(true);
  299. }
  300. }
  301. }
  302. }
  303. void custom_config_reset(void){
  304. void *p = (void*)(VIA_EEPROM_CUSTOM_CONFIG_ADDR);
  305. void *end = (void*)(VIA_EEPROM_CUSTOM_CONFIG_ADDR+VIA_EEPROM_CUSTOM_CONFIG_SIZE);
  306. while ( p != end ) {
  307. eeprom_update_byte(p, 0);
  308. ++p;
  309. }
  310. eeprom_update_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES, 0x1F);
  311. }
  312. void backlight_config_save(){
  313. eeprom_update_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT, kb_backlight_config.raw);
  314. }
  315. void custom_config_load(){
  316. kb_backlight_config.raw = eeprom_read_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT);
  317. #ifdef DYNAMIC_KEYMAP_ENABLE
  318. oled_mode = eeprom_read_byte((uint8_t*)EEPROM_DEFAULT_OLED);
  319. enabled_encoder_modes = eeprom_read_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES);
  320. #endif
  321. }
  322. // Called from via_init() if VIA_ENABLE
  323. // Called from matrix_init_kb() if not VIA_ENABLE
  324. void via_init_kb(void)
  325. {
  326. // If the EEPROM has the magic, the data is good.
  327. // OK to load from EEPROM.
  328. if (via_eeprom_is_valid()) {
  329. custom_config_load();
  330. } else {
  331. #ifdef DYNAMIC_KEYMAP_ENABLE
  332. // Reset the custom stuff
  333. custom_config_reset();
  334. #endif
  335. // DO NOT set EEPROM valid here, let caller do this
  336. }
  337. }
  338. void matrix_init_kb(void)
  339. {
  340. #ifndef VIA_ENABLE
  341. via_init_kb();
  342. via_eeprom_set_valid(true);
  343. #endif // VIA_ENABLE
  344. rtcGetTime(&RTCD1, &last_timespec);
  345. queue_for_send = true;
  346. backlight_init_ports();
  347. matrix_init_user();
  348. }
  349. void matrix_scan_kb(void) {
  350. rtcGetTime(&RTCD1, &last_timespec);
  351. uint16_t minutes_since_midnight = last_timespec.millisecond / 1000 / 60;
  352. if (minutes_since_midnight != last_minute){
  353. last_minute = minutes_since_midnight;
  354. if(!oled_sleeping){
  355. queue_for_send = true;
  356. }
  357. }
  358. #ifdef QWIIC_MICRO_OLED_ENABLE
  359. if (queue_for_send && oled_mode != OLED_OFF) {
  360. oled_sleeping = false;
  361. read_host_led_state();
  362. draw_ui();
  363. queue_for_send = false;
  364. }
  365. if (timer_elapsed(last_flush) > ScreenOffInterval && !oled_sleeping) {
  366. send_command(DISPLAYOFF); /* 0xAE */
  367. oled_sleeping = true;
  368. }
  369. #endif
  370. }
  371. //
  372. // In the case of VIA being disabled, we still need to check if
  373. // keyboard level EEPROM memory is valid before loading.
  374. // Thus these are copies of the same functions in VIA, since
  375. // the backlight settings reuse VIA's EEPROM magic/version,
  376. // and the ones in via.c won't be compiled in.
  377. //
  378. // Yes, this is sub-optimal, and is only here for completeness
  379. // (i.e. catering to the 1% of people that want wilba.tech LED bling
  380. // AND want persistent settings BUT DON'T want to use dynamic keymaps/VIA).
  381. //
  382. #ifndef VIA_ENABLE
  383. bool via_eeprom_is_valid(void)
  384. {
  385. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  386. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  387. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  388. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  389. return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
  390. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
  391. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
  392. }
  393. // Sets VIA/keyboard level usage of EEPROM to valid/invalid
  394. // Keyboard level code (eg. via_init_kb()) should not call this
  395. void via_eeprom_set_valid(bool valid)
  396. {
  397. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  398. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  399. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  400. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  401. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF);
  402. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF);
  403. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF);
  404. }
  405. void via_eeprom_reset(void)
  406. {
  407. // Set the VIA specific EEPROM state as invalid.
  408. via_eeprom_set_valid(false);
  409. // Set the TMK/QMK EEPROM state as invalid.
  410. eeconfig_disable();
  411. }
  412. #endif // VIA_ENABLE