dynamic_macro.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /* Convenience macros used for retrieving the debug info. All of them
  51. * need a `direction` variable accessible at the call site.
  52. */
  53. #define DYNAMIC_MACRO_CURRENT_SLOT() (direction > 0 ? 1 : 2)
  54. #define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) \
  55. ((int)(direction * ((POINTER) - (BEGIN))))
  56. #define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) \
  57. ((int)(direction * ((END2) - (BEGIN)) + 1))
  58. /**
  59. * Start recording of the dynamic macro.
  60. *
  61. * @param[out] macro_pointer The new macro buffer iterator.
  62. * @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
  63. */
  64. void dynamic_macro_record_start(
  65. keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
  66. {
  67. dprintln("dynamic macro recording: started");
  68. dynamic_macro_led_blink();
  69. clear_keyboard();
  70. layer_clear();
  71. *macro_pointer = macro_buffer;
  72. }
  73. /**
  74. * Play the dynamic macro.
  75. *
  76. * @param macro_buffer[in] The beginning of the macro buffer being played.
  77. * @param macro_end[in] The element after the last macro buffer element.
  78. * @param direction[in] Either +1 or -1, which way to iterate the buffer.
  79. */
  80. void dynamic_macro_play(
  81. keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
  82. {
  83. dprintf("dynamic macro: slot %d playback\n", DYNAMIC_MACRO_CURRENT_SLOT());
  84. uint32_t saved_layer_state = layer_state;
  85. clear_keyboard();
  86. layer_clear();
  87. while (macro_buffer != macro_end) {
  88. process_record(macro_buffer);
  89. macro_buffer += direction;
  90. }
  91. clear_keyboard();
  92. layer_state = saved_layer_state;
  93. }
  94. /**
  95. * Record a single key in a dynamic macro.
  96. *
  97. * @param macro_buffer[in] The start of the used macro buffer.
  98. * @param macro_pointer[in,out] The current buffer position.
  99. * @param macro2_end[in] The end of the other macro.
  100. * @param direction[in] Either +1 or -1, which way to iterate the buffer.
  101. * @param record[in] The current keypress.
  102. */
  103. void dynamic_macro_record_key(
  104. keyrecord_t *macro_buffer,
  105. keyrecord_t **macro_pointer,
  106. keyrecord_t *macro2_end,
  107. int8_t direction,
  108. keyrecord_t *record)
  109. {
  110. /* If we've just started recording, ignore all the key releases. */
  111. if (!record->event.pressed && *macro_pointer == macro_buffer) {
  112. dprintln("dynamic macro: ignoring a leading key-up event");
  113. return;
  114. }
  115. /* The other end of the other macro is the last buffer element it
  116. * is safe to use before overwriting the other macro.
  117. */
  118. if (*macro_pointer - direction != macro2_end) {
  119. **macro_pointer = *record;
  120. *macro_pointer += direction;
  121. } else {
  122. dynamic_macro_led_blink();
  123. }
  124. dprintf(
  125. "dynamic macro: slot %d length: %d/%d\n",
  126. DYNAMIC_MACRO_CURRENT_SLOT(),
  127. DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, *macro_pointer),
  128. DYNAMIC_MACRO_CURRENT_CAPACITY(macro_buffer, macro2_end));
  129. }
  130. /**
  131. * End recording of the dynamic macro. Essentially just update the
  132. * pointer to the end of the macro.
  133. */
  134. void dynamic_macro_record_end(
  135. keyrecord_t *macro_buffer,
  136. keyrecord_t *macro_pointer,
  137. int8_t direction,
  138. keyrecord_t **macro_end)
  139. {
  140. dynamic_macro_led_blink();
  141. /* Do not save the keys being held when stopping the recording,
  142. * i.e. the keys used to access the layer DYN_REC_STOP is on.
  143. */
  144. while (macro_pointer != macro_buffer &&
  145. (macro_pointer - direction)->event.pressed) {
  146. dprintln("dynamic macro: trimming a trailing key-down event");
  147. macro_pointer -= direction;
  148. }
  149. dprintf(
  150. "dynamic macro: slot %d saved, length: %d\n",
  151. DYNAMIC_MACRO_CURRENT_SLOT(),
  152. DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, macro_pointer));
  153. *macro_end = macro_pointer;
  154. }
  155. /* Handle the key events related to the dynamic macros. Should be
  156. * called from process_record_user() like this:
  157. *
  158. * bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  159. * if (!process_record_dynamic_macro(keycode, record)) {
  160. * return false;
  161. * }
  162. * <...THE REST OF THE FUNCTION...>
  163. * }
  164. */
  165. bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
  166. {
  167. /* Both macros use the same buffer but read/write on different
  168. * ends of it.
  169. *
  170. * Macro1 is written left-to-right starting from the beginning of
  171. * the buffer.
  172. *
  173. * Macro2 is written right-to-left starting from the end of the
  174. * buffer.
  175. *
  176. * &macro_buffer macro_end
  177. * v v
  178. * +------------------------------------------------------------+
  179. * |>>>>>> MACRO1 >>>>>> <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
  180. * +------------------------------------------------------------+
  181. * ^ ^
  182. * r_macro_end r_macro_buffer
  183. *
  184. * During the recording when one macro encounters the end of the
  185. * other macro, the recording is stopped. Apart from this, there
  186. * are no arbitrary limits for the macros' length in relation to
  187. * each other: for example one can either have two medium sized
  188. * macros or one long macro and one short macro. Or even one empty
  189. * and one using the whole buffer.
  190. */
  191. static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
  192. /* Pointer to the first buffer element after the first macro.
  193. * Initially points to the very beginning of the buffer since the
  194. * macro is empty. */
  195. static keyrecord_t *macro_end = macro_buffer;
  196. /* The other end of the macro buffer. Serves as the beginning of
  197. * the second macro. */
  198. static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
  199. /* Like macro_end but for the second macro. */
  200. static keyrecord_t *r_macro_end = r_macro_buffer;
  201. /* A persistent pointer to the current macro position (iterator)
  202. * used during the recording. */
  203. static keyrecord_t *macro_pointer = NULL;
  204. /* 0 - no macro is being recorded right now
  205. * 1,2 - either macro 1 or 2 is being recorded */
  206. static uint8_t macro_id = 0;
  207. if (macro_id == 0) {
  208. /* No macro recording in progress. */
  209. if (!record->event.pressed) {
  210. switch (keycode) {
  211. case DYN_REC_START1:
  212. dynamic_macro_record_start(&macro_pointer, macro_buffer);
  213. macro_id = 1;
  214. return false;
  215. case DYN_REC_START2:
  216. dynamic_macro_record_start(&macro_pointer, r_macro_buffer);
  217. macro_id = 2;
  218. return false;
  219. case DYN_MACRO_PLAY1:
  220. dynamic_macro_play(macro_buffer, macro_end, +1);
  221. return false;
  222. case DYN_MACRO_PLAY2:
  223. dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
  224. return false;
  225. }
  226. }
  227. } else {
  228. /* A macro is being recorded right now. */
  229. switch (keycode) {
  230. case DYN_REC_STOP:
  231. /* Stop the macro recording. */
  232. if (record->event.pressed) { /* Ignore the initial release
  233. * just after the recoding
  234. * starts. */
  235. switch (macro_id) {
  236. case 1:
  237. dynamic_macro_record_end(macro_buffer, macro_pointer, +1, &macro_end);
  238. break;
  239. case 2:
  240. dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
  241. break;
  242. }
  243. macro_id = 0;
  244. }
  245. return false;
  246. default:
  247. /* Store the key in the macro buffer and process it normally. */
  248. switch (macro_id) {
  249. case 1:
  250. dynamic_macro_record_key(macro_buffer, &macro_pointer, r_macro_end, +1, record);
  251. break;
  252. case 2:
  253. dynamic_macro_record_key(r_macro_buffer, &macro_pointer, macro_end, -1, record);
  254. break;
  255. }
  256. return true;
  257. break;
  258. }
  259. }
  260. return true;
  261. }
  262. #undef DYNAMIC_MACRO_CURRENT_SLOT
  263. #undef DYNAMIC_MACRO_CURRENT_LENGTH
  264. #undef DYNAMIC_MACRO_CURRENT_CAPACITY
  265. #endif