dynamic_macro.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */
  2. #ifndef DYNAMIC_MACROS_H
  3. #define DYNAMIC_MACROS_H
  4. #include "action_layer.h"
  5. #ifndef DYNAMIC_MACRO_SIZE
  6. /* May be overridden with a custom value. Be aware that the effective
  7. * macro length is half of this value: each keypress is recorded twice
  8. * because of the down-event and up-event. This is not a bug, it's the
  9. * intended behavior.
  10. *
  11. * Usually it should be fine to set the macro size to at least 256 but
  12. * there have been reports of it being too much in some users' cases,
  13. * so 128 is considered a safe default.
  14. */
  15. #define DYNAMIC_MACRO_SIZE 128
  16. #endif
  17. /* DYNAMIC_MACRO_RANGE must be set as the last element of user's
  18. * "planck_keycodes" enum prior to including this header. This allows
  19. * us to 'extend' it.
  20. */
  21. enum dynamic_macro_keycodes {
  22. DYN_REC_START1 = DYNAMIC_MACRO_RANGE,
  23. DYN_REC_START2,
  24. DYN_MACRO_PLAY1,
  25. DYN_MACRO_PLAY2,
  26. };
  27. /* Blink the LEDs to notify the user about some event. */
  28. void dynamic_macro_led_blink(void)
  29. {
  30. backlight_toggle();
  31. _delay_ms(100);
  32. backlight_toggle();
  33. }
  34. /**
  35. * Start recording of the dynamic macro.
  36. *
  37. * @param[out] macro_pointer The new macro buffer iterator.
  38. * @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
  39. */
  40. void dynamic_macro_record_start(
  41. keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
  42. {
  43. dynamic_macro_led_blink();
  44. clear_keyboard();
  45. layer_clear();
  46. *macro_pointer = macro_buffer;
  47. }
  48. /**
  49. * Play the dynamic macro.
  50. *
  51. * @param macro_buffer[in] The beginning of the macro buffer being played.
  52. * @param macro_end[in] The element after the last macro buffer element.
  53. * @param direction[in] Either +1 or -1, which way to iterate the buffer.
  54. */
  55. void dynamic_macro_play(
  56. keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
  57. {
  58. uint32_t saved_layer_state = layer_state;
  59. clear_keyboard();
  60. layer_clear();
  61. while (macro_buffer != macro_end) {
  62. process_record(macro_buffer);
  63. macro_buffer += direction;
  64. }
  65. clear_keyboard();
  66. layer_state = saved_layer_state;
  67. }
  68. /**
  69. * Record a single key in a dynamic macro.
  70. *
  71. * @param macro_pointer[in,out] The current buffer position.
  72. * @param macro_end2[in] The end of the other macro which shouldn't be overwritten.
  73. * @param direction[in] Either +1 or -1, which way to iterate the buffer.
  74. * @param record[in] The current keypress.
  75. */
  76. void dynamic_macro_record_key(
  77. keyrecord_t **macro_pointer,
  78. keyrecord_t *macro_end2,
  79. int8_t direction,
  80. keyrecord_t *record)
  81. {
  82. if (*macro_pointer + direction != macro_end2) {
  83. **macro_pointer = *record;
  84. *macro_pointer += direction;
  85. } else {
  86. /* Notify about the end of buffer. The blinks are paired
  87. * because they should happen on both down and up events. */
  88. backlight_toggle();
  89. }
  90. }
  91. /**
  92. * End recording of the dynamic macro. Essentially just update the
  93. * pointer to the end of the macro.
  94. */
  95. void dynamic_macro_record_end(keyrecord_t *macro_pointer, keyrecord_t **macro_end)
  96. {
  97. dynamic_macro_led_blink();
  98. *macro_end = macro_pointer;
  99. }
  100. /* Handle the key events related to the dynamic macros. Should be
  101. * called from process_record_user() like this:
  102. *
  103. * bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  104. * if (!process_record_dynamic_macro(keycode, record)) {
  105. * return false;
  106. * }
  107. * <...THE REST OF THE FUNCTION...>
  108. * }
  109. */
  110. bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
  111. {
  112. /* Both macros use the same buffer but read/write on different
  113. * ends of it.
  114. *
  115. * Macro1 is written left-to-right starting from the beginning of
  116. * the buffer.
  117. *
  118. * Macro2 is written right-to-left starting from the end of the
  119. * buffer.
  120. *
  121. * &macro_buffer macro_end
  122. * v v
  123. * +------------------------------------------------------------+
  124. * |>>>>>> MACRO1 >>>>>>| |<<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
  125. * +------------------------------------------------------------+
  126. * ^ ^
  127. * r_macro_end r_macro_buffer
  128. *
  129. * During the recording when one macro encounters the end of the
  130. * other macro, the recording is stopped. Apart from this, there
  131. * are no arbitrary limits for the macros' length in relation to
  132. * each other: for example one can either have two medium sized
  133. * macros or one long macro and one short macro. Or even one empty
  134. * and one using the whole buffer.
  135. */
  136. static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
  137. /* Pointer to the first buffer element after the first macro.
  138. * Initially points to the very beginning of the buffer since the
  139. * macro is empty. */
  140. static keyrecord_t *macro_end = macro_buffer;
  141. /* The other end of the macro buffer. Serves as the beginning of
  142. * the second macro. */
  143. static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
  144. /* Like macro_end but for the second macro. */
  145. static keyrecord_t *r_macro_end = r_macro_buffer;
  146. /* A persistent pointer to the current macro position (iterator)
  147. * used during the recording. */
  148. static keyrecord_t *macro_pointer = NULL;
  149. /* 0 - no macro is being recorded right now
  150. * 1,2 - either macro 1 or 2 is being recorded */
  151. static uint8_t macro_id = 0;
  152. if (macro_id == 0) {
  153. /* No macro recording in progress. */
  154. if (!record->event.pressed) {
  155. switch (keycode) {
  156. case DYN_REC_START1:
  157. dynamic_macro_record_start(&macro_pointer, macro_buffer);
  158. macro_id = 1;
  159. return false;
  160. case DYN_REC_START2:
  161. dynamic_macro_record_start(&macro_pointer, r_macro_buffer);
  162. macro_id = 2;
  163. return false;
  164. case DYN_MACRO_PLAY1:
  165. dynamic_macro_play(macro_buffer, macro_end, +1);
  166. return false;
  167. case DYN_MACRO_PLAY2:
  168. dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
  169. return false;
  170. }
  171. }
  172. } else {
  173. /* A macro is being recorded right now. */
  174. switch (keycode) {
  175. case MO(_DYN):
  176. /* Use the layer key used to access the macro recording as
  177. * a stop button. */
  178. if (record->event.pressed) { /* Ignore the initial release
  179. * just after the recoding
  180. * starts. */
  181. switch (macro_id) {
  182. case 1:
  183. dynamic_macro_record_end(macro_pointer, &macro_end);
  184. break;
  185. case 2:
  186. dynamic_macro_record_end(macro_pointer, &r_macro_end);
  187. break;
  188. }
  189. macro_id = 0;
  190. }
  191. return false;
  192. default:
  193. /* Store the key in the macro buffer and process it normally. */
  194. switch (macro_id) {
  195. case 1:
  196. dynamic_macro_record_key(&macro_pointer, r_macro_end, +1, record);
  197. break;
  198. case 2:
  199. dynamic_macro_record_key(&macro_pointer, macro_end, -1, record);
  200. break;
  201. }
  202. return true;
  203. break;
  204. }
  205. }
  206. return true;
  207. }
  208. #endif