satisfaction75.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "timer.h"
  10. #include "raw_hid.h"
  11. #include "dynamic_keymap.h"
  12. #include "tmk_core/common/eeprom.h"
  13. // HACK
  14. #include "keyboards/zeal60/zeal60_api.h" // Temporary hack
  15. #include "keyboards/zeal60/zeal60_keycodes.h" // Temporary hack
  16. /* Artificial delay added to get media keys to work in the encoder*/
  17. #define MEDIA_KEY_DELAY 10
  18. uint16_t last_flush;
  19. volatile uint8_t led_numlock = false;
  20. volatile uint8_t led_capslock = false;
  21. volatile uint8_t led_scrolllock = false;
  22. uint8_t layer;
  23. bool queue_for_send = false;
  24. bool clock_set_mode = false;
  25. uint8_t oled_mode = OLED_DEFAULT;
  26. bool oled_sleeping = false;
  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. backlight_config_t kb_backlight_config = {
  40. .enable = true,
  41. .breathing = true,
  42. .level = BACKLIGHT_LEVELS
  43. };
  44. bool eeprom_is_valid(void)
  45. {
  46. return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
  47. eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
  48. }
  49. void eeprom_set_valid(bool valid)
  50. {
  51. eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
  52. eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
  53. }
  54. void eeprom_reset(void)
  55. {
  56. // Set the Zeal60 specific EEPROM state as invalid.
  57. eeprom_set_valid(false);
  58. // Set the TMK/QMK EEPROM state as invalid.
  59. eeconfig_disable();
  60. }
  61. #ifdef RAW_ENABLE
  62. void raw_hid_receive( uint8_t *data, uint8_t length )
  63. {
  64. uint8_t *command_id = &(data[0]);
  65. uint8_t *command_data = &(data[1]);
  66. switch ( *command_id )
  67. {
  68. case id_get_protocol_version:
  69. {
  70. command_data[0] = PROTOCOL_VERSION >> 8;
  71. command_data[1] = PROTOCOL_VERSION & 0xFF;
  72. break;
  73. }
  74. case id_get_keyboard_value:
  75. {
  76. if ( command_data[0] == id_uptime )
  77. {
  78. uint32_t value = timer_read32();
  79. command_data[1] = (value >> 24 ) & 0xFF;
  80. command_data[2] = (value >> 16 ) & 0xFF;
  81. command_data[3] = (value >> 8 ) & 0xFF;
  82. command_data[4] = value & 0xFF;
  83. }
  84. else
  85. {
  86. *command_id = id_unhandled;
  87. }
  88. break;
  89. }
  90. #ifdef DYNAMIC_KEYMAP_ENABLE
  91. case id_dynamic_keymap_get_keycode:
  92. {
  93. uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
  94. command_data[3] = keycode >> 8;
  95. command_data[4] = keycode & 0xFF;
  96. break;
  97. }
  98. case id_dynamic_keymap_set_keycode:
  99. {
  100. dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
  101. break;
  102. }
  103. case id_dynamic_keymap_reset:
  104. {
  105. dynamic_keymap_reset();
  106. break;
  107. }
  108. case id_dynamic_keymap_macro_get_count:
  109. {
  110. command_data[0] = dynamic_keymap_macro_get_count();
  111. break;
  112. }
  113. case id_dynamic_keymap_macro_get_buffer_size:
  114. {
  115. uint16_t size = dynamic_keymap_macro_get_buffer_size();
  116. command_data[0] = size >> 8;
  117. command_data[1] = size & 0xFF;
  118. break;
  119. }
  120. case id_dynamic_keymap_macro_get_buffer:
  121. {
  122. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  123. uint16_t size = command_data[2]; // size <= 28
  124. dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
  125. break;
  126. }
  127. case id_dynamic_keymap_macro_set_buffer:
  128. {
  129. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  130. uint16_t size = command_data[2]; // size <= 28
  131. dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
  132. break;
  133. }
  134. case id_dynamic_keymap_macro_reset:
  135. {
  136. dynamic_keymap_macro_reset();
  137. break;
  138. }
  139. case id_dynamic_keymap_get_layer_count:
  140. {
  141. command_data[0] = dynamic_keymap_get_layer_count();
  142. break;
  143. }
  144. case id_dynamic_keymap_get_buffer:
  145. {
  146. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  147. uint16_t size = command_data[2]; // size <= 28
  148. dynamic_keymap_get_buffer( offset, size, &command_data[3] );
  149. break;
  150. }
  151. case id_dynamic_keymap_set_buffer:
  152. {
  153. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  154. uint16_t size = command_data[2]; // size <= 28
  155. dynamic_keymap_set_buffer( offset, size, &command_data[3] );
  156. break;
  157. }
  158. #endif // DYNAMIC_KEYMAP_ENABLE
  159. case id_eeprom_reset:
  160. {
  161. eeprom_reset();
  162. break;
  163. }
  164. case id_bootloader_jump:
  165. {
  166. // Need to send data back before the jump
  167. // Informs host that the command is handled
  168. raw_hid_send( data, length );
  169. // Give host time to read it
  170. wait_ms(100);
  171. bootloader_jump();
  172. break;
  173. }
  174. default:
  175. {
  176. // Unhandled message.
  177. *command_id = id_unhandled;
  178. break;
  179. }
  180. }
  181. // Return same buffer with values changed
  182. raw_hid_send( data, length );
  183. }
  184. #endif
  185. void read_host_led_state(void) {
  186. uint8_t leds = host_keyboard_leds();
  187. if (leds & (1 << USB_LED_NUM_LOCK)) {
  188. if (led_numlock == false){
  189. led_numlock = true;}
  190. } else {
  191. if (led_numlock == true){
  192. led_numlock = false;}
  193. }
  194. if (leds & (1 << USB_LED_CAPS_LOCK)) {
  195. if (led_capslock == false){
  196. led_capslock = true;}
  197. } else {
  198. if (led_capslock == true){
  199. led_capslock = false;}
  200. }
  201. if (leds & (1 << USB_LED_SCROLL_LOCK)) {
  202. if (led_scrolllock == false){
  203. led_scrolllock = true;}
  204. } else {
  205. if (led_scrolllock == true){
  206. led_scrolllock = false;}
  207. }
  208. }
  209. uint32_t layer_state_set_kb(uint32_t state) {
  210. state = layer_state_set_user(state);
  211. layer = biton32(state);
  212. queue_for_send = true;
  213. return state;
  214. }
  215. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  216. queue_for_send = true;
  217. switch (keycode) {
  218. case OLED_TOGG:
  219. if (record->event.pressed) {
  220. oled_mode = (oled_mode + 1) % _NUM_OLED_MODES;
  221. draw_ui();
  222. }
  223. return false;
  224. case CLOCK_SET:
  225. if (record->event.pressed) {
  226. if(clock_set_mode){
  227. pre_encoder_mode_change();
  228. clock_set_mode = false;
  229. encoder_mode = previous_encoder_mode;
  230. post_encoder_mode_change();
  231. }else{
  232. previous_encoder_mode = encoder_mode;
  233. pre_encoder_mode_change();
  234. clock_set_mode = true;
  235. encoder_mode = ENC_MODE_CLOCK_SET;
  236. post_encoder_mode_change();
  237. }
  238. }
  239. return false;
  240. case ENC_PRESS:
  241. if (record->event.pressed) {
  242. uint16_t mapped_code = handle_encoder_press();
  243. uint16_t held_keycode_timer = timer_read();
  244. if(mapped_code != 0){
  245. register_code(mapped_code);
  246. while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
  247. unregister_code(mapped_code);
  248. }
  249. } else {
  250. // Do something else when release
  251. }
  252. return false;
  253. default:
  254. break;
  255. }
  256. #ifdef DYNAMIC_KEYMAP_ENABLE
  257. // Handle macros
  258. if (record->event.pressed) {
  259. if ( keycode >= MACRO00 && keycode <= MACRO15 )
  260. {
  261. uint8_t id = keycode - MACRO00;
  262. dynamic_keymap_macro_send(id);
  263. return false;
  264. }
  265. }
  266. #endif //DYNAMIC_KEYMAP_ENABLE
  267. return process_record_user(keycode, record);
  268. }
  269. void encoder_update_kb(uint8_t index, bool clockwise) {
  270. encoder_value = (encoder_value + (clockwise ? 1 : -1)) % 64;
  271. queue_for_send = true;
  272. if (index == 0) {
  273. if (layer == 0){
  274. uint16_t mapped_code = 0;
  275. if (clockwise) {
  276. mapped_code = handle_encoder_clockwise();
  277. } else {
  278. mapped_code = handle_encoder_ccw();
  279. }
  280. uint16_t held_keycode_timer = timer_read();
  281. if(mapped_code != 0){
  282. register_code(mapped_code);
  283. while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
  284. unregister_code(mapped_code);
  285. }
  286. } else {
  287. if(clockwise){
  288. change_encoder_mode(false);
  289. } else {
  290. change_encoder_mode(true);
  291. }
  292. }
  293. }
  294. }
  295. void eeprom_init_kb(void)
  296. {
  297. // If the EEPROM has the magic, the data is good.
  298. // OK to load from EEPROM.
  299. if (eeprom_is_valid()) {
  300. //backlight_config_load();
  301. } else {
  302. // If the EEPROM has not been saved before, or is out of date,
  303. // save the default values to the EEPROM. Default values
  304. // come from construction of the zeal_backlight_config instance.
  305. //backlight_config_save();
  306. #ifdef DYNAMIC_KEYMAP_ENABLE
  307. // This resets the keymaps in EEPROM to what is in flash.
  308. dynamic_keymap_reset();
  309. // This resets the macros in EEPROM to nothing.
  310. dynamic_keymap_macro_reset();
  311. #endif
  312. // Save the magic number last, in case saving was interrupted
  313. eeprom_set_valid(true);
  314. }
  315. }
  316. void matrix_init_kb(void)
  317. {
  318. eeprom_init_kb();
  319. rtcGetTime(&RTCD1, &last_timespec);
  320. queue_for_send = true;
  321. backlight_init_ports();
  322. matrix_init_user();
  323. }
  324. void matrix_scan_kb(void) {
  325. rtcGetTime(&RTCD1, &last_timespec);
  326. uint16_t minutes_since_midnight = last_timespec.millisecond / 1000 / 60;
  327. if (minutes_since_midnight != last_minute){
  328. last_minute = minutes_since_midnight;
  329. if(!oled_sleeping){
  330. queue_for_send = true;
  331. }
  332. }
  333. if (queue_for_send && oled_mode != OLED_OFF) {
  334. oled_sleeping = false;
  335. read_host_led_state();
  336. draw_ui();
  337. queue_for_send = false;
  338. }
  339. if (timer_elapsed(last_flush) > ScreenOffInterval && !oled_sleeping) {
  340. send_command(DISPLAYOFF); /* 0xAE */
  341. oled_sleeping = true;
  342. }
  343. }