dynamic_macro.h 8.3 KB

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