satisfaction75.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. #ifdef VIA_ENABLE
  44. void backlight_get_value( uint8_t *data )
  45. {
  46. uint8_t *value_id = &(data[0]);
  47. uint8_t *value_data = &(data[1]);
  48. switch (*value_id)
  49. {
  50. case id_qmk_backlight_brightness:
  51. {
  52. // level / BACKLIGHT_LEVELS * 255
  53. value_data[0] = ((uint16_t)kb_backlight_config.level) * 255 / BACKLIGHT_LEVELS;
  54. break;
  55. }
  56. case id_qmk_backlight_effect:
  57. {
  58. value_data[0] = kb_backlight_config.breathing ? 1 : 0;
  59. break;
  60. }
  61. }
  62. }
  63. void backlight_set_value( uint8_t *data )
  64. {
  65. uint8_t *value_id = &(data[0]);
  66. uint8_t *value_data = &(data[1]);
  67. switch (*value_id)
  68. {
  69. case id_qmk_backlight_brightness:
  70. {
  71. // level / 255 * BACKLIGHT_LEVELS
  72. kb_backlight_config.level = ((uint16_t)value_data[0]) * BACKLIGHT_LEVELS / 255;
  73. backlight_set(kb_backlight_config.level);
  74. break;
  75. }
  76. case id_qmk_backlight_effect:
  77. {
  78. if ( value_data[0] == 0 ) {
  79. kb_backlight_config.breathing = false;
  80. breathing_disable();
  81. } else {
  82. kb_backlight_config.breathing = true;
  83. breathing_enable();
  84. }
  85. break;
  86. }
  87. }
  88. }
  89. void raw_hid_receive_kb( uint8_t *data, uint8_t length )
  90. {
  91. uint8_t *command_id = &(data[0]);
  92. uint8_t *command_data = &(data[1]);
  93. switch ( *command_id )
  94. {
  95. case id_get_keyboard_value:
  96. {
  97. switch( command_data[0])
  98. {
  99. case id_oled_default_mode:
  100. {
  101. uint8_t default_oled = eeprom_read_byte((uint8_t*)EEPROM_DEFAULT_OLED);
  102. command_data[1] = default_oled;
  103. break;
  104. }
  105. case id_oled_mode:
  106. {
  107. command_data[1] = oled_mode;
  108. break;
  109. }
  110. case id_encoder_modes:
  111. {
  112. command_data[1] = enabled_encoder_modes;
  113. break;
  114. }
  115. case id_encoder_custom:
  116. {
  117. uint8_t custom_encoder_idx = command_data[1];
  118. uint16_t keycode = retrieve_custom_encoder_config(custom_encoder_idx, ENC_CUSTOM_CW);
  119. command_data[2] = keycode >> 8;
  120. command_data[3] = keycode & 0xFF;
  121. keycode = retrieve_custom_encoder_config(custom_encoder_idx, ENC_CUSTOM_CCW);
  122. command_data[4] = keycode >> 8;
  123. command_data[5] = keycode & 0xFF;
  124. keycode = retrieve_custom_encoder_config(custom_encoder_idx, ENC_CUSTOM_PRESS);
  125. command_data[6] = keycode >> 8;
  126. command_data[7] = keycode & 0xFF;
  127. break;
  128. }
  129. default:
  130. {
  131. *command_id = id_unhandled;
  132. break;
  133. }
  134. }
  135. break;
  136. }
  137. case id_set_keyboard_value:
  138. {
  139. switch(command_data[0]){
  140. case id_oled_default_mode:
  141. {
  142. eeprom_update_byte((uint8_t*)EEPROM_DEFAULT_OLED, command_data[1]);
  143. break;
  144. }
  145. case id_oled_mode:
  146. {
  147. oled_mode = command_data[1];
  148. draw_ui();
  149. break;
  150. }
  151. case id_encoder_modes:
  152. {
  153. enabled_encoder_modes = command_data[1];
  154. eeprom_update_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES, enabled_encoder_modes);
  155. break;
  156. }
  157. case id_encoder_custom:
  158. {
  159. uint8_t custom_encoder_idx = command_data[1];
  160. uint8_t encoder_behavior = command_data[2];
  161. uint16_t keycode = (command_data[3] << 8) | command_data[4];
  162. set_custom_encoder_config(custom_encoder_idx, encoder_behavior, keycode);
  163. break;
  164. }
  165. default:
  166. {
  167. *command_id = id_unhandled;
  168. break;
  169. }
  170. }
  171. break;
  172. }
  173. case id_lighting_set_value:
  174. {
  175. backlight_set_value(command_data);
  176. break;
  177. }
  178. case id_lighting_get_value:
  179. {
  180. backlight_get_value(command_data);
  181. break;
  182. }
  183. case id_lighting_save:
  184. {
  185. backlight_config_save();
  186. break;
  187. }
  188. default:
  189. {
  190. // Unhandled message.
  191. *command_id = id_unhandled;
  192. break;
  193. }
  194. }
  195. // DO NOT call raw_hid_send(data,length) here, let caller do this
  196. }
  197. #endif
  198. void read_host_led_state(void) {
  199. uint8_t leds = host_keyboard_leds();
  200. if (leds & (1 << USB_LED_NUM_LOCK)) {
  201. if (led_numlock == false){
  202. led_numlock = true;}
  203. } else {
  204. if (led_numlock == true){
  205. led_numlock = false;}
  206. }
  207. if (leds & (1 << USB_LED_CAPS_LOCK)) {
  208. if (led_capslock == false){
  209. led_capslock = true;}
  210. } else {
  211. if (led_capslock == true){
  212. led_capslock = false;}
  213. }
  214. if (leds & (1 << USB_LED_SCROLL_LOCK)) {
  215. if (led_scrolllock == false){
  216. led_scrolllock = true;}
  217. } else {
  218. if (led_scrolllock == true){
  219. led_scrolllock = false;}
  220. }
  221. }
  222. uint32_t layer_state_set_kb(uint32_t state) {
  223. state = layer_state_set_user(state);
  224. layer = biton32(state);
  225. queue_for_send = true;
  226. return state;
  227. }
  228. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  229. queue_for_send = true;
  230. switch (keycode) {
  231. case OLED_TOGG:
  232. if(!clock_set_mode){
  233. if (record->event.pressed) {
  234. oled_mode = (oled_mode + 1) % _NUM_OLED_MODES;
  235. draw_ui();
  236. }
  237. }
  238. return false;
  239. case CLOCK_SET:
  240. if (record->event.pressed) {
  241. if(clock_set_mode){
  242. pre_encoder_mode_change();
  243. clock_set_mode = false;
  244. encoder_mode = previous_encoder_mode;
  245. post_encoder_mode_change();
  246. }else{
  247. previous_encoder_mode = encoder_mode;
  248. pre_encoder_mode_change();
  249. clock_set_mode = true;
  250. encoder_mode = ENC_MODE_CLOCK_SET;
  251. post_encoder_mode_change();
  252. }
  253. }
  254. return false;
  255. case ENC_PRESS:
  256. if (record->event.pressed) {
  257. uint16_t mapped_code = handle_encoder_press();
  258. uint16_t held_keycode_timer = timer_read();
  259. if(mapped_code != 0){
  260. register_code16(mapped_code);
  261. while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
  262. unregister_code16(mapped_code);
  263. }
  264. } else {
  265. // Do something else when release
  266. }
  267. return false;
  268. default:
  269. break;
  270. }
  271. return process_record_user(keycode, record);
  272. }
  273. void encoder_update_kb(uint8_t index, bool clockwise) {
  274. encoder_value = (encoder_value + (clockwise ? 1 : -1)) % 64;
  275. queue_for_send = true;
  276. if (index == 0) {
  277. if (layer == 0){
  278. uint16_t mapped_code = 0;
  279. if (clockwise) {
  280. mapped_code = handle_encoder_clockwise();
  281. } else {
  282. mapped_code = handle_encoder_ccw();
  283. }
  284. uint16_t held_keycode_timer = timer_read();
  285. if(mapped_code != 0){
  286. register_code16(mapped_code);
  287. while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
  288. unregister_code16(mapped_code);
  289. }
  290. } else {
  291. if(clockwise){
  292. change_encoder_mode(false);
  293. } else {
  294. change_encoder_mode(true);
  295. }
  296. }
  297. }
  298. }
  299. void custom_config_reset(void){
  300. void *p = (void*)(VIA_EEPROM_CUSTOM_CONFIG_ADDR);
  301. void *end = (void*)(VIA_EEPROM_CUSTOM_CONFIG_ADDR+VIA_EEPROM_CUSTOM_CONFIG_SIZE);
  302. while ( p != end ) {
  303. eeprom_update_byte(p, 0);
  304. ++p;
  305. }
  306. eeprom_update_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES, 0x1F);
  307. }
  308. void backlight_config_save(){
  309. eeprom_update_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT, kb_backlight_config.raw);
  310. }
  311. void custom_config_load(){
  312. kb_backlight_config.raw = eeprom_read_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT);
  313. #ifdef DYNAMIC_KEYMAP_ENABLE
  314. oled_mode = eeprom_read_byte((uint8_t*)EEPROM_DEFAULT_OLED);
  315. enabled_encoder_modes = eeprom_read_byte((uint8_t*)EEPROM_ENABLED_ENCODER_MODES);
  316. #endif
  317. }
  318. // Called from via_init() if VIA_ENABLE
  319. // Called from matrix_init_kb() if not VIA_ENABLE
  320. void via_init_kb(void)
  321. {
  322. // If the EEPROM has the magic, the data is good.
  323. // OK to load from EEPROM.
  324. if (via_eeprom_is_valid()) {
  325. custom_config_load();
  326. } else {
  327. #ifdef DYNAMIC_KEYMAP_ENABLE
  328. // Reset the custom stuff
  329. custom_config_reset();
  330. #endif
  331. // DO NOT set EEPROM valid here, let caller do this
  332. }
  333. }
  334. void matrix_init_kb(void)
  335. {
  336. #ifndef VIA_ENABLE
  337. via_init_kb();
  338. via_eeprom_set_valid(true);
  339. #endif // VIA_ENABLE
  340. rtcGetTime(&RTCD1, &last_timespec);
  341. queue_for_send = true;
  342. backlight_init_ports();
  343. matrix_init_user();
  344. }
  345. void matrix_scan_kb(void) {
  346. rtcGetTime(&RTCD1, &last_timespec);
  347. uint16_t minutes_since_midnight = last_timespec.millisecond / 1000 / 60;
  348. if (minutes_since_midnight != last_minute){
  349. last_minute = minutes_since_midnight;
  350. if(!oled_sleeping){
  351. queue_for_send = true;
  352. }
  353. }
  354. #ifdef QWIIC_MICRO_OLED_ENABLE
  355. if (queue_for_send && oled_mode != OLED_OFF) {
  356. oled_sleeping = false;
  357. read_host_led_state();
  358. draw_ui();
  359. queue_for_send = false;
  360. }
  361. if (timer_elapsed(last_flush) > ScreenOffInterval && !oled_sleeping) {
  362. send_command(DISPLAYOFF); /* 0xAE */
  363. oled_sleeping = true;
  364. }
  365. #endif
  366. }
  367. //
  368. // In the case of VIA being disabled, we still need to check if
  369. // keyboard level EEPROM memory is valid before loading.
  370. // Thus these are copies of the same functions in VIA, since
  371. // the backlight settings reuse VIA's EEPROM magic/version,
  372. // and the ones in via.c won't be compiled in.
  373. //
  374. // Yes, this is sub-optimal, and is only here for completeness
  375. // (i.e. catering to the 1% of people that want wilba.tech LED bling
  376. // AND want persistent settings BUT DON'T want to use dynamic keymaps/VIA).
  377. //
  378. #ifndef VIA_ENABLE
  379. bool via_eeprom_is_valid(void)
  380. {
  381. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  382. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  383. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  384. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  385. return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
  386. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
  387. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
  388. }
  389. // Sets VIA/keyboard level usage of EEPROM to valid/invalid
  390. // Keyboard level code (eg. via_init_kb()) should not call this
  391. void via_eeprom_set_valid(bool valid)
  392. {
  393. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  394. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  395. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  396. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  397. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF);
  398. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF);
  399. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF);
  400. }
  401. void via_eeprom_reset(void)
  402. {
  403. // Set the VIA specific EEPROM state as invalid.
  404. via_eeprom_set_valid(false);
  405. // Set the TMK/QMK EEPROM state as invalid.
  406. eeconfig_disable();
  407. }
  408. #endif // VIA_ENABLE