satisfaction75.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. bool encoder_update_kb(uint8_t index, bool clockwise) {
  278. if (!encoder_update_user(index, clockwise)) return false;
  279. encoder_value = (encoder_value + (clockwise ? 1 : -1)) % 64;
  280. queue_for_send = true;
  281. if (index == 0) {
  282. if (layer == 0){
  283. uint16_t mapped_code = 0;
  284. if (clockwise) {
  285. mapped_code = handle_encoder_clockwise();
  286. } else {
  287. mapped_code = handle_encoder_ccw();
  288. }
  289. uint16_t held_keycode_timer = timer_read();
  290. if(mapped_code != 0){
  291. register_code16(mapped_code);
  292. while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
  293. unregister_code16(mapped_code);
  294. }
  295. } else {
  296. if(clockwise){
  297. change_encoder_mode(false);
  298. } else {
  299. change_encoder_mode(true);
  300. }
  301. }
  302. }
  303. return true;
  304. }
  305. void custom_config_reset(void){
  306. void *p = (void*)(VIA_EEPROM_CUSTOM_CONFIG_ADDR);
  307. void *end = (void*)(VIA_EEPROM_CUSTOM_CONFIG_ADDR+VIA_EEPROM_CUSTOM_CONFIG_SIZE);
  308. while ( p != end ) {
  309. eeprom_update_byte(p, 0);
  310. ++p;
  311. }
  312. eeprom_update_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES, 0x1F);
  313. }
  314. void backlight_config_save(){
  315. eeprom_update_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT, kb_backlight_config.raw);
  316. }
  317. void custom_config_load(){
  318. kb_backlight_config.raw = eeprom_read_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT);
  319. #ifdef DYNAMIC_KEYMAP_ENABLE
  320. oled_mode = eeprom_read_byte((uint8_t*)EEPROM_DEFAULT_OLED);
  321. enabled_encoder_modes = eeprom_read_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES);
  322. #endif
  323. }
  324. // Called from via_init() if VIA_ENABLE
  325. // Called from matrix_init_kb() if not VIA_ENABLE
  326. void via_init_kb(void)
  327. {
  328. // If the EEPROM has the magic, the data is good.
  329. // OK to load from EEPROM.
  330. if (via_eeprom_is_valid()) {
  331. custom_config_load();
  332. } else {
  333. #ifdef DYNAMIC_KEYMAP_ENABLE
  334. // Reset the custom stuff
  335. custom_config_reset();
  336. #endif
  337. // DO NOT set EEPROM valid here, let caller do this
  338. }
  339. }
  340. void matrix_init_kb(void)
  341. {
  342. #ifndef VIA_ENABLE
  343. via_init_kb();
  344. via_eeprom_set_valid(true);
  345. #endif // VIA_ENABLE
  346. rtcGetTime(&RTCD1, &last_timespec);
  347. queue_for_send = true;
  348. backlight_init_ports();
  349. matrix_init_user();
  350. }
  351. void housekeeping_task_kb(void) {
  352. rtcGetTime(&RTCD1, &last_timespec);
  353. uint16_t minutes_since_midnight = last_timespec.millisecond / 1000 / 60;
  354. if (minutes_since_midnight != last_minute){
  355. last_minute = minutes_since_midnight;
  356. if(!oled_sleeping){
  357. queue_for_send = true;
  358. }
  359. }
  360. #ifdef QWIIC_MICRO_OLED_ENABLE
  361. if (queue_for_send && oled_mode != OLED_OFF) {
  362. oled_sleeping = false;
  363. read_host_led_state();
  364. draw_ui();
  365. queue_for_send = false;
  366. }
  367. if (timer_elapsed(last_flush) > ScreenOffInterval && !oled_sleeping) {
  368. send_command(DISPLAYOFF); /* 0xAE */
  369. oled_sleeping = true;
  370. }
  371. #endif
  372. }
  373. //
  374. // In the case of VIA being disabled, we still need to check if
  375. // keyboard level EEPROM memory is valid before loading.
  376. // Thus these are copies of the same functions in VIA, since
  377. // the backlight settings reuse VIA's EEPROM magic/version,
  378. // and the ones in via.c won't be compiled in.
  379. //
  380. // Yes, this is sub-optimal, and is only here for completeness
  381. // (i.e. catering to the 1% of people that want wilba.tech LED bling
  382. // AND want persistent settings BUT DON'T want to use dynamic keymaps/VIA).
  383. //
  384. #ifndef VIA_ENABLE
  385. bool via_eeprom_is_valid(void)
  386. {
  387. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  388. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  389. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  390. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  391. return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
  392. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
  393. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
  394. }
  395. // Sets VIA/keyboard level usage of EEPROM to valid/invalid
  396. // Keyboard level code (eg. via_init_kb()) should not call this
  397. void via_eeprom_set_valid(bool valid)
  398. {
  399. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  400. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  401. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  402. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  403. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF);
  404. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF);
  405. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF);
  406. }
  407. void via_eeprom_reset(void)
  408. {
  409. // Set the VIA specific EEPROM state as invalid.
  410. via_eeprom_set_valid(false);
  411. // Set the TMK/QMK EEPROM state as invalid.
  412. eeconfig_disable();
  413. }
  414. #endif // VIA_ENABLE