You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
8.1 KiB

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