]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_steno.c
[Keymap] Jarred's Plaid keymap (#6049)
[qmk_firmware.git] / quantum / process_keycode / process_steno.c
1 /* Copyright 2017 Joseph Wasson
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 #include "process_steno.h"
17 #include "quantum_keycodes.h"
18 #include "eeprom.h"
19 #include "keymap_steno.h"
20 #include "virtser.h"
21 #include <string.h>
22
23 // TxBolt Codes
24 #define TXB_NUL 0
25 #define TXB_S_L 0b00000001
26 #define TXB_T_L 0b00000010
27 #define TXB_K_L 0b00000100
28 #define TXB_P_L 0b00001000
29 #define TXB_W_L 0b00010000
30 #define TXB_H_L 0b00100000
31 #define TXB_R_L 0b01000001
32 #define TXB_A_L 0b01000010
33 #define TXB_O_L 0b01000100
34 #define TXB_STR 0b01001000
35 #define TXB_E_R 0b01010000
36 #define TXB_U_R 0b01100000
37 #define TXB_F_R 0b10000001
38 #define TXB_R_R 0b10000010
39 #define TXB_P_R 0b10000100
40 #define TXB_B_R 0b10001000
41 #define TXB_L_R 0b10010000
42 #define TXB_G_R 0b10100000
43 #define TXB_T_R 0b11000001
44 #define TXB_S_R 0b11000010
45 #define TXB_D_R 0b11000100
46 #define TXB_Z_R 0b11001000
47 #define TXB_NUM 0b11010000
48
49 #define TXB_GRP0 0b00000000
50 #define TXB_GRP1 0b01000000
51 #define TXB_GRP2 0b10000000
52 #define TXB_GRP3 0b11000000
53 #define TXB_GRPMASK 0b11000000
54
55 #define TXB_GET_GROUP(code) ((code & TXB_GRPMASK) >> 6)
56
57 #define BOLT_STATE_SIZE 4
58 #define GEMINI_STATE_SIZE 6
59 #define MAX_STATE_SIZE GEMINI_STATE_SIZE
60
61 static uint8_t state[MAX_STATE_SIZE] = {0};
62 static uint8_t chord[MAX_STATE_SIZE] = {0};
63 static int8_t pressed = 0;
64 static steno_mode_t mode;
65
66 static const uint8_t boltmap[64] PROGMEM = {
67   TXB_NUL, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM,
68   TXB_S_L, TXB_S_L, TXB_T_L, TXB_K_L, TXB_P_L, TXB_W_L, TXB_H_L,
69   TXB_R_L, TXB_A_L, TXB_O_L, TXB_STR, TXB_STR, TXB_NUL, TXB_NUL,
70   TXB_NUL, TXB_STR, TXB_STR, TXB_E_R, TXB_U_R, TXB_F_R, TXB_R_R,
71   TXB_P_R, TXB_B_R, TXB_L_R, TXB_G_R, TXB_T_R, TXB_S_R, TXB_D_R,
72   TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_Z_R
73 };
74
75 static void steno_clear_state(void) {
76   memset(state, 0, sizeof(state));
77   memset(chord, 0, sizeof(chord));
78 }
79
80 static void send_steno_state(uint8_t size, bool send_empty) {
81   for (uint8_t i = 0; i < size; ++i) {
82     if (chord[i] || send_empty) {
83       virtser_send(chord[i]);
84     }
85   }
86 }
87
88 void steno_init() {
89   if (!eeconfig_is_enabled()) {
90     eeconfig_init();
91   }
92   mode = eeprom_read_byte(EECONFIG_STENOMODE);
93 }
94
95 void steno_set_mode(steno_mode_t new_mode) {
96   steno_clear_state();
97   mode = new_mode;
98   eeprom_update_byte(EECONFIG_STENOMODE, mode);
99 }
100
101 /* override to intercept chords right before they get sent.
102  * return zero to suppress normal sending behavior.
103  */
104 __attribute__ ((weak))
105 bool send_steno_chord_user(steno_mode_t mode, uint8_t chord[6]) { return true; }
106
107 __attribute__ ((weak))
108 bool postprocess_steno_user(uint16_t keycode, keyrecord_t *record, steno_mode_t mode, uint8_t chord[6], int8_t pressed) { return true; }
109
110 __attribute__ ((weak))
111 bool process_steno_user(uint16_t keycode, keyrecord_t *record) { return true; }
112
113 static void send_steno_chord(void) {
114   if (send_steno_chord_user(mode, chord)) {
115     switch(mode) {
116       case STENO_MODE_BOLT:
117         send_steno_state(BOLT_STATE_SIZE, false);
118         virtser_send(0); // terminating byte
119         break;
120       case STENO_MODE_GEMINI:
121         chord[0] |= 0x80; // Indicate start of packet
122         send_steno_state(GEMINI_STATE_SIZE, true);
123         break;
124     }
125   }
126   steno_clear_state();
127 }
128
129 uint8_t *steno_get_state(void) {
130   return &state[0];
131 }
132
133 uint8_t *steno_get_chord(void) {
134   return &chord[0];
135 }
136
137 static bool update_state_bolt(uint8_t key, bool press) {
138   uint8_t boltcode = pgm_read_byte(boltmap + key);
139   if (press) {
140     state[TXB_GET_GROUP(boltcode)] |= boltcode;
141     chord[TXB_GET_GROUP(boltcode)] |= boltcode;
142   } else {
143     state[TXB_GET_GROUP(boltcode)] &= ~boltcode;
144   }
145   return false;
146 }
147
148 static bool update_state_gemini(uint8_t key, bool press) {
149   int idx = key / 7;
150   uint8_t bit = 1 << (6 - (key % 7));
151   if (press) {
152     state[idx] |= bit;
153     chord[idx] |= bit;
154   } else {
155     state[idx] &= ~bit;
156   }
157   return false;
158 }
159
160 bool process_steno(uint16_t keycode, keyrecord_t *record) {
161   switch (keycode) {
162     case QK_STENO_BOLT:
163       if (!process_steno_user(keycode, record)) {
164         return false;
165       }
166       if (IS_PRESSED(record->event)) {
167         steno_set_mode(STENO_MODE_BOLT);
168       }
169       return false;
170
171     case QK_STENO_GEMINI:
172       if (!process_steno_user(keycode, record)) {
173         return false;
174       }
175       if (IS_PRESSED(record->event)) {
176         steno_set_mode(STENO_MODE_GEMINI);
177       }
178       return false;
179
180     case STN__MIN...STN__MAX:
181       if (!process_steno_user(keycode, record)) {
182         return false;
183       }
184       switch(mode) {
185         case STENO_MODE_BOLT:
186           update_state_bolt(keycode - QK_STENO, IS_PRESSED(record->event));
187           break;
188         case STENO_MODE_GEMINI:
189           update_state_gemini(keycode - QK_STENO, IS_PRESSED(record->event));
190           break;
191       }
192       // allow postprocessing hooks
193       if (postprocess_steno_user(keycode, record, mode, chord, pressed)) {
194         if (IS_PRESSED(record->event)) {
195           ++pressed;
196         } else {
197           --pressed;
198           if (pressed <= 0) {
199             pressed = 0;
200             send_steno_chord();
201           }
202         }
203       }
204       return false;
205   }
206   return true;
207 }