dynamic_macro.h 7.2 KB

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