satisfaction_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // Copyright 2023 Andrew Kannan
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "satisfaction_core.h"
  4. #include "print.h"
  5. #include "debug.h"
  6. #include "matrix.h"
  7. #include "quantum.h"
  8. #include "encoder.h"
  9. #include <ch.h>
  10. #include <hal.h>
  11. #include "timer.h"
  12. #include "raw_hid.h"
  13. #include "dynamic_keymap.h"
  14. #include "eeprom.h"
  15. #include "version.h" // for QMK_BUILDDATE used in EEPROM magic
  16. /* Artificial delay added to get media keys to work in the encoder*/
  17. #define MEDIA_KEY_DELAY 10
  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 clock_set_mode = false;
  23. uint8_t oled_mode = OLED_DEFAULT;
  24. bool oled_repaint_requested = false;
  25. bool oled_wakeup_requested = false;
  26. uint32_t oled_sleep_timer;
  27. uint8_t encoder_value = 32;
  28. uint8_t encoder_mode = ENC_MODE_VOLUME;
  29. uint8_t enabled_encoder_modes = 0x1F;
  30. RTCDateTime last_timespec;
  31. uint16_t last_minute = 0;
  32. uint8_t time_config_idx = 0;
  33. int8_t hour_config = 0;
  34. int16_t minute_config = 0;
  35. int8_t year_config = 0;
  36. int8_t month_config = 0;
  37. int8_t day_config = 0;
  38. uint8_t previous_encoder_mode = 0;
  39. void board_init(void) {
  40. SYSCFG->CFGR1 |= SYSCFG_CFGR1_I2C1_DMA_RMP;
  41. SYSCFG->CFGR1 &= ~(SYSCFG_CFGR1_SPI2_DMA_RMP);
  42. }
  43. void keyboard_post_init_kb(void) {
  44. /*
  45. This is a workaround to some really weird behavior
  46. Without this code, the OLED will turn on, but not when you initially plug the keyboard in.
  47. You have to manually trigger a user reset to get the OLED to initialize properly
  48. I'm not sure what the root cause is at this time, but this workaround fixes it.
  49. */
  50. #ifdef OLED_ENABLE
  51. if(!is_oled_on()){
  52. wait_ms(3000);
  53. oled_init(OLED_ROTATION_0);
  54. }
  55. #endif
  56. keyboard_post_init_user();
  57. }
  58. #ifdef VIA_ENABLE
  59. void custom_set_value(uint8_t *data) {
  60. uint8_t *value_id = &(data[0]);
  61. uint8_t *value_data = &(data[1]);
  62. switch ( *value_id ) {
  63. case id_oled_default_mode:
  64. {
  65. eeprom_update_byte((uint8_t*)EEPROM_DEFAULT_OLED, value_data[0]);
  66. break;
  67. }
  68. case id_oled_mode:
  69. {
  70. oled_mode = value_data[0];
  71. oled_request_wakeup();
  72. break;
  73. }
  74. case id_encoder_modes:
  75. {
  76. uint8_t index = value_data[0];
  77. uint8_t enable = value_data[1];
  78. enabled_encoder_modes = (enabled_encoder_modes & ~(1<<index)) | (enable<<index);
  79. eeprom_update_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES, enabled_encoder_modes);
  80. break;
  81. }
  82. case id_encoder_custom:
  83. {
  84. uint8_t custom_encoder_idx = value_data[0];
  85. uint8_t encoder_behavior = value_data[1];
  86. uint16_t keycode = (value_data[2] << 8) | value_data[3];
  87. set_custom_encoder_config(custom_encoder_idx, encoder_behavior, keycode);
  88. break;
  89. }
  90. }
  91. }
  92. void custom_get_value(uint8_t *data) {
  93. uint8_t *value_id = &(data[0]);
  94. uint8_t *value_data = &(data[1]);
  95. switch ( *value_id ) {
  96. case id_oled_default_mode:
  97. {
  98. uint8_t default_oled = eeprom_read_byte((uint8_t*)EEPROM_DEFAULT_OLED);
  99. value_data[0] = default_oled;
  100. break;
  101. }
  102. case id_oled_mode:
  103. {
  104. value_data[0] = oled_mode;
  105. break;
  106. }
  107. case id_encoder_modes:
  108. {
  109. uint8_t index = value_data[0];
  110. value_data[1] = (enabled_encoder_modes & (1<<index)) ? 1 : 0;
  111. break;
  112. }
  113. case id_encoder_custom:
  114. {
  115. uint8_t custom_encoder_idx = value_data[0];
  116. uint8_t encoder_behavior = value_data[1];
  117. uint16_t keycode = retrieve_custom_encoder_config(custom_encoder_idx, encoder_behavior);
  118. value_data[2] = keycode >> 8;
  119. value_data[3] = keycode & 0xFF;
  120. break;
  121. }
  122. }
  123. }
  124. void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
  125. uint8_t *command_id = &(data[0]);
  126. uint8_t *channel_id = &(data[1]);
  127. uint8_t *value_id_and_data = &(data[2]);
  128. if ( *channel_id == id_custom_channel ) {
  129. switch ( *command_id )
  130. {
  131. case id_custom_set_value:
  132. {
  133. custom_set_value(value_id_and_data);
  134. break;
  135. }
  136. case id_custom_get_value:
  137. {
  138. custom_get_value(value_id_and_data);
  139. break;
  140. }
  141. case id_custom_save:
  142. {
  143. // values are saved in custom_set_value()
  144. break;
  145. }
  146. default:
  147. {
  148. // Unhandled message.
  149. *command_id = id_unhandled;
  150. break;
  151. }
  152. }
  153. return;
  154. }
  155. *command_id = id_unhandled;
  156. // DO NOT call raw_hid_send(data,length) here, let caller do this
  157. }
  158. #endif
  159. void read_host_led_state(void) {
  160. led_t led_state = host_keyboard_led_state();
  161. if (led_state.num_lock) {
  162. if (led_numlock == false){
  163. led_numlock = true;}
  164. } else {
  165. if (led_numlock == true){
  166. led_numlock = false;}
  167. }
  168. if (led_state.caps_lock) {
  169. if (led_capslock == false){
  170. led_capslock = true;}
  171. } else {
  172. if (led_capslock == true){
  173. led_capslock = false;}
  174. }
  175. if (led_state.scroll_lock) {
  176. if (led_scrolllock == false){
  177. led_scrolllock = true;}
  178. } else {
  179. if (led_scrolllock == true){
  180. led_scrolllock = false;}
  181. }
  182. }
  183. layer_state_t layer_state_set_kb(layer_state_t state) {
  184. state = layer_state_set_user(state);
  185. layer = get_highest_layer(state);
  186. oled_request_wakeup();
  187. return state;
  188. }
  189. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  190. oled_request_wakeup();
  191. switch (keycode) {
  192. case OLED_TOGG:
  193. if(!clock_set_mode){
  194. if (record->event.pressed) {
  195. oled_mode = (oled_mode + 1) % _NUM_OLED_MODES;
  196. }
  197. }
  198. return false;
  199. case CLOCK_SET:
  200. if (record->event.pressed) {
  201. if(clock_set_mode){
  202. pre_encoder_mode_change();
  203. clock_set_mode = false;
  204. encoder_mode = previous_encoder_mode;
  205. post_encoder_mode_change();
  206. }else{
  207. previous_encoder_mode = encoder_mode;
  208. pre_encoder_mode_change();
  209. clock_set_mode = true;
  210. encoder_mode = ENC_MODE_CLOCK_SET;
  211. post_encoder_mode_change();
  212. }
  213. }
  214. return false;
  215. case ENC_PRESS:
  216. if (record->event.pressed) {
  217. uint16_t mapped_code = handle_encoder_press();
  218. uint16_t held_keycode_timer = timer_read();
  219. if(mapped_code != 0){
  220. register_code16(mapped_code);
  221. while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
  222. unregister_code16(mapped_code);
  223. }
  224. } else {
  225. // Do something else when release
  226. }
  227. return false;
  228. default:
  229. break;
  230. }
  231. return process_record_user(keycode, record);
  232. }
  233. bool encoder_update_kb(uint8_t index, bool clockwise) {
  234. if (!encoder_update_user(index, clockwise)) return false;
  235. oled_request_wakeup();
  236. encoder_value = (encoder_value + (clockwise ? 1 : -1)) % 64;
  237. if (index == 0) {
  238. if (layer == 0){
  239. uint16_t mapped_code = 0;
  240. if (clockwise) {
  241. mapped_code = handle_encoder_clockwise();
  242. } else {
  243. mapped_code = handle_encoder_ccw();
  244. }
  245. uint16_t held_keycode_timer = timer_read();
  246. if(mapped_code != 0){
  247. register_code16(mapped_code);
  248. while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
  249. unregister_code16(mapped_code);
  250. }
  251. } else {
  252. if(clockwise){
  253. change_encoder_mode(false);
  254. } else {
  255. change_encoder_mode(true);
  256. }
  257. }
  258. }
  259. return true;
  260. }
  261. void custom_config_reset(void){
  262. void *p = (void*)(VIA_EEPROM_CUSTOM_CONFIG_ADDR);
  263. void *end = (void*)(VIA_EEPROM_CUSTOM_CONFIG_ADDR+VIA_EEPROM_CUSTOM_CONFIG_SIZE);
  264. while ( p != end ) {
  265. eeprom_update_byte(p, 0);
  266. ++p;
  267. }
  268. eeprom_update_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES, 0x1F);
  269. }
  270. void custom_config_load(void){
  271. #ifdef DYNAMIC_KEYMAP_ENABLE
  272. oled_mode = eeprom_read_byte((uint8_t*)EEPROM_DEFAULT_OLED);
  273. enabled_encoder_modes = eeprom_read_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES);
  274. #endif
  275. }
  276. // Called from via_init() if VIA_ENABLE
  277. // Called from matrix_init_kb() if not VIA_ENABLE
  278. void via_init_kb(void)
  279. {
  280. // This checks both an EEPROM reset (from bootmagic lite, keycodes)
  281. // and also firmware build date (from via_eeprom_is_valid())
  282. if (eeconfig_is_enabled()) {
  283. custom_config_load();
  284. } else {
  285. #ifdef DYNAMIC_KEYMAP_ENABLE
  286. // Reset the custom stuff
  287. custom_config_reset();
  288. #endif
  289. // DO NOT set EEPROM valid here, let caller do this
  290. }
  291. }
  292. void matrix_init_kb(void)
  293. {
  294. #ifndef VIA_ENABLE
  295. via_init_kb();
  296. via_eeprom_set_valid(true);
  297. #endif // VIA_ENABLE
  298. rtcGetTime(&RTCD1, &last_timespec);
  299. matrix_init_user();
  300. oled_request_wakeup();
  301. }
  302. void housekeeping_task_kb(void) {
  303. rtcGetTime(&RTCD1, &last_timespec);
  304. uint16_t minutes_since_midnight = last_timespec.millisecond / 1000 / 60;
  305. if (minutes_since_midnight != last_minute){
  306. last_minute = minutes_since_midnight;
  307. oled_request_repaint();
  308. }
  309. }
  310. //
  311. // In the case of VIA being disabled, we still need to check if
  312. // keyboard level EEPROM memory is valid before loading.
  313. // Thus these are copies of the same functions in VIA, since
  314. // the backlight settings reuse VIA's EEPROM magic/version,
  315. // and the ones in via.c won't be compiled in.
  316. //
  317. // Yes, this is sub-optimal, and is only here for completeness
  318. // (i.e. catering to the 1% of people that want wilba.tech LED bling
  319. // AND want persistent settings BUT DON'T want to use dynamic keymaps/VIA).
  320. //
  321. #ifndef VIA_ENABLE
  322. bool via_eeprom_is_valid(void)
  323. {
  324. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  325. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  326. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  327. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  328. return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
  329. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
  330. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
  331. }
  332. // Sets VIA/keyboard level usage of EEPROM to valid/invalid
  333. // Keyboard level code (eg. via_init_kb()) should not call this
  334. void via_eeprom_set_valid(bool valid)
  335. {
  336. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  337. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  338. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  339. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  340. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF);
  341. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF);
  342. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF);
  343. }
  344. void via_eeprom_reset(void)
  345. {
  346. // Set the VIA specific EEPROM state as invalid.
  347. via_eeprom_set_valid(false);
  348. // Set the TMK/QMK EEPROM state as invalid.
  349. eeconfig_disable();
  350. }
  351. #endif // VIA_ENABLE