]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/action_macro.c
7726b1190769a869d7e3a711e6b077fcfab91ebe
[qmk_firmware.git] / tmk_core / common / action_macro.c
1 /*
2 Copyright 2013 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "action.h"
18 #include "action_util.h"
19 #include "action_macro.h"
20 #include "wait.h"
21
22 #ifdef DEBUG_ACTION
23 #include "debug.h"
24 #else
25 #include "nodebug.h"
26 #endif
27
28
29 #ifndef NO_ACTION_MACRO
30
31 #define MACRO_READ()  (macro = MACRO_GET(macro_p++))
32 void action_macro_play(const macro_t *macro_p)
33 {
34     macro_t macro = END;
35     uint8_t interval = 0;
36
37     if (!macro_p) return;
38     while (true) {
39         switch (MACRO_READ()) {
40             case KEY_DOWN:
41                 MACRO_READ();
42                 dprintf("KEY_DOWN(%02X)\n", macro);
43                 if (IS_MOD(macro)) {
44                     add_macro_mods(MOD_BIT(macro));
45                     send_keyboard_report();
46                 } else {
47                     register_code(macro);
48                 }
49                 break;
50             case KEY_UP:
51                 MACRO_READ();
52                 dprintf("KEY_UP(%02X)\n", macro);
53                 if (IS_MOD(macro)) {
54                     del_macro_mods(MOD_BIT(macro));
55                     send_keyboard_report();
56                 } else {
57                     unregister_code(macro);
58                 }
59                 break;
60             case WAIT:
61                 MACRO_READ();
62                 dprintf("WAIT(%u)\n", macro);
63                 { uint8_t ms = macro; while (ms--) wait_ms(1); }
64                 break;
65             case INTERVAL:
66                 interval = MACRO_READ();
67                 dprintf("INTERVAL(%u)\n", interval);
68                 break;
69             case 0x04 ... 0x73:
70                 dprintf("DOWN(%02X)\n", macro);
71                 register_code(macro);
72                 break;
73             case 0x84 ... 0xF3:
74                 dprintf("UP(%02X)\n", macro);
75                 unregister_code(macro&0x7F);
76                 break;
77             case END:
78             default:
79                 return;
80         }
81         // interval
82         { uint8_t ms = interval; while (ms--) wait_ms(1); }
83     }
84 }
85 #endif